blob: 8b53881ed9cb833c6d0ba328053b591cc1382874 [file] [log] [blame]
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +01001/*
2 * (C) 2015 by Sysmocom s.f.m.c. GmbH
3 *
4 * Author: Jacob Erlbeck <jerlbeck@sysmocom.de>
5 *
6 * All Rights Reserved
7 *
8 * 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
24#include <osmocom/core/stats.h>
25
26#include <string.h>
27#include <stdint.h>
28#include <errno.h>
29
30#include <osmocom/core/utils.h>
31#include <osmocom/core/logging.h>
32#include <osmocom/core/rate_ctr.h>
33#include <osmocom/core/stat_item.h>
34#include <osmocom/core/msgb.h>
Harald Welte1554f802016-11-11 15:06:06 +010035#include <osmocom/core/stats.h>
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +010036
37static int osmo_stats_reporter_statsd_send_counter(struct osmo_stats_reporter *srep,
38 const struct rate_ctr_group *ctrg,
39 const struct rate_ctr_desc *desc,
40 int64_t value, int64_t delta);
41static int osmo_stats_reporter_statsd_send_item(struct osmo_stats_reporter *srep,
42 const struct osmo_stat_item_group *statg,
Alexander Couzens27a35ed2016-10-04 17:13:58 +020043 const struct osmo_stat_item_desc *desc, int64_t value);
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +010044
45struct osmo_stats_reporter *osmo_stats_reporter_create_statsd(const char *name)
46{
47 struct osmo_stats_reporter *srep;
48 srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_STATSD, name);
49
50 srep->have_net_config = 1;
51
52 srep->open = osmo_stats_reporter_udp_open;
53 srep->close = osmo_stats_reporter_udp_close;
54 srep->send_counter = osmo_stats_reporter_statsd_send_counter;
55 srep->send_item = osmo_stats_reporter_statsd_send_item;
56
57 return srep;
58}
59
60static int osmo_stats_reporter_statsd_send(struct osmo_stats_reporter *srep,
Alexander Couzens27a35ed2016-10-04 17:13:58 +020061 const char *name1, unsigned int index1, const char *name2, int64_t value,
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +010062 const char *unit)
63{
64 char *buf;
65 int buf_size;
66 int nchars, rc = 0;
67 char *fmt = NULL;
68 char *prefix = srep->name_prefix;
69 int old_len = msgb_length(srep->buffer);
70
71 if (prefix) {
72 if (name1) {
73 if (index1 != 0)
74 fmt = "%1$s.%2$s.%6$u.%3$s:%4$d|%5$s";
75 else
76 fmt = "%1$s.%2$s.%3$s:%4$d|%5$s";
77 } else {
78 fmt = "%1$s.%2$0.0s%3$s:%4$d|%5$s";
79 }
80 } else {
81 prefix = "";
82 if (name1) {
83 if (index1 != 0)
84 fmt = "%1$s%2$s.%6$u.%3$s:%4$d|%5$s";
85 else
86 fmt = "%1$s%2$s.%3$s:%4$d|%5$s";
87 } else {
88 fmt = "%1$s%2$0.0s%3$s:%4$d|%5$s";
89 }
90 }
91
92 if (srep->agg_enabled) {
93 if (msgb_length(srep->buffer) > 0 &&
94 msgb_tailroom(srep->buffer) > 0)
95 {
96 msgb_put_u8(srep->buffer, '\n');
97 }
98 }
99
100 buf = (char *)msgb_put(srep->buffer, 0);
101 buf_size = msgb_tailroom(srep->buffer);
102
103 nchars = snprintf(buf, buf_size, fmt,
104 prefix, name1, name2,
105 value, unit, index1);
106
107 if (nchars >= buf_size) {
108 /* Truncated */
109 /* Restore original buffer (without trailing LF) */
110 msgb_trim(srep->buffer, old_len);
111 /* Send it */
112 rc = osmo_stats_reporter_send_buffer(srep);
113
114 /* Try again */
115 buf = (char *)msgb_put(srep->buffer, 0);
116 buf_size = msgb_tailroom(srep->buffer);
117
118 nchars = snprintf(buf, buf_size, fmt,
119 prefix, name1, name2,
120 value, unit, index1);
121
122 if (nchars >= buf_size)
123 return -EMSGSIZE;
124 }
125
126 if (nchars > 0)
127 msgb_trim(srep->buffer, msgb_length(srep->buffer) + nchars);
128
129 if (!srep->agg_enabled)
130 rc = osmo_stats_reporter_send_buffer(srep);
131
132 return rc;
133}
134
135static int osmo_stats_reporter_statsd_send_counter(struct osmo_stats_reporter *srep,
136 const struct rate_ctr_group *ctrg,
137 const struct rate_ctr_desc *desc,
138 int64_t value, int64_t delta)
139{
140 if (ctrg)
141 return osmo_stats_reporter_statsd_send(srep,
142 ctrg->desc->group_name_prefix,
143 ctrg->idx,
144 desc->name, delta, "c");
145 else
146 return osmo_stats_reporter_statsd_send(srep,
147 NULL, 0,
148 desc->name, delta, "c");
149}
150
151static int osmo_stats_reporter_statsd_send_item(struct osmo_stats_reporter *srep,
152 const struct osmo_stat_item_group *statg,
Alexander Couzens27a35ed2016-10-04 17:13:58 +0200153 const struct osmo_stat_item_desc *desc, int64_t value)
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100154{
Holger Hans Peter Freyther5ab8e2c2015-12-17 14:13:22 +0100155 const char *unit = desc->unit;
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +0100156
157 if (unit == OSMO_STAT_ITEM_NO_UNIT) {
158 unit = "g";
159 if (value < 0)
160 osmo_stats_reporter_statsd_send(srep,
161 statg->desc->group_name_prefix,
162 statg->idx,
163 desc->name, 0, unit);
164 }
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100165 return osmo_stats_reporter_statsd_send(srep,
166 statg->desc->group_name_prefix,
167 statg->idx,
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +0100168 desc->name, value, unit);
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100169}