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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
use parity_scale_codec::Encode;
use polkadot_node_primitives::PoV;
use polkadot_primitives::v2::{
BlakeTwo256, CandidateHash, Hash, HashT, Id as ParaId, ValidatorIndex,
};
use sc_network::PeerId;
use std::{fmt, sync::Arc};
use super::INSTANCE;
#[derive(Debug)]
pub struct PerLeafSpan {
leaf_span: Arc<Span>,
span: Span,
}
impl PerLeafSpan {
pub fn new(leaf_span: Arc<Span>, name: &'static str) -> Self {
let span = leaf_span.child(name);
Self { span, leaf_span }
}
pub fn leaf_span(&self) -> &Arc<Span> {
&self.leaf_span
}
}
impl std::ops::Deref for PerLeafSpan {
type Target = Span;
fn deref(&self) -> &Span {
&self.span
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
#[non_exhaustive]
pub enum Stage {
CandidateBacking = 2,
StatementDistribution = 3,
PoVDistribution = 4,
AvailabilityDistribution = 5,
AvailabilityRecovery = 6,
BitfieldDistribution = 7,
ApprovalChecking = 8,
}
pub enum Span {
Enabled(mick_jaeger::Span),
Disabled,
}
pub(crate) type TraceIdentifier = u128;
#[inline]
pub fn hash_to_trace_identifier(hash: Hash) -> TraceIdentifier {
let mut buf = [0u8; 16];
buf.copy_from_slice(&hash.as_ref()[0..16]);
u128::from_be_bytes(buf) as TraceIdentifier
}
pub trait LazyIdent {
fn eval(&self) -> TraceIdentifier;
fn extra_tags(&self, _span: &mut Span) {}
}
impl<'a> LazyIdent for &'a [u8] {
fn eval(&self) -> TraceIdentifier {
hash_to_trace_identifier(BlakeTwo256::hash_of(self))
}
}
impl LazyIdent for &PoV {
fn eval(&self) -> TraceIdentifier {
hash_to_trace_identifier(self.hash())
}
fn extra_tags(&self, span: &mut Span) {
span.add_pov(self)
}
}
impl LazyIdent for Hash {
fn eval(&self) -> TraceIdentifier {
hash_to_trace_identifier(*self)
}
fn extra_tags(&self, span: &mut Span) {
span.add_string_fmt_debug_tag("relay-parent", self);
}
}
impl LazyIdent for &Hash {
fn eval(&self) -> TraceIdentifier {
hash_to_trace_identifier(**self)
}
fn extra_tags(&self, span: &mut Span) {
span.add_string_fmt_debug_tag("relay-parent", self);
}
}
impl LazyIdent for CandidateHash {
fn eval(&self) -> TraceIdentifier {
hash_to_trace_identifier(self.0)
}
fn extra_tags(&self, span: &mut Span) {
span.add_string_fmt_debug_tag("candidate-hash", &self.0);
span.add_string_tag("traceID", self.eval().to_string());
}
}
impl Span {
pub fn new<I: LazyIdent>(identifier: I, span_name: &'static str) -> Span {
let mut span = INSTANCE
.read_recursive()
.span(|| <I as LazyIdent>::eval(&identifier), span_name)
.into();
<I as LazyIdent>::extra_tags(&identifier, &mut span);
span
}
pub fn from_encodable<I: Encode>(identifier: I, span_name: &'static str) -> Span {
INSTANCE
.read_recursive()
.span(
move || {
let bytes = identifier.encode();
LazyIdent::eval(&bytes.as_slice())
},
span_name,
)
.into()
}
pub fn child(&self, name: &'static str) -> Self {
match self {
Self::Enabled(inner) => Self::Enabled(inner.child(name)),
Self::Disabled => Self::Disabled,
}
}
#[inline(always)]
pub fn with_string_tag<V: ToString>(mut self, tag: &'static str, val: V) -> Self {
self.add_string_tag::<V>(tag, val);
self
}
#[inline(always)]
pub fn with_peer_id(self, peer: &PeerId) -> Self {
self.with_string_tag("peer-id", &peer.to_base58())
}
#[inline(always)]
pub fn with_candidate(self, candidate_hash: CandidateHash) -> Self {
self.with_string_fmt_debug_tag("candidate-hash", &candidate_hash.0)
}
#[inline(always)]
pub fn with_para_id(self, para_id: ParaId) -> Self {
self.with_int_tag("para-id", u32::from(para_id) as i64)
}
#[inline(always)]
pub fn with_stage(self, stage: Stage) -> Self {
self.with_string_tag("candidate-stage", stage as u8)
}
#[inline(always)]
pub fn with_validator_index(self, validator: ValidatorIndex) -> Self {
self.with_string_tag("validator-index", &validator.0)
}
#[inline(always)]
pub fn with_chunk_index(self, chunk_index: u32) -> Self {
self.with_string_tag("chunk-index", chunk_index)
}
#[inline(always)]
pub fn with_relay_parent(self, relay_parent: Hash) -> Self {
self.with_string_fmt_debug_tag("relay-parent", relay_parent)
}
#[inline(always)]
pub fn with_claimed_validator_index(self, claimed_validator_index: ValidatorIndex) -> Self {
self.with_string_tag("claimed-validator", &claimed_validator_index.0)
}
#[inline(always)]
pub fn with_pov(mut self, pov: &PoV) -> Self {
self.add_pov(pov);
self
}
#[inline(always)]
pub fn with_int_tag(mut self, tag: &'static str, i: i64) -> Self {
self.add_int_tag(tag, i);
self
}
#[inline(always)]
pub fn with_uint_tag(mut self, tag: &'static str, u: u64) -> Self {
self.add_uint_tag(tag, u);
self
}
#[inline(always)]
pub fn with_string_fmt_debug_tag<V: fmt::Debug>(mut self, tag: &'static str, val: V) -> Self {
self.add_string_tag(tag, format!("{:?}", val));
self
}
#[inline(always)]
pub fn add_follows_from(&mut self, other: &Self) {
match (self, other) {
(Self::Enabled(ref mut inner), Self::Enabled(ref other_inner)) =>
inner.add_follows_from(&other_inner),
_ => {},
}
}
#[inline(always)]
pub fn add_pov(&mut self, pov: &PoV) {
if self.is_enabled() {
self.add_string_fmt_debug_tag("pov", pov.hash());
}
}
#[inline(always)]
pub fn add_para_id(&mut self, para_id: ParaId) {
self.add_int_tag("para-id", u32::from(para_id) as i64);
}
pub fn add_string_tag<V: ToString>(&mut self, tag: &'static str, val: V) {
match self {
Self::Enabled(ref mut inner) => inner.add_string_tag(tag, val.to_string().as_str()),
Self::Disabled => {},
}
}
pub fn add_string_fmt_debug_tag<V: fmt::Debug>(&mut self, tag: &'static str, val: V) {
match self {
Self::Enabled(ref mut inner) =>
inner.add_string_tag(tag, format!("{:?}", val).as_str()),
Self::Disabled => {},
}
}
pub fn add_int_tag(&mut self, tag: &'static str, value: i64) {
match self {
Self::Enabled(ref mut inner) => inner.add_int_tag(tag, value),
Self::Disabled => {},
}
}
pub fn add_uint_tag(&mut self, tag: &'static str, value: u64) {
match self {
Self::Enabled(ref mut inner) => inner.add_int_tag(tag, value as i64),
Self::Disabled => {},
}
}
pub const fn is_enabled(&self) -> bool {
match self {
Span::Enabled(_) => true,
_ => false,
}
}
pub fn trace_id(&self) -> Option<TraceIdentifier> {
match self {
Span::Enabled(inner) => Some(inner.trace_id().get()),
_ => None,
}
}
}
impl std::fmt::Debug for Span {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "<jaeger span>")
}
}
impl From<Option<mick_jaeger::Span>> for Span {
fn from(src: Option<mick_jaeger::Span>) -> Self {
if let Some(span) = src {
Self::Enabled(span)
} else {
Self::Disabled
}
}
}
impl From<mick_jaeger::Span> for Span {
fn from(src: mick_jaeger::Span) -> Self {
Self::Enabled(src)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Jaeger;
const RAW: [u8; 32] = [
0xFF, 0xAA, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x78, 0x89, 0x9A, 0xAB, 0xBC, 0xCD, 0xDE,
0xEF, 0x00, 0x01, 0x02, 0x03, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,
0x0E, 0x0F,
];
#[test]
fn hash_derived_identifier_is_leading_16bytes() {
let candidate_hash = dbg!(Hash::from(&RAW));
let trace_id = dbg!(hash_to_trace_identifier(candidate_hash));
for (idx, (a, b)) in candidate_hash
.as_bytes()
.iter()
.take(16)
.zip(trace_id.to_be_bytes().iter())
.enumerate()
{
assert_eq!(*a, *b, "Index [{}] does not match: {} != {}", idx, a, b);
}
}
#[test]
fn extra_tags_do_not_change_trace_id() {
Jaeger::test_setup();
let candidate_hash = dbg!(Hash::from(&RAW));
let trace_id = hash_to_trace_identifier(candidate_hash);
let span = Span::new(candidate_hash, "foo");
assert_eq!(span.trace_id(), Some(trace_id));
let span = span.with_int_tag("tag", 7i64);
assert_eq!(span.trace_id(), Some(trace_id));
}
}