Skip to content

Add getCurrentSpan and fix a bug in extractSpan #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions api/trace/+opentelemetry/+trace/Context.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
function sp = extractSpan(context)
% Extract span from context
% SP = OPENTELEMETRY.TRACE.CONTEXT.EXTRACTSPAN(CTXT) extracts
% span SP from a context object CTXT. SP is a nonrecording span
% such that ISRECORDING(SP) returns false. If CTXT does not
% span SP from a context object CTXT. If CTXT does not
% contain any spans, SP will be an invalid span with all-zero
% trace and span IDs.
%
Expand Down
1 change: 0 additions & 1 deletion api/trace/+opentelemetry/+trace/Span.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
obj.Proxy = libmexclass.proxy.Proxy("Name", ...
"libmexclass.opentelemetry.SpanProxy", ...
"ConstructorArguments", {context.Proxy.ID});
obj.Name = ""; % unknown name when span is extracted from context, leave blank
else % in is a proxy object
obj.Proxy = proxy;
obj.Name = spname;
Expand Down
14 changes: 14 additions & 0 deletions api/trace/+opentelemetry/+trace/getCurrentSpan.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function sp = getCurrentSpan()
% Retrieve the current span
% SP = OPENTELEMETRY.TRACE.GETCURRENTSPAN() returns the current span.
% If there is not current span, SP will be an invalid span with all-zero
% trace and span IDs.
%
% See also OPENTELEMETRY.TRACE.SPAN,
% OPENTELEMETRY.CONTEXT.GETCURRENTCONTEXT
% OPENTELEMETRY.TRACE.CONTEXT.EXTRACTSPAN

% Copyright 2024 The MathWorks, Inc.

ctx = opentelemetry.context.getCurrentContext;
sp = opentelemetry.trace.Context.extractSpan(ctx);
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function handleError(obj, ME)
% spans and their corresponding scopes. Rethrow the
% exception ME.
if ~isempty(obj.Instrumentor.Spans)
setStatus(obj.Instrumentor.Spans(end), "Error");
setStatus(obj.Instrumentor.Spans(end), "Error", ME.message);
for i = length(obj.Instrumentor.Spans):-1:1
obj.Instrumentor.Spans(i) = [];
obj.Instrumentor.Scopes(i) = [];
Expand Down Expand Up @@ -195,4 +195,4 @@ function handleError(obj, ME)
% file
f = processFileInput(f);
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@

% Copyright 2024 The MathWorks, Inc.

% add an attribute about input
sp = opentelemetry.trace.getCurrentSpan;
setAttributes(sp, "DataSize", n);

[x, y] = generate_data(n);
yf = best_fit_line(x,y);
12 changes: 11 additions & 1 deletion test/tautotrace.m
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ function testError(testCase)

% check error status
verifyEqual(testCase, results{1}.resourceSpans.scopeSpans.spans.status.code, 2); % error
verifyEqual(testCase, results{1}.resourceSpans.scopeSpans.spans.status.message, ...
'Input must be a numeric scalar');
verifyEmpty(testCase, fieldnames(results{2}.resourceSpans.scopeSpans.spans.status)); % ok, no error
end

Expand Down Expand Up @@ -287,6 +289,8 @@ function testHandleError(testCase)

% check error status
verifyEqual(testCase, results{1}.resourceSpans.scopeSpans.spans.status.code, 2); % error
verifyEqual(testCase, results{1}.resourceSpans.scopeSpans.spans.status.message, ...
'Input must be a numeric scalar');
verifyEmpty(testCase, fieldnames(results{2}.resourceSpans.scopeSpans.spans.status)); % ok, no error
end

Expand Down Expand Up @@ -349,7 +353,8 @@ function testAutoManualInstrument(testCase)
at = opentelemetry.autoinstrument.AutoTrace(@manual_instrumented_example);

% run the example
[~] = beginTrace(at, 100);
n = 100;
[~] = beginTrace(at, n);

% perform test comparisons
results = readJsonResults(testCase);
Expand Down Expand Up @@ -377,6 +382,11 @@ function testAutoManualInstrument(testCase)
verifyEqual(testCase, results{1}.resourceSpans.scopeSpans.spans.parentSpanId, results{2}.resourceSpans.scopeSpans.spans.spanId);
verifyEqual(testCase, results{3}.resourceSpans.scopeSpans.spans.parentSpanId, results{5}.resourceSpans.scopeSpans.spans.spanId);
verifyEqual(testCase, results{4}.resourceSpans.scopeSpans.spans.parentSpanId, results{5}.resourceSpans.scopeSpans.spans.spanId);

% check attribute
verifyNumElements(testCase, results{6}.resourceSpans.scopeSpans.spans.attributes, 1);
verifyEqual(testCase, results{6}.resourceSpans.scopeSpans.spans.attributes.key, 'DataSize');
verifyEqual(testCase, results{6}.resourceSpans.scopeSpans.spans.attributes.value.doubleValue, n);
end
end
end
Loading