From 947831e650ae482faec0308955c5cbd19d96a6c1 Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Thu, 5 Sep 2024 16:44:50 -0700 Subject: [PATCH 1/5] [EXPORTER] Fix the format of SpanLink for ETW --- .../opentelemetry/exporters/etw/etw_fields.h | 5 +++- .../opentelemetry/exporters/etw/etw_tracer.h | 28 ++++++++++++++++--- 2 files changed, 28 insertions(+), 5 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..dd18e00891 100644 --- a/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h +++ b/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h @@ -214,17 +214,37 @@ class Tracer : public opentelemetry::trace::Tracer, { size_t idx = 0; 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 link. + const size_t linkValueSize = 80; + linksValue.reserve(linkValueSize + (links.size() - 1) * (linkValueSize - 1)); + linksValue += "["; + links.ForEachKeyValue([&](opentelemetry::trace::SpanContext ctx, const opentelemetry::common::KeyValueIterable &) { - if (!linksValue.empty()) + if (idx == 0) { - linksValue += ','; - linksValue += ToLowerBase16(ctx.span_id()); + linksValue += "{\"" ETW_FIELD_SPAN_LINKS_TO_SPAN_ID "\":\""; } + else { + linksValue += ",{\"" ETW_FIELD_SPAN_LINKS_TO_SPAN_ID "\":\""; + } + + linksValue += ToLowerBase16(ctx.span_id()); + linksValue += "\",\"" ETW_FIELD_SPAN_LINKS_TO_TRACE_ID "\":\""; + linksValue += ToLowerBase16(ctx.trace_id()); + linksValue += "\"}"; + idx++; return true; }); - attributes[ETW_FIELD_SPAN_LINKS] = linksValue; + + linksValue += "]"; + + attributes[ETW_FIELD_SPAN_LINKS] = std::move(linksValue); } } From 668887c62878bdab930041a4f9dd1a5960ac0fb4 Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Thu, 5 Sep 2024 16:48:48 -0700 Subject: [PATCH 2/5] Format --- exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h b/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h index dd18e00891..525a7aad02 100644 --- a/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h +++ b/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h @@ -229,7 +229,8 @@ class Tracer : public opentelemetry::trace::Tracer, { linksValue += "{\"" ETW_FIELD_SPAN_LINKS_TO_SPAN_ID "\":\""; } - else { + else + { linksValue += ",{\"" ETW_FIELD_SPAN_LINKS_TO_SPAN_ID "\":\""; } From 0801c49697306e1b881fe8a101636b3db38245f8 Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Fri, 6 Sep 2024 08:49:58 -0700 Subject: [PATCH 3/5] Fix naming --- .../etw/include/opentelemetry/exporters/etw/etw_tracer.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h b/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h index 525a7aad02..b4cb4c297d 100644 --- a/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h +++ b/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h @@ -218,9 +218,9 @@ class Tracer : public opentelemetry::trace::Tracer, // 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 link. - const size_t linkValueSize = 80; - linksValue.reserve(linkValueSize + (links.size() - 1) * (linkValueSize - 1)); + // 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, From c59c8c7b734da45108d2f9e2b456c1fbb2d64c72 Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Fri, 6 Sep 2024 09:01:52 -0700 Subject: [PATCH 4/5] Make the first element a bool variable --- .../etw/include/opentelemetry/exporters/etw/etw_tracer.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h b/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h index b4cb4c297d..ba613e4381 100644 --- a/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h +++ b/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h @@ -212,7 +212,7 @@ 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. @@ -225,8 +225,9 @@ class Tracer : public opentelemetry::trace::Tracer, links.ForEachKeyValue([&](opentelemetry::trace::SpanContext ctx, const opentelemetry::common::KeyValueIterable &) { - if (idx == 0) + if (first) { + first = false; linksValue += "{\"" ETW_FIELD_SPAN_LINKS_TO_SPAN_ID "\":\""; } else @@ -239,7 +240,6 @@ class Tracer : public opentelemetry::trace::Tracer, linksValue += ToLowerBase16(ctx.trace_id()); linksValue += "\"}"; - idx++; return true; }); From 88b581c6b6794ae998dbd8b4aef910daab220e38 Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Fri, 6 Sep 2024 09:04:42 -0700 Subject: [PATCH 5/5] Format code --- exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h b/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h index ba613e4381..272558cead 100644 --- a/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h +++ b/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h @@ -220,7 +220,8 @@ class Tracer : public opentelemetry::trace::Tracer, // [{"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.reserve(kSingleSpanLinkSizeInBytes + + (links.size() - 1) * (kSingleSpanLinkSizeInBytes - 1)); linksValue += "["; links.ForEachKeyValue([&](opentelemetry::trace::SpanContext ctx,