1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
7 #include "opentelemetry/nostd/shared_ptr.h"
8 #include "opentelemetry/nostd/string_view.h"
9 #include "opentelemetry/nostd/unique_ptr.h"
10 #include "opentelemetry/nostd/variant.h"
11 #include "opentelemetry/trace/span_id.h"
13 #include "opentelemetry/exporters/etw/etw_provider.h"
15 OPENTELEMETRY_BEGIN_NAMESPACE
21 * @brief TelemetryProvider Options passed via SDK API.
23 using TelemetryProviderOptions
=
24 std::map
<std::string
, nostd::variant
<std::string
, uint64_t, float, bool>>;
27 * @brief TelemetryProvider runtime configuration class. Internal representation
28 * of TelemetryProviderOptions used by various components of SDK.
32 bool enableTraceId
; // Set `TraceId` on ETW events
33 bool enableSpanId
; // Set `SpanId` on ETW events
34 bool enableActivityId
; // Assign `SpanId` to `ActivityId`
35 bool enableActivityTracking
; // Emit TraceLogging events for Span/Start and Span/Stop Not used
37 bool enableRelatedActivityId
; // Assign parent `SpanId` to `RelatedActivityId`
38 bool enableAutoParent
; // Start new spans as children of current active span, Not used for Logs
39 ETWProvider::EventFormat
40 encoding
; // Event encoding to use for this provider (TLD, MsgPack, XML, etc.).
41 } TelemetryProviderConfiguration
;
44 * @brief Helper template to convert a variant value from TelemetryProviderOptions to
45 * LoggerProviderConfiguration
47 * @param options TelemetryProviderOptions passed on API surface
48 * @param key Option name
49 * @param value Reference to destination value
50 * @param defaultValue Default value if option is not supplied
53 static inline void GetOption(const TelemetryProviderOptions
&options
,
58 auto it
= options
.find(key
);
59 if (it
!= options
.end())
61 auto val
= it
->second
;
62 value
= nostd::get
<T
>(val
);
71 * @brief Helper template to convert encoding config option to EventFormat.
72 * Configuration option passed as `options["encoding"] = "MsgPack"`.
73 * Default encoding is TraceLogging Dynamic Manifest (TLD).
75 * Valid encoding names listed below.
77 * For MessagePack encoding:
86 * For TraceLogging Dynamic encoding:
91 static inline ETWProvider::EventFormat
GetEncoding(const TelemetryProviderOptions
&options
)
93 ETWProvider::EventFormat evtFmt
= ETWProvider::EventFormat::ETW_MANIFEST
;
95 auto it
= options
.find("encoding");
96 if (it
!= options
.end())
98 auto varValue
= it
->second
;
99 std::string val
= nostd::get
<std::string
>(varValue
);
101 #pragma warning(push)
102 #pragma warning(disable : 4307) /* Integral constant overflow - OK while computing hash */
103 auto h
= utils::hashCode(val
.c_str());
106 case CONST_HASHCODE(MSGPACK
):
108 case CONST_HASHCODE(MsgPack
):
110 case CONST_HASHCODE(MessagePack
):
111 evtFmt
= ETWProvider::EventFormat::ETW_MSGPACK
;
114 case CONST_HASHCODE(XML
):
116 case CONST_HASHCODE(xml
):
117 evtFmt
= ETWProvider::EventFormat::ETW_XML
;
120 case CONST_HASHCODE(TLD
):
122 case CONST_HASHCODE(tld
):
124 evtFmt
= ETWProvider::EventFormat::ETW_MANIFEST
;
137 * @brief Utility template to obtain etw::TracerProvider._config or etw::LoggerProvider._config
139 * @tparam T etw::TracerProvider
140 * @param t etw::TracerProvider ref
141 * @return TelemetryProviderConfiguration ref
144 TelemetryProviderConfiguration
&GetConfiguration(T
&t
)
150 * @brief Utility template to convert SpanId or TraceId to hex.
151 * @param id - value of SpanId or TraceId
152 * @return Hexadecimal representation of Id as string.
155 static inline std::string
ToLowerBase16(const T
&id
)
157 char buf
[2 * T::kSize
] = {0};
158 id
.ToLowerBase16(buf
);
159 return std::string(buf
, sizeof(buf
));
163 * @brief Utility method to convert span_id (8 byte) to ActivityId GUID (16 bytes)
164 * @param span OpenTelemetry Span Id object
165 * @return GUID struct containing 8-bytes of SpanId + 8 NUL bytes.
167 static inline bool CopySpanIdToActivityId(const opentelemetry::trace::SpanId
&span_id
,
170 memset(&outGuid
, 0, sizeof(outGuid
));
171 if (!span_id
.IsValid())
175 auto spanId
= span_id
.Id().data();
176 uint8_t *guidPtr
= reinterpret_cast<uint8_t *>(&outGuid
);
177 for (size_t i
= 0; i
< 8; i
++)
179 guidPtr
[i
] = spanId
[i
];
185 } // namespace exporter
186 OPENTELEMETRY_END_NAMESPACE