@@ -82,6 +82,36 @@ struct span {
82
82
static struct span * active_spans = NULL ;
83
83
static struct span * current ;
84
84
85
+ static void init_span (struct span * s ,
86
+ size_t key ,
87
+ const char * name ,
88
+ struct span * parent )
89
+ {
90
+ struct timeabs now = time_now ();
91
+
92
+ s -> key = key ;
93
+ randombytes_buf (s -> id , SPAN_ID_SIZE );
94
+ s -> start_time = (now .ts .tv_sec * 1000000 ) + now .ts .tv_nsec / 1000 ;
95
+ s -> parent = parent ;
96
+ s -> tags = notleak (tal_arr (NULL , struct span_tag , 0 ));
97
+ s -> name = notleak (tal_strdup (NULL , name ));
98
+ s -> suspended = false;
99
+
100
+ /* If this is a new root span we also need to associate a new
101
+ * trace_id with it. */
102
+ if (!s -> parent ) {
103
+ randombytes_buf (s -> trace_id , TRACE_ID_SIZE );
104
+ } else {
105
+ memcpy (s -> parent_id , parent -> id , SPAN_ID_SIZE );
106
+ memcpy (s -> trace_id , parent -> trace_id , TRACE_ID_SIZE );
107
+ }
108
+ }
109
+
110
+ /* FIXME: forward decls for minimal patch size */
111
+ static struct span * trace_span_slot (void );
112
+ static size_t trace_key (const void * key );
113
+ static void trace_span_clear (struct span * s );
114
+
85
115
/* If the `CLN_TRACEPARENT` envvar is set, we inject that as the
86
116
* parent for the startup. This allows us to integrate the startup
87
117
* tracing with whatever tooling we build around it. This only has an
@@ -95,7 +125,10 @@ static void trace_inject_traceparent(void)
95
125
return ;
96
126
97
127
assert (strlen (traceparent ) == TRACEPARENT_LEN );
98
- trace_span_start ("" , active_spans );
128
+ current = trace_span_slot ();
129
+ assert (current );
130
+
131
+ init_span (current , trace_key (active_spans ), "" , NULL );
99
132
current -> remote = true;
100
133
assert (current && !current -> parent );
101
134
if (!hex_decode (traceparent + 3 , 2 * TRACE_ID_SIZE , current -> trace_id ,
@@ -104,7 +137,8 @@ static void trace_inject_traceparent(void)
104
137
SPAN_ID_SIZE )) {
105
138
/* We failed to parse the traceparent, abandon. */
106
139
fprintf (stderr , "Failed!" );
107
- trace_span_end (active_spans );
140
+ trace_span_clear (current );
141
+ current = NULL ;
108
142
}
109
143
}
110
144
@@ -191,9 +225,6 @@ static struct span *trace_span_find(size_t key)
191
225
return NULL ;
192
226
}
193
227
194
- /* FIXME: Forward declaration for minimal patch size */
195
- static void trace_span_clear (struct span * s );
196
-
197
228
/**
198
229
* Find an empty slot for a new span.
199
230
*/
@@ -286,7 +317,6 @@ static void trace_span_clear(struct span *s)
286
317
void trace_span_start (const char * name , const void * key )
287
318
{
288
319
size_t numkey = trace_key (key );
289
- struct timeabs now = time_now ();
290
320
291
321
if (disable_trace )
292
322
return ;
@@ -297,23 +327,7 @@ void trace_span_start(const char *name, const void *key)
297
327
struct span * s = trace_span_slot ();
298
328
if (!s )
299
329
return ;
300
- s -> key = numkey ;
301
- randombytes_buf (s -> id , SPAN_ID_SIZE );
302
- s -> start_time = (now .ts .tv_sec * 1000000 ) + now .ts .tv_nsec / 1000 ;
303
- s -> parent = current ;
304
- s -> tags = notleak (tal_arr (NULL , struct span_tag , 0 ));
305
- s -> name = notleak (tal_strdup (NULL , name ));
306
- s -> suspended = false;
307
-
308
- /* If this is a new root span we also need to associate a new
309
- * trace_id with it. */
310
- if (!current ) {
311
- randombytes_buf (s -> trace_id , TRACE_ID_SIZE );
312
- } else {
313
- memcpy (s -> parent_id , current -> id , SPAN_ID_SIZE );
314
- memcpy (s -> trace_id , current -> trace_id , TRACE_ID_SIZE );
315
- }
316
-
330
+ init_span (s , numkey , name , current );
317
331
current = s ;
318
332
trace_check_tree ();
319
333
DTRACE_PROBE1 (lightningd , span_start , s -> id );
@@ -358,14 +372,6 @@ void trace_span_end(const void *key)
358
372
359
373
/* Now reset the span */
360
374
trace_span_clear (s );
361
-
362
- /* One last special case: if the parent is remote, it must be
363
- * the root. And we should terminate that trace along with
364
- * this one. */
365
- if (current && current -> remote ) {
366
- assert (current -> parent == NULL );
367
- current = NULL ;
368
- }
369
375
trace_check_tree ();
370
376
}
371
377
@@ -401,7 +407,7 @@ void trace_span_suspend_(const void *key, const char *lbl)
401
407
assert (current == span );
402
408
assert (!span -> suspended );
403
409
span -> suspended = true;
404
- current = NULL ;
410
+ current = current -> parent ;
405
411
DTRACE_PROBE1 (lightningd , span_suspend , span -> id );
406
412
if (trace_to_file ) {
407
413
char span_id [HEX_SPAN_ID_SIZE ];
0 commit comments