Skip to content

Commit

Permalink
[EXPORTER] Fix the format of SpanLink for ETW (#3054)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomsonTan authored Sep 8, 2024
1 parent d623bc5 commit 4eb78a6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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

Expand Down
34 changes: 28 additions & 6 deletions exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 4eb78a6

Please sign in to comment.