diff --git a/api/trace/+opentelemetry/+trace/Context.m b/api/trace/+opentelemetry/+trace/Context.m index 6e85dd2..0c7a0b5 100644 --- a/api/trace/+opentelemetry/+trace/Context.m +++ b/api/trace/+opentelemetry/+trace/Context.m @@ -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. % diff --git a/api/trace/+opentelemetry/+trace/Span.m b/api/trace/+opentelemetry/+trace/Span.m index 7ea4541..e07aab9 100644 --- a/api/trace/+opentelemetry/+trace/Span.m +++ b/api/trace/+opentelemetry/+trace/Span.m @@ -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; diff --git a/api/trace/+opentelemetry/+trace/getCurrentSpan.m b/api/trace/+opentelemetry/+trace/getCurrentSpan.m new file mode 100644 index 0000000..ac757a6 --- /dev/null +++ b/api/trace/+opentelemetry/+trace/getCurrentSpan.m @@ -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); diff --git a/auto-instrumentation/+opentelemetry/+autoinstrument/AutoTrace.m b/auto-instrumentation/+opentelemetry/+autoinstrument/AutoTrace.m index 11aa03f..7bb6353 100644 --- a/auto-instrumentation/+opentelemetry/+autoinstrument/AutoTrace.m +++ b/auto-instrumentation/+opentelemetry/+autoinstrument/AutoTrace.m @@ -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) = []; @@ -195,4 +195,4 @@ function handleError(obj, ME) % file f = processFileInput(f); end -end \ No newline at end of file +end diff --git a/test/autotrace_examples/manual_instrumented_example/manual_instrumented_example.m b/test/autotrace_examples/manual_instrumented_example/manual_instrumented_example.m index 020f6cb..85e0080 100644 --- a/test/autotrace_examples/manual_instrumented_example/manual_instrumented_example.m +++ b/test/autotrace_examples/manual_instrumented_example/manual_instrumented_example.m @@ -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); \ No newline at end of file diff --git a/test/tautotrace.m b/test/tautotrace.m index 5265595..3a74696 100644 --- a/test/tautotrace.m +++ b/test/tautotrace.m @@ -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 @@ -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 @@ -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); @@ -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 \ No newline at end of file