Skip to content

Commit b55ffdf

Browse files
committed
Fix a bug in span/setStatus #20
1 parent 4c6e09f commit b55ffdf

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ function setStatus(obj, status, description)
132132
% new status is not valid, ignore
133133
return
134134
end
135-
description = opentelemetry.common.mustBeScalarString(description);
135+
if nargin < 3
136+
description = "";
137+
else
138+
description = opentelemetry.common.mustBeScalarString(description);
139+
end
136140
obj.Proxy.setStatus(status, description);
137141
end
138142

@@ -181,8 +185,7 @@ function setStatus(obj, status, description)
181185
function attrs = processAttributes(attrsin)
182186
import opentelemetry.trace.Span.processAttribute
183187

184-
nin = length(attrsin);
185-
if nin == 1 && isa(attrsin{1}, "dictionary")
188+
if isscalar(attrsin) && isa(attrsin{1}, "dictionary")
186189
% dictionary case
187190
attrtbl = entries(attrsin{1});
188191
nattr = height(attrtbl);
@@ -203,6 +206,7 @@ function setStatus(obj, status, description)
203206
attrs = attrs(:);
204207
else
205208
% NV pairs
209+
nin = length(attrsin);
206210
if rem(nin,2) ~= 0
207211
% Mismatched name-value pairs. Ignore all attributes.
208212
attrs = cell(1,0);

test/ttrace.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,28 @@ function testTime(testCase)
262262
"convertFrom", "posixtime") - endtime), seconds(2));
263263
end
264264

265+
function testStatus(testCase)
266+
% testStatus: setting status
267+
tp = opentelemetry.sdk.trace.TracerProvider();
268+
tr = getTracer(tp, "foo");
269+
sp = startSpan(tr, "bar");
270+
setStatus(sp, "ok");
271+
endSpan(sp);
272+
273+
sp1 = startSpan(tr, "quz");
274+
setStatus(sp1, "Error", "Something went wrong.") % with description
275+
endSpan(sp1);
276+
277+
% perform test comparisons
278+
results = readJsonResults(testCase);
279+
% status codes
280+
% Unset: 0
281+
% Ok: 1
282+
% Error: 2
283+
verifyEqual(testCase, results{1}.resourceSpans.scopeSpans.spans.status.code, 1);
284+
verifyEqual(testCase, results{2}.resourceSpans.scopeSpans.spans.status.code, 2);
285+
end
286+
265287
function testAttributes(testCase)
266288
% testAttributes: specifying attributes when starting spans
267289

0 commit comments

Comments
 (0)