Skip to content

Commit b756a12

Browse files
committed
Merge pull request #9 from csfrancis/sampling_rate_for_timing
Use sampling rate for timing + code cleanup
2 parents 7b5fff7 + 410c8bd commit b756a12

File tree

1 file changed

+13
-38
lines changed

1 file changed

+13
-38
lines changed

ngx_http_statsd.c

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
#define STATSD_TYPE_COUNTER 0x0001
1919
#define STATSD_TYPE_TIMING 0x0002
2020

21-
/* Maximum 64-bit number: -9223372036854775807 (20 characters) */
22-
#define STATSD_MAX_INT_STR 20
21+
#define STATSD_MAX_STR 256
2322

2423
#define ngx_conf_merge_ptr_value(conf, prev, default) \
2524
if (conf == NGX_CONF_UNSET_PTR) { \
@@ -236,9 +235,8 @@ ngx_http_statsd_valid_value(ngx_str_t *value)
236235
ngx_int_t
237236
ngx_http_statsd_handler(ngx_http_request_t *r)
238237
{
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;
242240
ngx_http_statsd_conf_t *ulcf;
243241
ngx_statsd_stat_t *stats;
244242
ngx_statsd_stat_t stat;
@@ -263,16 +261,6 @@ ngx_http_statsd_handler(ngx_http_request_t *r)
263261
return NGX_OK;
264262
}
265263

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-
276264
stats = ulcf->stats->elts;
277265
for (c = 0; c < ulcf->stats->nelts; c++) {
278266

@@ -289,34 +277,21 @@ ngx_http_statsd_handler(ngx_http_request_t *r)
289277
continue;
290278
};
291279

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-
310280
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) {
311289
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);
313291
} 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);
315293
}
316294
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);
320295
}
321296
}
322297

0 commit comments

Comments
 (0)