From 4a4dbbcf6971f281c997fd568efd2d16ff88e6c1 Mon Sep 17 00:00:00 2001 From: Unbit Date: Tue, 15 Oct 2013 14:08:01 +0200 Subject: [PATCH] ported the stats plugin to the metric subsystem --- core/metrics.c | 16 +++++++ plugins/stats_pusher_statsd/plugin.c | 69 ++++++++++------------------ uwsgi.h | 2 + 3 files changed, 41 insertions(+), 46 deletions(-) diff --git a/core/metrics.c b/core/metrics.c index 65ecdcd1..956b2e4d 100644 --- a/core/metrics.c +++ b/core/metrics.c @@ -191,6 +191,7 @@ struct uwsgi_metric *uwsgi_register_metric(char *name, char *oid, uint8_t value_ metric = uwsgi_calloc(sizeof(struct uwsgi_metric)); // always make a copy of the name (se we can use stack for building strings) metric->name = uwsgi_str(name); + metric->name_len = strlen(metric->name); if (old_metric) { old_metric->next = metric; } @@ -450,6 +451,9 @@ void uwsgi_setup_metrics() { uwsgi_metric_name("worker.%d.delta_requests", i) ; uwsgi_metric_oid("3.%d.2", i); uwsgi_register_metric(buf, buf2, UWSGI_METRIC_ABSOLUTE, UWSGI_METRIC_PTR, &uwsgi.workers[i].delta_requests, 0, NULL); + uwsgi_metric_name("worker.%d.avg_response_time", i) ; uwsgi_metric_oid("3.%d.8", i); + uwsgi_register_metric(buf, buf2, UWSGI_METRIC_GAUGE, UWSGI_METRIC_PTR, &uwsgi.workers[i].avg_response_time, 0, NULL); + int j; for(j=0;jdata; // reset the buffer - ub->pos = 0; - if (uwsgi_buffer_append(ub, sn->prefix, sn->prefix_len)) return -1; - if (uwsgi_buffer_append(ub, ".worker", 7)) return -1; - if (uwsgi_buffer_num64(ub, wid)) return -1; - if (uwsgi_buffer_append(ub, ".", 1)) return -1; - if (uwsgi_buffer_append(ub, metric, metric_len)) return -1; - if (uwsgi_buffer_append(ub, ":", 1)) return -1; - if (uwsgi_buffer_num64(ub, value)) return -1; - if (uwsgi_buffer_append(ub, "|g", 2)) return -1; - - if (sendto(sn->fd, ub->buf, ub->pos, 0, (struct sockaddr *) &sn->addr.sa_in, sn->addr_len) < 0) { - uwsgi_error("stats_pusher_statsd()/sendto()"); - } - return 0; -} - -static int statsd_send_core_gauge(struct uwsgi_buffer *ub, struct uwsgi_stats_pusher_instance *uspi, int wid, int coreid, char *metric, uint16_t metric_len, int64_t value) { - struct statsd_node *sn = (struct statsd_node *) uspi->data; - // reset the buffer ub->pos = 0; - if (uwsgi_buffer_append(ub, sn->prefix, sn->prefix_len)) return -1; - if (uwsgi_buffer_append(ub, ".worker", 7)) return -1; - if (uwsgi_buffer_num64(ub, wid)) return -1; - if (uwsgi_buffer_append(ub, ".core", 5)) return -1; - if (uwsgi_buffer_num64(ub, coreid)) return -1; - if (uwsgi_buffer_append(ub, ".", 1)) return -1; - if (uwsgi_buffer_append(ub, metric, metric_len)) return -1; - if (uwsgi_buffer_append(ub, ":", 1)) return -1; + if (uwsgi_buffer_append(ub, sn->prefix, sn->prefix_len)) return -1; + if (uwsgi_buffer_append(ub, ".", 1)) return -1; + if (uwsgi_buffer_append(ub, metric, metric_len)) return -1; + if (uwsgi_buffer_append(ub, ":", 1)) return -1; if (uwsgi_buffer_num64(ub, value)) return -1; - if (uwsgi_buffer_append(ub, "|g", 2)) return -1; + if (uwsgi_buffer_append(ub, type, 2)) return -1; - if (sendto(sn->fd, ub->buf, ub->pos, 0, (struct sockaddr *) &sn->addr.sa_in, sn->addr_len) < 0) { - uwsgi_error("stats_pusher_statsd()/sendto()"); - } + if (sendto(sn->fd, ub->buf, ub->pos, 0, (struct sockaddr *) &sn->addr.sa_in, sn->addr_len) < 0) { + uwsgi_error("statsd_send_metric()/sendto()"); + } + + return 0; - return 0; } @@ -106,20 +84,19 @@ static void stats_pusher_statsd(struct uwsgi_stats_pusher_instance *uspi, time_t // we use the same buffer for all of the packets struct uwsgi_buffer *ub = uwsgi_buffer_new(uwsgi.page_size); - - int i, j; - // send workers metrics - for(i=1;i<=uwsgi.numproc;i++) { - if (statsd_send_worker_gauge(ub, uspi, i, "requests", 8, uwsgi.workers[i].requests)) goto end; - for(j=0;jtype == UWSGI_METRIC_GAUGE) { + statsd_send_metric(ub, uspi, um->name, um->name_len, um->initial_value+*um->value, "|g"); } + else { + statsd_send_metric(ub, uspi, um->name, um->name_len, um->initial_value+*um->value, "|m"); + } + uwsgi_rwunlock(uwsgi.metrics_lock); + um = um->next; } -end: uwsgi_buffer_destroy(ub); } diff --git a/uwsgi.h b/uwsgi.h index 8dffabf8..0c1ddaf9 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -4412,6 +4412,8 @@ struct uwsgi_metric { char *name; char *oid; + size_t name_len; + // pre-computed snmp representation char *asn; size_t asn_size;