Asterisk - The Open Source Telephony Project  18.5.0
Macros | Functions
statsd.h File Reference
#include "asterisk/optional_api.h"
Include dependency graph for statsd.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define AST_STATSD_COUNTER   "c"
 
#define AST_STATSD_GAUGE   "g"
 Support for publishing to a statsd server. More...
 
#define AST_STATSD_GUAGE   AST_STATSD_GAUGE
 
#define AST_STATSD_HISTOGRAM   "h"
 
#define AST_STATSD_METER   "m"
 
#define AST_STATSD_TIMER   "ms"
 

Functions

void ast_statsd_log (const char *metric_name, const char *metric_type, intmax_t value)
 Send a stat to the configured statsd server. More...
 
void ast_statsd_log_full (const char *metric_name, const char *metric_type, intmax_t value, double sample_rate)
 Send a stat to the configured statsd server. More...
 
void ast_statsd_log_full_va (const char *metric_name, const char *metric_type, intmax_t value, double sample_rate,...)
 Send a stat to the configured statsd server. More...
 
void ast_statsd_log_sample (const char *metric_name, intmax_t value, double sample_rate)
 Send a random sampling of a stat to the configured statsd server. More...
 
void ast_statsd_log_string (const char *metric_name, const char *metric_type, const char *value, double sample_rate)
 Send a stat to the configured statsd server. More...
 
void ast_statsd_log_string_va (const char *metric_name, const char *metric_type, const char *value, double sample_rate,...)
 Send a stat to the configured statsd server. More...
 

Macro Definition Documentation

◆ AST_STATSD_COUNTER

#define AST_STATSD_COUNTER   "c"

A change in a value.

Definition at line 39 of file statsd.h.

Referenced by ast_statsd_log_sample().

◆ AST_STATSD_GAUGE

#define AST_STATSD_GAUGE   "g"

◆ AST_STATSD_GUAGE

#define AST_STATSD_GUAGE   AST_STATSD_GAUGE

Embarrassingly, gauge was misspelled for quite some time.

Deprecated:
You should spell gauge correctly.

Definition at line 37 of file statsd.h.

◆ AST_STATSD_HISTOGRAM

#define AST_STATSD_HISTOGRAM   "h"

Distribution of values over time.

Definition at line 43 of file statsd.h.

◆ AST_STATSD_METER

#define AST_STATSD_METER   "m"

Events over time. Sorta like increment-only counters.

Definition at line 45 of file statsd.h.

Referenced by statsmaker().

◆ AST_STATSD_TIMER

#define AST_STATSD_TIMER   "ms"

Measure of milliseconds.

Definition at line 41 of file statsd.h.

Referenced by sip_options_contact_status_notify_task(), and updates().

Function Documentation

◆ ast_statsd_log()

void ast_statsd_log ( const char *  metric_name,
const char *  metric_type,
intmax_t  value 
)

Send a stat to the configured statsd server.

Parameters
metric_nameString (UTF-8) name of the metric.
metric_typeType of metric to send.
valueValue to send.
Since
12

Definition at line 222 of file res_statsd.c.

References ast_statsd_log_string(), and value.

Referenced by load_module(), statsmaker(), and updates().

224 {
225  char char_value[30];
226  snprintf(char_value, sizeof(char_value), "%jd", value);
227 
228  ast_statsd_log_string(metric_name, metric_type, char_value, 1.0);
229 }
int value
Definition: syslog.c:37
void AST_OPTIONAL_API_NAME() ast_statsd_log_string(const char *metric_name, const char *metric_type, const char *value, double sample_rate)
Send a stat to the configured statsd server.
Definition: res_statsd.c:111

◆ ast_statsd_log_full()

void ast_statsd_log_full ( const char *  metric_name,
const char *  metric_type,
intmax_t  value,
double  sample_rate 
)

Send a stat to the configured statsd server.

The is nearly 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.

Parameters
metric_nameString (UTF-8) name of the metric.
type_strType of metric to send.
valueValue to send.
sample_ratePercentage of samples to send.
Since
12

Definition at line 164 of file res_statsd.c.

References ast_statsd_log_string(), and value.

Referenced by ast_statsd_log_full_va().

166 {
167  char char_value[30];
168  snprintf(char_value, sizeof(char_value), "%jd", value);
169 
170  ast_statsd_log_string(metric_name, metric_type, char_value, sample_rate);
171 
172 }
int value
Definition: syslog.c:37
void AST_OPTIONAL_API_NAME() ast_statsd_log_string(const char *metric_name, const char *metric_type, const char *value, double sample_rate)
Send a stat to the configured statsd server.
Definition: res_statsd.c:111

◆ ast_statsd_log_full_va()

void ast_statsd_log_full_va ( const char *  metric_name,
const char *  metric_type,
intmax_t  value,
double  sample_rate,
  ... 
)

Send a stat to the configured statsd server.

Since
13.7.0

This is the most flexible function for sending a message to the statsd server. In addition to allowing the value and sample rate to be specified, the metric_name can be formed as a printf style string with variable arguments.

Parameters
metric_nameFormat string (UTF-8) specifying the name of the metric.
metric_typeType of metric to send.
valueValue to send.
sample_ratePercentage of samples to send.

Example Usage:

ast_statsd_log_full_va(AST_STATSD_TIMER, rtt, 1.0, "endpoint.%s.rtt", endpoint_name);

Definition at line 199 of file res_statsd.c.

References AST_DYNSTR_BUILD_FAILED, ast_statsd_log_full(), ast_str_buffer(), ast_str_set_va(), ast_str_thread_get(), buf, statsd_buf, and value.

Referenced by ast_sip_initialize_sorcery_location(), handle_endpoint_update(), and sip_options_contact_status_notify_task().

201 {
202  struct ast_str *buf;
203  va_list ap;
204  int res;
205 
206  buf = ast_str_thread_get(&statsd_buf, 128);
207  if (!buf) {
208  return;
209  }
210 
211  va_start(ap, sample_rate);
212  res = ast_str_set_va(&buf, 0, metric_name, ap);
213  va_end(ap);
214 
215  if (res == AST_DYNSTR_BUILD_FAILED) {
216  return;
217  }
218 
219  ast_statsd_log_full(ast_str_buffer(buf), metric_type, value, sample_rate);
220 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
int ast_str_set_va(struct ast_str **buf, ssize_t max_len, const char *fmt, va_list ap)
Set a dynamic string from a va_list.
Definition: strings.h:982
int value
Definition: syslog.c:37
void AST_OPTIONAL_API_NAME() ast_statsd_log_full(const char *metric_name, const char *metric_type, intmax_t value, double sample_rate)
Send a stat to the configured statsd server.
Definition: res_statsd.c:164
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
static struct ast_threadstorage statsd_buf
Definition: res_statsd.c:174
struct ast_str * ast_str_thread_get(struct ast_threadstorage *ts, size_t init_len)
Retrieve a thread locally stored dynamic string.
Definition: strings.h:861

◆ ast_statsd_log_sample()

void ast_statsd_log_sample ( const char *  metric_name,
intmax_t  value,
double  sample_rate 
)

Send a random sampling of a stat to the configured statsd server.

The type of sampled metrics is always AST_STATSD_COUNTER. The given sample_rate should be a percentage between 0.0 and 1.0. If it's <= 0.0, then no samples will be sent. If it's >= 1.0, then all samples will be sent.

Parameters
metric_nameString (UTF-8) name of the metric.
valueValue to send.
sample_ratePercentage of samples to send.
Since
12

Definition at line 231 of file res_statsd.c.

References AST_STATSD_COUNTER, ast_statsd_log_string(), and value.

233 {
234  char char_value[30];
235  snprintf(char_value, sizeof(char_value), "%jd", value);
236 
237  ast_statsd_log_string(metric_name, AST_STATSD_COUNTER, char_value,
238  sample_rate);
239 }
int value
Definition: syslog.c:37
void AST_OPTIONAL_API_NAME() ast_statsd_log_string(const char *metric_name, const char *metric_type, const char *value, double sample_rate)
Send a stat to the configured statsd server.
Definition: res_statsd.c:111
#define AST_STATSD_COUNTER
Definition: statsd.h:39

◆ ast_statsd_log_string()

void ast_statsd_log_string ( const char *  metric_name,
const char *  metric_type,
const char *  value,
double  sample_rate 
)

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.

Parameters
metric_nameString (UTF-8) name of the metric.
type_strType of metric to send.
valueValue to send.
sample_ratePercentage of samples to send.
Since
13

Definition at line 111 of file res_statsd.c.

References conf_global_options::add_newline, ao2_cleanup, ao2_global_obj_ref, ast_debug, ast_free, ast_random_double, ast_sendto(), ast_str_append(), ast_str_buffer(), ast_str_create, ast_str_strlen(), ast_strlen_zero, conf_server(), conf::global, len(), conf_global_options::prefix, socket_fd, and value.

Referenced by ast_statsd_log(), ast_statsd_log_full(), ast_statsd_log_sample(), ast_statsd_log_string_va(), handle_endpoint_update(), sip_outbound_registration_client_state_destroy(), sip_outbound_registration_state_alloc(), statsd_exec(), update_endpoint_state(), and updates().

113 {
114  struct conf *cfg;
115  struct ast_str *msg;
116  size_t len;
117  struct ast_sockaddr statsd_server;
118 
119  if (socket_fd == -1) {
120  return;
121  }
122 
123  /* Rates <= 0.0 never get logged.
124  * Rates >= 1.0 always get logged.
125  * All others leave it to chance.
126  */
127  if (sample_rate <= 0.0 ||
128  (sample_rate < 1.0 && sample_rate < ast_random_double())) {
129  return;
130  }
131 
132  cfg = ao2_global_obj_ref(confs);
133  conf_server(cfg, &statsd_server);
134 
135  msg = ast_str_create(40);
136  if (!msg) {
137  ao2_cleanup(cfg);
138  return;
139  }
140 
141  if (!ast_strlen_zero(cfg->global->prefix)) {
142  ast_str_append(&msg, 0, "%s.", cfg->global->prefix);
143  }
144 
145  ast_str_append(&msg, 0, "%s:%s|%s", metric_name, value, metric_type);
146 
147  if (sample_rate < 1.0) {
148  ast_str_append(&msg, 0, "|@%.2f", sample_rate);
149  }
150 
151  if (cfg->global->add_newline) {
152  ast_str_append(&msg, 0, "\n");
153  }
154 
155  len = ast_str_strlen(msg);
156 
157  ast_debug(6, "Sending statistic %s to StatsD server\n", ast_str_buffer(msg));
158  ast_sendto(socket_fd, ast_str_buffer(msg), len, 0, &statsd_server);
159 
160  ao2_cleanup(cfg);
161  ast_free(msg);
162 }
ssize_t ast_sendto(int sockfd, const void *buf, size_t len, int flags, const struct ast_sockaddr *dest_addr)
Wrapper around sendto(2) that uses ast_sockaddr.
Definition: netsock2.c:614
struct conf_global_options * global
Definition: res_statsd.c:97
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1091
#define ao2_global_obj_ref(holder)
Definition: astobj2.h:925
int value
Definition: syslog.c:37
Socket address structure.
Definition: netsock2.h:97
#define ast_strlen_zero(foo)
Definition: strings.h:52
All configuration options for statsd client.
Definition: res_statsd.c:95
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define ast_free(a)
Definition: astmm.h:182
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:688
char prefix[MAX_PREFIX+1]
Definition: res_statsd.c:91
static int socket_fd
Definition: res_statsd.c:80
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define ast_random_double()
Returns a random number between 0.0 and 1.0, inclusive.
Definition: utils.h:599
static void conf_server(const struct conf *cfg, struct ast_sockaddr *addr)
Definition: res_statsd.c:103
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620

◆ ast_statsd_log_string_va()

void ast_statsd_log_string_va ( const char *  metric_name,
const char *  metric_type,
const char *  value,
double  sample_rate,
  ... 
)

Send a stat to the configured statsd server.

Since
13.7.0

This is the most flexible function for sending a message to the statsd server. In addition to allowing the string value and sample rate to be specified, the metric_name can be formed as a printf style string with variable arguments.

Parameters
metric_nameFormat string (UTF-8) specifying the name of the metric.
metric_typeType of metric to send.
valueValue to send.
sample_ratePercentage of samples to send.

Example Usage:

ast_statsd_log_string_va(AST_STATSD_GAUGE, "+1", 1.0, "endpoints.states.%s", state_name);

Definition at line 176 of file res_statsd.c.

References AST_DYNSTR_BUILD_FAILED, ast_statsd_log_string(), ast_str_buffer(), ast_str_set_va(), ast_str_thread_get(), buf, statsd_buf, and value.

Referenced by ast_res_pjsip_find_or_create_contact_status(), sip_options_contact_status_notify_task(), sip_options_remove_contact_status(), sip_options_set_contact_status(), sip_outbound_registration_client_state_destroy(), sip_outbound_registration_state_alloc(), and update_client_state_status().

178 {
179  struct ast_str *buf;
180  va_list ap;
181  int res;
182 
183  buf = ast_str_thread_get(&statsd_buf, 128);
184  if (!buf) {
185  return;
186  }
187 
188  va_start(ap, sample_rate);
189  res = ast_str_set_va(&buf, 0, metric_name, ap);
190  va_end(ap);
191 
192  if (res == AST_DYNSTR_BUILD_FAILED) {
193  return;
194  }
195 
196  ast_statsd_log_string(ast_str_buffer(buf), metric_type, value, sample_rate);
197 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
int ast_str_set_va(struct ast_str **buf, ssize_t max_len, const char *fmt, va_list ap)
Set a dynamic string from a va_list.
Definition: strings.h:982
int value
Definition: syslog.c:37
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
static struct ast_threadstorage statsd_buf
Definition: res_statsd.c:174
void AST_OPTIONAL_API_NAME() ast_statsd_log_string(const char *metric_name, const char *metric_type, const char *value, double sample_rate)
Send a stat to the configured statsd server.
Definition: res_statsd.c:111
struct ast_str * ast_str_thread_get(struct ast_threadstorage *ts, size_t init_len)
Retrieve a thread locally stored dynamic string.
Definition: strings.h:861