]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Copyright The OpenTelemetry Authors |
2 | // SPDX-License-Identifier: Apache-2.0 | |
3 | ||
4 | #pragma once | |
5 | ||
6 | #include <chrono> | |
7 | #include <unordered_map> | |
8 | #include <vector> | |
9 | #include "opentelemetry/common/attribute_value.h" | |
10 | #include "opentelemetry/common/timestamp.h" | |
11 | #include "opentelemetry/nostd/string_view.h" | |
12 | #include "opentelemetry/sdk/common/attribute_utils.h" | |
13 | #include "opentelemetry/sdk/trace/recordable.h" | |
14 | #include "opentelemetry/trace/canonical_code.h" | |
15 | #include "opentelemetry/trace/span.h" | |
16 | #include "opentelemetry/trace/span_id.h" | |
17 | #include "opentelemetry/trace/trace_id.h" | |
18 | ||
19 | #include <string> | |
20 | ||
21 | OPENTELEMETRY_BEGIN_NAMESPACE | |
22 | namespace sdk | |
23 | { | |
24 | namespace trace | |
25 | { | |
26 | /** | |
27 | * Class for storing events in SpanData. | |
28 | */ | |
29 | class SpanDataEvent | |
30 | { | |
31 | public: | |
32 | SpanDataEvent(std::string name, | |
33 | opentelemetry::common::SystemTimestamp timestamp, | |
34 | const opentelemetry::common::KeyValueIterable &attributes) | |
35 | : name_(name), timestamp_(timestamp), attribute_map_(attributes) | |
36 | {} | |
37 | ||
38 | /** | |
39 | * Get the name for this event | |
40 | * @return the name for this event | |
41 | */ | |
42 | std::string GetName() const noexcept { return name_; } | |
43 | ||
44 | /** | |
45 | * Get the timestamp for this event | |
46 | * @return the timestamp for this event | |
47 | */ | |
48 | opentelemetry::common::SystemTimestamp GetTimestamp() const noexcept { return timestamp_; } | |
49 | ||
50 | /** | |
51 | * Get the attributes for this event | |
52 | * @return the attributes for this event | |
53 | */ | |
54 | const std::unordered_map<std::string, common::OwnedAttributeValue> &GetAttributes() const noexcept | |
55 | { | |
56 | return attribute_map_.GetAttributes(); | |
57 | } | |
58 | ||
59 | private: | |
60 | std::string name_; | |
61 | opentelemetry::common::SystemTimestamp timestamp_; | |
62 | common::AttributeMap attribute_map_; | |
63 | }; | |
64 | ||
65 | /** | |
66 | * Class for storing links in SpanData. | |
67 | */ | |
68 | class SpanDataLink | |
69 | { | |
70 | public: | |
71 | SpanDataLink(opentelemetry::trace::SpanContext span_context, | |
72 | const opentelemetry::common::KeyValueIterable &attributes) | |
73 | : span_context_(span_context), attribute_map_(attributes) | |
74 | {} | |
75 | ||
76 | /** | |
77 | * Get the attributes for this link | |
78 | * @return the attributes for this link | |
79 | */ | |
80 | const std::unordered_map<std::string, common::OwnedAttributeValue> &GetAttributes() const noexcept | |
81 | { | |
82 | return attribute_map_.GetAttributes(); | |
83 | } | |
84 | ||
85 | /** | |
86 | * Get the span context for this link | |
87 | * @return the span context for this link | |
88 | */ | |
89 | const opentelemetry::trace::SpanContext &GetSpanContext() const noexcept { return span_context_; } | |
90 | ||
91 | private: | |
92 | opentelemetry::trace::SpanContext span_context_; | |
93 | common::AttributeMap attribute_map_; | |
94 | }; | |
95 | ||
96 | /** | |
97 | * SpanData is a representation of all data collected by a span. | |
98 | */ | |
99 | class SpanData final : public Recordable | |
100 | { | |
101 | public: | |
102 | SpanData() : resource_{nullptr}, instrumentation_library_{nullptr} {} | |
103 | /** | |
104 | * Get the trace id for this span | |
105 | * @return the trace id for this span | |
106 | */ | |
107 | opentelemetry::trace::TraceId GetTraceId() const noexcept { return span_context_.trace_id(); } | |
108 | ||
109 | /** | |
110 | * Get the span id for this span | |
111 | * @return the span id for this span | |
112 | */ | |
113 | opentelemetry::trace::SpanId GetSpanId() const noexcept { return span_context_.span_id(); } | |
114 | ||
115 | /** | |
116 | * Get the span context for this span | |
117 | * @return the span context for this span | |
118 | */ | |
119 | const opentelemetry::trace::SpanContext &GetSpanContext() const noexcept { return span_context_; } | |
120 | ||
121 | /** | |
122 | * Get the parent span id for this span | |
123 | * @return the span id for this span's parent | |
124 | */ | |
125 | opentelemetry::trace::SpanId GetParentSpanId() const noexcept { return parent_span_id_; } | |
126 | ||
127 | /** | |
128 | * Get the name for this span | |
129 | * @return the name for this span | |
130 | */ | |
131 | opentelemetry::nostd::string_view GetName() const noexcept { return name_; } | |
132 | ||
133 | /** | |
134 | * Get the kind of this span | |
135 | * @return the kind of this span | |
136 | */ | |
137 | opentelemetry::trace::SpanKind GetSpanKind() const noexcept { return span_kind_; } | |
138 | ||
139 | /** | |
140 | * Get the status for this span | |
141 | * @return the status for this span | |
142 | */ | |
143 | opentelemetry::trace::StatusCode GetStatus() const noexcept { return status_code_; } | |
144 | ||
145 | /** | |
146 | * Get the status description for this span | |
147 | * @return the description of the the status of this span | |
148 | */ | |
149 | opentelemetry::nostd::string_view GetDescription() const noexcept { return status_desc_; } | |
150 | ||
151 | /** | |
152 | * Get the attributes associated with the resource | |
153 | * @returns the attributes associated with the resource configured for TracerProvider | |
154 | */ | |
155 | ||
156 | const opentelemetry::sdk::resource::Resource &GetResource() const noexcept | |
157 | { | |
158 | if (resource_ == nullptr) | |
159 | { | |
160 | // this shouldn't happen as TraceProvider provides default resources | |
161 | static opentelemetry::sdk::resource::Resource resource = | |
162 | opentelemetry::sdk::resource::Resource::GetEmpty(); | |
163 | return resource; | |
164 | } | |
165 | return *resource_; | |
166 | } | |
167 | ||
168 | /** | |
169 | * Get the attributes associated with the resource | |
170 | * @returns the attributes associated with the resource configured for TracerProvider | |
171 | */ | |
172 | ||
173 | const opentelemetry::sdk::trace::InstrumentationLibrary &GetInstrumentationLibrary() | |
174 | const noexcept | |
175 | { | |
176 | if (instrumentation_library_ == nullptr) | |
177 | { | |
178 | // this shouldn't happen as Tracer ensures there is valid default instrumentation library. | |
179 | static std::unique_ptr<opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary> | |
180 | instrumentation_library = | |
181 | opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary::Create( | |
182 | "unknown_service"); | |
183 | return *instrumentation_library; | |
184 | } | |
185 | return *instrumentation_library_; | |
186 | } | |
187 | ||
188 | /** | |
189 | * Get the start time for this span | |
190 | * @return the start time for this span | |
191 | */ | |
192 | opentelemetry::common::SystemTimestamp GetStartTime() const noexcept { return start_time_; } | |
193 | ||
194 | /** | |
195 | * Get the duration for this span | |
196 | * @return the duration for this span | |
197 | */ | |
198 | std::chrono::nanoseconds GetDuration() const noexcept { return duration_; } | |
199 | ||
200 | /** | |
201 | * Get the attributes for this span | |
202 | * @return the attributes for this span | |
203 | */ | |
204 | const std::unordered_map<std::string, common::OwnedAttributeValue> &GetAttributes() const noexcept | |
205 | { | |
206 | return attribute_map_.GetAttributes(); | |
207 | } | |
208 | ||
209 | /** | |
210 | * Get the events associated with this span | |
211 | * @return the events associated with this span | |
212 | */ | |
213 | const std::vector<SpanDataEvent> &GetEvents() const noexcept { return events_; } | |
214 | ||
215 | /** | |
216 | * Get the links associated with this span | |
217 | * @return the links associated with this span | |
218 | */ | |
219 | const std::vector<SpanDataLink> &GetLinks() const noexcept { return links_; } | |
220 | ||
221 | void SetIdentity(const opentelemetry::trace::SpanContext &span_context, | |
222 | opentelemetry::trace::SpanId parent_span_id) noexcept override | |
223 | { | |
224 | span_context_ = span_context; | |
225 | parent_span_id_ = parent_span_id; | |
226 | } | |
227 | ||
228 | void SetAttribute(nostd::string_view key, | |
229 | const opentelemetry::common::AttributeValue &value) noexcept override | |
230 | { | |
231 | attribute_map_.SetAttribute(key, value); | |
232 | } | |
233 | ||
234 | void AddEvent(nostd::string_view name, | |
235 | opentelemetry::common::SystemTimestamp timestamp = | |
236 | opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now()), | |
237 | const opentelemetry::common::KeyValueIterable &attributes = | |
238 | opentelemetry::common::KeyValueIterableView<std::map<std::string, int>>( | |
239 | {})) noexcept override | |
240 | { | |
241 | SpanDataEvent event(std::string(name), timestamp, attributes); | |
242 | events_.push_back(event); | |
243 | } | |
244 | ||
245 | void AddLink(const opentelemetry::trace::SpanContext &span_context, | |
246 | const opentelemetry::common::KeyValueIterable &attributes) noexcept override | |
247 | { | |
248 | SpanDataLink link(span_context, attributes); | |
249 | links_.push_back(link); | |
250 | } | |
251 | ||
252 | void SetStatus(opentelemetry::trace::StatusCode code, | |
253 | nostd::string_view description) noexcept override | |
254 | { | |
255 | status_code_ = code; | |
256 | status_desc_ = std::string(description); | |
257 | } | |
258 | ||
259 | void SetName(nostd::string_view name) noexcept override | |
260 | { | |
261 | name_ = std::string(name.data(), name.length()); | |
262 | } | |
263 | ||
264 | void SetSpanKind(opentelemetry::trace::SpanKind span_kind) noexcept override | |
265 | { | |
266 | span_kind_ = span_kind; | |
267 | } | |
268 | ||
269 | void SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept override | |
270 | { | |
271 | resource_ = &resource; | |
272 | } | |
273 | ||
274 | void SetStartTime(opentelemetry::common::SystemTimestamp start_time) noexcept override | |
275 | { | |
276 | start_time_ = start_time; | |
277 | } | |
278 | ||
279 | void SetDuration(std::chrono::nanoseconds duration) noexcept override { duration_ = duration; } | |
280 | ||
281 | void SetInstrumentationLibrary( | |
282 | const InstrumentationLibrary &instrumentation_library) noexcept override | |
283 | { | |
284 | instrumentation_library_ = &instrumentation_library; | |
285 | } | |
286 | ||
287 | private: | |
288 | opentelemetry::trace::SpanContext span_context_{false, false}; | |
289 | opentelemetry::trace::SpanId parent_span_id_; | |
290 | opentelemetry::common::SystemTimestamp start_time_; | |
291 | std::chrono::nanoseconds duration_{0}; | |
292 | std::string name_; | |
293 | opentelemetry::trace::StatusCode status_code_{opentelemetry::trace::StatusCode::kUnset}; | |
294 | std::string status_desc_; | |
295 | common::AttributeMap attribute_map_; | |
296 | std::vector<SpanDataEvent> events_; | |
297 | std::vector<SpanDataLink> links_; | |
298 | opentelemetry::trace::SpanKind span_kind_{opentelemetry::trace::SpanKind::kInternal}; | |
299 | const opentelemetry::sdk::resource::Resource *resource_; | |
300 | const InstrumentationLibrary *instrumentation_library_; | |
301 | }; | |
302 | } // namespace trace | |
303 | } // namespace sdk | |
304 | OPENTELEMETRY_END_NAMESPACE |