Skip to content

Commit c3b1ef7

Browse files
committed
fix a bug in reading context
1 parent 93718ae commit c3b1ef7

1 file changed

Lines changed: 8 additions & 27 deletions

File tree

‎examples/github/github_example.m‎

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,20 @@ function github_example()
66
runOnce(@initTracer);
77

88
%% Parse TRACEPARENT
9-
traceparent = getenv("TRACEPARENT");
10-
traceparent = strtrim(traceparent);
9+
traceparent = strtrim(string(getenv("TRACEPARENT")));
1110
assert(strlength(traceparent) > 0, "TRACEPARENT environment variable is not set.");
1211

1312
% Format: <version>-<traceId>-<spanId>-<traceFlags>
1413
parts = split(traceparent, "-");
15-
traceIdHex = char(parts(2)); % 32 hex chars
16-
spanIdHex = char(parts(3)); % 16 hex chars
17-
traceFlagsHex = char(parts(4)); % 2 hex chars
18-
19-
% take care of little endian
20-
[~,~,endian] = computer;
21-
if isequal(endian, 'L')
22-
traceIdHexHi = bigEndianToLittleEndian(traceIdHex(1:16));
23-
traceIdHexLo = bigEndianToLittleEndian(traceIdHex(17:32));
24-
spanIdHex = bigEndianToLittleEndian(spanIdHex);
25-
end
14+
assert(numel(parts) == 4, "TRACEPARENT is not in the expected format.");
15+
traceIdHex = parts(2); % 32 hex chars
16+
spanIdHex = parts(3); % 16 hex chars
17+
traceFlagsHex = parts(4); % 2 hex chars
18+
issampled = bitand(hex2dec(traceFlagsHex), 1) == 1;
2619

2720
%% Build parent SpanContext
28-
parentCtx = opentelemetry.trace.SpanContext(string(traceIdHexHi) + string(traceIdHexLo), ...
29-
string(spanIdHex));
21+
% IDs are passed as hex strings in W3C byte order; no endianness conversion needed
22+
parentCtx = opentelemetry.trace.SpanContext(traceIdHex, spanIdHex, IsSampled=issampled);
3023
parentscope = makeCurrent(parentCtx);
3124

3225
%% Create tracer
@@ -99,18 +92,6 @@ function github_example()
9992
fprintf("github_example complete. Trace ID: %s\n", traceIdHex);
10093
end
10194

102-
function val = hexToUint64(hexstr)
103-
% HEXTOUINT64 Convert a 16-character hex string to uint64 without
104-
% precision loss. Splits into two 8-char (32-bit) halves.
105-
hi = uint64(hex2dec(hexstr(1:8)));
106-
lo = uint64(hex2dec(hexstr(9:16)));
107-
val = bitor(bitshift(hi, 32), lo);
108-
end
109-
110-
function outbytes = bigEndianToLittleEndian(inbytes)
111-
outbytes= reshape(fliplr(reshape(inbytes, 2, [])), 1, []);
112-
end
113-
11495
function initTracer
11596
% set up global TracerProvider
11697
resource = dictionary("service.name", "OpenTelemetry-Matlab_examples");

0 commit comments

Comments
 (0)