|
1 | 1 | classdef SpanContext < handle |
2 | 2 | % The part of a span that is propagated. |
3 | 3 |
|
4 | | -% Copyright 2023 The MathWorks, Inc. |
| 4 | +% Copyright 2023-2024 The MathWorks, Inc. |
5 | 5 |
|
6 | 6 | properties (Dependent, SetAccess=private) |
7 | 7 | TraceId (1,1) string % Trace identifier represented as a string of 32 hexadecimal digits |
|
14 | 14 | Proxy % Proxy object to interface C++ code |
15 | 15 | end |
16 | 16 |
|
17 | | - methods (Access={?opentelemetry.trace.Span,?opentelemetry.trace.Link}) |
18 | | - function obj = SpanContext(proxy) |
19 | | - if nargin < 1 |
| 17 | + methods |
| 18 | + function obj = SpanContext(traceid, spanid, varargin) |
| 19 | + % Span context |
| 20 | + % SC = OPENTELEMETRY.TRACE.SPANCONTEXT(TRACEID, SPANID) |
| 21 | + % creates a span context with the specified trace and span |
| 22 | + % IDs. Trace and span IDs must be strings or char vectors |
| 23 | + % containing a hexadecimal number. Trace IDs must be 32 |
| 24 | + % hexadecimal digits long and span IDs must be 16 |
| 25 | + % hexadecimal digits long. Valid IDs must be non-zero. |
| 26 | + % |
| 27 | + % SC = OPENTELEMETRY.TRACE.SPANCONTEXT(TRACEID, SPANID, |
| 28 | + % PARAM1, VALUE1, PARAM2, VALUE2, ...) specifies optional |
| 29 | + % parameter name/value pairs. Parameters are: |
| 30 | + % "IsSampled" - Whether span is sampled. Default is |
| 31 | + % true. |
| 32 | + % "IsRemote" - Whether span is created in a remote |
| 33 | + % process. Default is true. |
| 34 | + |
| 35 | + if nargin == 1 && isa(traceid, "libmexclass.proxy.Proxy") |
| 36 | + % internal calls to constructor with a proxy |
| 37 | + obj.Proxy = traceid; |
| 38 | + else |
| 39 | + narginchk(2, inf); |
| 40 | + traceid_len = 32; |
| 41 | + spanid_len = 16; |
| 42 | + if ~((isstring(traceid) || (ischar(traceid) && isrow(traceid))) && ... |
| 43 | + strlength(traceid) == traceid_len && all(isstrprop(traceid, "xdigit"))) |
| 44 | + traceid = repmat('0', 1, traceid_len); % replace any illegal values with an all-zeros invalid ID |
| 45 | + end |
| 46 | + if ~((isstring(spanid) || (ischar(spanid) && isrow(spanid))) && ... |
| 47 | + strlength(spanid) == spanid_len && all(isstrprop(spanid, "xdigit"))) |
| 48 | + spanid = repmat('0', 1, spanid_len); % replace any illegal values with an all-zeros invalid ID |
| 49 | + end |
| 50 | + % convert IDs from string to uint8 array |
| 51 | + traceid = uint8(hex2dec(reshape(char(traceid), 2, traceid_len/2).')); |
| 52 | + spanid = uint8(hex2dec(reshape(char(spanid), 2, spanid_len/2).')); |
| 53 | + |
| 54 | + % default option values |
| 55 | + issampled = true; |
| 56 | + isremote = true; |
| 57 | + if nargin > 2 |
| 58 | + optionnames = ["IsSampled", "IsRemote"]; |
| 59 | + for i = 1:2:length(varargin) |
| 60 | + try |
| 61 | + namei = validatestring(varargin{i}, optionnames); |
| 62 | + catch |
| 63 | + % invalid option, ignore |
| 64 | + continue |
| 65 | + end |
| 66 | + valuei = varargin{i+1}; |
| 67 | + if strcmp(namei, "IsSampled") |
| 68 | + if (isnumeric(valuei) || islogical(valuei)) && isscalar(valuei) |
| 69 | + issampled = logical(valuei); |
| 70 | + end |
| 71 | + else % strcmp(namei, "IsRemote") |
| 72 | + if (isnumeric(valuei) || islogical(valuei)) && isscalar(valuei) |
| 73 | + isremote = logical(valuei); |
| 74 | + end |
| 75 | + end |
| 76 | + end |
| 77 | + end |
| 78 | + |
20 | 79 | obj.Proxy = libmexclass.proxy.Proxy("Name", ... |
21 | 80 | "libmexclass.opentelemetry.SpanContextProxy", ... |
22 | | - "ConstructorArguments", {}); |
23 | | - else |
24 | | - obj.Proxy = proxy; |
| 81 | + "ConstructorArguments", {traceid, spanid, issampled, isremote}); |
25 | 82 | end |
26 | 83 | end |
27 | 84 | end |
|
70 | 127 | % ISSAMPLED, ISVALID |
71 | 128 | tf = obj.Proxy.isRemote(); |
72 | 129 | end |
| 130 | + |
| 131 | + function scope = makeCurrent(obj) |
| 132 | + % MAKECURRENT Make span the current span |
| 133 | + % SCOPE = MAKECURRENT(SPCTXT) makes the span represented by |
| 134 | + % span context SPCTXT as the current span, by |
| 135 | + % inserting it into the current context. Returns a scope |
| 136 | + % object SCOPE that determines the duration when span is current. |
| 137 | + % When SCOPE is deleted, span will no longer be current. |
| 138 | + % |
| 139 | + % See also OPENTELEMETRY.CONTEXT.CONTEXT, |
| 140 | + % OPENTELEMETRY.GETCURRENTCONTEXT, OPENTELEMETRY.TRACE.SCOPE |
| 141 | + |
| 142 | + % return a warning if no output specified |
| 143 | + if nargout == 0 |
| 144 | + warning("opentelemetry:trace:SpanContext:makeCurrent:NoOutputSpecified", ... |
| 145 | + "Calling makeCurrent without specifying an output has no effect.") |
| 146 | + end |
| 147 | + id = obj.Proxy.makeCurrent(); |
| 148 | + scopeproxy = libmexclass.proxy.Proxy("Name", ... |
| 149 | + "libmexclass.opentelemetry.ScopeProxy", "ID", id); |
| 150 | + scope = opentelemetry.trace.Scope(scopeproxy); |
| 151 | + end |
| 152 | + |
| 153 | + function context = insertSpan(obj, context) |
| 154 | + % INSERTSPAN Insert span into a context and return a new context. |
| 155 | + % NEWCTXT = INSERTSPAN(SPCTXT, CTXT) inserts the span |
| 156 | + % represented by span context SPCTXT into context CTXT and |
| 157 | + % returns a new context. |
| 158 | + % |
| 159 | + % NEWCTXT = INSERTSPAN(SPCTXT) inserts into the current context. |
| 160 | + % |
| 161 | + % See also OPENTELEMETRY.TRACE.CONTEXT.EXTRACTSPAN |
| 162 | + if nargin < 2 |
| 163 | + context = opentelemetry.context.getCurrentContext(); |
| 164 | + end |
| 165 | + contextid = obj.Proxy.insertSpan(context.Proxy.ID); |
| 166 | + contextproxy = libmexclass.proxy.Proxy("Name", ... |
| 167 | + "libmexclass.opentelemetry.ContextProxy", "ID", contextid); |
| 168 | + context = opentelemetry.context.Context(contextproxy); |
| 169 | + end |
73 | 170 | end |
74 | 171 |
|
75 | 172 | end |
0 commit comments