Skip to content

Commit e902e8a

Browse files
Create trace_utils.h
1 parent 62f5cb2 commit e902e8a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <util/random/random.h>
5+
#include <util/string/hex.h>
6+
7+
namespace NYdb {
8+
9+
class TTraceUtils {
10+
public:
11+
// Генерация traceparent в формате W3C Trace Context
12+
static std::string GenerateTraceParent() {
13+
// Формат: 00-<trace-id>-<span-id>-<flags>
14+
return "00-" + GenerateRandomHex(32) + "-" + GenerateRandomHex(16) + "-01";
15+
}
16+
17+
private:
18+
// Генерация случайной hex-строки заданной длины
19+
static std::string GenerateRandomHex(size_t length) {
20+
const size_t byteLength = length / 2;
21+
std::string bytes(byteLength, "\0");
22+
23+
for (size_t i = 0; i < byteLength; ++i) {
24+
bytes[i] = static_cast<char>(RandomNumber<ui8>());
25+
}
26+
27+
return HexEncode(bytes.data(), bytes.size());
28+
}
29+
};
30+
} // namespace NYdb

0 commit comments

Comments
 (0)