Skip to content

Commit 70f74d7

Browse files
authored
Merge pull request #69 from open-telemetry/main
[EXPORTER] Fix the format of SpanLink for ETW (open-telemetry#3054)
2 parents bbac344 + 4eb78a6 commit 70f74d7

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

exporters/etw/include/opentelemetry/exporters/etw/etw_fields.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@
113113
# define ETW_FIELD_SPAN_KIND "Kind" /* Span Kind */
114114
# define ETW_FIELD_SPAN_LINKS "Links" /* Span Links array */
115115

116+
# define ETW_FIELD_SPAN_LINKS_TO_SPAN_ID "toSpanId" /* Span Links toSpanId */
117+
# define ETW_FIELD_SPAN_LINKS_TO_TRACE_ID "toTraceId" /* Span Links toTraceId */
118+
116119
# define ETW_FIELD_PAYLOAD_NAME "Name" /* ETW Payload["Name"] */
117120

118121
/* Span option constants */
@@ -136,7 +139,7 @@
136139
/* Log specific */
137140
# define ETW_FIELD_LOG_BODY "body" /* Log body */
138141
# define ETW_FIELD_LOG_SEVERITY_TEXT "severityText" /* Sev text */
139-
# define ETW_FIELD_LOG_SEVERITY_NUM "severityNumber" /* Sev num */
142+
# define ETW_FIELD_LOG_SEVERITY_NUM "severityNumber" /* Sev num */
140143

141144
#endif
142145

exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h

+28-6
Original file line numberDiff line numberDiff line change
@@ -212,19 +212,41 @@ class Tracer : public opentelemetry::trace::Tracer,
212212
// Add `SpanLinks` attribute if the list is not empty
213213
if (links.size())
214214
{
215-
size_t idx = 0;
215+
bool first = true;
216216
std::string linksValue;
217+
218+
// reserve space for all the SpanLinks.
219+
// A single SpanLink will be outptut as:
220+
// [{"toSpanId":"9a43c801557f26b7","toTraceId":"ac6cd70ac4bb168a99cb7651b048d965"}]
221+
// The second and above link output to string are 1 byte less than the first SpanLink.
222+
const size_t kSingleSpanLinkSizeInBytes = 80;
223+
linksValue.reserve(kSingleSpanLinkSizeInBytes +
224+
(links.size() - 1) * (kSingleSpanLinkSizeInBytes - 1));
225+
linksValue += "[";
226+
217227
links.ForEachKeyValue([&](opentelemetry::trace::SpanContext ctx,
218228
const opentelemetry::common::KeyValueIterable &) {
219-
if (!linksValue.empty())
229+
if (first)
230+
{
231+
first = false;
232+
linksValue += "{\"" ETW_FIELD_SPAN_LINKS_TO_SPAN_ID "\":\"";
233+
}
234+
else
220235
{
221-
linksValue += ',';
222-
linksValue += ToLowerBase16(ctx.span_id());
236+
linksValue += ",{\"" ETW_FIELD_SPAN_LINKS_TO_SPAN_ID "\":\"";
223237
}
224-
idx++;
238+
239+
linksValue += ToLowerBase16(ctx.span_id());
240+
linksValue += "\",\"" ETW_FIELD_SPAN_LINKS_TO_TRACE_ID "\":\"";
241+
linksValue += ToLowerBase16(ctx.trace_id());
242+
linksValue += "\"}";
243+
225244
return true;
226245
});
227-
attributes[ETW_FIELD_SPAN_LINKS] = linksValue;
246+
247+
linksValue += "]";
248+
249+
attributes[ETW_FIELD_SPAN_LINKS] = std::move(linksValue);
228250
}
229251
}
230252

0 commit comments

Comments
 (0)