blob: b89ec9213f638dfe4031e422b71da52e191ec68d [file] [log] [blame]
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +01001/*
Harald Weltee08da972017-11-13 01:00:26 +09002 * (C) 2015 by sysmocom - s.f.m.c. GmbH
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +01003 * Author: Jacob Erlbeck <jerlbeck@sysmocom.de>
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +01004 * All Rights Reserved
5 *
Harald Weltee08da972017-11-13 01:00:26 +09006 * SPDX-License-Identifier: GPL-2.0+
7 *
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +01008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +010018 */
19
Harald Welteeb5b6ce2017-10-15 20:03:24 +020020/*! \addtogroup stats
21 * @{
22 * \file stats_statsd.c */
23
Harald Welte67bdd802017-01-15 17:56:11 +010024#include "config.h"
25#if !defined(EMBEDDED)
26
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +010027#include <osmocom/core/stats.h>
28
29#include <string.h>
30#include <stdint.h>
Pau Espin Pedrol3f2775b2020-11-28 01:03:56 +010031#include <inttypes.h>
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +010032#include <errno.h>
33
34#include <osmocom/core/utils.h>
35#include <osmocom/core/logging.h>
36#include <osmocom/core/rate_ctr.h>
37#include <osmocom/core/stat_item.h>
38#include <osmocom/core/msgb.h>
Harald Welte1554f802016-11-11 15:06:06 +010039#include <osmocom/core/stats.h>
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +010040
41static int osmo_stats_reporter_statsd_send_counter(struct osmo_stats_reporter *srep,
42 const struct rate_ctr_group *ctrg,
43 const struct rate_ctr_desc *desc,
44 int64_t value, int64_t delta);
45static int osmo_stats_reporter_statsd_send_item(struct osmo_stats_reporter *srep,
46 const struct osmo_stat_item_group *statg,
Alexander Couzens27a35ed2016-10-04 17:13:58 +020047 const struct osmo_stat_item_desc *desc, int64_t value);
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +010048
Harald Welteeb5b6ce2017-10-15 20:03:24 +020049/*! Create a stats_reporter reporting to statsd. This creates a stats_reporter
50 * instance which reports the related statistics data to statsd.
51 * \param[in] name Name of the to-be-created stats_reporter
52 * \returns stats_reporter on success; NULL on error */
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +010053struct osmo_stats_reporter *osmo_stats_reporter_create_statsd(const char *name)
54{
55 struct osmo_stats_reporter *srep;
56 srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_STATSD, name);
57
58 srep->have_net_config = 1;
59
60 srep->open = osmo_stats_reporter_udp_open;
61 srep->close = osmo_stats_reporter_udp_close;
62 srep->send_counter = osmo_stats_reporter_statsd_send_counter;
63 srep->send_item = osmo_stats_reporter_statsd_send_item;
64
65 return srep;
66}
67
Alexander Couzens9af70762018-07-24 16:37:54 +020068/*! Replace all illegal ':' in the stats name, but not when used as value seperator.
69 * ':' is used as seperator between the name and the value in the statsd protocol.
70 * \param[inout] buf is a null terminated string containing name, value, unit. */
71static void osmo_stats_reporter_sanitize_name(char *buf)
72{
73 /* e.g. msc.loc_update_type:normal:1|c -> msc.loc_update_type.normal:1|c
74 * last is the seperator between name and value */
75 char *last = strrchr(buf, ':');
76 char *tmp = strchr(buf, ':');
77
78 if (!last)
79 return;
80
81 while (tmp < last) {
82 *tmp = '.';
83 tmp = strchr(buf, ':');
84 }
85}
86
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +010087static int osmo_stats_reporter_statsd_send(struct osmo_stats_reporter *srep,
Pau Espin Pedrol09f075f2021-05-31 13:10:24 +020088 const char *name1, const char *index1, const char *name2, int64_t value,
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +010089 const char *unit)
90{
91 char *buf;
92 int buf_size;
93 int nchars, rc = 0;
94 char *fmt = NULL;
95 char *prefix = srep->name_prefix;
96 int old_len = msgb_length(srep->buffer);
97
98 if (prefix) {
Kirill Zakharenko0ae0fa12020-04-23 17:33:12 +030099 if (name1)
Pau Espin Pedrol09f075f2021-05-31 13:10:24 +0200100 fmt = "%1$s.%2$s.%6$s.%3$s:%4$" PRId64 "|%5$s";
Kirill Zakharenko0ae0fa12020-04-23 17:33:12 +0300101 else
Pau Espin Pedrol3f2775b2020-11-28 01:03:56 +0100102 fmt = "%1$s.%2$0.0s%3$s:%4$" PRId64 "|%5$s";
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100103 } else {
104 prefix = "";
Kirill Zakharenko0ae0fa12020-04-23 17:33:12 +0300105 if (name1)
Pau Espin Pedrol09f075f2021-05-31 13:10:24 +0200106 fmt = "%1$s%2$s.%6$s.%3$s:%4$" PRId64 "|%5$s";
Kirill Zakharenko0ae0fa12020-04-23 17:33:12 +0300107 else
Pau Espin Pedrol3f2775b2020-11-28 01:03:56 +0100108 fmt = "%1$s%2$0.0s%3$s:%4$" PRId64 "|%5$s";
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100109 }
110
111 if (srep->agg_enabled) {
112 if (msgb_length(srep->buffer) > 0 &&
113 msgb_tailroom(srep->buffer) > 0)
114 {
115 msgb_put_u8(srep->buffer, '\n');
116 }
117 }
118
119 buf = (char *)msgb_put(srep->buffer, 0);
120 buf_size = msgb_tailroom(srep->buffer);
121
122 nchars = snprintf(buf, buf_size, fmt,
123 prefix, name1, name2,
124 value, unit, index1);
125
126 if (nchars >= buf_size) {
127 /* Truncated */
128 /* Restore original buffer (without trailing LF) */
129 msgb_trim(srep->buffer, old_len);
130 /* Send it */
131 rc = osmo_stats_reporter_send_buffer(srep);
132
133 /* Try again */
134 buf = (char *)msgb_put(srep->buffer, 0);
135 buf_size = msgb_tailroom(srep->buffer);
136
137 nchars = snprintf(buf, buf_size, fmt,
138 prefix, name1, name2,
139 value, unit, index1);
140
141 if (nchars >= buf_size)
142 return -EMSGSIZE;
143 }
144
Alexander Couzens9af70762018-07-24 16:37:54 +0200145 if (nchars > 0) {
146 osmo_stats_reporter_sanitize_name(buf);
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100147 msgb_trim(srep->buffer, msgb_length(srep->buffer) + nchars);
Alexander Couzens9af70762018-07-24 16:37:54 +0200148 }
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100149
150 if (!srep->agg_enabled)
151 rc = osmo_stats_reporter_send_buffer(srep);
152
153 return rc;
154}
155
156static int osmo_stats_reporter_statsd_send_counter(struct osmo_stats_reporter *srep,
157 const struct rate_ctr_group *ctrg,
158 const struct rate_ctr_desc *desc,
159 int64_t value, int64_t delta)
160{
Pau Espin Pedrol09f075f2021-05-31 13:10:24 +0200161 char buf_idx[64];
162 const char *idx_name = buf_idx;
163 const char *prefix;
164
165 if (ctrg) {
166 prefix = ctrg->desc->group_name_prefix;
167 if (ctrg->name)
168 idx_name = ctrg->name;
169 else
170 snprintf(buf_idx, sizeof(buf_idx), "%u", ctrg->idx);
171 } else {
172 prefix = NULL;
173 buf_idx[0] = '0';
174 buf_idx[1] = '\n';
175 }
176 return osmo_stats_reporter_statsd_send(srep, prefix, idx_name, desc->name, delta, "c");
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100177}
178
179static int osmo_stats_reporter_statsd_send_item(struct osmo_stats_reporter *srep,
180 const struct osmo_stat_item_group *statg,
Alexander Couzens27a35ed2016-10-04 17:13:58 +0200181 const struct osmo_stat_item_desc *desc, int64_t value)
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100182{
Pau Espin Pedrol09f075f2021-05-31 13:10:24 +0200183 char buf_idx[64];
184 char *idx_name;
185 if (statg->name)
186 idx_name = statg->name;
187 else {
188 snprintf(buf_idx, sizeof(buf_idx), "%u", statg->idx);
189 idx_name = buf_idx;
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +0100190 }
Pau Espin Pedrol09f075f2021-05-31 13:10:24 +0200191
192 if (value < 0)
193 value = 0;
194
195 return osmo_stats_reporter_statsd_send(srep, statg->desc->group_name_prefix,
196 idx_name, desc->name, value, "g");
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100197}
Harald Welte67bdd802017-01-15 17:56:11 +0100198#endif /* !EMBEDDED */
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200199
200/* @} */