blob: d44966773d6511b262b0620711e3b1439255ba62 [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 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
Harald Welteeb5b6ce2017-10-15 20:03:24 +020024/*! \addtogroup stats
25 * @{
26 * \file stats_statsd.c */
27
Harald Welte67bdd802017-01-15 17:56:11 +010028#include "config.h"
29#if !defined(EMBEDDED)
30
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +010031#include <osmocom/core/stats.h>
32
33#include <string.h>
34#include <stdint.h>
35#include <errno.h>
36
37#include <osmocom/core/utils.h>
38#include <osmocom/core/logging.h>
39#include <osmocom/core/rate_ctr.h>
40#include <osmocom/core/stat_item.h>
41#include <osmocom/core/msgb.h>
Harald Welte1554f802016-11-11 15:06:06 +010042#include <osmocom/core/stats.h>
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +010043
44static int osmo_stats_reporter_statsd_send_counter(struct osmo_stats_reporter *srep,
45 const struct rate_ctr_group *ctrg,
46 const struct rate_ctr_desc *desc,
47 int64_t value, int64_t delta);
48static int osmo_stats_reporter_statsd_send_item(struct osmo_stats_reporter *srep,
49 const struct osmo_stat_item_group *statg,
Alexander Couzens27a35ed2016-10-04 17:13:58 +020050 const struct osmo_stat_item_desc *desc, int64_t value);
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +010051
Harald Welteeb5b6ce2017-10-15 20:03:24 +020052/*! Create a stats_reporter reporting to statsd. This creates a stats_reporter
53 * instance which reports the related statistics data to statsd.
54 * \param[in] name Name of the to-be-created stats_reporter
55 * \returns stats_reporter on success; NULL on error */
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +010056struct osmo_stats_reporter *osmo_stats_reporter_create_statsd(const char *name)
57{
58 struct osmo_stats_reporter *srep;
59 srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_STATSD, name);
60
61 srep->have_net_config = 1;
62
63 srep->open = osmo_stats_reporter_udp_open;
64 srep->close = osmo_stats_reporter_udp_close;
65 srep->send_counter = osmo_stats_reporter_statsd_send_counter;
66 srep->send_item = osmo_stats_reporter_statsd_send_item;
67
68 return srep;
69}
70
Alexander Couzens9af70762018-07-24 16:37:54 +020071/*! Replace all illegal ':' in the stats name, but not when used as value seperator.
72 * ':' is used as seperator between the name and the value in the statsd protocol.
73 * \param[inout] buf is a null terminated string containing name, value, unit. */
74static void osmo_stats_reporter_sanitize_name(char *buf)
75{
76 /* e.g. msc.loc_update_type:normal:1|c -> msc.loc_update_type.normal:1|c
77 * last is the seperator between name and value */
78 char *last = strrchr(buf, ':');
79 char *tmp = strchr(buf, ':');
80
81 if (!last)
82 return;
83
84 while (tmp < last) {
85 *tmp = '.';
86 tmp = strchr(buf, ':');
87 }
88}
89
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +010090static int osmo_stats_reporter_statsd_send(struct osmo_stats_reporter *srep,
Alexander Couzens27a35ed2016-10-04 17:13:58 +020091 const char *name1, unsigned int index1, const char *name2, int64_t value,
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +010092 const char *unit)
93{
94 char *buf;
95 int buf_size;
96 int nchars, rc = 0;
97 char *fmt = NULL;
98 char *prefix = srep->name_prefix;
99 int old_len = msgb_length(srep->buffer);
100
101 if (prefix) {
Kirill Zakharenko0ae0fa12020-04-23 17:33:12 +0300102 if (name1)
103 fmt = "%1$s.%2$s.%6$u.%3$s:%4$d|%5$s";
104 else
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100105 fmt = "%1$s.%2$0.0s%3$s:%4$d|%5$s";
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100106 } else {
107 prefix = "";
Kirill Zakharenko0ae0fa12020-04-23 17:33:12 +0300108 if (name1)
109 fmt = "%1$s%2$s.%6$u.%3$s:%4$d|%5$s";
110 else
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100111 fmt = "%1$s%2$0.0s%3$s:%4$d|%5$s";
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100112 }
113
114 if (srep->agg_enabled) {
115 if (msgb_length(srep->buffer) > 0 &&
116 msgb_tailroom(srep->buffer) > 0)
117 {
118 msgb_put_u8(srep->buffer, '\n');
119 }
120 }
121
122 buf = (char *)msgb_put(srep->buffer, 0);
123 buf_size = msgb_tailroom(srep->buffer);
124
125 nchars = snprintf(buf, buf_size, fmt,
126 prefix, name1, name2,
127 value, unit, index1);
128
129 if (nchars >= buf_size) {
130 /* Truncated */
131 /* Restore original buffer (without trailing LF) */
132 msgb_trim(srep->buffer, old_len);
133 /* Send it */
134 rc = osmo_stats_reporter_send_buffer(srep);
135
136 /* Try again */
137 buf = (char *)msgb_put(srep->buffer, 0);
138 buf_size = msgb_tailroom(srep->buffer);
139
140 nchars = snprintf(buf, buf_size, fmt,
141 prefix, name1, name2,
142 value, unit, index1);
143
144 if (nchars >= buf_size)
145 return -EMSGSIZE;
146 }
147
Alexander Couzens9af70762018-07-24 16:37:54 +0200148 if (nchars > 0) {
149 osmo_stats_reporter_sanitize_name(buf);
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100150 msgb_trim(srep->buffer, msgb_length(srep->buffer) + nchars);
Alexander Couzens9af70762018-07-24 16:37:54 +0200151 }
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100152
153 if (!srep->agg_enabled)
154 rc = osmo_stats_reporter_send_buffer(srep);
155
156 return rc;
157}
158
159static int osmo_stats_reporter_statsd_send_counter(struct osmo_stats_reporter *srep,
160 const struct rate_ctr_group *ctrg,
161 const struct rate_ctr_desc *desc,
162 int64_t value, int64_t delta)
163{
164 if (ctrg)
165 return osmo_stats_reporter_statsd_send(srep,
166 ctrg->desc->group_name_prefix,
167 ctrg->idx,
168 desc->name, delta, "c");
169 else
170 return osmo_stats_reporter_statsd_send(srep,
171 NULL, 0,
172 desc->name, delta, "c");
173}
174
175static int osmo_stats_reporter_statsd_send_item(struct osmo_stats_reporter *srep,
176 const struct osmo_stat_item_group *statg,
Alexander Couzens27a35ed2016-10-04 17:13:58 +0200177 const struct osmo_stat_item_desc *desc, int64_t value)
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100178{
Daniel Willmann0c878fd2018-10-11 17:55:28 +0200179 if (value < 0) {
180 return osmo_stats_reporter_statsd_send(srep,
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +0100181 statg->desc->group_name_prefix,
182 statg->idx,
Daniel Willmann0c878fd2018-10-11 17:55:28 +0200183 desc->name, 0, "g");
184 } else {
185 return osmo_stats_reporter_statsd_send(srep,
186 statg->desc->group_name_prefix,
187 statg->idx,
188 desc->name, value, "g");
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +0100189 }
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100190}
Harald Welte67bdd802017-01-15 17:56:11 +0100191#endif /* !EMBEDDED */
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200192
193/* @} */