azure-core
Loading...
Searching...
No Matches
service_tracing.hpp
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
9
10#pragma once
11
17namespace Azure { namespace Core { namespace Tracing { namespace _internal {
18
29 class ServiceSpan final : public Span {
30 private:
31 std::shared_ptr<Span> m_span;
32
33 friend class TracingContextFactory;
34 ServiceSpan() = default;
35 explicit ServiceSpan(std::shared_ptr<Span> span) : m_span(span) {}
36
37 ServiceSpan(const ServiceSpan&) = delete;
38 ServiceSpan& operator=(ServiceSpan const&) = delete;
39
40 ServiceSpan& operator=(ServiceSpan&&) noexcept = default;
41
42 public:
43 ServiceSpan(ServiceSpan&& that) = default;
44
45 ~ServiceSpan()
46 {
47 if (m_span)
48 {
49 m_span->End({});
50 }
51 }
52
53 void End() { End({}); }
54
55 void End(Azure::Nullable<Azure::DateTime> endTime) override
56 {
57 if (m_span)
58 {
59 m_span->End(endTime);
60 }
61 }
62 void SetStatus(Azure::Core::Tracing::_internal::SpanStatus const& status)
63 {
64 SetStatus(status, {});
65 }
66 void SetStatus(
67 Azure::Core::Tracing::_internal::SpanStatus const& status,
68 std::string const& description) override
69 {
70 if (m_span)
71 {
72 m_span->SetStatus(status, description);
73 }
74 }
75
81 void AddAttributes(AttributeSet const& attributeToAdd) override
82 {
83 if (m_span)
84 {
85 m_span->AddAttributes(attributeToAdd);
86 }
87 }
88
95 void AddAttribute(std::string const& attributeName, std::string const& attributeValue) override
96 {
97 if (m_span)
98 {
99 m_span->AddAttribute(attributeName, attributeValue);
100 }
101 }
102
112 void AddEvent(std::string const& eventName, AttributeSet const& eventAttributes) override
113 {
114 if (m_span)
115 {
116 m_span->AddEvent(eventName, eventAttributes);
117 }
118 }
119
127 void AddEvent(std::string const& eventName) override
128 {
129 if (m_span)
130 {
131 m_span->AddEvent(eventName);
132 }
133 }
134
141 void AddEvent(std::exception const& exception) override
142 {
143 if (m_span)
144 {
145 m_span->AddEvent(exception);
146 m_span->SetStatus(SpanStatus::Error, {});
147 }
148 }
149
156 void PropagateToHttpHeaders(Azure::Core::Http::Request& request) override
157 {
158 if (m_span)
159 {
160 m_span->PropagateToHttpHeaders(request);
161 }
162 }
163 };
164
171 class TracingContextFactory final {
172 private:
173 std::string m_serviceName;
174 std::string m_packageName;
175 std::string m_packageVersion;
176 std::shared_ptr<Azure::Core::Tracing::_internal::Tracer> m_serviceTracer;
177
186 static Azure::Core::Context::Key ContextSpanKey;
187 static Azure::Core::Context::Key TracingFactoryContextKey;
188
189 public:
200 TracingContextFactory(
201 Azure::Core::_internal::ClientOptions const& options,
202 std::string const& serviceName,
203 std::string const& packageName,
204 std::string packageVersion)
205 : m_serviceName{serviceName}, m_packageName{packageName}, m_packageVersion{packageVersion}
206 {
207 // If the caller has configured a tracing provider, use it. Otherwise, use the default
208 // provider.
209 if (options.Telemetry.TracingProvider)
210 {
211 m_serviceTracer
212 = Azure::Core::Tracing::_internal::TracerProviderImplGetter::TracerImplFromTracer(
213 options.Telemetry.TracingProvider)
214 ->CreateTracer(packageName, packageVersion);
215 }
216 }
230 [[deprecated]] TracingContextFactory(
231 Azure::Core::_internal::ClientOptions const& options,
232 std::string const& serviceName,
233 std::string packageVersion)
234 : TracingContextFactory(options, serviceName, serviceName, packageVersion)
235 {
236 }
237
238 TracingContextFactory() = default;
239 TracingContextFactory(TracingContextFactory const&) = default;
240
244 struct TracingContext
245 {
249 Azure::Core::Context Context;
254 ServiceSpan Span;
255 };
256
269 TracingContext CreateTracingContext(
270 std::string const& spanName,
271 Azure::Core::Context const& context) const;
272
283 TracingContext CreateTracingContext(
284 std::string const& spanName,
285 Azure::Core::Tracing::_internal::CreateSpanOptions& spanOptions,
286 Azure::Core::Context const& context) const;
287
288 std::unique_ptr<Azure::Core::Tracing::_internal::AttributeSet> CreateAttributeSet() const;
289
292 bool HasTracer() const { return static_cast<bool>(m_serviceTracer); }
293
294 static std::unique_ptr<TracingContextFactory> CreateFromContext(
295 Azure::Core::Context const& context);
296 };
297
305 class TracingAttributes
306 : public Azure::Core::_internal::ExtendableEnumeration<TracingAttributes> {
307 public:
308 explicit TracingAttributes(std::string const& that) : ExtendableEnumeration(that) {}
309
318 AZ_CORE_DLLEXPORT const static TracingAttributes AzNamespace;
319
325 AZ_CORE_DLLEXPORT const static TracingAttributes RequestId;
326
332 AZ_CORE_DLLEXPORT const static TracingAttributes ServiceRequestId;
333
338 AZ_CORE_DLLEXPORT const static TracingAttributes HttpMethod;
339
344 AZ_CORE_DLLEXPORT const static TracingAttributes HttpUrl;
345
350 AZ_CORE_DLLEXPORT const static TracingAttributes HttpStatusCode;
351
357 AZ_CORE_DLLEXPORT const static TracingAttributes HttpUserAgent;
358
363 AZ_CORE_DLLEXPORT const static TracingAttributes NetPeerName;
364
369 AZ_CORE_DLLEXPORT const static TracingAttributes NetPeerPort;
370 };
371
372}}}} // namespace Azure::Core::Tracing::_internal
A key used to store and retrieve data in an Azure::Core::Context object.
Definition context.hpp:77
A context is a node within a unidirectional tree that represents deadlines and key/value pairs.
Definition context.hpp:72
A request message from a client to a server.
Definition http.hpp:182
Manages an optional contained value, i.e. a value that may or may not be present.
Definition nullable.hpp:30
Base type for all client option types, exposes various common client options like Retry and Transport...
Context for canceling long running operations.
Internal utility functions for extendable enumerations.
HttpStatusCode
Defines the possible HTTP status codes.
Definition http_status_code.hpp:18
Compute the hash value for the input binary data, using SHA256, SHA384 and SHA512.
Definition azure_assert.hpp:57
Internal classes which abstract the OpenTelemetry API surface.
Declaration of the UserAgentGenerator type.