|
1 | 1 | classdef Tracer < handle |
2 | 2 | % A tracer that is used to create spans. |
3 | 3 |
|
4 | | - % Copyright 2023 The MathWorks, Inc. |
| 4 | + % Copyright 2023-2024 The MathWorks, Inc. |
5 | 5 |
|
6 | 6 | properties (SetAccess=immutable) |
7 | 7 | Name (1,1) string % Tracer name |
|
42 | 42 | % a dictionary. |
43 | 43 | % "Links" - Link objects that specifies relationships |
44 | 44 | % with other spans. |
45 | | - % |
| 45 | + % |
46 | 46 | % See also OPENTELEMETRY.TRACE.SPAN, |
47 | 47 | % OPENTELEMETRY.TRACE.LINK, OPENTELEMETRY.CONTEXT.CONTEXT |
48 | | - arguments |
49 | | - obj |
50 | | - spname |
| 48 | + arguments |
| 49 | + obj |
| 50 | + spname |
51 | 51 | end |
52 | 52 | arguments (Repeating) |
53 | 53 | trailingnames |
54 | 54 | trailingvalues |
55 | 55 | end |
| 56 | + |
56 | 57 | import opentelemetry.common.processAttributes |
57 | | - % validate the trailing names and values |
58 | | - optionnames = ["Context", "SpanKind", "StartTime", "Attributes", "Links"]; |
59 | | - % define default values |
60 | | - contextid = intmax("uint64"); % default value which means no context supplied |
61 | | - spankind = "internal"; |
62 | | - starttime = NaN; |
63 | | - attributekeys = string.empty(); |
64 | | - attributevalues = {}; |
65 | | - links = {}; |
66 | | - % Loop through Name-Value pairs |
67 | | - for i = 1:length(trailingnames) |
68 | | - try |
69 | | - namei = validatestring(trailingnames{i}, optionnames); |
70 | | - catch |
71 | | - % invalid option, ignore |
72 | | - continue |
73 | | - end |
74 | | - if strcmp(namei, "Context") |
75 | | - context = trailingvalues{i}; |
76 | | - if isa(context, "opentelemetry.context.Context") |
77 | | - contextid = context.Proxy.ID; |
78 | | - end |
79 | | - elseif strcmp(namei, "SpanKind") |
| 58 | + |
| 59 | + if nargin == 2 |
| 60 | + spname = opentelemetry.common.mustBeScalarString(spname); |
| 61 | + id = obj.Proxy.startSpanWithNameOnly(spname); |
| 62 | + spanproxy = libmexclass.proxy.Proxy("Name", ... |
| 63 | + "libmexclass.opentelemetry.SpanProxy", "ID", id); |
| 64 | + span = opentelemetry.trace.Span(spanproxy, spname); |
| 65 | + else |
| 66 | + |
| 67 | + % validate the trailing names and values |
| 68 | + optionnames = ["Context", "SpanKind", "StartTime", "Attributes", "Links"]; |
| 69 | + % define default values |
| 70 | + contextid = intmax("uint64"); % default value which means no context supplied |
| 71 | + spankind = "internal"; |
| 72 | + starttime = NaN; |
| 73 | + attributekeys = string.empty(); |
| 74 | + attributevalues = {}; |
| 75 | + links = {}; |
| 76 | + % variables to keep track of which proxy function to call |
| 77 | + specifyoptions = false; |
| 78 | + specifyattributes = false; |
| 79 | + |
| 80 | + % Loop through Name-Value pairs |
| 81 | + for i = 1:length(trailingnames) |
80 | 82 | try |
81 | | - spankind = validatestring(trailingvalues{i}, ... |
82 | | - ["internal", "server", "client", "producer", "consumer"]); |
| 83 | + namei = validatestring(trailingnames{i}, optionnames); |
83 | 84 | catch |
84 | | - % invalid span kind. Ignore |
| 85 | + % invalid option, ignore |
| 86 | + continue |
85 | 87 | end |
86 | | - elseif strcmp(namei, "StartTime") |
87 | | - valuei = trailingvalues{i}; |
88 | | - if isdatetime(valuei) && isscalar(valuei) && ~isnat(valuei) |
89 | | - starttime = posixtime(valuei); |
90 | | - end |
91 | | - elseif strcmp(namei, "Attributes") |
92 | | - [attributekeys, attributevalues] = processAttributes(trailingvalues{i}, true); |
93 | | - elseif strcmp(namei, "Links") |
94 | | - valuei = trailingvalues{i}; |
95 | | - if isa(valuei, "opentelemetry.trace.Link") |
96 | | - nlinks = numel(valuei); |
97 | | - links = cell(3,nlinks); |
98 | | - for li = 1:nlinks |
99 | | - links{1,li} = valuei(li).Target.Proxy.ID; |
100 | | - linkattrs = valuei(li).Attributes; |
101 | | - [linkattrkeys, linkattrvalues] = processAttributes(linkattrs, true); |
102 | | - links{2,li} = linkattrkeys; |
103 | | - links{3,li} = linkattrvalues; |
| 88 | + if strcmp(namei, "Context") |
| 89 | + context = trailingvalues{i}; |
| 90 | + if isa(context, "opentelemetry.context.Context") |
| 91 | + contextid = context.Proxy.ID; |
| 92 | + specifyoptions = true; |
104 | 93 | end |
105 | | - links = reshape(links,1,[]); % flatten into a row vector |
| 94 | + elseif strcmp(namei, "SpanKind") |
| 95 | + try |
| 96 | + spankind = validatestring(trailingvalues{i}, ... |
| 97 | + ["internal", "server", "client", "producer", "consumer"]); |
| 98 | + specifyoptions = true; |
| 99 | + catch |
| 100 | + % invalid span kind. Ignore |
| 101 | + end |
| 102 | + elseif strcmp(namei, "StartTime") |
| 103 | + valuei = trailingvalues{i}; |
| 104 | + if isdatetime(valuei) && isscalar(valuei) && ~isnat(valuei) |
| 105 | + starttime = posixtime(valuei); |
| 106 | + specifyoptions = true; |
| 107 | + end |
| 108 | + elseif strcmp(namei, "Attributes") |
| 109 | + [attributekeys, attributevalues] = processAttributes(trailingvalues{i}, true); |
| 110 | + specifyattributes = true; |
| 111 | + elseif strcmp(namei, "Links") |
| 112 | + valuei = trailingvalues{i}; |
| 113 | + if isa(valuei, "opentelemetry.trace.Link") |
| 114 | + nlinks = numel(valuei); |
| 115 | + links = cell(3,nlinks); |
| 116 | + for li = 1:nlinks |
| 117 | + links{1,li} = valuei(li).Target.Proxy.ID; |
| 118 | + linkattrs = valuei(li).Attributes; |
| 119 | + [linkattrkeys, linkattrvalues] = processAttributes(linkattrs, true); |
| 120 | + links{2,li} = linkattrkeys; |
| 121 | + links{3,li} = linkattrvalues; |
| 122 | + end |
| 123 | + links = reshape(links,1,[]); % flatten into a row vector |
| 124 | + specifyattributes = true; |
| 125 | + end |
| 126 | + |
106 | 127 | end |
107 | | - |
108 | 128 | end |
| 129 | + spname = opentelemetry.common.mustBeScalarString(spname); |
| 130 | + if ~specifyoptions && ~specifyattributes |
| 131 | + id = obj.Proxy.startSpanWithNameOnly(spname); |
| 132 | + elseif specifyoptions && ~specifyattributes |
| 133 | + id = obj.Proxy.startSpanWithNameAndOptions(spname, ... |
| 134 | + contextid, spankind, starttime); |
| 135 | + elseif ~specifyoptions && specifyattributes |
| 136 | + id = obj.Proxy.startSpanWithNameAndAttributes(spname, ... |
| 137 | + attributekeys, attributevalues, links{:}); |
| 138 | + else % specifyoptions && specifyattributes |
| 139 | + id = obj.Proxy.startSpanWithNameOptionsAttributes(spname, ... |
| 140 | + contextid, spankind, starttime, attributekeys, attributevalues, links{:}); |
| 141 | + end |
| 142 | + |
| 143 | + spanproxy = libmexclass.proxy.Proxy("Name", ... |
| 144 | + "libmexclass.opentelemetry.SpanProxy", "ID", id); |
| 145 | + span = opentelemetry.trace.Span(spanproxy, spname); |
109 | 146 | end |
110 | | - spname = opentelemetry.common.mustBeScalarString(spname); |
111 | | - id = obj.Proxy.startSpan(spname, contextid, spankind, starttime, ... |
112 | | - attributekeys, attributevalues, links{:}); |
113 | | - spanproxy = libmexclass.proxy.Proxy("Name", ... |
114 | | - "libmexclass.opentelemetry.SpanProxy", "ID", id); |
115 | | - span = opentelemetry.trace.Span(spanproxy, spname); |
116 | 147 | end |
117 | 148 | end |
118 | 149 |
|
|
0 commit comments