Skip to content

Commit

Permalink
common: Make trace debugging output configurate at compile time
Browse files Browse the repository at this point in the history
Just added a couple of compile-time guards and sprinkled the invariant
checking in a couple of places (disabled if compile time guard is
unset).
  • Loading branch information
cdecker committed Nov 22, 2024
1 parent fe376f2 commit cc7a826
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
23 changes: 18 additions & 5 deletions common/trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
*/
#define TRACEPARENT_LEN (2 + 1 + 32 + 1 + 16 + 1 + 2)

#ifdef TRACE_DEBUG
#define TRACE_DBG(args...) fprintf(stderr, args)
#else
#define TRACE_DBG(args...)
#endif

const char *trace_service_name = "lightningd";

Expand Down Expand Up @@ -98,6 +102,8 @@ static void trace_inject_traceparent(void)
}
}

#ifdef TRACE_DEBUG

/** Quickly print out the entries in the `active_spans`. */
static void trace_spans_print(void)
{
Expand Down Expand Up @@ -136,8 +142,12 @@ static void trace_check_tree(void)
assert(c->parent == NULL);
}
}
#else
static inline void trace_check_tree(void) {}
#endif

static void trace_init(void) {
static void trace_init(void)
{
if (active_spans)
return;
active_spans = calloc(MAX_ACTIVE_SPANS, sizeof(struct span));
Expand Down Expand Up @@ -238,7 +248,8 @@ static void trace_span_clear(struct span *s)
{
s->key = 0;
memset(s->id, 0, SPAN_ID_SIZE);
memset(s->trace_id, 0, TRACE_ID_SIZE);;
memset(s->trace_id, 0, TRACE_ID_SIZE);
;
s->parent = NULL;
s->name = tal_free(s->name);
s->tags = tal_free(s->tags);
Expand All @@ -250,6 +261,7 @@ void trace_span_start(const char *name, const void *key)
struct timeabs now = time_now();

trace_init();
trace_check_tree();

assert(trace_span_find(numkey) == NULL);
struct span *s = trace_span_slot();
Expand All @@ -270,8 +282,8 @@ void trace_span_start(const char *name, const void *key)
}

current = s;
DTRACE_PROBE1(lightningd, span_start, s->id);
trace_check_tree();
DTRACE_PROBE1(lightningd, span_start, s->id);
}

void trace_span_remote(u8 trace_id[TRACE_ID_SIZE], u8 span_id[SPAN_ID_SIZE])
Expand All @@ -286,13 +298,13 @@ void trace_span_end(const void *key)
assert(s && "Span to end not found");
assert(s == current && "Ending a span that isn't the current one");

trace_check_tree();

struct timeabs now = time_now();
s->end_time = (now.ts.tv_sec * 1000000) + now.ts.tv_nsec / 1000;
DTRACE_PROBE1(lightningd, span_end, s->id);
trace_emit(s);

trace_check_tree();

/* Reset the context span we are in. */
current = s->parent;

Expand All @@ -306,6 +318,7 @@ void trace_span_end(const void *key)
assert(current->parent == NULL);
current = NULL;
}
trace_check_tree();
}

void trace_span_tag(const void *key, const char *name, const char *value)
Expand Down
1 change: 1 addition & 0 deletions common/trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#define SPAN_ID_SIZE 8
#define TRACE_ID_SIZE 16
#undef TRACE_DEBUG

void trace_span_start(const char *name, const void *key);
void trace_span_end(const void *key);
Expand Down

0 comments on commit cc7a826

Please sign in to comment.