-
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
base: main
Are you sure you want to change the base?
Changes from all commits
e29cb41
607fd1c
e5af77f
fad89b4
cbaf74e
2d2ae1a
4bcac3b
4bba95b
84046f1
b2f90e1
88690b0
b49fd2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,11 @@ find_package(jwt-cpp REQUIRED) | |
find_package(GTest REQUIRED) | ||
find_package(double-conversion REQUIRED) | ||
|
||
# OpenTelemetry | ||
if(YDB_SDK_TRACING) | ||
find_package(opentelemetry-cpp REQUIRED) | ||
endif() | ||
|
||
# RapidJSON | ||
if (YDB_SDK_USE_RAPID_JSON) | ||
find_package(RapidJSON REQUIRED) | ||
|
@@ -85,4 +90,4 @@ target_include_directories(nayuki_md5 PUBLIC | |
$<INSTALL_INTERFACE:third_party/nayuki_md5> | ||
) | ||
|
||
_ydb_sdk_install_targets(TARGETS nayuki_md5) | ||
_ydb_sdk_install_targets(TARGETS nayuki_md5) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лишние изменения |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Отступы в CMakeLists везде по 2 пробела |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
add_executable(tracing_example tracing_example.cpp) | ||
|
||
if(YDB_SDK_TRACING) | ||
target_link_libraries(tracing_example PRIVATE | ||
ydb-cpp-sdk | ||
${OPENTELEMETRY_LIBRARIES} | ||
) | ||
else() | ||
target_link_libraries(tracing_example PRIVATE ydb-cpp-sdk) | ||
target_compile_definitions(tracing_example PRIVATE -DYDB_SDK_TRACING_DISABLED) | ||
endif() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
target_link_libraries(tracing_example PRIVATE opentelemetry-cpp::api)
|
Gazizonoki marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#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() { | ||
auto exporter = opentelemetry::exporter::jaeger::JaegerExporterFactory::Create(); | ||
auto provider = opentelemetry::sdk::trace::TracerProviderFactory::Create(std::move(exporter)); | ||
auto otelTracer = provider->GetTracer("ydb-cpp-sdk"); | ||
|
||
auto ydbTracer = std::make_shared<NYdb::NTracing::TOpenTelemetryTracer>(otelTracer); | ||
|
||
auto driver = NYdb::TDriver( | ||
NYdb::TDriverConfig() | ||
.SetEndpoint("localhost:2136") | ||
.SetDatabase("/local") | ||
.SetTracer(ydbTracer) | ||
); | ||
|
||
auto client = NYdb::NTable::TTableClient(driver); | ||
auto session = client.CreateSession().GetValueSync(); | ||
session.ExecuteDataQuery("SELECT 1", NYdb::NTable::TTxControl::BeginTx().CommitTx()).GetValueSync(); | ||
|
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#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_; } | ||
|
||
static std::shared_ptr<TTraceContext> GenerateNew(); | ||
std::shared_ptr<TTraceContext> CreateChild() const; | ||
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; | ||
|
||
virtual std::string GetCurrentTraceParent() const { | ||
if (auto ctx = GetCurrentContext()) { | ||
return ctx->ToTraceParent(); | ||
} | ||
return ""; | ||
} | ||
}; | ||
|
||
} // namespace NTracing | ||
} // namespace NYdb |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#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 AddEvent(const std::string&, const TAttributeMap& = {}) override {} | ||
void SetStatus(bool, const std::string& = "") override {} | ||
void End() override {} | ||
|
||
const TTraceContext& GetContext() const override { | ||
static TTraceContext emptyContext("", ""); | ||
return emptyContext; | ||
} | ||
}; | ||
|
||
class TNoopTracer : public ITracer { | ||
public: | ||
std::unique_ptr<ISpan> StartSpan( | ||
const std::string&, | ||
const TAttributeMap& = {}, | ||
std::shared_ptr<TTraceContext> = nullptr) override | ||
{ | ||
return std::make_unique<TNoopSpan>(); | ||
} | ||
|
||
std::shared_ptr<TTraceContext> GetCurrentContext() const override { | ||
return nullptr; | ||
} | ||
}; | ||
|
||
} // namespace NTracing | ||
} // namespace NYdb |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Флаг не совсем корректно назван, мы включаем не трейсинг целиком, мы включаем именно конкретный opentelemetry плагин