1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
#pragma once
#include <library/cpp/actors/core/monotonic.h>
#include <library/cpp/actors/protos/actors.pb.h>
#include <library/cpp/string_utils/base64/base64.h>
#include <util/stream/output.h>
#include <util/random/random.h>
#include <util/random/fast.h>
#include <util/string/printf.h>
#include <array>
namespace NWilson {
class TTraceId {
using TTrace = std::array<ui64, 2>;
TTrace TraceId; // Random id of topmost client request
ui64 SpanId;
union {
struct {
ui32 Verbosity : 4;
ui32 TimeToLive : 12;
};
ui32 Raw;
};
private:
TTraceId(TTrace traceId, ui64 spanId, ui8 verbosity, ui32 timeToLive)
: TraceId(traceId)
{
if (timeToLive == Max<ui32>()) {
timeToLive = 4095;
}
Y_ABORT_UNLESS(verbosity <= 15);
Y_ABORT_UNLESS(timeToLive <= 4095);
SpanId = spanId;
Verbosity = verbosity;
TimeToLive = timeToLive;
}
static TTrace GenerateTraceId() {
for (;;) {
TTrace res;
ui32 *p = reinterpret_cast<ui32*>(res.data());
TReallyFastRng32 rng(RandomNumber<ui64>());
p[0] = rng();
p[1] = rng();
p[2] = rng();
p[3] = rng();
if (res[0] || res[1]) {
return res;
}
}
}
static ui64 GenerateSpanId() {
for (;;) {
if (const ui64 res = RandomNumber<ui64>(); res) { // SpanId can't be zero
return res;
}
}
}
public:
using TSerializedTraceId = char[sizeof(TTrace) + sizeof(ui64) + sizeof(ui32)];
public:
TTraceId(ui64) // NBS stub
: TTraceId()
{}
TTraceId() {
TraceId.fill(0);
SpanId = 0;
Raw = 0;
}
explicit TTraceId(TTrace traceId)
: TraceId(traceId)
{
SpanId = 0;
Raw = 0;
}
// allow move semantic
TTraceId(TTraceId&& other)
: TraceId(other.TraceId)
, SpanId(other.SpanId)
, Raw(other.Raw)
{
other.TraceId.fill(0);
other.SpanId = 1; // make it explicitly invalid
other.Raw = 0;
}
// explicit copy
explicit TTraceId(const TTraceId& other)
: TraceId(other.TraceId)
, SpanId(other.SpanId)
, Raw(other.Raw)
{
// validate trace id only when we are making a copy
other.Validate();
}
TTraceId(const TSerializedTraceId& in) {
const char *p = in;
memcpy(TraceId.data(), p, sizeof(TraceId));
p += sizeof(TraceId);
memcpy(&SpanId, p, sizeof(SpanId));
p += sizeof(SpanId);
memcpy(&Raw, p, sizeof(Raw));
p += sizeof(Raw);
Y_DEBUG_ABORT_UNLESS(p - in == sizeof(TSerializedTraceId));
}
TTraceId(const NActorsProto::TTraceId& pb)
: TTraceId()
{
if (pb.HasData()) {
const auto& data = pb.GetData();
if (data.size() == sizeof(TSerializedTraceId)) {
*this = *reinterpret_cast<const TSerializedTraceId*>(data.data());
}
}
}
void Serialize(TSerializedTraceId *out) const {
char *p = *out;
memcpy(p, TraceId.data(), sizeof(TraceId));
p += sizeof(TraceId);
memcpy(p, &SpanId, sizeof(SpanId));
p += sizeof(SpanId);
memcpy(p, &Raw, sizeof(Raw));
p += sizeof(Raw);
Y_DEBUG_ABORT_UNLESS(p - *out == sizeof(TSerializedTraceId));
}
void Serialize(NActorsProto::TTraceId *pb) const {
if (*this) {
TSerializedTraceId data;
Serialize(&data);
pb->SetData(reinterpret_cast<const char*>(&data), sizeof(data));
}
}
TTraceId& operator=(TTraceId&& other) {
if (this != &other) {
TraceId = other.TraceId;
SpanId = other.SpanId;
Raw = other.Raw;
other.TraceId.fill(0);
other.SpanId = 1; // make it explicitly invalid
other.Raw = 0;
}
return *this;
}
// do not allow implicit copy of trace id
TTraceId& operator=(const TTraceId& other) = delete;
static TTraceId NewTraceId(ui8 verbosity, ui32 timeToLive) {
return TTraceId(GenerateTraceId(), 0, verbosity, timeToLive);
}
static TTraceId NewTraceIdThrottled(ui8 verbosity, ui32 timeToLive, std::atomic<NActors::TMonotonic>& counter,
NActors::TMonotonic now, TDuration periodBetweenSamples) {
static_assert(std::atomic<NActors::TMonotonic>::is_always_lock_free);
for (;;) {
NActors::TMonotonic ts = counter.load();
if (now < ts) {
return {};
} else if (counter.compare_exchange_strong(ts, now + periodBetweenSamples)) {
return NewTraceId(verbosity, timeToLive);
}
}
}
static TTraceId NewTraceId() { // NBS stub
return TTraceId();
}
TTraceId Span(ui8 verbosity) const {
Validate();
if (!*this || !TimeToLive) {
return TTraceId();
} else if (verbosity <= Verbosity) {
return TTraceId(TraceId, GenerateSpanId(), Verbosity, TimeToLive - 1);
} else {
return TTraceId(TraceId, SpanId, Verbosity, TimeToLive - 1);
}
}
TTraceId Span() const { // compatibility stub
return {};
}
// Check if request tracing is enabled
explicit operator bool() const {
return TraceId[0] || TraceId[1];
}
bool IsRoot() const {
return !SpanId;
}
friend bool operator==(const TTraceId& x, const TTraceId& y) {
return x.TraceId == y.TraceId && x.SpanId == y.SpanId && x.Raw == y.Raw;
}
ui8 GetVerbosity() const {
return Verbosity;
}
const void *GetTraceIdPtr() const { return TraceId.data(); }
static constexpr size_t GetTraceIdSize() { return sizeof(TTrace); }
const void *GetSpanIdPtr() const { return &SpanId; }
static constexpr size_t GetSpanIdSize() { return sizeof(ui64); }
void Validate() const {
Y_DEBUG_ABORT_UNLESS(*this || !SpanId);
}
// for compatibility with NBS
TTraceId Clone() const { return NWilson::TTraceId(*this); }
ui64 GetTraceId() const { return 0; }
void OutputSpanId(IOutputStream&) const {}
};
}
|