Skip to content

KIKIMR-20533 Implemented conversion from OTEL header to traceId #607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions ydb/library/actors/wilson/wilson_trace.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,67 @@
#include "wilson_trace.h"

#include <util/generic/algorithm.h>
#include <util/string/hex.h>

namespace NWilson {
TTraceId TTraceId::FromTraceparentHeader(const TStringBuf header) {
constexpr size_t versionChars = 2; // Only version 0 is supported
constexpr size_t versionStart = 0;

constexpr size_t traceIdChars = 32;
constexpr size_t traceIdStart = versionStart + versionChars + 1;
static_assert(traceIdChars == TTraceId::GetTraceIdSize() * 2);

constexpr size_t parentSpanIdChars = 16;
constexpr size_t parentSpanIdStart = traceIdStart + traceIdChars + 1;
static_assert(parentSpanIdChars == TTraceId::GetSpanIdSize() * 2);

constexpr size_t traceFlagsChars = 2;
constexpr size_t traceFlagsStart = parentSpanIdStart + parentSpanIdChars + 1;

constexpr size_t expectedHeaderSize =
versionChars + traceIdChars + parentSpanIdChars + traceFlagsChars + 3;

if (header.Size() != expectedHeaderSize) {
return {};
}

auto isHex = [](char c) {
return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
};

if (!AllOf(header.substr(versionStart, versionChars), isHex) ||
!AllOf(header.substr(traceIdStart, traceIdChars), isHex) ||
!AllOf(header.substr(parentSpanIdStart, parentSpanIdChars), isHex) ||
!AllOf(header.substr(traceFlagsStart, traceFlagsChars), isHex)) {
return {};
}

if (header[traceIdStart - 1] != '-' || header[parentSpanIdStart - 1] != '-' || header[traceIdChars - 1] != '-') {
return {};
}

ui8 version;
HexDecode(header.Data(), versionChars, &version);
if (version != 0) {
return {};
}

TTrace traceId;
ui64 spanId;
static_assert(traceIdChars == 2 * sizeof(traceId));
static_assert(parentSpanIdChars == 2 * sizeof(spanId));
HexDecode(header.Data() + traceIdStart, traceIdChars, &traceId);
HexDecode(header.Data() + parentSpanIdStart, parentSpanIdChars, &spanId);

if ((traceId[0] == 0 && traceId[1] == 0) || spanId == 0) {
return {};
}

return TTraceId(traceId, spanId, 15, Max<ui32>());
}

TString TTraceId::GetHexTraceId() const {
return HexEncode(GetTraceIdPtr(), GetTraceIdSize());
}
}
10 changes: 4 additions & 6 deletions ydb/library/actors/wilson/wilson_trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

#include <library/cpp/string_utils/base64/base64.h>

#include <util/stream/output.h>
#include <util/random/random.h>
#include <util/random/fast.h>

#include <util/string/hex.h>
#include <util/stream/output.h>
#include <util/string/printf.h>

#include <array>
Expand Down Expand Up @@ -186,6 +184,8 @@ namespace NWilson {
return TTraceId();
}

static TTraceId FromTraceparentHeader(const TStringBuf header);

TTraceId Span(ui8 verbosity) const {
Validate();
if (!*this || !TimeToLive) {
Expand Down Expand Up @@ -223,9 +223,7 @@ namespace NWilson {
const void *GetSpanIdPtr() const { return &SpanId; }
static constexpr size_t GetSpanIdSize() { return sizeof(ui64); }

TString GetHexTraceId() const {
return HexEncode(GetTraceIdPtr(), GetTraceIdSize());
}
TString GetHexTraceId() const;

void Validate() const {
Y_DEBUG_ABORT_UNLESS(*this || !SpanId);
Expand Down