From 4eb78a6a04994b48a6d81318539d826801d148c1 Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Sun, 8 Sep 2024 00:22:15 -0700 Subject: [PATCH] [EXPORTER] Fix the format of SpanLink for ETW (#3054) --- .../opentelemetry/exporters/etw/etw_fields.h | 5 ++- .../opentelemetry/exporters/etw/etw_tracer.h | 34 +++++++++++++++---- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/exporters/etw/include/opentelemetry/exporters/etw/etw_fields.h b/exporters/etw/include/opentelemetry/exporters/etw/etw_fields.h index 041929d1c0..ea4354426d 100644 --- a/exporters/etw/include/opentelemetry/exporters/etw/etw_fields.h +++ b/exporters/etw/include/opentelemetry/exporters/etw/etw_fields.h @@ -113,6 +113,9 @@ # define ETW_FIELD_SPAN_KIND "Kind" /* Span Kind */ # define ETW_FIELD_SPAN_LINKS "Links" /* Span Links array */ +# define ETW_FIELD_SPAN_LINKS_TO_SPAN_ID "toSpanId" /* Span Links toSpanId */ +# define ETW_FIELD_SPAN_LINKS_TO_TRACE_ID "toTraceId" /* Span Links toTraceId */ + # define ETW_FIELD_PAYLOAD_NAME "Name" /* ETW Payload["Name"] */ /* Span option constants */ @@ -136,7 +139,7 @@ /* Log specific */ # define ETW_FIELD_LOG_BODY "body" /* Log body */ # define ETW_FIELD_LOG_SEVERITY_TEXT "severityText" /* Sev text */ -# define ETW_FIELD_LOG_SEVERITY_NUM "severityNumber" /* Sev num */ +# define ETW_FIELD_LOG_SEVERITY_NUM "severityNumber" /* Sev num */ #endif diff --git a/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h b/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h index 9071fd47d4..272558cead 100644 --- a/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h +++ b/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h @@ -212,19 +212,41 @@ class Tracer : public opentelemetry::trace::Tracer, // Add `SpanLinks` attribute if the list is not empty if (links.size()) { - size_t idx = 0; + bool first = true; std::string linksValue; + + // reserve space for all the SpanLinks. + // A single SpanLink will be outptut as: + // [{"toSpanId":"9a43c801557f26b7","toTraceId":"ac6cd70ac4bb168a99cb7651b048d965"}] + // The second and above link output to string are 1 byte less than the first SpanLink. + const size_t kSingleSpanLinkSizeInBytes = 80; + linksValue.reserve(kSingleSpanLinkSizeInBytes + + (links.size() - 1) * (kSingleSpanLinkSizeInBytes - 1)); + linksValue += "["; + links.ForEachKeyValue([&](opentelemetry::trace::SpanContext ctx, const opentelemetry::common::KeyValueIterable &) { - if (!linksValue.empty()) + if (first) + { + first = false; + linksValue += "{\"" ETW_FIELD_SPAN_LINKS_TO_SPAN_ID "\":\""; + } + else { - linksValue += ','; - linksValue += ToLowerBase16(ctx.span_id()); + linksValue += ",{\"" ETW_FIELD_SPAN_LINKS_TO_SPAN_ID "\":\""; } - idx++; + + linksValue += ToLowerBase16(ctx.span_id()); + linksValue += "\",\"" ETW_FIELD_SPAN_LINKS_TO_TRACE_ID "\":\""; + linksValue += ToLowerBase16(ctx.trace_id()); + linksValue += "\"}"; + return true; }); - attributes[ETW_FIELD_SPAN_LINKS] = linksValue; + + linksValue += "]"; + + attributes[ETW_FIELD_SPAN_LINKS] = std::move(linksValue); } }