[Asterisk-code-review] StatsD: Add res statsd compatibility (asterisk[master])
Tyler Cambron
asteriskteam at digium.com
Tue Nov 3 14:45:58 CST 2015
Tyler Cambron has uploaded a new change for review.
https://gerrit.asterisk.org/1561
Change subject: StatsD: Add res_statsd compatibility
......................................................................
StatsD: Add res_statsd compatibility
Added a new api to res_statsd.c to allow it to receive a
character pointer for the value argument. This allows for a
'+' and a '-' to easily be sent with the value.
ASTERISK-25419
Reported By: Ashley Sanders
Change-Id: Id6bb53600943d27347d2bcae26c0bd5643567611
---
M include/asterisk/statsd.h
M res/res_statsd.c
M res/res_statsd.exports.in
3 files changed, 68 insertions(+), 0 deletions(-)
git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/61/1561/1
diff --git a/include/asterisk/statsd.h b/include/asterisk/statsd.h
index 8e5e2f9..a04407f 100644
--- a/include/asterisk/statsd.h
+++ b/include/asterisk/statsd.h
@@ -42,6 +42,22 @@
/*!
* \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.
+ *
* The is the most flexible function for sending a message to the statsd server,
* but also the least easy to use. See ast_statsd_log() or
* ast_statsd_log_sample() for a slightly more convenient interface.
diff --git a/res/res_statsd.c b/res/res_statsd.c
index fefe395..e1b1a4d 100644
--- a/res/res_statsd.c
+++ b/res/res_statsd.c
@@ -97,6 +97,57 @@
}
}
+void AST_OPTIONAL_API_NAME(ast_statsd_log_string)(const char *metric_name,
+ const char *metric_type, const char *value, double sample_rate)
+{
+ RAII_VAR(struct conf *, cfg, NULL, ao2_cleanup);
+ RAII_VAR(struct ast_str *, msg, NULL, ast_free);
+ size_t len;
+ struct ast_sockaddr statsd_server;
+
+ if (socket_fd == -1) {
+ return;
+ }
+
+ cfg = ao2_global_obj_ref(confs);
+ conf_server(cfg, &statsd_server);
+
+ /* Rates <= 0.0 never get logged.
+ * Rates >= 1.0 always get logged.
+ * All others leave it to chance.
+ */
+ if (sample_rate <= 0.0 ||
+ (sample_rate < 1.0 && sample_rate < ast_random_double())) {
+ return;
+ }
+
+ cfg = ao2_global_obj_ref(confs);
+
+ msg = ast_str_create(40);
+ if (!msg) {
+ return;
+ }
+
+ if (!ast_strlen_zero(cfg->global->prefix)) {
+ ast_str_append(&msg, 0, "%s.", cfg->global->prefix);
+ }
+
+ ast_str_append(&msg, 0, "%s:%s|%s", metric_name, value, metric_type);
+
+ if (sample_rate < 1.0) {
+ ast_str_append(&msg, 0, "|@%.2f", sample_rate);
+ }
+
+ if (cfg->global->add_newline) {
+ ast_str_append(&msg, 0, "\n");
+ }
+
+ len = ast_str_strlen(msg);
+
+ ast_debug(6, "send: %s\n", ast_str_buffer(msg));
+ ast_sendto(socket_fd, ast_str_buffer(msg), len, 0, &statsd_server);
+}
+
void AST_OPTIONAL_API_NAME(ast_statsd_log_full)(const char *metric_name,
const char *metric_type, intmax_t value, double sample_rate)
{
diff --git a/res/res_statsd.exports.in b/res/res_statsd.exports.in
index 6f02b25..d4a79c1 100644
--- a/res/res_statsd.exports.in
+++ b/res/res_statsd.exports.in
@@ -3,6 +3,7 @@
LINKER_SYMBOL_PREFIX*ast_statsd_log;
LINKER_SYMBOL_PREFIX*ast_statsd_log_full;
LINKER_SYMBOL_PREFIX*ast_statsd_log_sample;
+ LINKER_SYMBOL_PREFIX*ast_statsd_log_string;
local:
*;
};
--
To view, visit https://gerrit.asterisk.org/1561
To unsubscribe, visit https://gerrit.asterisk.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id6bb53600943d27347d2bcae26c0bd5643567611
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Tyler Cambron <tcambron at digium.com>
More information about the asterisk-code-review
mailing list