Skip to content

Commit 758c09f

Browse files
authored
Merge pull request #171 from mathworks/autotrace
Add getCurrentSpan and fix a bug in extractSpan
2 parents b012253 + 618e16c commit 758c09f

File tree

6 files changed

+32
-6
lines changed

6 files changed

+32
-6
lines changed

api/trace/+opentelemetry/+trace/Context.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
function sp = extractSpan(context)
88
% Extract span from context
99
% SP = OPENTELEMETRY.TRACE.CONTEXT.EXTRACTSPAN(CTXT) extracts
10-
% span SP from a context object CTXT. SP is a nonrecording span
11-
% such that ISRECORDING(SP) returns false. If CTXT does not
10+
% span SP from a context object CTXT. If CTXT does not
1211
% contain any spans, SP will be an invalid span with all-zero
1312
% trace and span IDs.
1413
%

api/trace/+opentelemetry/+trace/Span.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
obj.Proxy = libmexclass.proxy.Proxy("Name", ...
2121
"libmexclass.opentelemetry.SpanProxy", ...
2222
"ConstructorArguments", {context.Proxy.ID});
23-
obj.Name = ""; % unknown name when span is extracted from context, leave blank
2423
else % in is a proxy object
2524
obj.Proxy = proxy;
2625
obj.Name = spname;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function sp = getCurrentSpan()
2+
% Retrieve the current span
3+
% SP = OPENTELEMETRY.TRACE.GETCURRENTSPAN() returns the current span.
4+
% If there is not current span, SP will be an invalid span with all-zero
5+
% trace and span IDs.
6+
%
7+
% See also OPENTELEMETRY.TRACE.SPAN,
8+
% OPENTELEMETRY.CONTEXT.GETCURRENTCONTEXT
9+
% OPENTELEMETRY.TRACE.CONTEXT.EXTRACTSPAN
10+
11+
% Copyright 2024 The MathWorks, Inc.
12+
13+
ctx = opentelemetry.context.getCurrentContext;
14+
sp = opentelemetry.trace.Context.extractSpan(ctx);

auto-instrumentation/+opentelemetry/+autoinstrument/AutoTrace.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ function handleError(obj, ME)
150150
% spans and their corresponding scopes. Rethrow the
151151
% exception ME.
152152
if ~isempty(obj.Instrumentor.Spans)
153-
setStatus(obj.Instrumentor.Spans(end), "Error");
153+
setStatus(obj.Instrumentor.Spans(end), "Error", ME.message);
154154
for i = length(obj.Instrumentor.Spans):-1:1
155155
obj.Instrumentor.Spans(i) = [];
156156
obj.Instrumentor.Scopes(i) = [];
@@ -195,4 +195,4 @@ function handleError(obj, ME)
195195
% file
196196
f = processFileInput(f);
197197
end
198-
end
198+
end

test/autotrace_examples/manual_instrumented_example/manual_instrumented_example.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@
44

55
% Copyright 2024 The MathWorks, Inc.
66

7+
% add an attribute about input
8+
sp = opentelemetry.trace.getCurrentSpan;
9+
setAttributes(sp, "DataSize", n);
10+
711
[x, y] = generate_data(n);
812
yf = best_fit_line(x,y);

test/tautotrace.m

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ function testError(testCase)
253253

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

@@ -287,6 +289,8 @@ function testHandleError(testCase)
287289

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

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

351355
% run the example
352-
[~] = beginTrace(at, 100);
356+
n = 100;
357+
[~] = beginTrace(at, n);
353358

354359
% perform test comparisons
355360
results = readJsonResults(testCase);
@@ -377,6 +382,11 @@ function testAutoManualInstrument(testCase)
377382
verifyEqual(testCase, results{1}.resourceSpans.scopeSpans.spans.parentSpanId, results{2}.resourceSpans.scopeSpans.spans.spanId);
378383
verifyEqual(testCase, results{3}.resourceSpans.scopeSpans.spans.parentSpanId, results{5}.resourceSpans.scopeSpans.spans.spanId);
379384
verifyEqual(testCase, results{4}.resourceSpans.scopeSpans.spans.parentSpanId, results{5}.resourceSpans.scopeSpans.spans.spanId);
385+
386+
% check attribute
387+
verifyNumElements(testCase, results{6}.resourceSpans.scopeSpans.spans.attributes, 1);
388+
verifyEqual(testCase, results{6}.resourceSpans.scopeSpans.spans.attributes.key, 'DataSize');
389+
verifyEqual(testCase, results{6}.resourceSpans.scopeSpans.spans.attributes.value.doubleValue, n);
380390
end
381391
end
382392
end

0 commit comments

Comments
 (0)