1
1
#include "config.h"
2
2
#include <assert.h>
3
+ #include <ccan/endian/endian.h>
3
4
#include <ccan/htable/htable.h>
4
5
#include <ccan/str/hex/hex.h>
5
6
#include <ccan/tal/tal.h>
6
7
#include <ccan/time/time.h>
7
8
#include <common/json_stream.h>
8
9
#include <common/memleak.h>
10
+ #include <common/pseudorand.h>
9
11
#include <common/trace.h>
10
- #include <sodium/randombytes.h>
11
12
#include <stdio.h>
12
13
#include <unistd.h>
13
14
16
17
17
18
#define MAX_ACTIVE_SPANS 128
18
19
19
- #define HEX_SPAN_ID_SIZE (2*SPAN_ID_SIZE+1)
20
- #define HEX_TRACE_ID_SIZE (2 * TRACE_ID_SIZE + 1)
21
-
22
20
/* The traceperent format is defined in W3C Trace Context RFC[1].
23
21
* Its format is defined as
24
22
*
@@ -52,13 +50,13 @@ struct span_tag {
52
50
53
51
struct span {
54
52
/* Our own id */
55
- u8 id [ SPAN_ID_SIZE ] ;
53
+ u64 id ;
56
54
57
55
/* 0 if we have no parent. */
58
- u8 parent_id [ SPAN_ID_SIZE ] ;
56
+ u64 parent_id ;
59
57
60
58
/* The trace_id for this span and all its children. */
61
- u8 trace_id [ TRACE_ID_SIZE ] ;
59
+ u64 trace_id_hi , trace_id_lo ;
62
60
63
61
u64 start_time ;
64
62
u64 end_time ;
@@ -88,7 +86,9 @@ static struct span *current;
88
86
* own parent. */
89
87
static void trace_inject_traceparent (void )
90
88
{
91
- char * traceparent ;
89
+ const char * traceparent ;
90
+ be64 trace_hi , trace_lo , span ;
91
+
92
92
traceparent = getenv ("CLN_TRACEPARENT" );
93
93
if (!traceparent )
94
94
return ;
@@ -97,13 +97,17 @@ static void trace_inject_traceparent(void)
97
97
trace_span_start ("" , active_spans );
98
98
current -> remote = true;
99
99
assert (current && !current -> parent );
100
- if (! hex_decode ( traceparent + 3 , 2 * TRACE_ID_SIZE , current -> trace_id ,
101
- TRACE_ID_SIZE ) ||
102
- !hex_decode (traceparent + 36 , 2 * SPAN_ID_SIZE , current -> id ,
103
- SPAN_ID_SIZE )) {
100
+
101
+ if (! hex_decode ( traceparent + 3 , 16 , & trace_hi , sizeof ( trace_hi ))
102
+ || !hex_decode (traceparent + 3 + 16 , 16 , & trace_lo , sizeof ( trace_lo ))
103
+ || ! hex_decode ( traceparent + 3 + 16 + 16 + 1 , 16 , & span , sizeof ( span ) )) {
104
104
/* We failed to parse the traceparent, abandon. */
105
105
fprintf (stderr , "Failed!" );
106
106
trace_span_end (active_spans );
107
+ } else {
108
+ current -> trace_id_hi = be64_to_cpu (trace_hi );
109
+ current -> trace_id_lo = be64_to_cpu (trace_lo );
110
+ current -> id = be64_to_cpu (span );
107
111
}
108
112
}
109
113
@@ -217,9 +221,7 @@ static struct span *trace_span_slot(void)
217
221
218
222
static void trace_emit (struct span * s )
219
223
{
220
- char span_id [HEX_SPAN_ID_SIZE ];
221
- char trace_id [HEX_TRACE_ID_SIZE ];
222
- char parent_span_id [HEX_SPAN_ID_SIZE ];
224
+ char span_id [hex_str_size (sizeof (s -> id ))];
223
225
char buffer [MAX_BUF_SIZE + 1 ];
224
226
size_t len ;
225
227
@@ -229,12 +231,7 @@ static void trace_emit(struct span *s)
229
231
if (s -> remote )
230
232
return ;
231
233
232
- hex_encode (s -> id , SPAN_ID_SIZE , span_id , HEX_SPAN_ID_SIZE );
233
- hex_encode (s -> trace_id , TRACE_ID_SIZE , trace_id , HEX_TRACE_ID_SIZE );
234
-
235
- if (s -> parent )
236
- hex_encode (s -> parent_id , SPAN_ID_SIZE , parent_span_id , HEX_SPAN_ID_SIZE );
237
-
234
+ sprintf (span_id , "%016" PRIx64 , s -> id );
238
235
len = snprintf (buffer , MAX_BUF_SIZE ,
239
236
"[{\"id\": \"%s\", \"name\": \"%s\", "
240
237
"\"timestamp\": %" PRIu64 ", \"duration\": %" PRIu64 ","
@@ -243,8 +240,8 @@ static void trace_emit(struct span *s)
243
240
244
241
if (s -> parent != NULL ) {
245
242
len += snprintf (buffer + len , MAX_BUF_SIZE - len ,
246
- "\"parentId\": \"%s \"," ,
247
- parent_span_id );
243
+ "\"parentId\": \"%016" PRIx64 " \"," ,
244
+ s -> parent_id );
248
245
if (len > MAX_BUF_SIZE )
249
246
len = MAX_BUF_SIZE ;
250
247
}
@@ -265,10 +262,12 @@ static void trace_emit(struct span *s)
265
262
len = MAX_BUF_SIZE ;
266
263
}
267
264
len += snprintf (buffer + len , MAX_BUF_SIZE - len ,
268
- "}, \"traceId\": \"%s\"}]" , trace_id );
265
+ "}, \"traceId\": \"%016" PRIx64 "%016" PRIx64 "\"}]" ,
266
+ s -> trace_id_hi , s -> trace_id_lo );
269
267
if (len > MAX_BUF_SIZE )
270
268
len = MAX_BUF_SIZE ;
271
269
buffer [len ] = '\0' ;
270
+ /* FIXME: span_id here is in hex, could be u64? */
272
271
DTRACE_PROBE2 (lightningd , span_emit , span_id , buffer );
273
272
}
274
273
@@ -277,14 +276,7 @@ static void trace_emit(struct span *s)
277
276
*/
278
277
static void trace_span_clear (struct span * s )
279
278
{
280
- s -> key = 0 ;
281
- memset (s -> id , 0 , SPAN_ID_SIZE );
282
- memset (s -> trace_id , 0 , TRACE_ID_SIZE );
283
-
284
- s -> parent = NULL ;
285
- s -> name = NULL ;
286
- for (size_t i = 0 ; i < SPAN_MAX_TAGS ; i ++ )
287
- s -> tags [i ].name = NULL ;
279
+ memset (s , 0 , sizeof (* s ));
288
280
}
289
281
290
282
void trace_span_start_ (const char * name , const void * key )
@@ -302,26 +294,28 @@ void trace_span_start_(const char *name, const void *key)
302
294
if (!s )
303
295
return ;
304
296
s -> key = numkey ;
305
- randombytes_buf ( s -> id , SPAN_ID_SIZE );
297
+ s -> id = pseudorand_u64 ( );
306
298
s -> start_time = (now .ts .tv_sec * 1000000 ) + now .ts .tv_nsec / 1000 ;
307
299
s -> parent = current ;
308
300
s -> name = name ;
309
301
310
302
/* If this is a new root span we also need to associate a new
311
303
* trace_id with it. */
312
304
if (!current ) {
313
- randombytes_buf (s -> trace_id , TRACE_ID_SIZE );
305
+ s -> trace_id_hi = pseudorand_u64 ();
306
+ s -> trace_id_lo = pseudorand_u64 ();
314
307
} else {
315
- memcpy (s -> parent_id , current -> id , SPAN_ID_SIZE );
316
- memcpy (s -> trace_id , current -> trace_id , TRACE_ID_SIZE );
308
+ s -> parent_id = current -> id ;
309
+ s -> trace_id_hi = current -> trace_id_hi ;
310
+ s -> trace_id_lo = current -> trace_id_lo ;
317
311
}
318
312
319
313
current = s ;
320
314
trace_check_tree ();
321
315
DTRACE_PROBE1 (lightningd , span_start , s -> id );
322
316
}
323
317
324
- void trace_span_remote (u8 trace_id [ TRACE_ID_SIZE ], u8 span_id [ SPAN_ID_SIZE ] )
318
+ void trace_span_remote (u64 trace_id_hi , u64 trade_id_lo , u64 span_id )
325
319
{
326
320
abort ();
327
321
}
0 commit comments