blob: c3f739e22a8476ff073c7c2e9da72ec9b6ef7912 [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) {
102 if (name1) {
103 if (index1 != 0)
104 fmt = "%1$s.%2$s.%6$u.%3$s:%4$d|%5$s";
105 else
106 fmt = "%1$s.%2$s.%3$s:%4$d|%5$s";
107 } else {
108 fmt = "%1$s.%2$0.0s%3$s:%4$d|%5$s";
109 }
110 } else {
111 prefix = "";
112 if (name1) {
113 if (index1 != 0)
114 fmt = "%1$s%2$s.%6$u.%3$s:%4$d|%5$s";
115 else
116 fmt = "%1$s%2$s.%3$s:%4$d|%5$s";
117 } else {
118 fmt = "%1$s%2$0.0s%3$s:%4$d|%5$s";
119 }
120 }
121
122 if (srep->agg_enabled) {
123 if (msgb_length(srep->buffer) > 0 &&
124 msgb_tailroom(srep->buffer) > 0)
125 {
126 msgb_put_u8(srep->buffer, '\n');
127 }
128 }
129
130 buf = (char *)msgb_put(srep->buffer, 0);
131 buf_size = msgb_tailroom(srep->buffer);
132
133 nchars = snprintf(buf, buf_size, fmt,
134 prefix, name1, name2,
135 value, unit, index1);
136
137 if (nchars >= buf_size) {
138 /* Truncated */
139 /* Restore original buffer (without trailing LF) */
140 msgb_trim(srep->buffer, old_len);
141 /* Send it */
142 rc = osmo_stats_reporter_send_buffer(srep);
143
144 /* Try again */
145 buf = (char *)msgb_put(srep->buffer, 0);
146 buf_size = msgb_tailroom(srep->buffer);
147
148 nchars = snprintf(buf, buf_size, fmt,
149 prefix, name1, name2,
150 value, unit, index1);
151
152 if (nchars >= buf_size)
153 return -EMSGSIZE;
154 }
155
Alexander Couzens9af70762018-07-24 16:37:54 +0200156 if (nchars > 0) {
157 osmo_stats_reporter_sanitize_name(buf);
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100158 msgb_trim(srep->buffer, msgb_length(srep->buffer) + nchars);
Alexander Couzens9af70762018-07-24 16:37:54 +0200159 }
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100160
161 if (!srep->agg_enabled)
162 rc = osmo_stats_reporter_send_buffer(srep);
163
164 return rc;
165}
166
167static int osmo_stats_reporter_statsd_send_counter(struct osmo_stats_reporter *srep,
168 const struct rate_ctr_group *ctrg,
169 const struct rate_ctr_desc *desc,
170 int64_t value, int64_t delta)
171{
172 if (ctrg)
173 return osmo_stats_reporter_statsd_send(srep,
174 ctrg->desc->group_name_prefix,
175 ctrg->idx,
176 desc->name, delta, "c");
177 else
178 return osmo_stats_reporter_statsd_send(srep,
179 NULL, 0,
180 desc->name, delta, "c");
181}
182
183static int osmo_stats_reporter_statsd_send_item(struct osmo_stats_reporter *srep,
184 const struct osmo_stat_item_group *statg,
Alexander Couzens27a35ed2016-10-04 17:13:58 +0200185 const struct osmo_stat_item_desc *desc, int64_t value)
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100186{
Daniel Willmann0c878fd2018-10-11 17:55:28 +0200187 if (value < 0) {
188 return osmo_stats_reporter_statsd_send(srep,
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +0100189 statg->desc->group_name_prefix,
190 statg->idx,
Daniel Willmann0c878fd2018-10-11 17:55:28 +0200191 desc->name, 0, "g");
192 } else {
193 return osmo_stats_reporter_statsd_send(srep,
194 statg->desc->group_name_prefix,
195 statg->idx,
196 desc->name, value, "g");
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +0100197 }
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100198}
Harald Welte67bdd802017-01-15 17:56:11 +0100199#endif /* !EMBEDDED */
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200200
201/* @} */