summaryrefslogtreecommitdiff
path: root/res/res_statsd.c
diff options
context:
space:
mode:
authorMatt Jordan <mjordan@digium.com>2015-11-18 10:05:07 -0600
committerMatt Jordan <mjordan@digium.com>2015-11-18 16:48:13 -0600
commit3354b325c67824b4aa052fb81693d28e792886a6 (patch)
tree736eb29bfe70db91cb3397d884c7a04103d3a0ea /res/res_statsd.c
parent1e0040b88f83688e67d71521177cf4fa962bf32a (diff)
res_statsd: Add functions that support variable arguments
Often, the metric names of statistics we are generating for StatsD have some dynamic component to them. This can be the name of a particular resource, or some internal status label in Asterisk. With the current set of functions, callers of the statsd API must first build the metric name themselves, then pass this to the API functions. This results in a large amount of boilerplate code and usage of either fixed length static buffers or dynamic memory allocation, neither of which is desireable. This patch adds two new functions to the StatsD API that support a printf style format specifier for constructing the metric name. A dynamic string, allocated in threadstorage, is used to build the metric name. This eases the burden on users of the StatsD API. Change-Id: If533c72d1afa26d807508ea48b4d8c7b32f414ea
Diffstat (limited to 'res/res_statsd.c')
-rw-r--r--res/res_statsd.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/res/res_statsd.c b/res/res_statsd.c
index b8b79be07..4eb526071 100644
--- a/res/res_statsd.c
+++ b/res/res_statsd.c
@@ -160,6 +160,54 @@ void AST_OPTIONAL_API_NAME(ast_statsd_log_full)(const char *metric_name,
}
+AST_THREADSTORAGE(statsd_buf);
+
+void AST_OPTIONAL_API_NAME(ast_statsd_log_string_va)(const char *metric_name,
+ const char *metric_type, const char *value, double sample_rate, ...)
+{
+ struct ast_str *buf;
+ va_list ap;
+ int res;
+
+ buf = ast_str_thread_get(&statsd_buf, 128);
+ if (!buf) {
+ return;
+ }
+
+ va_start(ap, sample_rate);
+ res = ast_str_set_va(&buf, 0, metric_name, ap);
+ va_end(ap);
+
+ if (res == AST_DYNSTR_BUILD_FAILED) {
+ return;
+ }
+
+ ast_statsd_log_string(ast_str_buffer(buf), metric_type, value, sample_rate);
+}
+
+void AST_OPTIONAL_API_NAME(ast_statsd_log_full_va)(const char *metric_name,
+ const char *metric_type, intmax_t value, double sample_rate, ...)
+{
+ struct ast_str *buf;
+ va_list ap;
+ int res;
+
+ buf = ast_str_thread_get(&statsd_buf, 128);
+ if (!buf) {
+ return;
+ }
+
+ va_start(ap, sample_rate);
+ res = ast_str_set_va(&buf, 0, metric_name, ap);
+ va_end(ap);
+
+ if (res == AST_DYNSTR_BUILD_FAILED) {
+ return;
+ }
+
+ ast_statsd_log_full(ast_str_buffer(buf), metric_type, value, sample_rate);
+}
+
void AST_OPTIONAL_API_NAME(ast_statsd_log)(const char *metric_name,
const char *metric_type, intmax_t value)
{