1
1
#include "config.h"
2
2
#include <assert.h>
3
+ #include <ccan/err/err.h>
3
4
#include <ccan/htable/htable.h>
4
5
#include <ccan/str/hex/hex.h>
5
6
#include <ccan/tal/str/str.h>
8
9
#include <common/json_stream.h>
9
10
#include <common/memleak.h>
10
11
#include <common/trace.h>
12
+ #include <fcntl.h>
11
13
#include <sodium/randombytes.h>
12
14
#include <stdio.h>
13
15
#include <unistd.h>
42
44
43
45
const char * trace_service_name = "lightningd" ;
44
46
static bool disable_trace = false;
47
+ static FILE * trace_to_file = NULL ;
45
48
46
49
struct span_tag {
47
50
char * name , * value ;
@@ -151,11 +154,20 @@ static inline void trace_check_tree(void) {}
151
154
152
155
static void trace_init (void )
153
156
{
157
+ const char * dev_trace_file ;
154
158
if (active_spans )
155
159
return ;
156
160
active_spans = calloc (MAX_ACTIVE_SPANS , sizeof (struct span ));
157
161
158
162
current = NULL ;
163
+ dev_trace_file = getenv ("CLN_DEV_TRACE_FILE" );
164
+ if (dev_trace_file ) {
165
+ const char * fname = tal_fmt (tmpctx , "%s.%u" ,
166
+ dev_trace_file , (unsigned )getpid ());
167
+ trace_to_file = fopen (fname , "a+" );
168
+ if (!trace_to_file )
169
+ err (1 , "Opening CLN_DEV_TRACE_FILE %s" , fname );
170
+ }
159
171
trace_inject_traceparent ();
160
172
}
161
173
@@ -250,6 +262,10 @@ static void trace_emit(struct span *s)
250
262
251
263
tal_append_fmt (& res , "}, \"traceId\": \"%s\"}]" , trace_id );
252
264
DTRACE_PROBE2 (lightningd , span_emit , span_id , res );
265
+ if (trace_to_file ) {
266
+ fprintf (trace_to_file , "span_emit %s %s\n" , span_id , res );
267
+ fflush (trace_to_file );
268
+ }
253
269
tal_free (res );
254
270
}
255
271
@@ -301,6 +317,12 @@ void trace_span_start(const char *name, const void *key)
301
317
current = s ;
302
318
trace_check_tree ();
303
319
DTRACE_PROBE1 (lightningd , span_start , s -> id );
320
+ if (trace_to_file ) {
321
+ char span_id [HEX_SPAN_ID_SIZE ];
322
+ hex_encode (s -> id , SPAN_ID_SIZE , span_id , HEX_SPAN_ID_SIZE );
323
+ fprintf (trace_to_file , "span_start %s\n" , span_id );
324
+ fflush (trace_to_file );
325
+ }
304
326
}
305
327
306
328
void trace_span_remote (u8 trace_id [TRACE_ID_SIZE ], u8 span_id [SPAN_ID_SIZE ])
@@ -323,6 +345,12 @@ void trace_span_end(const void *key)
323
345
struct timeabs now = time_now ();
324
346
s -> end_time = (now .ts .tv_sec * 1000000 ) + now .ts .tv_nsec / 1000 ;
325
347
DTRACE_PROBE1 (lightningd , span_end , s -> id );
348
+ if (trace_to_file ) {
349
+ char span_id [HEX_SPAN_ID_SIZE ];
350
+ hex_encode (s -> id , SPAN_ID_SIZE , span_id , HEX_SPAN_ID_SIZE );
351
+ fprintf (trace_to_file , "span_end %s\n" , span_id );
352
+ fflush (trace_to_file );
353
+ }
326
354
trace_emit (s );
327
355
328
356
/* Reset the context span we are in. */
@@ -375,6 +403,12 @@ void trace_span_suspend_(const void *key, const char *lbl)
375
403
span -> suspended = true;
376
404
current = NULL ;
377
405
DTRACE_PROBE1 (lightningd , span_suspend , span -> id );
406
+ if (trace_to_file ) {
407
+ char span_id [HEX_SPAN_ID_SIZE ];
408
+ hex_encode (span -> id , SPAN_ID_SIZE , span_id , HEX_SPAN_ID_SIZE );
409
+ fprintf (trace_to_file , "span_suspend %s\n" , span_id );
410
+ fflush (trace_to_file );
411
+ }
378
412
}
379
413
380
414
static void destroy_trace_span (const void * key )
@@ -387,6 +421,8 @@ static void destroy_trace_span(const void *key)
387
421
return ;
388
422
389
423
/* Otherwise resume so we can terminate it */
424
+ if (trace_to_file )
425
+ fprintf (trace_to_file , "destroying span\n" );
390
426
trace_span_resume (key );
391
427
trace_span_end (key );
392
428
}
@@ -410,6 +446,12 @@ void trace_span_resume_(const void *key, const char *lbl)
410
446
current -> suspended = false;
411
447
TRACE_DBG ("Resuming span %s (%zu)\n" , current -> name , current -> key );
412
448
DTRACE_PROBE1 (lightningd , span_resume , current -> id );
449
+ if (trace_to_file ) {
450
+ char span_id [HEX_SPAN_ID_SIZE ];
451
+ hex_encode (current -> id , SPAN_ID_SIZE , span_id , HEX_SPAN_ID_SIZE );
452
+ fprintf (trace_to_file , "span_resume %s\n" , span_id );
453
+ fflush (trace_to_file );
454
+ }
413
455
}
414
456
415
457
void trace_cleanup (void )
0 commit comments