@@ -257,11 +257,15 @@ static struct span *trace_span_slot(void)
257
257
return s ;
258
258
}
259
259
260
+ #define MAX_BUF_SIZE 2048
261
+
260
262
static void trace_emit (struct span * s )
261
263
{
262
264
char span_id [HEX_SPAN_ID_SIZE ];
263
265
char trace_id [HEX_TRACE_ID_SIZE ];
264
266
char parent_span_id [HEX_SPAN_ID_SIZE ];
267
+ char buffer [MAX_BUF_SIZE + 1 ];
268
+ size_t len ;
265
269
266
270
/* If this is a remote span it's not up to us to emit it. Make
267
271
* this a no-op. `trace_span_end` will take care of cleaning
@@ -275,36 +279,46 @@ static void trace_emit(struct span *s)
275
279
if (s -> parent )
276
280
hex_encode (s -> parent_id , SPAN_ID_SIZE , parent_span_id , HEX_SPAN_ID_SIZE );
277
281
278
- char * res = tal_fmt (
279
- NULL ,
280
- "[{\"id\": \"%s\", \"name\": \"%s\", "
281
- "\"timestamp\": %" PRIu64 ", \"duration\": %" PRIu64 "," ,
282
- span_id , s -> name , s -> start_time , s -> end_time - s -> start_time );
283
-
284
- tal_append_fmt (& res , "\"localEndpoint\": { \"serviceName\": \"%s\"}, " ,
285
- trace_service_name );
282
+ len = snprintf (buffer , MAX_BUF_SIZE ,
283
+ "[{\"id\":\"%s\",\"name\":\"%s\","
284
+ "\"timestamp\":%" PRIu64 ",\"duration\":%" PRIu64 ","
285
+ "\"localEndpoint\":{\"serviceName\":\"%s\"}," ,
286
+ span_id , s -> name , s -> start_time , s -> end_time - s -> start_time , trace_service_name );
286
287
287
288
if (s -> parent != NULL ) {
288
- tal_append_fmt (& res , "\"parentId\": \"%s\"," , parent_span_id );
289
+ len += snprintf (buffer + len , MAX_BUF_SIZE - len ,
290
+ "\"parentId\":\"%s\"," ,
291
+ parent_span_id );
292
+ if (len > MAX_BUF_SIZE )
293
+ len = MAX_BUF_SIZE ;
289
294
}
290
295
291
- tal_append_fmt (& res , "\"tags\": {" );
296
+ len += snprintf (buffer + len , MAX_BUF_SIZE - len ,
297
+ "\"tags\":{" );
298
+ if (len > MAX_BUF_SIZE )
299
+ len = MAX_BUF_SIZE ;
292
300
for (size_t i = 0 ; i < SPAN_MAX_TAGS ; i ++ ) {
293
301
if (!s -> tags [i ].name )
294
302
continue ;
295
- tal_append_fmt (& res , "%s\"%s\": \"%.*s\"" , i == 0 ? "" : ", " ,
296
- s -> tags [i ].name ,
297
- s -> tags [i ].valuelen ,
298
- s -> tags [i ].valuestr );
303
+ len += snprintf (buffer + len , MAX_BUF_SIZE - len ,
304
+ "%s\"%s\":\"%.*s\"" , i == 0 ? "" : ", " ,
305
+ s -> tags [i ].name ,
306
+ s -> tags [i ].valuelen ,
307
+ s -> tags [i ].valuestr );
308
+ if (len > MAX_BUF_SIZE )
309
+ len = MAX_BUF_SIZE ;
299
310
}
300
311
301
- tal_append_fmt (& res , "}, \"traceId\": \"%s\"}]" , trace_id );
302
- DTRACE_PROBE2 (lightningd , span_emit , span_id , res );
312
+ len += snprintf (buffer + len , MAX_BUF_SIZE - len ,
313
+ "},\"traceId\":\"%s\"}]" , trace_id );
314
+ if (len > MAX_BUF_SIZE )
315
+ len = MAX_BUF_SIZE ;
316
+ buffer [len ] = '\0' ;
317
+ DTRACE_PROBE2 (lightningd , span_emit , span_id , buffer );
303
318
if (trace_to_file ) {
304
- fprintf (trace_to_file , "span_emit %s %s\n" , span_id , res );
319
+ fprintf (trace_to_file , "span_emit %s %s\n" , span_id , buffer );
305
320
fflush (trace_to_file );
306
321
}
307
- tal_free (res );
308
322
}
309
323
310
324
/**
0 commit comments