18
18
#define STATSD_TYPE_COUNTER 0x0001
19
19
#define STATSD_TYPE_TIMING 0x0002
20
20
21
- /* Maximum 64-bit number: -9223372036854775807 (20 characters) */
22
- #define STATSD_MAX_INT_STR 20
21
+ #define STATSD_MAX_STR 256
23
22
24
23
#define ngx_conf_merge_ptr_value (conf , prev , default ) \
25
24
if (conf == NGX_CONF_UNSET_PTR) { \
@@ -236,9 +235,8 @@ ngx_http_statsd_valid_value(ngx_str_t *value)
236
235
ngx_int_t
237
236
ngx_http_statsd_handler (ngx_http_request_t * r )
238
237
{
239
- u_char * line , * p ;
240
- uint line_len ;
241
- size_t len ;
238
+ u_char line [STATSD_MAX_STR ], * p ;
239
+ const char * metric_type ;
242
240
ngx_http_statsd_conf_t * ulcf ;
243
241
ngx_statsd_stat_t * stats ;
244
242
ngx_statsd_stat_t stat ;
@@ -263,16 +261,6 @@ ngx_http_statsd_handler(ngx_http_request_t *r)
263
261
return NGX_OK ;
264
262
}
265
263
266
- line_len = 100 ;
267
- #if defined nginx_version && nginx_version >= 7003
268
- line = ngx_pnalloc (r -> pool , line_len );
269
- #else
270
- line = ngx_palloc (r -> pool , line_len );
271
- #endif
272
- if (line == NULL ) {
273
- return NGX_ERROR ;
274
- }
275
-
276
264
stats = ulcf -> stats -> elts ;
277
265
for (c = 0 ; c < ulcf -> stats -> nelts ; c ++ ) {
278
266
@@ -289,34 +277,21 @@ ngx_http_statsd_handler(ngx_http_request_t *r)
289
277
continue ;
290
278
};
291
279
292
- len = s .len ;
293
- len += sizeof (":" ) - 1 ;
294
- len += STATSD_MAX_INT_STR ;
295
- len += sizeof ("|c|@0.00" ) - 1 ;
296
-
297
- if (line_len < len ) {
298
- // Redimension buffer.
299
- line_len = len ;
300
- #if defined nginx_version && nginx_version >= 7003
301
- line = ngx_pnalloc (r -> pool , line_len );
302
- #else
303
- line = ngx_palloc (r -> pool , line_len );
304
- #endif
305
- if (line == NULL ) {
306
- return NGX_ERROR ;
307
- };
308
- };
309
-
310
280
if (stat .type == STATSD_TYPE_COUNTER ) {
281
+ metric_type = "c" ;
282
+ } else if (stat .type == STATSD_TYPE_TIMING ) {
283
+ metric_type = "ms" ;
284
+ } else {
285
+ metric_type = NULL ;
286
+ }
287
+
288
+ if (metric_type ) {
311
289
if (ulcf -> sample_rate < 100 ) {
312
- p = ngx_sprintf (line , "%V:%d|c |@0.%02d" , & s , n , ulcf -> sample_rate );
290
+ p = ngx_snprintf (line , STATSD_MAX_STR , "%V:%d|%s |@0.%02d" , & s , n , metric_type , ulcf -> sample_rate );
313
291
} else {
314
- p = ngx_sprintf (line , "%V:%d|c " , & s , n );
292
+ p = ngx_snprintf (line , STATSD_MAX_STR , "%V:%d|%s " , & s , n , metric_type );
315
293
}
316
294
ngx_http_statsd_udp_send (ulcf -> endpoint , line , p - line );
317
- } else if (stat .type == STATSD_TYPE_TIMING ) {
318
- p = ngx_sprintf (line , "%V:%d|ms" , & s , n );
319
- ngx_http_statsd_udp_send (ulcf -> endpoint , line , p - line );
320
295
}
321
296
}
322
297
0 commit comments