2023-07-27 14:43:53 +09:30
|
|
|
#include "config.h"
|
|
|
|
|
#include <assert.h>
|
2025-11-20 12:07:12 +10:30
|
|
|
#include <ccan/array_size/array_size.h>
|
2025-11-20 12:07:12 +10:30
|
|
|
#include <ccan/crypto/siphash24/siphash24.h>
|
2025-04-23 10:24:40 +09:30
|
|
|
#include <ccan/endian/endian.h>
|
2025-04-23 10:24:18 +09:30
|
|
|
#include <ccan/err/err.h>
|
2025-11-20 12:07:12 +10:30
|
|
|
#include <ccan/htable/htable_type.h>
|
2023-07-27 14:43:53 +09:30
|
|
|
#include <ccan/str/hex/hex.h>
|
|
|
|
|
#include <ccan/tal/str/str.h>
|
|
|
|
|
#include <common/json_stream.h>
|
2025-10-23 10:09:12 +10:30
|
|
|
#include <common/memleak.h>
|
2025-04-23 10:24:40 +09:30
|
|
|
#include <common/pseudorand.h>
|
2023-07-27 14:43:53 +09:30
|
|
|
#include <common/trace.h>
|
2025-10-22 19:44:27 +10:30
|
|
|
#include <inttypes.h>
|
2023-07-27 14:43:53 +09:30
|
|
|
#include <stdio.h>
|
2025-03-31 15:12:01 +10:30
|
|
|
#include <unistd.h>
|
2023-07-27 14:43:53 +09:30
|
|
|
|
|
|
|
|
#if HAVE_USDT
|
2025-03-31 15:12:01 +10:30
|
|
|
#include <sys/sdt.h>
|
2023-07-27 14:47:22 +09:30
|
|
|
|
2023-12-01 17:58:55 +01:00
|
|
|
/* The traceperent format is defined in W3C Trace Context RFC[1].
|
|
|
|
|
* Its format is defined as
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* version-format = trace-id "-" parent-id "-" trace-flags
|
|
|
|
|
* trace-id = 32HEXDIGLC ; 16 bytes array identifier. All zeroes forbidden
|
|
|
|
|
* parent-id = 16HEXDIGLC ; 8 bytes array identifier. All zeroes forbidden
|
|
|
|
|
* trace-flags = 2HEXDIGLC ; 8 bit flags. Currently, only one bit is used.
|
|
|
|
|
* ```
|
|
|
|
|
*
|
|
|
|
|
* [1] https://www.w3.org/TR/trace-context/
|
|
|
|
|
*/
|
|
|
|
|
#define TRACEPARENT_LEN (2 + 1 + 32 + 1 + 16 + 1 + 2)
|
2023-07-27 14:43:53 +09:30
|
|
|
|
2024-11-22 11:25:58 +01:00
|
|
|
#ifdef TRACE_DEBUG
|
2024-11-21 12:16:56 +01:00
|
|
|
#define TRACE_DBG(args...) fprintf(stderr, args)
|
2024-11-22 11:25:58 +01:00
|
|
|
#else
|
|
|
|
|
#define TRACE_DBG(args...)
|
|
|
|
|
#endif
|
2024-11-21 12:16:56 +01:00
|
|
|
|
2023-07-27 14:43:53 +09:30
|
|
|
const char *trace_service_name = "lightningd";
|
2025-03-31 15:12:01 +10:30
|
|
|
static bool disable_trace = false;
|
2025-04-23 10:24:18 +09:30
|
|
|
static FILE *trace_to_file = NULL;
|
2023-07-27 14:43:53 +09:30
|
|
|
|
2025-04-23 10:24:40 +09:30
|
|
|
#define SPAN_MAX_TAGS 2
|
|
|
|
|
|
2023-07-27 14:43:53 +09:30
|
|
|
struct span_tag {
|
2025-04-23 10:24:40 +09:30
|
|
|
const char *name;
|
|
|
|
|
const char *valuestr;
|
|
|
|
|
int valuelen;
|
2023-07-27 14:43:53 +09:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct span {
|
2023-12-01 17:58:55 +01:00
|
|
|
/* Our own id */
|
2025-04-23 10:24:40 +09:30
|
|
|
u64 id;
|
2023-12-01 17:58:55 +01:00
|
|
|
|
|
|
|
|
/* The trace_id for this span and all its children. */
|
2025-04-23 10:24:40 +09:30
|
|
|
u64 trace_id_hi, trace_id_lo;
|
2023-12-01 17:58:55 +01:00
|
|
|
|
2023-07-27 14:43:53 +09:30
|
|
|
u64 start_time;
|
|
|
|
|
u64 end_time;
|
2023-12-01 17:58:55 +01:00
|
|
|
|
|
|
|
|
/* The unique key used to find ourselves in the active
|
|
|
|
|
* spans. */
|
|
|
|
|
size_t key;
|
|
|
|
|
struct span *parent;
|
2025-04-23 10:24:40 +09:30
|
|
|
struct span_tag tags[SPAN_MAX_TAGS];
|
2025-04-23 10:24:40 +09:30
|
|
|
const char *name;
|
2023-12-01 17:58:55 +01:00
|
|
|
|
2025-04-23 09:55:15 +09:30
|
|
|
bool suspended;
|
2023-07-27 14:43:53 +09:30
|
|
|
};
|
|
|
|
|
|
2025-11-20 12:07:12 +10:30
|
|
|
static size_t span_keyof(const struct span *span)
|
|
|
|
|
{
|
|
|
|
|
return span->key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static size_t span_key_hash(size_t key)
|
|
|
|
|
{
|
|
|
|
|
return siphash24(siphash_seed(), &key, sizeof(key));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool span_key_eq(const struct span *span, size_t key)
|
|
|
|
|
{
|
|
|
|
|
return span->key == key;
|
|
|
|
|
}
|
|
|
|
|
HTABLE_DEFINE_NODUPS_TYPE(struct span, span_keyof, span_key_hash, span_key_eq,
|
|
|
|
|
span_htable);
|
|
|
|
|
|
2025-11-20 12:07:12 +10:30
|
|
|
static struct span fixed_spans[8];
|
2025-11-20 12:07:12 +10:30
|
|
|
static struct span_htable *spans = NULL;
|
2023-07-27 14:43:53 +09:30
|
|
|
static struct span *current;
|
|
|
|
|
|
2025-04-23 10:24:32 +09:30
|
|
|
static void init_span(struct span *s,
|
|
|
|
|
size_t key,
|
|
|
|
|
const char *name,
|
|
|
|
|
struct span *parent)
|
|
|
|
|
{
|
2025-11-13 16:02:35 +10:30
|
|
|
struct timeabs now = time_now(); /* discouraged: but tracing wants non-dev time */
|
2025-04-23 10:24:32 +09:30
|
|
|
|
|
|
|
|
s->key = key;
|
2025-04-23 10:24:40 +09:30
|
|
|
s->id = pseudorand_u64();
|
2025-04-23 10:24:32 +09:30
|
|
|
s->start_time = (now.ts.tv_sec * 1000000) + now.ts.tv_nsec / 1000;
|
|
|
|
|
s->parent = parent;
|
2025-04-23 10:24:40 +09:30
|
|
|
s->name = name;
|
2025-04-23 10:24:32 +09:30
|
|
|
s->suspended = false;
|
2025-11-20 12:07:12 +10:30
|
|
|
for (size_t i = 0; i < SPAN_MAX_TAGS; i++)
|
|
|
|
|
s->tags[i].name = NULL;
|
2025-04-23 10:24:32 +09:30
|
|
|
|
|
|
|
|
/* If this is a new root span we also need to associate a new
|
|
|
|
|
* trace_id with it. */
|
|
|
|
|
if (!s->parent) {
|
2025-04-23 10:24:40 +09:30
|
|
|
s->trace_id_hi = pseudorand_u64();
|
|
|
|
|
s->trace_id_lo = pseudorand_u64();
|
2025-04-23 10:24:32 +09:30
|
|
|
} else {
|
2025-04-23 10:24:40 +09:30
|
|
|
s->trace_id_hi = current->trace_id_hi;
|
|
|
|
|
s->trace_id_lo = current->trace_id_lo;
|
2025-04-23 10:24:32 +09:30
|
|
|
}
|
2025-11-20 12:07:12 +10:30
|
|
|
span_htable_add(spans, s);
|
2025-04-23 10:24:32 +09:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* FIXME: forward decls for minimal patch size */
|
|
|
|
|
static struct span *trace_span_slot(void);
|
|
|
|
|
static size_t trace_key(const void *key);
|
|
|
|
|
static void trace_span_clear(struct span *s);
|
|
|
|
|
|
2023-12-01 17:58:55 +01:00
|
|
|
/* If the `CLN_TRACEPARENT` envvar is set, we inject that as the
|
|
|
|
|
* parent for the startup. This allows us to integrate the startup
|
|
|
|
|
* tracing with whatever tooling we build around it. This only has an
|
|
|
|
|
* effect if the envvar is set, otherwise the startup will create its
|
|
|
|
|
* own parent. */
|
|
|
|
|
static void trace_inject_traceparent(void)
|
2023-07-27 14:43:53 +09:30
|
|
|
{
|
2025-04-23 10:24:40 +09:30
|
|
|
const char *traceparent;
|
|
|
|
|
be64 trace_hi, trace_lo, span;
|
|
|
|
|
|
2023-12-01 17:58:55 +01:00
|
|
|
traceparent = getenv("CLN_TRACEPARENT");
|
|
|
|
|
if (!traceparent)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
assert(strlen(traceparent) == TRACEPARENT_LEN);
|
2025-04-23 10:24:32 +09:30
|
|
|
current = trace_span_slot();
|
|
|
|
|
assert(current);
|
|
|
|
|
|
2025-11-20 12:07:12 +10:30
|
|
|
init_span(current, trace_key(&spans), "", NULL);
|
2023-12-01 17:58:55 +01:00
|
|
|
assert(current && !current->parent);
|
2025-04-23 10:24:40 +09:30
|
|
|
|
|
|
|
|
if (!hex_decode(traceparent + 3, 16, &trace_hi, sizeof(trace_hi))
|
|
|
|
|
|| !hex_decode(traceparent + 3 + 16, 16, &trace_lo, sizeof(trace_lo))
|
|
|
|
|
|| !hex_decode(traceparent + 3 + 16 + 16 + 1, 16, &span, sizeof(span))) {
|
2023-12-01 17:58:55 +01:00
|
|
|
/* We failed to parse the traceparent, abandon. */
|
|
|
|
|
fprintf(stderr, "Failed!");
|
2025-04-23 10:24:32 +09:30
|
|
|
trace_span_clear(current);
|
|
|
|
|
current = NULL;
|
2025-04-23 10:24:40 +09:30
|
|
|
} else {
|
|
|
|
|
current->trace_id_hi = be64_to_cpu(trace_hi);
|
|
|
|
|
current->trace_id_lo = be64_to_cpu(trace_lo);
|
|
|
|
|
current->id = be64_to_cpu(span);
|
2023-12-01 17:58:55 +01:00
|
|
|
}
|
2023-07-27 14:43:53 +09:30
|
|
|
}
|
|
|
|
|
|
2025-11-20 12:07:12 +10:30
|
|
|
static void memleak_scan_spans(struct htable *memtable, struct span_htable *spantable)
|
|
|
|
|
{
|
|
|
|
|
struct span_htable_iter i;
|
|
|
|
|
const struct span *span;
|
|
|
|
|
|
|
|
|
|
for (span = span_htable_first(spantable, &i);
|
|
|
|
|
span;
|
|
|
|
|
span = span_htable_next(spantable, &i)) {
|
|
|
|
|
memleak_ptr(memtable, span);
|
|
|
|
|
memleak_scan_region(memtable, span, sizeof(*span));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-22 11:25:58 +01:00
|
|
|
static void trace_init(void)
|
|
|
|
|
{
|
2025-04-23 10:24:18 +09:30
|
|
|
const char *dev_trace_file;
|
2025-04-24 06:48:16 +09:30
|
|
|
|
2025-11-20 12:07:12 +10:30
|
|
|
if (spans)
|
2023-12-01 17:58:55 +01:00
|
|
|
return;
|
2025-04-23 10:24:40 +09:30
|
|
|
|
2025-11-20 12:07:12 +10:30
|
|
|
/* We can't use new_htable here because we put non-tal
|
|
|
|
|
* objects in our htable, and that breaks memleak_scan_htable! */
|
|
|
|
|
spans = notleak(tal(NULL, struct span_htable));
|
|
|
|
|
memleak_add_helper(spans, memleak_scan_spans);
|
2025-11-20 12:07:12 +10:30
|
|
|
span_htable_init(spans);
|
2023-12-01 17:58:55 +01:00
|
|
|
|
2023-07-27 14:43:53 +09:30
|
|
|
current = NULL;
|
2025-04-23 10:24:18 +09:30
|
|
|
dev_trace_file = getenv("CLN_DEV_TRACE_FILE");
|
|
|
|
|
if (dev_trace_file) {
|
|
|
|
|
const char *fname = tal_fmt(tmpctx, "%s.%u",
|
|
|
|
|
dev_trace_file, (unsigned)getpid());
|
|
|
|
|
trace_to_file = fopen(fname, "a+");
|
|
|
|
|
if (!trace_to_file)
|
|
|
|
|
err(1, "Opening CLN_DEV_TRACE_FILE %s", fname);
|
|
|
|
|
}
|
2023-12-01 17:58:55 +01:00
|
|
|
trace_inject_traceparent();
|
2023-07-27 14:43:53 +09:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert the pointer to a context object to a numeric key.
|
|
|
|
|
*/
|
|
|
|
|
static size_t trace_key(const void *key)
|
|
|
|
|
{
|
|
|
|
|
return (size_t)key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct span *trace_span_find(size_t key)
|
|
|
|
|
{
|
2025-11-20 12:07:12 +10:30
|
|
|
return span_htable_get(spans, key);
|
2023-07-27 14:43:53 +09:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find an empty slot for a new span.
|
|
|
|
|
*/
|
|
|
|
|
static struct span *trace_span_slot(void)
|
|
|
|
|
{
|
2025-11-20 12:07:12 +10:30
|
|
|
/* Look for a free fixed slot. */
|
|
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(fixed_spans); i++) {
|
|
|
|
|
if (fixed_spans[i].key == 0)
|
|
|
|
|
return &fixed_spans[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Those are used up, we have to allocate. */
|
2025-11-20 12:07:12 +10:30
|
|
|
return tal(spans, struct span);
|
2023-07-27 14:43:53 +09:30
|
|
|
}
|
|
|
|
|
|
2025-04-23 10:24:40 +09:30
|
|
|
#define MAX_BUF_SIZE 2048
|
|
|
|
|
|
2023-07-27 14:43:53 +09:30
|
|
|
static void trace_emit(struct span *s)
|
|
|
|
|
{
|
2025-04-23 10:24:40 +09:30
|
|
|
char span_id[hex_str_size(sizeof(s->id))];
|
2025-04-23 10:24:40 +09:30
|
|
|
char buffer[MAX_BUF_SIZE + 1];
|
|
|
|
|
size_t len;
|
2023-12-01 17:58:55 +01:00
|
|
|
|
2025-04-23 10:24:40 +09:30
|
|
|
snprintf(span_id, sizeof(span_id), "%016"PRIx64, s->id);
|
2025-04-23 10:24:40 +09:30
|
|
|
len = snprintf(buffer, MAX_BUF_SIZE,
|
|
|
|
|
"[{\"id\":\"%s\",\"name\":\"%s\","
|
|
|
|
|
"\"timestamp\":%"PRIu64",\"duration\":%"PRIu64","
|
|
|
|
|
"\"localEndpoint\":{\"serviceName\":\"%s\"},",
|
|
|
|
|
span_id, s->name, s->start_time, s->end_time - s->start_time, trace_service_name);
|
2023-07-27 14:43:53 +09:30
|
|
|
|
|
|
|
|
if (s->parent != NULL) {
|
2025-04-23 10:24:40 +09:30
|
|
|
len += snprintf(buffer + len, MAX_BUF_SIZE - len,
|
2025-04-23 10:24:40 +09:30
|
|
|
"\"parentId\":\"%016"PRIx64"\",",
|
2025-04-23 10:24:40 +09:30
|
|
|
s->parent->id);
|
2025-04-23 10:24:40 +09:30
|
|
|
if (len > MAX_BUF_SIZE)
|
|
|
|
|
len = MAX_BUF_SIZE;
|
2023-07-27 14:43:53 +09:30
|
|
|
}
|
|
|
|
|
|
2025-04-23 10:24:40 +09:30
|
|
|
len += snprintf(buffer + len, MAX_BUF_SIZE - len,
|
|
|
|
|
"\"tags\":{");
|
|
|
|
|
if (len > MAX_BUF_SIZE)
|
|
|
|
|
len = MAX_BUF_SIZE;
|
2025-04-23 10:24:40 +09:30
|
|
|
for (size_t i = 0; i < SPAN_MAX_TAGS; i++) {
|
|
|
|
|
if (!s->tags[i].name)
|
|
|
|
|
continue;
|
2025-04-23 10:24:40 +09:30
|
|
|
len += snprintf(buffer + len, MAX_BUF_SIZE - len,
|
|
|
|
|
"%s\"%s\":\"%.*s\"", i == 0 ? "" : ", ",
|
|
|
|
|
s->tags[i].name,
|
|
|
|
|
s->tags[i].valuelen,
|
|
|
|
|
s->tags[i].valuestr);
|
|
|
|
|
if (len > MAX_BUF_SIZE)
|
|
|
|
|
len = MAX_BUF_SIZE;
|
2023-07-27 14:43:53 +09:30
|
|
|
}
|
|
|
|
|
|
2025-04-23 10:24:40 +09:30
|
|
|
len += snprintf(buffer + len, MAX_BUF_SIZE - len,
|
2025-04-23 10:24:40 +09:30
|
|
|
"},\"traceId\":\"%016"PRIx64"%016"PRIx64"\"}]",
|
|
|
|
|
s->trace_id_hi, s->trace_id_lo);
|
2025-04-23 10:24:40 +09:30
|
|
|
if (len > MAX_BUF_SIZE)
|
|
|
|
|
len = MAX_BUF_SIZE;
|
|
|
|
|
buffer[len] = '\0';
|
2025-04-23 10:24:40 +09:30
|
|
|
/* FIXME: span_id here is in hex, could be u64? */
|
2025-04-23 10:24:40 +09:30
|
|
|
DTRACE_PROBE2(lightningd, span_emit, span_id, buffer);
|
2025-04-23 10:24:18 +09:30
|
|
|
if (trace_to_file) {
|
2025-04-23 10:24:40 +09:30
|
|
|
fprintf(trace_to_file, "span_emit %s %s\n", span_id, buffer);
|
2025-04-23 10:24:18 +09:30
|
|
|
fflush(trace_to_file);
|
|
|
|
|
}
|
2023-07-27 14:43:53 +09:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Release the span back into the pool of available spans.
|
|
|
|
|
*/
|
|
|
|
|
static void trace_span_clear(struct span *s)
|
|
|
|
|
{
|
2025-11-20 12:07:12 +10:30
|
|
|
if (!span_htable_del(spans, s))
|
|
|
|
|
abort();
|
2025-11-20 12:07:12 +10:30
|
|
|
|
|
|
|
|
/* If s is actually in fixed_spans, just zero it out. */
|
|
|
|
|
if (s >= fixed_spans && s < fixed_spans + ARRAY_SIZE(fixed_spans)) {
|
|
|
|
|
s->key = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Dynamically allocated, so we need to free it */
|
2025-11-20 12:07:12 +10:30
|
|
|
tal_free(s);
|
2023-07-27 14:43:53 +09:30
|
|
|
}
|
|
|
|
|
|
2025-04-23 10:24:40 +09:30
|
|
|
void trace_span_start_(const char *name, const void *key)
|
2023-07-27 14:43:53 +09:30
|
|
|
{
|
|
|
|
|
size_t numkey = trace_key(key);
|
|
|
|
|
|
2025-03-31 15:12:01 +10:30
|
|
|
if (disable_trace)
|
|
|
|
|
return;
|
2023-12-01 17:58:55 +01:00
|
|
|
trace_init();
|
|
|
|
|
|
2023-07-27 14:43:53 +09:30
|
|
|
assert(trace_span_find(numkey) == NULL);
|
|
|
|
|
struct span *s = trace_span_slot();
|
2025-03-31 15:12:01 +10:30
|
|
|
if (!s)
|
|
|
|
|
return;
|
2025-04-23 10:24:32 +09:30
|
|
|
init_span(s, numkey, name, current);
|
2023-07-27 14:43:53 +09:30
|
|
|
current = s;
|
2024-11-22 11:25:58 +01:00
|
|
|
DTRACE_PROBE1(lightningd, span_start, s->id);
|
2025-04-23 10:24:18 +09:30
|
|
|
if (trace_to_file) {
|
2025-04-23 10:24:40 +09:30
|
|
|
fprintf(trace_to_file, "span_start %016"PRIx64"\n", s->id);
|
2025-04-23 10:24:18 +09:30
|
|
|
fflush(trace_to_file);
|
|
|
|
|
}
|
2023-07-27 14:43:53 +09:30
|
|
|
}
|
|
|
|
|
|
2025-04-23 10:24:40 +09:30
|
|
|
void trace_span_remote(u64 trace_id_hi, u64 trade_id_lo, u64 span_id)
|
2023-12-01 17:58:55 +01:00
|
|
|
{
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-27 14:43:53 +09:30
|
|
|
void trace_span_end(const void *key)
|
|
|
|
|
{
|
2025-03-31 15:12:01 +10:30
|
|
|
if (disable_trace)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-07-27 14:43:53 +09:30
|
|
|
size_t numkey = trace_key(key);
|
|
|
|
|
struct span *s = trace_span_find(numkey);
|
|
|
|
|
assert(s && "Span to end not found");
|
|
|
|
|
assert(s == current && "Ending a span that isn't the current one");
|
|
|
|
|
|
2025-11-13 16:02:35 +10:30
|
|
|
struct timeabs now = time_now(); /* discouraged: but tracing wants non-dev time */
|
2023-07-27 14:43:53 +09:30
|
|
|
s->end_time = (now.ts.tv_sec * 1000000) + now.ts.tv_nsec / 1000;
|
2023-07-27 14:47:22 +09:30
|
|
|
DTRACE_PROBE1(lightningd, span_end, s->id);
|
2025-04-23 10:24:18 +09:30
|
|
|
if (trace_to_file) {
|
2025-04-23 10:24:40 +09:30
|
|
|
fprintf(trace_to_file, "span_end %016"PRIx64"\n", s->id);
|
2025-04-23 10:24:18 +09:30
|
|
|
fflush(trace_to_file);
|
|
|
|
|
}
|
2023-07-27 14:43:53 +09:30
|
|
|
trace_emit(s);
|
|
|
|
|
|
|
|
|
|
/* Reset the context span we are in. */
|
|
|
|
|
current = s->parent;
|
|
|
|
|
|
|
|
|
|
/* Now reset the span */
|
|
|
|
|
trace_span_clear(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void trace_span_tag(const void *key, const char *name, const char *value)
|
|
|
|
|
{
|
2025-03-31 15:12:01 +10:30
|
|
|
if (disable_trace)
|
|
|
|
|
return;
|
|
|
|
|
|
2025-04-23 10:24:40 +09:30
|
|
|
assert(name);
|
2023-07-27 14:43:53 +09:30
|
|
|
size_t numkey = trace_key(key);
|
|
|
|
|
struct span *span = trace_span_find(numkey);
|
|
|
|
|
assert(span);
|
|
|
|
|
|
2025-04-23 10:24:40 +09:30
|
|
|
for (size_t i = 0; i < SPAN_MAX_TAGS; i++) {
|
|
|
|
|
struct span_tag *t = &span->tags[i];
|
|
|
|
|
if (!t->name) {
|
|
|
|
|
t->name = name;
|
|
|
|
|
t->valuestr = value;
|
|
|
|
|
t->valuelen = strlen(value);
|
|
|
|
|
if (t->valuestr[0] == '"'
|
|
|
|
|
&& t->valuelen > 1
|
|
|
|
|
&& t->valuestr[t->valuelen-1] == '"') {
|
|
|
|
|
t->valuestr++;
|
|
|
|
|
t->valuelen -= 2;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-11-13 14:52:43 +10:30
|
|
|
}
|
2025-04-23 10:24:40 +09:30
|
|
|
abort();
|
2023-07-27 14:43:53 +09:30
|
|
|
}
|
|
|
|
|
|
2024-11-21 12:16:56 +01:00
|
|
|
void trace_span_suspend_(const void *key, const char *lbl)
|
2023-07-27 14:43:53 +09:30
|
|
|
{
|
2025-03-31 15:12:01 +10:30
|
|
|
if (disable_trace)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-07-27 14:47:22 +09:30
|
|
|
size_t numkey = trace_key(key);
|
|
|
|
|
struct span *span = trace_span_find(numkey);
|
2024-11-21 12:16:56 +01:00
|
|
|
TRACE_DBG("Suspending span %s (%zu)\n", current->name, current->key);
|
|
|
|
|
assert(current == span);
|
2025-04-23 09:55:15 +09:30
|
|
|
assert(!span->suspended);
|
|
|
|
|
span->suspended = true;
|
2025-04-23 10:24:32 +09:30
|
|
|
current = current->parent;
|
2023-07-27 14:47:22 +09:30
|
|
|
DTRACE_PROBE1(lightningd, span_suspend, span->id);
|
2025-04-23 10:24:18 +09:30
|
|
|
if (trace_to_file) {
|
2025-04-23 10:24:40 +09:30
|
|
|
fprintf(trace_to_file, "span_suspend %016"PRIx64"\n", span->id);
|
2025-04-23 10:24:18 +09:30
|
|
|
fflush(trace_to_file);
|
|
|
|
|
}
|
2023-07-27 14:43:53 +09:30
|
|
|
}
|
|
|
|
|
|
2025-04-03 07:03:59 +10:30
|
|
|
static void destroy_trace_span(const void *key)
|
|
|
|
|
{
|
|
|
|
|
size_t numkey = trace_key(key);
|
|
|
|
|
struct span *span = trace_span_find(numkey);
|
|
|
|
|
|
|
|
|
|
/* It's usually ended normally. */
|
|
|
|
|
if (!span)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* Otherwise resume so we can terminate it */
|
2025-04-23 10:24:18 +09:30
|
|
|
if (trace_to_file)
|
|
|
|
|
fprintf(trace_to_file, "destroying span\n");
|
2025-04-03 07:03:59 +10:30
|
|
|
trace_span_resume(key);
|
|
|
|
|
trace_span_end(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void trace_span_suspend_may_free_(const void *key, const char *lbl)
|
|
|
|
|
{
|
|
|
|
|
if (disable_trace)
|
|
|
|
|
return;
|
|
|
|
|
trace_span_suspend_(key, lbl);
|
|
|
|
|
tal_add_destructor(key, destroy_trace_span);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-21 12:16:56 +01:00
|
|
|
void trace_span_resume_(const void *key, const char *lbl)
|
2023-07-27 14:43:53 +09:30
|
|
|
{
|
2025-03-31 15:12:01 +10:30
|
|
|
if (disable_trace)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-07-27 14:43:53 +09:30
|
|
|
size_t numkey = trace_key(key);
|
|
|
|
|
current = trace_span_find(numkey);
|
2025-04-23 09:55:15 +09:30
|
|
|
assert(current->suspended);
|
|
|
|
|
current->suspended = false;
|
2024-11-21 12:16:56 +01:00
|
|
|
TRACE_DBG("Resuming span %s (%zu)\n", current->name, current->key);
|
2023-07-27 14:47:22 +09:30
|
|
|
DTRACE_PROBE1(lightningd, span_resume, current->id);
|
2025-04-23 10:24:18 +09:30
|
|
|
if (trace_to_file) {
|
2025-04-23 10:24:40 +09:30
|
|
|
fprintf(trace_to_file, "span_resume %016"PRIx64"\n", current->id);
|
2025-04-23 10:24:18 +09:30
|
|
|
fflush(trace_to_file);
|
|
|
|
|
}
|
2023-07-27 14:43:53 +09:30
|
|
|
}
|
2023-12-15 10:51:56 +10:30
|
|
|
|
|
|
|
|
void trace_cleanup(void)
|
|
|
|
|
{
|
2025-11-20 12:07:12 +10:30
|
|
|
spans = tal_free(spans);
|
2023-12-15 10:51:56 +10:30
|
|
|
}
|
|
|
|
|
|
2023-07-27 14:43:53 +09:30
|
|
|
#else /* HAVE_USDT */
|
|
|
|
|
|
2025-04-23 10:24:40 +09:30
|
|
|
void trace_span_start_(const char *name, const void *key) {}
|
2023-07-27 14:43:53 +09:30
|
|
|
void trace_span_end(const void *key) {}
|
2024-11-21 12:16:56 +01:00
|
|
|
void trace_span_suspend_(const void *key, const char *lbl) {}
|
2025-04-03 07:03:59 +10:30
|
|
|
void trace_span_suspend_may_free_(const void *key, const char *lbl) {}
|
2024-11-21 12:16:56 +01:00
|
|
|
void trace_span_resume_(const void *key, const char *lbl) {}
|
2023-07-27 14:43:53 +09:30
|
|
|
void trace_span_tag(const void *key, const char *name, const char *value) {}
|
2023-12-15 10:51:56 +10:30
|
|
|
void trace_cleanup(void) {}
|
2023-07-27 14:43:53 +09:30
|
|
|
|
|
|
|
|
#endif /* HAVE_USDT */
|