1
1
#include "config.h"
2
2
#include <assert.h>
3
+ #include <ccan/endian/endian.h>
3
4
#include <ccan/err/err.h>
4
5
#include <ccan/htable/htable.h>
5
6
#include <ccan/str/hex/hex.h>
8
9
#include <ccan/time/time.h>
9
10
#include <common/json_stream.h>
10
11
#include <common/memleak.h>
12
+ #include <common/pseudorand.h>
11
13
#include <common/trace.h>
12
14
#include <fcntl.h>
13
- #include <sodium/randombytes.h>
14
15
#include <stdio.h>
15
16
#include <unistd.h>
16
17
19
20
20
21
#define MAX_ACTIVE_SPANS 128
21
22
22
- #define HEX_SPAN_ID_SIZE (2*SPAN_ID_SIZE+1)
23
- #define HEX_TRACE_ID_SIZE (2 * TRACE_ID_SIZE + 1)
24
-
25
23
/* The traceperent format is defined in W3C Trace Context RFC[1].
26
24
* Its format is defined as
27
25
*
@@ -56,13 +54,13 @@ struct span_tag {
56
54
57
55
struct span {
58
56
/* Our own id */
59
- u8 id [ SPAN_ID_SIZE ] ;
57
+ u64 id ;
60
58
61
59
/* 0 if we have no parent. */
62
- u8 parent_id [ SPAN_ID_SIZE ] ;
60
+ u64 parent_id ;
63
61
64
62
/* The trace_id for this span and all its children. */
65
- u8 trace_id [ TRACE_ID_SIZE ] ;
63
+ u64 trace_id_hi , trace_id_lo ;
66
64
67
65
u64 start_time ;
68
66
u64 end_time ;
@@ -94,7 +92,7 @@ static void init_span(struct span *s,
94
92
struct timeabs now = time_now ();
95
93
96
94
s -> key = key ;
97
- randombytes_buf ( s -> id , SPAN_ID_SIZE );
95
+ s -> id = pseudorand_u64 ( );
98
96
s -> start_time = (now .ts .tv_sec * 1000000 ) + now .ts .tv_nsec / 1000 ;
99
97
s -> parent = parent ;
100
98
s -> name = name ;
@@ -103,10 +101,12 @@ static void init_span(struct span *s,
103
101
/* If this is a new root span we also need to associate a new
104
102
* trace_id with it. */
105
103
if (!s -> parent ) {
106
- randombytes_buf (s -> trace_id , TRACE_ID_SIZE );
104
+ s -> trace_id_hi = pseudorand_u64 ();
105
+ s -> trace_id_lo = pseudorand_u64 ();
107
106
} else {
108
- memcpy (s -> parent_id , parent -> id , SPAN_ID_SIZE );
109
- memcpy (s -> trace_id , parent -> trace_id , TRACE_ID_SIZE );
107
+ s -> parent_id = current -> id ;
108
+ s -> trace_id_hi = current -> trace_id_hi ;
109
+ s -> trace_id_lo = current -> trace_id_lo ;
110
110
}
111
111
}
112
112
@@ -122,7 +122,9 @@ static void trace_span_clear(struct span *s);
122
122
* own parent. */
123
123
static void trace_inject_traceparent (void )
124
124
{
125
- char * traceparent ;
125
+ const char * traceparent ;
126
+ be64 trace_hi , trace_lo , span ;
127
+
126
128
traceparent = getenv ("CLN_TRACEPARENT" );
127
129
if (!traceparent )
128
130
return ;
@@ -134,14 +136,18 @@ static void trace_inject_traceparent(void)
134
136
init_span (current , trace_key (active_spans ), "" , NULL );
135
137
current -> remote = true;
136
138
assert (current && !current -> parent );
137
- if (! hex_decode ( traceparent + 3 , 2 * TRACE_ID_SIZE , current -> trace_id ,
138
- TRACE_ID_SIZE ) ||
139
- !hex_decode (traceparent + 36 , 2 * SPAN_ID_SIZE , current -> id ,
140
- SPAN_ID_SIZE )) {
139
+
140
+ if (! hex_decode ( traceparent + 3 , 16 , & trace_hi , sizeof ( trace_hi ))
141
+ || !hex_decode (traceparent + 3 + 16 , 16 , & trace_lo , sizeof ( trace_lo ))
142
+ || ! hex_decode ( traceparent + 3 + 16 + 16 + 1 , 16 , & span , sizeof ( span ) )) {
141
143
/* We failed to parse the traceparent, abandon. */
142
144
fprintf (stderr , "Failed!" );
143
145
trace_span_clear (current );
144
146
current = NULL ;
147
+ } else {
148
+ current -> trace_id_hi = be64_to_cpu (trace_hi );
149
+ current -> trace_id_lo = be64_to_cpu (trace_lo );
150
+ current -> id = be64_to_cpu (span );
145
151
}
146
152
}
147
153
@@ -261,9 +267,7 @@ static struct span *trace_span_slot(void)
261
267
262
268
static void trace_emit (struct span * s )
263
269
{
264
- char span_id [HEX_SPAN_ID_SIZE ];
265
- char trace_id [HEX_TRACE_ID_SIZE ];
266
- char parent_span_id [HEX_SPAN_ID_SIZE ];
270
+ char span_id [hex_str_size (sizeof (s -> id ))];
267
271
char buffer [MAX_BUF_SIZE + 1 ];
268
272
size_t len ;
269
273
@@ -273,12 +277,7 @@ static void trace_emit(struct span *s)
273
277
if (s -> remote )
274
278
return ;
275
279
276
- hex_encode (s -> id , SPAN_ID_SIZE , span_id , HEX_SPAN_ID_SIZE );
277
- hex_encode (s -> trace_id , TRACE_ID_SIZE , trace_id , HEX_TRACE_ID_SIZE );
278
-
279
- if (s -> parent )
280
- hex_encode (s -> parent_id , SPAN_ID_SIZE , parent_span_id , HEX_SPAN_ID_SIZE );
281
-
280
+ snprintf (span_id , sizeof (span_id ), "%016" PRIx64 , s -> id );
282
281
len = snprintf (buffer , MAX_BUF_SIZE ,
283
282
"[{\"id\":\"%s\",\"name\":\"%s\","
284
283
"\"timestamp\":%" PRIu64 ",\"duration\":%" PRIu64 ","
@@ -287,8 +286,8 @@ static void trace_emit(struct span *s)
287
286
288
287
if (s -> parent != NULL ) {
289
288
len += snprintf (buffer + len , MAX_BUF_SIZE - len ,
290
- "\"parentId\":\"%s \"," ,
291
- parent_span_id );
289
+ "\"parentId\":\"%016" PRIx64 " \"," ,
290
+ s -> parent_id );
292
291
if (len > MAX_BUF_SIZE )
293
292
len = MAX_BUF_SIZE ;
294
293
}
@@ -310,10 +309,12 @@ static void trace_emit(struct span *s)
310
309
}
311
310
312
311
len += snprintf (buffer + len , MAX_BUF_SIZE - len ,
313
- "},\"traceId\":\"%s\"}]" , trace_id );
312
+ "},\"traceId\":\"%016" PRIx64 "%016" PRIx64 "\"}]" ,
313
+ s -> trace_id_hi , s -> trace_id_lo );
314
314
if (len > MAX_BUF_SIZE )
315
315
len = MAX_BUF_SIZE ;
316
316
buffer [len ] = '\0' ;
317
+ /* FIXME: span_id here is in hex, could be u64? */
317
318
DTRACE_PROBE2 (lightningd , span_emit , span_id , buffer );
318
319
if (trace_to_file ) {
319
320
fprintf (trace_to_file , "span_emit %s %s\n" , span_id , buffer );
@@ -326,14 +327,7 @@ static void trace_emit(struct span *s)
326
327
*/
327
328
static void trace_span_clear (struct span * s )
328
329
{
329
- s -> key = 0 ;
330
- memset (s -> id , 0 , SPAN_ID_SIZE );
331
- memset (s -> trace_id , 0 , TRACE_ID_SIZE );
332
-
333
- s -> parent = NULL ;
334
- s -> name = NULL ;
335
- for (size_t i = 0 ; i < SPAN_MAX_TAGS ; i ++ )
336
- s -> tags [i ].name = NULL ;
330
+ memset (s , 0 , sizeof (* s ));
337
331
}
338
332
339
333
void trace_span_start_ (const char * name , const void * key )
@@ -354,14 +348,12 @@ void trace_span_start_(const char *name, const void *key)
354
348
trace_check_tree ();
355
349
DTRACE_PROBE1 (lightningd , span_start , s -> id );
356
350
if (trace_to_file ) {
357
- char span_id [HEX_SPAN_ID_SIZE ];
358
- hex_encode (s -> id , SPAN_ID_SIZE , span_id , HEX_SPAN_ID_SIZE );
359
- fprintf (trace_to_file , "span_start %s\n" , span_id );
351
+ fprintf (trace_to_file , "span_start %016" PRIx64 "\n" , s -> id );
360
352
fflush (trace_to_file );
361
353
}
362
354
}
363
355
364
- void trace_span_remote (u8 trace_id [ TRACE_ID_SIZE ], u8 span_id [ SPAN_ID_SIZE ] )
356
+ void trace_span_remote (u64 trace_id_hi , u64 trade_id_lo , u64 span_id )
365
357
{
366
358
abort ();
367
359
}
@@ -382,9 +374,7 @@ void trace_span_end(const void *key)
382
374
s -> end_time = (now .ts .tv_sec * 1000000 ) + now .ts .tv_nsec / 1000 ;
383
375
DTRACE_PROBE1 (lightningd , span_end , s -> id );
384
376
if (trace_to_file ) {
385
- char span_id [HEX_SPAN_ID_SIZE ];
386
- hex_encode (s -> id , SPAN_ID_SIZE , span_id , HEX_SPAN_ID_SIZE );
387
- fprintf (trace_to_file , "span_end %s\n" , span_id );
377
+ fprintf (trace_to_file , "span_end %016" PRIx64 "\n" , s -> id );
388
378
fflush (trace_to_file );
389
379
}
390
380
trace_emit (s );
@@ -439,9 +429,7 @@ void trace_span_suspend_(const void *key, const char *lbl)
439
429
current = current -> parent ;
440
430
DTRACE_PROBE1 (lightningd , span_suspend , span -> id );
441
431
if (trace_to_file ) {
442
- char span_id [HEX_SPAN_ID_SIZE ];
443
- hex_encode (span -> id , SPAN_ID_SIZE , span_id , HEX_SPAN_ID_SIZE );
444
- fprintf (trace_to_file , "span_suspend %s\n" , span_id );
432
+ fprintf (trace_to_file , "span_suspend %016" PRIx64 "\n" , span -> id );
445
433
fflush (trace_to_file );
446
434
}
447
435
}
@@ -482,9 +470,7 @@ void trace_span_resume_(const void *key, const char *lbl)
482
470
TRACE_DBG ("Resuming span %s (%zu)\n" , current -> name , current -> key );
483
471
DTRACE_PROBE1 (lightningd , span_resume , current -> id );
484
472
if (trace_to_file ) {
485
- char span_id [HEX_SPAN_ID_SIZE ];
486
- hex_encode (current -> id , SPAN_ID_SIZE , span_id , HEX_SPAN_ID_SIZE );
487
- fprintf (trace_to_file , "span_resume %s\n" , span_id );
473
+ fprintf (trace_to_file , "span_resume %016" PRIx64 "\n" , current -> id );
488
474
fflush (trace_to_file );
489
475
}
490
476
}
0 commit comments