76 #define AST_API_MODULE
77 #include "asterisk/statsd.h"
79 #define DEFAULT_STATSD_PORT 8125
84 static int socket_fd = -1;
118 const char *metric_type,
const char *value,
double sample_rate)
125 if (socket_fd == -1) {
133 if (sample_rate <= 0.0 ||
139 conf_server(cfg, &statsd_server);
152 ast_str_append(&msg, 0,
"%s_meter:%s|%s", metric_name, value, AST_STATSD_COUNTER);
154 ast_str_append(&msg, 0,
"%s:%s|%s", metric_name, value, metric_type);
157 if (sample_rate < 1.0) {
175 const char *metric_type, intmax_t value,
double sample_rate)
178 snprintf(char_value,
sizeof(char_value),
"%jd", value);
180 ast_statsd_log_string(metric_name, metric_type, char_value, sample_rate);
187 const char *metric_type,
const char *value,
double sample_rate, ...)
198 va_start(ap, sample_rate);
206 ast_statsd_log_string(
ast_str_buffer(buf), metric_type, value, sample_rate);
210 const char *metric_type, intmax_t value,
double sample_rate, ...)
221 va_start(ap, sample_rate);
229 ast_statsd_log_full(
ast_str_buffer(buf), metric_type, value, sample_rate);
233 const char *metric_type, intmax_t value)
236 snprintf(char_value,
sizeof(char_value),
"%jd", value);
238 ast_statsd_log_string(metric_name, metric_type, char_value, 1.0);
242 intmax_t value,
double sample_rate)
245 snprintf(char_value,
sizeof(char_value),
"%jd", value);
247 ast_statsd_log_string(metric_name, AST_STATSD_COUNTER, char_value,
256 .item_offset = offsetof(
struct conf, global),
257 .category =
"general",
266 struct conf *cfg = obj;
279 if (!(cfg->
global = ao2_alloc(
sizeof(*cfg->
global), NULL))) {
295 .files = ACO_FILES(&conf_file));
304 static int statsd_init(
void)
312 ast_debug(3,
"Configuring StatsD client.\n");
314 if (socket_fd == -1) {
315 ast_debug(3,
"Creating StatsD socket.\n");
316 socket_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
317 if (socket_fd == -1) {
318 perror(
"Error creating StatsD socket");
323 conf_server(cfg, &statsd_server);
325 AST_SOCKADDR_STR_DEFAULT);
326 ast_debug(3,
" StatsD server = %s.\n", server);
333 static void statsd_shutdown(
void)
335 ast_debug(3,
"Shutting down StatsD client.\n");
336 if (socket_fd != -1) {
342 static int unload_module(
void)
350 static int load_module(
void)
380 ast_log(LOG_NOTICE,
"Could not load statsd config; using defaults\n");
388 ast_log(LOG_ERROR,
"Failed to initialize statsd defaults.\n");
410 static int reload_module(
void)
435 AST_MODULE_INFO(
ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER,
"StatsD client support",
436 .support_level = AST_MODULE_SUPPORT_EXTENDED,
438 .unload = unload_module,
439 .reload = reload_module,
Type for default handler for ast_sockaddrs.
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
static struct aco_file conf_file
The conf file that's processed for the module.
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.
Asterisk main include file. File version handling, generic pbx functions.
struct conf_global_options * global
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
#define aco_option_register(info, name, matchtype, types, default_val, opt_type, flags,...)
Register a config option.
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.
#define CHARFLDSET(type, field)
A helper macro to pass the appropriate arguments to aco_option_register for OPT_CHAR_ARRAY_T.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
enum aco_process_status aco_process_config(struct aco_info *info, int reload)
Process a config info via the options registered with an aco_info.
#define ao2_global_obj_ref(holder)
Get a reference to the object stored in the global holder.
The representation of a single configuration file to be processed.
Socket address structure.
#define ACO_TYPES(...)
A helper macro to ensure that aco_info types always have a sentinel.
struct ast_sockaddr statsd_server
All configuration options for http media cache.
#define ast_sockaddr_port(addr)
Get the port number of a socket address.
Type for default option handler for character array strings.
#define FLDSET(type,...)
Convert a struct and list of fields to an argument list of field offsets.
int aco_info_init(struct aco_info *info)
Initialize an aco_info structure.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
#define ast_debug(level,...)
Log a DEBUG message.
The config had not been edited and no changes applied.
Their was an error and no changes were applied.
Configuration option-handling.
Support for dynamic strings.
#define ast_sockaddr_set_port(addr, port)
Sets the port number of a socket address.
void aco_info_destroy(struct aco_info *info)
Destroy an initialized aco_info struct.
#define ao2_global_obj_release(holder)
Release the ao2 object held in the global holder.
Type for default option handler for bools (ast_true/ast_false)
char * ast_sockaddr_stringify_fmt(const struct ast_sockaddr *addr, int format)
Convert a socket address to a string.
static void * conf_alloc(void)
Allocate an ast_ari_conf for config parsing.
int aco_set_defaults(struct aco_type *type, const char *category, void *obj)
Set all default options of obj.
The config was processed and applied.
Module has failed to load, may be in an inconsistent state.
static struct aco_type global_option
An aco_type structure to link the "general" category to the skel_global_config type.
Global configuration options for statsd client.
static int is_enabled(void)
Helper function to check if module is enabled.
#define AST_YESNO(x)
return Yes or No depending on the argument.
#define ao2_global_obj_replace_unref(holder, obj)
Replace an ao2 object in the global holder, throwing away any old object.
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
char prefix[MAX_PREFIX+1]
Type information about a category-level configurable object.
static int enabled
Whether or not we are storing history.
#define ast_random_double()
Returns a random number between 0.0 and 1.0, inclusive.
#define AST_OPTIONAL_API_NAME(name)
Expands to the name of the implementation function.
struct ast_str * ast_str_thread_get(struct ast_threadstorage *ts, size_t init_len)
Retrieve a thread locally stored dynamic string.
#define AO2_GLOBAL_OBJ_STATIC(name)
Define a global object holder to be used to hold an ao2 object, statically initialized.
#define ASTERISK_GPL_KEY
The text the key() function should return.
static void conf_destructor(void *obj)
ast_ari_conf destructor.
Asterisk module definitions.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
#define CONFIG_INFO_STANDARD(name, arr, alloc,...)
Declare an aco_info struct with default module and preload values.
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.