blob: 28134109dd62116a0e32e109788d5656ce67c049 [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>
35
36static int osmo_stats_reporter_statsd_send_counter(struct osmo_stats_reporter *srep,
37 const struct rate_ctr_group *ctrg,
38 const struct rate_ctr_desc *desc,
39 int64_t value, int64_t delta);
40static int osmo_stats_reporter_statsd_send_item(struct osmo_stats_reporter *srep,
41 const struct osmo_stat_item_group *statg,
42 const struct osmo_stat_item_desc *desc, int value);
43
44struct osmo_stats_reporter *osmo_stats_reporter_create_statsd(const char *name)
45{
46 struct osmo_stats_reporter *srep;
47 srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_STATSD, name);
48
49 srep->have_net_config = 1;
50
51 srep->open = osmo_stats_reporter_udp_open;
52 srep->close = osmo_stats_reporter_udp_close;
53 srep->send_counter = osmo_stats_reporter_statsd_send_counter;
54 srep->send_item = osmo_stats_reporter_statsd_send_item;
55
56 return srep;
57}
58
59static int osmo_stats_reporter_statsd_send(struct osmo_stats_reporter *srep,
60 const char *name1, unsigned int index1, const char *name2, int value,
61 const char *unit)
62{
63 char *buf;
64 int buf_size;
65 int nchars, rc = 0;
66 char *fmt = NULL;
67 char *prefix = srep->name_prefix;
68 int old_len = msgb_length(srep->buffer);
69
70 if (prefix) {
71 if (name1) {
72 if (index1 != 0)
73 fmt = "%1$s.%2$s.%6$u.%3$s:%4$d|%5$s";
74 else
75 fmt = "%1$s.%2$s.%3$s:%4$d|%5$s";
76 } else {
77 fmt = "%1$s.%2$0.0s%3$s:%4$d|%5$s";
78 }
79 } else {
80 prefix = "";
81 if (name1) {
82 if (index1 != 0)
83 fmt = "%1$s%2$s.%6$u.%3$s:%4$d|%5$s";
84 else
85 fmt = "%1$s%2$s.%3$s:%4$d|%5$s";
86 } else {
87 fmt = "%1$s%2$0.0s%3$s:%4$d|%5$s";
88 }
89 }
90
91 if (srep->agg_enabled) {
92 if (msgb_length(srep->buffer) > 0 &&
93 msgb_tailroom(srep->buffer) > 0)
94 {
95 msgb_put_u8(srep->buffer, '\n');
96 }
97 }
98
99 buf = (char *)msgb_put(srep->buffer, 0);
100 buf_size = msgb_tailroom(srep->buffer);
101
102 nchars = snprintf(buf, buf_size, fmt,
103 prefix, name1, name2,
104 value, unit, index1);
105
106 if (nchars >= buf_size) {
107 /* Truncated */
108 /* Restore original buffer (without trailing LF) */
109 msgb_trim(srep->buffer, old_len);
110 /* Send it */
111 rc = osmo_stats_reporter_send_buffer(srep);
112
113 /* Try again */
114 buf = (char *)msgb_put(srep->buffer, 0);
115 buf_size = msgb_tailroom(srep->buffer);
116
117 nchars = snprintf(buf, buf_size, fmt,
118 prefix, name1, name2,
119 value, unit, index1);
120
121 if (nchars >= buf_size)
122 return -EMSGSIZE;
123 }
124
125 if (nchars > 0)
126 msgb_trim(srep->buffer, msgb_length(srep->buffer) + nchars);
127
128 if (!srep->agg_enabled)
129 rc = osmo_stats_reporter_send_buffer(srep);
130
131 return rc;
132}
133
134static int osmo_stats_reporter_statsd_send_counter(struct osmo_stats_reporter *srep,
135 const struct rate_ctr_group *ctrg,
136 const struct rate_ctr_desc *desc,
137 int64_t value, int64_t delta)
138{
139 if (ctrg)
140 return osmo_stats_reporter_statsd_send(srep,
141 ctrg->desc->group_name_prefix,
142 ctrg->idx,
143 desc->name, delta, "c");
144 else
145 return osmo_stats_reporter_statsd_send(srep,
146 NULL, 0,
147 desc->name, delta, "c");
148}
149
150static int osmo_stats_reporter_statsd_send_item(struct osmo_stats_reporter *srep,
151 const struct osmo_stat_item_group *statg,
152 const struct osmo_stat_item_desc *desc, int value)
153{
Holger Hans Peter Freyther5ab8e2c2015-12-17 14:13:22 +0100154 const char *unit = desc->unit;
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +0100155
156 if (unit == OSMO_STAT_ITEM_NO_UNIT) {
157 unit = "g";
158 if (value < 0)
159 osmo_stats_reporter_statsd_send(srep,
160 statg->desc->group_name_prefix,
161 statg->idx,
162 desc->name, 0, unit);
163 }
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100164 return osmo_stats_reporter_statsd_send(srep,
165 statg->desc->group_name_prefix,
166 statg->idx,
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +0100167 desc->name, value, unit);
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100168}