2
2
#include <assert.h>
3
3
#include <ccan/htable/htable.h>
4
4
#include <ccan/str/hex/hex.h>
5
- #include <ccan/tal/str/str.h>
6
5
#include <ccan/tal/tal.h>
7
6
#include <ccan/time/time.h>
8
7
#include <common/json_stream.h>
@@ -214,11 +213,15 @@ static struct span *trace_span_slot(void)
214
213
return s ;
215
214
}
216
215
216
+ #define MAX_BUF_SIZE 512
217
+
217
218
static void trace_emit (struct span * s )
218
219
{
219
220
char span_id [HEX_SPAN_ID_SIZE ];
220
221
char trace_id [HEX_TRACE_ID_SIZE ];
221
222
char parent_span_id [HEX_SPAN_ID_SIZE ];
223
+ char buffer [MAX_BUF_SIZE + 1 ];
224
+ size_t len ;
222
225
223
226
/* If this is a remote span it's not up to us to emit it. Make
224
227
* this a no-op. `trace_span_end` will take care of cleaning
@@ -232,32 +235,41 @@ static void trace_emit(struct span *s)
232
235
if (s -> parent )
233
236
hex_encode (s -> parent_id , SPAN_ID_SIZE , parent_span_id , HEX_SPAN_ID_SIZE );
234
237
235
- char * res = tal_fmt (
236
- NULL ,
237
- "[{\"id\": \"%s\", \"name\": \"%s\", "
238
- "\"timestamp\": %" PRIu64 ", \"duration\": %" PRIu64 "," ,
239
- span_id , s -> name , s -> start_time , s -> end_time - s -> start_time );
240
-
241
- tal_append_fmt (& res , "\"localEndpoint\": { \"serviceName\": \"%s\"}, " ,
242
- trace_service_name );
238
+ len = snprintf (buffer , MAX_BUF_SIZE ,
239
+ "[{\"id\": \"%s\", \"name\": \"%s\", "
240
+ "\"timestamp\": %" PRIu64 ", \"duration\": %" PRIu64 ","
241
+ "\"localEndpoint\": { \"serviceName\": \"%s\"}, " ,
242
+ span_id , s -> name , s -> start_time , s -> end_time - s -> start_time , trace_service_name );
243
243
244
244
if (s -> parent != NULL ) {
245
- tal_append_fmt (& res , "\"parentId\": \"%s\"," , parent_span_id );
245
+ len += snprintf (buffer + len , MAX_BUF_SIZE - len ,
246
+ "\"parentId\": \"%s\"," ,
247
+ parent_span_id );
248
+ if (len > MAX_BUF_SIZE )
249
+ len = MAX_BUF_SIZE ;
246
250
}
247
251
248
- tal_append_fmt (& res , "\"tags\": {" );
252
+ len += snprintf (buffer + len , MAX_BUF_SIZE - len ,
253
+ "\"tags\": {" );
254
+ if (len > MAX_BUF_SIZE )
255
+ len = MAX_BUF_SIZE ;
249
256
for (size_t i = 0 ; i < SPAN_MAX_TAGS ; i ++ ) {
250
257
if (!s -> tags [i ].name )
251
258
continue ;
252
- tal_append_fmt (& res , "%s\"%s\": \"%.*s\"" , i == 0 ? "" : ", " ,
253
- s -> tags [i ].name ,
254
- s -> tags [i ].valuelen ,
255
- s -> tags [i ].valuestr );
259
+ len += snprintf (buffer + len , MAX_BUF_SIZE - len ,
260
+ "%s\"%s\": \"%.*s\"" , i == 0 ? "" : ", " ,
261
+ s -> tags [i ].name ,
262
+ s -> tags [i ].valuelen ,
263
+ s -> tags [i ].valuestr );
264
+ if (len > MAX_BUF_SIZE )
265
+ len = MAX_BUF_SIZE ;
256
266
}
257
-
258
- tal_append_fmt (& res , "}, \"traceId\": \"%s\"}]" , trace_id );
259
- DTRACE_PROBE2 (lightningd , span_emit , span_id , res );
260
- tal_free (res );
267
+ len += snprintf (buffer + len , MAX_BUF_SIZE - len ,
268
+ "}, \"traceId\": \"%s\"}]" , trace_id );
269
+ if (len > MAX_BUF_SIZE )
270
+ len = MAX_BUF_SIZE ;
271
+ buffer [len ] = '\0' ;
272
+ DTRACE_PROBE2 (lightningd , span_emit , span_id , buffer );
261
273
}
262
274
263
275
/**
0 commit comments