azure-core
Loading...
Searching...
No Matches
service_tracing.hpp
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
8
9#pragma once
10
16namespace Azure { namespace Core { namespace Tracing { namespace _internal {
17
28 class ServiceSpan final : public Span {
29 private:
30 std::shared_ptr<Span> m_span;
31
32 friend class TracingContextFactory;
33 ServiceSpan() = default;
34 explicit ServiceSpan(std::shared_ptr<Span> span) : m_span(span) {}
35
36 ServiceSpan(const ServiceSpan&) = delete;
37 ServiceSpan& operator=(ServiceSpan const&) = delete;
38
39 ServiceSpan& operator=(ServiceSpan&&) noexcept = default;
40
41 public:
42 ServiceSpan(ServiceSpan&& that) = default;
43
44 ~ServiceSpan()
45 {
46 if (m_span)
47 {
48 m_span->End({});
49 }
50 }
51
52 void End() { End({}); }
53
54 void End(Azure::Nullable<Azure::DateTime> endTime) override
55 {
56 if (m_span)
57 {
58 m_span->End(endTime);
59 }
60 }
61 void SetStatus(Azure::Core::Tracing::_internal::SpanStatus const& status)
62 {
63 SetStatus(status, {});
64 }
65 void SetStatus(
66 Azure::Core::Tracing::_internal::SpanStatus const& status,
67 std::string const& description) override
68 {
69 if (m_span)
70 {
71 m_span->SetStatus(status, description);
72 }
73 }
74
80 void AddAttributes(AttributeSet const& attributeToAdd) override
81 {
82 if (m_span)
83 {
84 m_span->AddAttributes(attributeToAdd);
85 }
86 }
87
94 void AddAttribute(std::string const& attributeName, std::string const& attributeValue) override
95 {
96 if (m_span)
97 {
98 m_span->AddAttribute(attributeName, attributeValue);
99 }
100 }
101
111 void AddEvent(std::string const& eventName, AttributeSet const& eventAttributes) override
112 {
113 if (m_span)
114 {
115 m_span->AddEvent(eventName, eventAttributes);
116 }
117 }
118
126 void AddEvent(std::string const& eventName) override
127 {
128 if (m_span)
129 {
130 m_span->AddEvent(eventName);
131 }
132 }
133
140 void AddEvent(std::exception const& exception) override
141 {
142 if (m_span)
143 {
144 m_span->AddEvent(exception);
145 m_span->SetStatus(SpanStatus::Error, {});
146 }
147 }
148
155 void PropagateToHttpHeaders(Azure::Core::Http::Request& request) override
156 {
157 if (m_span)
158 {
159 m_span->PropagateToHttpHeaders(request);
160 }
161 }
162 };
163
170 class TracingContextFactory final {
171 private:
172 std::string m_serviceName;
173 std::string m_packageName;
174 std::string m_packageVersion;
175 std::shared_ptr<Azure::Core::Tracing::_internal::Tracer> m_serviceTracer;
176
185 static Azure::Core::Context::Key ContextSpanKey;
186 static Azure::Core::Context::Key TracingFactoryContextKey;
187
188 public:
199 TracingContextFactory(
200 Azure::Core::_internal::ClientOptions const& options,
201 std::string const& serviceName,
202 std::string const& packageName,
203 std::string packageVersion)
204 : m_serviceName{serviceName}, m_packageName{packageName}, m_packageVersion{packageVersion}
205 {
206 // If the caller has configured a tracing provider, use it. Otherwise, use the default
207 // provider.
208 if (options.Telemetry.TracingProvider)
209 {
210 m_serviceTracer
211 = Azure::Core::Tracing::_internal::TracerProviderImplGetter::TracerImplFromTracer(
212 options.Telemetry.TracingProvider)
213 ->CreateTracer(packageName, packageVersion);
214 }
215 }
229 [[deprecated]] TracingContextFactory(
230 Azure::Core::_internal::ClientOptions const& options,
231 std::string const& serviceName,
232 std::string packageVersion)
233 : TracingContextFactory(options, serviceName, serviceName, packageVersion)
234 {
235 }
236
237 TracingContextFactory() = default;
238 TracingContextFactory(TracingContextFactory const&) = default;
239
243 struct TracingContext
244 {
248 Azure::Core::Context Context;
253 ServiceSpan Span;
254 };
255
268 TracingContext CreateTracingContext(
269 std::string const& spanName,
270 Azure::Core::Context const& context) const;
271
282 TracingContext CreateTracingContext(
283 std::string const& spanName,
284 Azure::Core::Tracing::_internal::CreateSpanOptions& spanOptions,
285 Azure::Core::Context const& context) const;
286
287 std::unique_ptr<Azure::Core::Tracing::_internal::AttributeSet> CreateAttributeSet() const;
288
291 bool HasTracer() const { return static_cast<bool>(m_serviceTracer); }
292
293 static std::unique_ptr<TracingContextFactory> CreateFromContext(
294 Azure::Core::Context const& context);
295 };
296
304 class TracingAttributes
305 : public Azure::Core::_internal::ExtendableEnumeration<TracingAttributes> {
306 public:
307 explicit TracingAttributes(std::string const& that) : ExtendableEnumeration(that) {}
308
317 AZ_CORE_DLLEXPORT const static TracingAttributes AzNamespace;
318
324 AZ_CORE_DLLEXPORT const static TracingAttributes RequestId;
325
331 AZ_CORE_DLLEXPORT const static TracingAttributes ServiceRequestId;
332
337 AZ_CORE_DLLEXPORT const static TracingAttributes HttpMethod;
338
343 AZ_CORE_DLLEXPORT const static TracingAttributes HttpUrl;
344
349 AZ_CORE_DLLEXPORT const static TracingAttributes HttpStatusCode;
350
356 AZ_CORE_DLLEXPORT const static TracingAttributes HttpUserAgent;
357
362 AZ_CORE_DLLEXPORT const static TracingAttributes NetPeerName;
363
368 AZ_CORE_DLLEXPORT const static TracingAttributes NetPeerPort;
369 };
370
371}}}} // 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.