Skip to content
This repository was archived by the owner on Nov 14, 2019. It is now read-only.

Commit 15e00f1

Browse files
committed
Merge pull request #9 from github/cmn/gauges-relative
Adds support for incrementing and decrementing gauges
2 parents 6318124 + 5ea9f40 commit 15e00f1

5 files changed

Lines changed: 50 additions & 31 deletions

File tree

src/metric.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ new_metric(struct brubeck_server *server, const char *key, size_t key_len, uint8
3030
return metric;
3131
}
3232

33-
typedef void (*mt_prototype_record)(struct brubeck_metric *, value_t, value_t);
33+
typedef void (*mt_prototype_record)(struct brubeck_metric *, value_t, value_t, uint8_t);
3434
typedef void (*mt_prototype_sample)(struct brubeck_metric *, brubeck_sample_cb, void *);
3535

3636

@@ -40,11 +40,15 @@ typedef void (*mt_prototype_sample)(struct brubeck_metric *, brubeck_sample_cb,
4040
* ALLOC: mt + 4 bytes
4141
*********************************************/
4242
static void
43-
gauge__record(struct brubeck_metric *metric, value_t value, value_t sample_freq)
43+
gauge__record(struct brubeck_metric *metric, value_t value, value_t sample_freq, uint8_t modifiers)
4444
{
4545
pthread_spin_lock(&metric->lock);
4646
{
47-
metric->as.gauge.value = value;
47+
if (modifiers & BRUBECK_MOD_RELATIVE_VALUE) {
48+
metric->as.gauge.value += value;
49+
} else {
50+
metric->as.gauge.value = value;
51+
}
4852
}
4953
pthread_spin_unlock(&metric->lock);
5054
}
@@ -70,7 +74,7 @@ gauge__sample(struct brubeck_metric *metric, brubeck_sample_cb sample, void *opa
7074
* ALLOC: mt + 4
7175
*********************************************/
7276
static void
73-
meter__record(struct brubeck_metric *metric, value_t value, value_t sample_freq)
77+
meter__record(struct brubeck_metric *metric, value_t value, value_t sample_freq, uint8_t modifiers)
7478
{
7579
/* upsample */
7680
value *= sample_freq;
@@ -104,7 +108,7 @@ meter__sample(struct brubeck_metric *metric, brubeck_sample_cb sample, void *opa
104108
* ALLOC: mt + 4 + 4 + 4
105109
*********************************************/
106110
static void
107-
counter__record(struct brubeck_metric *metric, value_t value, value_t sample_freq)
111+
counter__record(struct brubeck_metric *metric, value_t value, value_t sample_freq, uint8_t modifiers)
108112
{
109113
/* upsample */
110114
value *= sample_freq;
@@ -146,7 +150,7 @@ counter__sample(struct brubeck_metric *metric, brubeck_sample_cb sample, void *o
146150
* ALLOC: mt + 16 + 4
147151
*********************************************/
148152
static void
149-
histogram__record(struct brubeck_metric *metric, value_t value, value_t sample_freq)
153+
histogram__record(struct brubeck_metric *metric, value_t value, value_t sample_freq, uint8_t modifiers)
150154
{
151155
pthread_spin_lock(&metric->lock);
152156
{
@@ -266,9 +270,9 @@ void brubeck_metric_sample(struct brubeck_metric *metric, brubeck_sample_cb cb,
266270
_prototypes[metric->type].sample(metric, cb, backend);
267271
}
268272

269-
void brubeck_metric_record(struct brubeck_metric *metric, value_t value, value_t sample_freq)
273+
void brubeck_metric_record(struct brubeck_metric *metric, value_t value, value_t sample_freq, uint8_t modifiers)
270274
{
271-
_prototypes[metric->type].record(metric, value, sample_freq);
275+
_prototypes[metric->type].record(metric, value, sample_freq, modifiers);
272276
}
273277

274278
struct brubeck_backend *

src/metric.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ enum brubeck_metric_t {
1010
BRUBECK_MT_INTERNAL_STATS
1111
};
1212

13+
enum brubeck_metric_mod_t {
14+
BRUBECK_MOD_RELATIVE_VALUE = 1
15+
};
16+
1317
enum brubeck_aggregate_t {
1418
BRUBECK_AG_LAST,
1519
BRUBECK_AG_SUM,
@@ -56,7 +60,7 @@ typedef void (*brubeck_sample_cb)(
5660
void *backend);
5761

5862
void brubeck_metric_sample(struct brubeck_metric *metric, brubeck_sample_cb cb, void *backend);
59-
void brubeck_metric_record(struct brubeck_metric *metric, value_t value, value_t sample_rate);
63+
void brubeck_metric_record(struct brubeck_metric *metric, value_t value, value_t sample_rate, uint8_t modifiers);
6064

6165
struct brubeck_metric *brubeck_metric_new(struct brubeck_server *server, const char *, size_t, uint8_t);
6266
struct brubeck_metric *brubeck_metric_find(struct brubeck_server *server, const char *, size_t, uint8_t);

src/samplers/statsd.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static void statsd_run_recvmsg(struct brubeck_statsd *statsd, int sock)
8888
}
8989

9090
static inline char *
91-
parse_float(char *buffer, value_t *result)
91+
parse_float(char *buffer, value_t *result, uint8_t *mods)
9292
{
9393
int negative = 0;
9494
char *start = buffer;
@@ -97,6 +97,10 @@ parse_float(char *buffer, value_t *result)
9797
if (*buffer == '-') {
9898
++buffer;
9999
negative = 1;
100+
*mods |= BRUBECK_MOD_RELATIVE_VALUE;
101+
} else if (*buffer == '+') {
102+
++buffer;
103+
*mods |= BRUBECK_MOD_RELATIVE_VALUE;
100104
}
101105

102106
while (*buffer >= '0' && *buffer <= '9') {
@@ -166,7 +170,8 @@ int brubeck_statsd_msg_parse(struct brubeck_statsd_msg *msg, char *buffer, char
166170
* ^^^
167171
*/
168172
{
169-
buffer = parse_float(buffer, &msg->value);
173+
msg->modifiers = 0;
174+
buffer = parse_float(buffer, &msg->value, &msg->modifiers);
170175

171176
if (*buffer != '|')
172177
return -1;
@@ -211,8 +216,9 @@ int brubeck_statsd_msg_parse(struct brubeck_statsd_msg *msg, char *buffer, char
211216
{
212217
if (buffer[0] == '|' && buffer[1] == '@') {
213218
double sample_rate;
219+
uint8_t dummy;
214220

215-
buffer = parse_float(buffer + 2, &sample_rate);
221+
buffer = parse_float(buffer + 2, &sample_rate, &dummy);
216222
if (sample_rate <= 0.0 || sample_rate > 1.0)
217223
return -1;
218224

@@ -246,7 +252,7 @@ void brubeck_statsd_packet_parse(struct brubeck_server *server, char *buffer, ch
246252
brubeck_stats_inc(server, metrics);
247253
metric = brubeck_metric_find(server, msg.key, msg.key_len, msg.type);
248254
if (metric != NULL)
249-
brubeck_metric_record(metric, msg.value, msg.sample_freq);
255+
brubeck_metric_record(metric, msg.value, msg.sample_freq, msg.modifiers);
250256
}
251257

252258
/* move buf past this stat */

src/samplers/statsd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ struct brubeck_statsd_msg {
99
uint16_t type; /* type of the messaged, as a brubeck_mt_t */
1010
value_t value; /* floating point value of the message */
1111
value_t sample_freq; /* floating poit sample freq (1.0 / sample_rate) */
12+
uint8_t modifiers; /* modifiers, as a brubeck_metric_mod_t */
1213
};
1314

1415
struct brubeck_statsd {

tests/statsd_msg.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "sput.h"
44
#include "brubeck.h"
55

6-
static void must_parse(const char *msg_text, double value, double sample)
6+
static void must_parse(const char *msg_text, double value, double sample, uint8_t modifiers)
77
{
88
struct brubeck_statsd_msg msg;
99
char buffer[128];
@@ -13,6 +13,7 @@ static void must_parse(const char *msg_text, double value, double sample)
1313
sput_fail_unless(brubeck_statsd_msg_parse(&msg, buffer, buffer + len) == 0, msg_text);
1414
sput_fail_unless(value == msg.value, "msg.value == expected");
1515
sput_fail_unless(sample == msg.sample_freq, "msg.sample_rate == expected");
16+
sput_fail_unless(modifiers == msg.modifiers, "msg.modifiers == expected");
1617
}
1718

1819
static void must_not_parse(const char *msg_text)
@@ -27,23 +28,26 @@ static void must_not_parse(const char *msg_text)
2728

2829
void test_statsd_msg__parse_strings(void)
2930
{
30-
must_parse("github.auth.fingerprint.sha1:1|c", 1, 1.0);
31-
must_parse("github.auth.fingerprint.sha1:1|c|@0.1", 1, 10.0);
32-
must_parse("github.auth.fingerprint.sha1:1|g", 1, 1.0);
33-
must_parse("lol:1|ms", 1, 1.0);
34-
must_parse("this.is.sparta:199812|C", 199812, 1.0);
35-
must_parse("this.is.sparta:0012|h", 12, 1.0);
36-
must_parse("this.is.sparta:23.23|g", 23.23, 1.0);
37-
must_parse("this.is.sparta:0.232030|g", 0.23203, 1.0);
38-
must_parse("this.are.some.floats:1234567.89|g", 1234567.89, 1.0);
39-
must_parse("this.are.some.floats:1234567.89|g|@0.025", 1234567.89, 40.0);
40-
must_parse("this.are.some.floats:1234567.89|g|@0.25", 1234567.89, 4.0);
41-
must_parse("this.are.some.floats:1234567.89|g|@0.01", 1234567.89, 100.0);
42-
must_parse("this.are.some.floats:1234567.89|g|@000.0100", 1234567.89, 100.0);
43-
must_parse("this.are.some.floats:1234567.89|g|@1.0", 1234567.89, 1.0);
44-
must_parse("this.are.some.floats:1234567.89|g|@1", 1234567.89, 1.0);
45-
must_parse("this.are.some.floats:1234567.89|g|@1.", 1234567.89, 1.0);
46-
must_parse("this.are.some.floats:|g", 0.0, 1.0);
31+
must_parse("github.auth.fingerprint.sha1:1|c", 1, 1.0, 0);
32+
must_parse("github.auth.fingerprint.sha1:1|c|@0.1", 1, 10.0, 0);
33+
must_parse("github.auth.fingerprint.sha1:1|g", 1, 1.0, 0);
34+
must_parse("lol:1|ms", 1, 1.0, 0);
35+
must_parse("this.is.sparta:199812|C", 199812, 1.0, 0);
36+
must_parse("this.is.sparta:0012|h", 12, 1.0, 0);
37+
must_parse("this.is.sparta:23.23|g", 23.23, 1.0, 0);
38+
must_parse("this.is.sparta:0.232030|g", 0.23203, 1.0, 0);
39+
must_parse("this.are.some.floats:1234567.89|g", 1234567.89, 1.0, 0);
40+
must_parse("this.are.some.floats:1234567.89|g|@0.025", 1234567.89, 40.0, 0);
41+
must_parse("this.are.some.floats:1234567.89|g|@0.25", 1234567.89, 4.0, 0);
42+
must_parse("this.are.some.floats:1234567.89|g|@0.01", 1234567.89, 100.0, 0);
43+
must_parse("this.are.some.floats:1234567.89|g|@000.0100", 1234567.89, 100.0, 0);
44+
must_parse("this.are.some.floats:1234567.89|g|@1.0", 1234567.89, 1.0, 0);
45+
must_parse("this.are.some.floats:1234567.89|g|@1", 1234567.89, 1.0, 0);
46+
must_parse("this.are.some.floats:1234567.89|g|@1.", 1234567.89, 1.0, 0);
47+
must_parse("this.are.some.floats:|g", 0.0, 1.0, 0);
48+
must_parse("this.are.some.floats:1234567.89|g", 1234567.89, 1.0, 0);
49+
must_parse("gauge.increment:+1|g", 1, 1.0, BRUBECK_MOD_RELATIVE_VALUE);
50+
must_parse("gauge.decrement:-1|g", -1, 1.0, BRUBECK_MOD_RELATIVE_VALUE);
4751

4852
must_not_parse("this.are.some.floats:12.89.23|g");
4953
must_not_parse("this.are.some.floats:12.89|a");

0 commit comments

Comments
 (0)