Merge "StatsD: Add res_statsd compatibility"

changes/71/1571/1
Joshua Colp 10 years ago committed by Gerrit Code Review
commit f12ebe3584

@ -39,6 +39,22 @@
/*! Events over time. Sorta like increment-only counters. */ /*! Events over time. Sorta like increment-only counters. */
#define AST_STATSD_METER "m" #define AST_STATSD_METER "m"
/*!
* \brief Send a stat to the configured statsd server.
*
* This function uses a character argument for value instead of
* an intmax_t argument. This is designed to be simpler to use for
* updating a current value rather than resetting it.
*
* \param metric_name String (UTF-8) name of the metric.
* \param type_str Type of metric to send.
* \param value Value to send.
* \param sample_rate Percentage of samples to send.
* \since 13
*/
AST_OPTIONAL_API(void, ast_statsd_log_string, (const char *metric_name,
const char *metric_type, const char *value, double sample_rate), {});
/*! /*!
* \brief Send a stat to the configured statsd server. * \brief Send a stat to the configured statsd server.
* *

@ -97,11 +97,11 @@ static void conf_server(const struct conf *cfg, struct ast_sockaddr *addr)
} }
} }
void AST_OPTIONAL_API_NAME(ast_statsd_log_full)(const char *metric_name, void AST_OPTIONAL_API_NAME(ast_statsd_log_string)(const char *metric_name,
const char *metric_type, intmax_t value, double sample_rate) const char *metric_type, const char *value, double sample_rate)
{ {
RAII_VAR(struct conf *, cfg, NULL, ao2_cleanup); struct conf *cfg;
RAII_VAR(struct ast_str *, msg, NULL, ast_free); struct ast_str *msg;
size_t len; size_t len;
struct ast_sockaddr statsd_server; struct ast_sockaddr statsd_server;
@ -109,9 +109,6 @@ void AST_OPTIONAL_API_NAME(ast_statsd_log_full)(const char *metric_name,
return; return;
} }
cfg = ao2_global_obj_ref(confs);
conf_server(cfg, &statsd_server);
/* Rates <= 0.0 never get logged. /* Rates <= 0.0 never get logged.
* Rates >= 1.0 always get logged. * Rates >= 1.0 always get logged.
* All others leave it to chance. * All others leave it to chance.
@ -122,9 +119,11 @@ void AST_OPTIONAL_API_NAME(ast_statsd_log_full)(const char *metric_name,
} }
cfg = ao2_global_obj_ref(confs); cfg = ao2_global_obj_ref(confs);
conf_server(cfg, &statsd_server);
msg = ast_str_create(40); msg = ast_str_create(40);
if (!msg) { if (!msg) {
ao2_cleanup(cfg);
return; return;
} }
@ -132,7 +131,7 @@ void AST_OPTIONAL_API_NAME(ast_statsd_log_full)(const char *metric_name,
ast_str_append(&msg, 0, "%s.", cfg->global->prefix); ast_str_append(&msg, 0, "%s.", cfg->global->prefix);
} }
ast_str_append(&msg, 0, "%s:%jd|%s", metric_name, value, metric_type); ast_str_append(&msg, 0, "%s:%s|%s", metric_name, value, metric_type);
if (sample_rate < 1.0) { if (sample_rate < 1.0) {
ast_str_append(&msg, 0, "|@%.2f", sample_rate); ast_str_append(&msg, 0, "|@%.2f", sample_rate);
@ -144,20 +143,39 @@ void AST_OPTIONAL_API_NAME(ast_statsd_log_full)(const char *metric_name,
len = ast_str_strlen(msg); len = ast_str_strlen(msg);
ast_debug(6, "send: %s\n", ast_str_buffer(msg)); ast_debug(6, "Sending statistic %s to StatsD server\n", ast_str_buffer(msg));
ast_sendto(socket_fd, ast_str_buffer(msg), len, 0, &statsd_server); ast_sendto(socket_fd, ast_str_buffer(msg), len, 0, &statsd_server);
ao2_cleanup(cfg);
ast_free(msg);
}
void AST_OPTIONAL_API_NAME(ast_statsd_log_full)(const char *metric_name,
const char *metric_type, intmax_t value, double sample_rate)
{
char char_value[30];
snprintf(char_value, sizeof(char_value), "%jd", value);
ast_statsd_log_string(metric_name, metric_type, char_value, sample_rate);
} }
void AST_OPTIONAL_API_NAME(ast_statsd_log)(const char *metric_name, void AST_OPTIONAL_API_NAME(ast_statsd_log)(const char *metric_name,
const char *metric_type, intmax_t value) const char *metric_type, intmax_t value)
{ {
ast_statsd_log_full(metric_name, metric_type, value, 1.0); char char_value[30];
snprintf(char_value, sizeof(char_value), "%jd", value);
ast_statsd_log_string(metric_name, metric_type, char_value, 1.0);
} }
void AST_OPTIONAL_API_NAME(ast_statsd_log_sample)(const char *metric_name, void AST_OPTIONAL_API_NAME(ast_statsd_log_sample)(const char *metric_name,
intmax_t value, double sample_rate) intmax_t value, double sample_rate)
{ {
ast_statsd_log_full(metric_name, AST_STATSD_COUNTER, value, char char_value[30];
snprintf(char_value, sizeof(char_value), "%jd", value);
ast_statsd_log_string(metric_name, AST_STATSD_COUNTER, char_value,
sample_rate); sample_rate);
} }

@ -3,6 +3,7 @@
LINKER_SYMBOL_PREFIX*ast_statsd_log; LINKER_SYMBOL_PREFIX*ast_statsd_log;
LINKER_SYMBOL_PREFIX*ast_statsd_log_full; LINKER_SYMBOL_PREFIX*ast_statsd_log_full;
LINKER_SYMBOL_PREFIX*ast_statsd_log_sample; LINKER_SYMBOL_PREFIX*ast_statsd_log_sample;
LINKER_SYMBOL_PREFIX*ast_statsd_log_string;
local: local:
*; *;
}; };

Loading…
Cancel
Save