-
Notifications
You must be signed in to change notification settings - Fork 20
Support tracing in SDK and add loading to OpenTelemetry client #517
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
Open
zarinatlupova
wants to merge
12
commits into
ydb-platform:main
Choose a base branch
from
zarinatlupova:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e29cb41
Create tracing_example.cpp
zarinatlupova 607fd1c
Update CMakeLists.txt
zarinatlupova e5af77f
Create tracer.h
zarinatlupova fad89b4
Create otel_tracer.h
zarinatlupova cbaf74e
Create noop_tracer.h
zarinatlupova 2d2ae1a
Update driver.h
zarinatlupova 4bcac3b
Update request_settings.h
zarinatlupova 4bba95b
Update grpc_connections.h
zarinatlupova 84046f1
Update compose.yml
zarinatlupova b2f90e1
Update tracer.h
zarinatlupova 88690b0
Update otel_tracer.h
zarinatlupova b49fd2d
Support tracing in SDK
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <ydb-cpp-sdk/client/tracing/otel_tracer.h> | ||
#include <opentelemetry/sdk/trace/tracer_provider.h> | ||
#include <opentelemetry/exporters/jaeger/jaeger_exporter.h> | ||
#include <ydb-cpp-sdk/client/driver.h> | ||
|
||
int main() { | ||
// 1. Настройка OpenTelemetry с экспортером в Jaeger | ||
Gazizonoki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
auto exporter = opentelemetry::exporter::jaeger::JaegerExporterFactory::Create(); | ||
auto provider = opentelemetry::sdk::trace::TracerProviderFactory::Create(std::move(exporter)); | ||
auto otel_tracer = provider->GetTracer("ydb-cpp-sdk"); | ||
|
||
// 2. Создание адаптера для YDB SDK | ||
auto ydb_tracer = std::make_shared<NYdb::NTracing::TOpenTelemetryTracer>(otel_tracer); | ||
|
||
// 3. Инициализация драйвера YDB с трейсером | ||
auto driver = NYdb::TDriver( | ||
NYdb::TDriverConfig() | ||
.SetEndpoint("grpc://localhost:2136") | ||
.SetDatabase("/local") | ||
.SetTracer(ydb_tracer) | ||
); | ||
Gazizonoki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 4. Тестовый запрос (спан создастся автоматически внутри SDK) | ||
auto client = NYdb::NTable::TTableClient(driver); | ||
auto session = client.CreateSession().GetValueSync(); | ||
session.ExecuteDataQuery("SELECT 1", NYdb::NTable::TTxControl::BeginTx().CommitTx()).GetValueSync(); | ||
|
||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Gazizonoki marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
|
||
#include <ydb-cpp-sdk/client/tracing/tracer.h> | ||
|
||
namespace NYdb::inline V3 { | ||
namespace NTracing { | ||
|
||
class TNoopSpan : public ISpan { | ||
public: | ||
void AddAttribute(const std::string&, const std::string&) override {} | ||
void End() override {} | ||
}; | ||
|
||
class TNoopTracer : public ITracer { | ||
public: | ||
std::unique_ptr<ISpan> StartSpan(const std::string&) override { | ||
return std::make_unique<TNoopSpan>(); | ||
} | ||
}; | ||
|
||
} // namespace NTracing | ||
} // namespace NYdb |
Gazizonoki marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#pragma once | ||
|
||
#include "tracer.h" | ||
|
||
#include <opentelemetry/trace/tracer.h> | ||
#include <opentelemetry/trace/span.h> | ||
#include <opentelemetry/trace/span_context.h> | ||
#include <opentelemetry/context/runtime_context.h> | ||
#include <opentelemetry/common/key_value_iterable_view.h> | ||
|
||
namespace NYdb { | ||
namespace NTracing { | ||
|
||
class OpenTelemetrySpan : public ISpan { | ||
Gazizonoki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public: | ||
OpenTelemetrySpan(opentelemetry::trace::Span span, std::shared_ptr<TTraceContext> context) | ||
: span_(std::move(span)), context_(std::move(context)) {} | ||
|
||
void AddAttribute(const std::string& key, const std::string& value) override { | ||
span_.SetAttribute(key, value); | ||
} | ||
|
||
void AddEvent(const std::string& name, const TAttributeMap& attributes = {}) override { | ||
// Преобразуем std::unordered_map в вектор KeyValue для OpenTelemetry | ||
std::vector<opentelemetry::common::KeyValue> otelAttributes; | ||
otelAttributes.reserve(attributes.size()); | ||
for (const auto& [k, v] : attributes) { | ||
otelAttributes.emplace_back(k, v); | ||
} | ||
span_.AddEvent(name, otelAttributes); | ||
} | ||
|
||
void SetStatus(bool isError, const std::string& description = "") override { | ||
span_.SetStatus(isError ? opentelemetry::trace::StatusCode::kError | ||
: opentelemetry::trace::StatusCode::kOk, | ||
description); | ||
} | ||
|
||
void End() override { | ||
span_.End(); | ||
} | ||
|
||
const TTraceContext& GetContext() const override { | ||
return *context_; | ||
} | ||
|
||
private: | ||
opentelemetry::trace::Span span_; | ||
std::shared_ptr<TTraceContext> context_; | ||
}; | ||
|
||
|
||
class OpenTelemetryTracer : public ITracer { | ||
Gazizonoki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public: | ||
explicit OpenTelemetryTracer(std::shared_ptr<opentelemetry::trace::Tracer> tracer) | ||
: tracer_(std::move(tracer)) {} | ||
|
||
std::unique_ptr<ISpan> StartSpan( | ||
const std::string& name, | ||
const TAttributeMap& attributes = {}, | ||
std::shared_ptr<TTraceContext> parentContext = nullptr | ||
) override { | ||
opentelemetry::context::Context otelContext; | ||
|
||
if (parentContext) { | ||
auto traceId = opentelemetry::trace::TraceId::FromHex(parentContext->GetTraceId()); | ||
auto spanId = opentelemetry::trace::SpanId::FromHex(parentContext->GetSpanId()); | ||
|
||
auto spanContext = opentelemetry::trace::SpanContext::Create( | ||
traceId, | ||
spanId, | ||
opentelemetry::trace::TraceFlags::kIsSampled, | ||
false // remote | ||
); | ||
|
||
otelContext = opentelemetry::context::Context{}.SetValue( | ||
opentelemetry::trace::kSpanKey, | ||
opentelemetry::trace::Span{spanContext}); | ||
} | ||
|
||
auto span = tracer_->StartSpan(name, attributes, otelContext); | ||
|
||
auto context = parentContext ? parentContext->CreateChild() : TTraceContext::GenerateNew(); | ||
|
||
return std::make_unique<OpenTelemetrySpan>(std::move(span), std::move(context)); | ||
} | ||
|
||
std::shared_ptr<TTraceContext> GetCurrentContext() const override { | ||
auto currentSpan = opentelemetry::trace::GetCurrentSpan(); | ||
if (!currentSpan.IsValid()) { | ||
return nullptr; | ||
} | ||
auto spanContext = currentSpan.GetContext(); | ||
|
||
return std::make_shared<TTraceContext>( | ||
spanContext.trace_id().ToHex(), | ||
spanContext.span_id().ToHex(), | ||
spanContext.IsValid() ? spanContext.trace_id().ToHex() : ""); | ||
} | ||
|
||
private: | ||
std::shared_ptr<opentelemetry::trace::Tracer> tracer_; | ||
}; | ||
|
||
} // namespace NTracing | ||
} // namespace NYdb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <memory> | ||
#include <unordered_map> | ||
|
||
namespace NYdb { | ||
namespace NTracing { | ||
|
||
using TAttributeMap = std::unordered_map<std::string, std::string>; | ||
|
||
// Контекст с идентификаторами трассировки | ||
class TTraceContext { | ||
Gazizonoki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public: | ||
TTraceContext(std::string traceId, std::string spanId, std::string parentSpanId = "") | ||
: TraceId_(std::move(traceId)) | ||
, SpanId_(std::move(spanId)) | ||
, ParentSpanId_(std::move(parentSpanId)) | ||
{} | ||
|
||
const std::string& GetTraceId() const { return TraceId_; } | ||
const std::string& GetSpanId() const { return SpanId_; } | ||
const std::string& GetParentSpanId() const { return ParentSpanId_; } | ||
|
||
// Генерация нового контекста (создает новые уникальные traceId и spanId) | ||
static std::shared_ptr<TTraceContext> GenerateNew(); | ||
|
||
// Создание дочернего контекста (новый spanId, тот же traceId) | ||
std::shared_ptr<TTraceContext> CreateChild() const; | ||
|
||
// Формирование W3C traceparent ("00-traceId-spanId-01") | ||
std::string ToTraceParent() const; | ||
|
||
private: | ||
std::string TraceId_; | ||
std::string SpanId_; | ||
std::string ParentSpanId_; | ||
}; | ||
|
||
|
||
class ISpan { | ||
public: | ||
virtual ~ISpan() = default; | ||
|
||
virtual void AddAttribute(const std::string& key, const std::string& value) = 0; | ||
virtual void AddEvent(const std::string& name, const TAttributeMap& attributes = {}) = 0; | ||
virtual void SetStatus(bool isError, const std::string& description = "") = 0; | ||
virtual void End() = 0; | ||
|
||
virtual const TTraceContext& GetContext() const = 0; | ||
}; | ||
|
||
|
||
class ITracer { | ||
public: | ||
virtual ~ITracer() = default; | ||
|
||
// Создать новый спан с именем, атрибутами и опциональным родительским контекстом | ||
virtual std::unique_ptr<ISpan> StartSpan( | ||
const std::string& name, | ||
const TAttributeMap& attributes = {}, | ||
std::shared_ptr<TTraceContext> parentContext = nullptr) = 0; | ||
|
||
// Получить текущий контекст | ||
virtual std::shared_ptr<TTraceContext> GetCurrentContext() const = 0; | ||
|
||
// Получить W3C traceparent текущего контекста (если есть) | ||
virtual std::string GetCurrentTraceParent() const { | ||
if (auto ctx = GetCurrentContext()) { | ||
return ctx->ToTraceParent(); | ||
} | ||
return ""; | ||
} | ||
}; | ||
|
||
} // namespace NTracing | ||
} // namespace NYdb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.