Skip to content

Commit b2f90e1

Browse files
Update tracer.h
1 parent 84046f1 commit b2f90e1

File tree

1 file changed

+55
-3
lines changed
  • include/ydb-cpp-sdk/client/tracing

1 file changed

+55
-3
lines changed

include/ydb-cpp-sdk/client/tracing/tracer.h

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,76 @@
11
#pragma once
22

3-
#include <memory>
43
#include <string>
4+
#include <memory>
5+
#include <unordered_map>
56

6-
namespace NYdb::inline V3 {
7+
namespace NYdb {
78
namespace NTracing {
89

10+
using TAttributeMap = std::unordered_map<std::string, std::string>;
11+
12+
// Контекст с идентификаторами трассировки
13+
class TTraceContext {
14+
public:
15+
TTraceContext(std::string traceId, std::string spanId, std::string parentSpanId = "")
16+
: TraceId_(std::move(traceId))
17+
, SpanId_(std::move(spanId))
18+
, ParentSpanId_(std::move(parentSpanId))
19+
{}
20+
21+
const std::string& GetTraceId() const { return TraceId_; }
22+
const std::string& GetSpanId() const { return SpanId_; }
23+
const std::string& GetParentSpanId() const { return ParentSpanId_; }
24+
25+
// Генерация нового контекста (создает новые уникальные traceId и spanId)
26+
static std::shared_ptr<TTraceContext> GenerateNew();
27+
28+
// Создание дочернего контекста (новый spanId, тот же traceId)
29+
std::shared_ptr<TTraceContext> CreateChild() const;
30+
31+
// Формирование W3C traceparent ("00-traceId-spanId-01")
32+
std::string ToTraceParent() const;
33+
34+
private:
35+
std::string TraceId_;
36+
std::string SpanId_;
37+
std::string ParentSpanId_;
38+
};
39+
40+
941
class ISpan {
1042
public:
1143
virtual ~ISpan() = default;
1244

1345
virtual void AddAttribute(const std::string& key, const std::string& value) = 0;
46+
virtual void AddEvent(const std::string& name, const TAttributeMap& attributes = {}) = 0;
47+
virtual void SetStatus(bool isError, const std::string& description = "") = 0;
1448
virtual void End() = 0;
49+
50+
virtual const TTraceContext& GetContext() const = 0;
1551
};
1652

53+
1754
class ITracer {
1855
public:
1956
virtual ~ITracer() = default;
2057

21-
virtual std::unique_ptr<ISpan> StartSpan(const std::string& name) = 0;
58+
// Создать новый спан с именем, атрибутами и опциональным родительским контекстом
59+
virtual std::unique_ptr<ISpan> StartSpan(
60+
const std::string& name,
61+
const TAttributeMap& attributes = {},
62+
std::shared_ptr<TTraceContext> parentContext = nullptr) = 0;
63+
64+
// Получить текущий контекст
65+
virtual std::shared_ptr<TTraceContext> GetCurrentContext() const = 0;
66+
67+
// Получить W3C traceparent текущего контекста (если есть)
68+
virtual std::string GetCurrentTraceParent() const {
69+
if (auto ctx = GetCurrentContext()) {
70+
return ctx->ToTraceParent();
71+
}
72+
return "";
73+
}
2274
};
2375

2476
} // namespace NTracing

0 commit comments

Comments
 (0)