|
1 | 1 | #pragma once
|
2 | 2 |
|
3 |
| -#include <memory> |
4 | 3 | #include <string>
|
| 4 | +#include <memory> |
| 5 | +#include <unordered_map> |
5 | 6 |
|
6 |
| -namespace NYdb::inline V3 { |
| 7 | +namespace NYdb { |
7 | 8 | namespace NTracing {
|
8 | 9 |
|
| 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 | + |
9 | 41 | class ISpan {
|
10 | 42 | public:
|
11 | 43 | virtual ~ISpan() = default;
|
12 | 44 |
|
13 | 45 | 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; |
14 | 48 | virtual void End() = 0;
|
| 49 | + |
| 50 | + virtual const TTraceContext& GetContext() const = 0; |
15 | 51 | };
|
16 | 52 |
|
| 53 | + |
17 | 54 | class ITracer {
|
18 | 55 | public:
|
19 | 56 | virtual ~ITracer() = default;
|
20 | 57 |
|
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 | + } |
22 | 74 | };
|
23 | 75 |
|
24 | 76 | } // namespace NTracing
|
|
0 commit comments