azure-core
Loading...
Searching...
No Matches
service_tracing.hpp
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
8
9#include <utility>
10
11#pragma once
12
18namespace Azure { namespace Core { namespace Tracing { namespace _internal {
19
30 class ServiceSpan final : public Span {
31 private:
32 std::shared_ptr<Span> m_span;
33
34 friend class TracingContextFactory;
35 ServiceSpan() = default;
36 explicit ServiceSpan(std::shared_ptr<Span> span) : m_span(std::move(span)) {}
37
38 ServiceSpan(const ServiceSpan&) = delete;
39 ServiceSpan& operator=(ServiceSpan const&) = delete;
40
41 ServiceSpan& operator=(ServiceSpan&&) noexcept = default;
42
43 public:
44 ServiceSpan(ServiceSpan&& that) = default;
45
46 ~ServiceSpan() override
47 {
48 if (m_span)
49 {
50 m_span->End({});
51 }
52 }
53
54 void End() { End({}); }
55
56 void End(Azure::Nullable<Azure::DateTime> endTime) override
57 {
58 if (m_span)
59 {
60 m_span->End(endTime);
61 }
62 }
63 void SetStatus(Azure::Core::Tracing::_internal::SpanStatus const& status)
64 {
65 SetStatus(status, {});
66 }
67 void SetStatus(
68 Azure::Core::Tracing::_internal::SpanStatus const& status,
69 std::string const& description) override
70 {
71 if (m_span)
72 {
73 m_span->SetStatus(status, description);
74 }
75 }
76
82 void AddAttributes(AttributeSet const& attributeToAdd) override
83 {
84 if (m_span)
85 {
86 m_span->AddAttributes(attributeToAdd);
87 }
88 }
89
96 void AddAttribute(std::string const& attributeName, std::string const& attributeValue) override
97 {
98 if (m_span)
99 {
100 m_span->AddAttribute(attributeName, attributeValue);
101 }
102 }
103
113 void AddEvent(std::string const& eventName, AttributeSet const& eventAttributes) override
114 {
115 if (m_span)
116 {
117 m_span->AddEvent(eventName, eventAttributes);
118 }
119 }
120
128 void AddEvent(std::string const& eventName) override
129 {
130 if (m_span)
131 {
132 m_span->AddEvent(eventName);
133 }
134 }
135
142 void AddEvent(std::exception const& exception) override
143 {
144 if (m_span)
145 {
146 m_span->AddEvent(exception);
147 m_span->SetStatus(SpanStatus::Error, {});
148 }
149 }
150
157 void PropagateToHttpHeaders(Azure::Core::Http::Request& request) override
158 {
159 if (m_span)
160 {
161 m_span->PropagateToHttpHeaders(request);
162 }
163 }
164 };
165
172 class TracingContextFactory final {
173 private:
174 std::string m_serviceName;
175 std::string m_packageName;
176 std::string m_packageVersion;
177 std::shared_ptr<Azure::Core::Tracing::_internal::Tracer> m_serviceTracer;
178
187 static Azure::Core::Context::Key ContextSpanKey;
188 static Azure::Core::Context::Key TracingFactoryContextKey;
189
190 public:
201 TracingContextFactory(
202 Azure::Core::_internal::ClientOptions const& options,
203 std::string const& serviceName,
204 std::string const& packageName,
205 std::string packageVersion)
206 : m_serviceName{serviceName}, m_packageName{packageName}, m_packageVersion{packageVersion}
207 {
208 // If the caller has configured a tracing provider, use it. Otherwise, use the default
209 // provider.
210 if (options.Telemetry.TracingProvider)
211 {
212 m_serviceTracer
213 = Azure::Core::Tracing::_internal::TracerProviderImplGetter::TracerImplFromTracer(
214 options.Telemetry.TracingProvider)
215 ->CreateTracer(packageName, packageVersion);
216 }
217 }
231 [[deprecated]] TracingContextFactory(
232 Azure::Core::_internal::ClientOptions const& options,
233 std::string const& serviceName,
234 std::string packageVersion)
235 : TracingContextFactory(options, serviceName, serviceName, packageVersion)
236 {
237 }
238
239 TracingContextFactory() = default;
240 TracingContextFactory(TracingContextFactory const&) = default;
241
245 struct TracingContext
246 {
250 Azure::Core::Context Context;
255 ServiceSpan Span;
256 };
257
270 TracingContext CreateTracingContext(
271 std::string const& spanName,
272 Azure::Core::Context const& context) const;
273
284 TracingContext CreateTracingContext(
285 std::string const& spanName,
286 Azure::Core::Tracing::_internal::CreateSpanOptions& spanOptions,
287 Azure::Core::Context const& context) const;
288
289 std::unique_ptr<Azure::Core::Tracing::_internal::AttributeSet> CreateAttributeSet() const;
290
293 bool HasTracer() const { return static_cast<bool>(m_serviceTracer); }
294
295 static std::unique_ptr<TracingContextFactory> CreateFromContext(
296 Azure::Core::Context const& context);
297 };
298
306 class TracingAttributes
307 : public Azure::Core::_internal::ExtendableEnumeration<TracingAttributes> {
308 public:
309 explicit TracingAttributes(std::string const& that) : ExtendableEnumeration(that) {}
310
319 AZ_CORE_DLLEXPORT const static TracingAttributes AzNamespace;
320
326 AZ_CORE_DLLEXPORT const static TracingAttributes RequestId;
327
333 AZ_CORE_DLLEXPORT const static TracingAttributes ServiceRequestId;
334
339 AZ_CORE_DLLEXPORT const static TracingAttributes HttpMethod;
340
345 AZ_CORE_DLLEXPORT const static TracingAttributes HttpUrl;
346
351 AZ_CORE_DLLEXPORT const static TracingAttributes HttpStatusCode;
352
358 AZ_CORE_DLLEXPORT const static TracingAttributes HttpUserAgent;
359
364 AZ_CORE_DLLEXPORT const static TracingAttributes NetPeerName;
365
370 AZ_CORE_DLLEXPORT const static TracingAttributes NetPeerPort;
371 };
372
373}}}} // 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:183
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.