forked from netdata/netdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_write.cc
117 lines (87 loc) · 3.02 KB
/
remote_write.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// SPDX-License-Identifier: GPL-3.0-or-later
#include <snappy.h>
#include "remote_write.pb.h"
#include "remote_write.h"
using namespace prometheus;
google::protobuf::Arena arena;
WriteRequest *write_request;
void init_write_request() {
GOOGLE_PROTOBUF_VERIFY_VERSION;
write_request = google::protobuf::Arena::CreateMessage<WriteRequest>(&arena);
}
void clear_write_request() {
write_request->clear_timeseries();
}
void add_host_info(const char *name, const char *instance, const char *application, const char *version, const int64_t timestamp) {
TimeSeries *timeseries;
Sample *sample;
Label *label;
timeseries = write_request->add_timeseries();
label = timeseries->add_labels();
label->set_name("__name__");
label->set_value(name);
label = timeseries->add_labels();
label->set_name("instance");
label->set_value(instance);
if(application) {
label = timeseries->add_labels();
label->set_name("application");
label->set_value(application);
}
if(version) {
label = timeseries->add_labels();
label->set_name("version");
label->set_value(version);
}
sample = timeseries->add_samples();
sample->set_value(1);
sample->set_timestamp(timestamp);
}
// adds tag to the last created timeseries
void add_tag(char *tag, char *value) {
TimeSeries *timeseries;
Label *label;
timeseries = write_request->mutable_timeseries(write_request->timeseries_size() - 1);
label = timeseries->add_labels();
label->set_name(tag);
label->set_value(value);
}
void add_metric(const char *name, const char *chart, const char *family, const char *dimension, const char *instance, const double value, const int64_t timestamp) {
TimeSeries *timeseries;
Sample *sample;
Label *label;
timeseries = write_request->add_timeseries();
label = timeseries->add_labels();
label->set_name("__name__");
label->set_value(name);
label = timeseries->add_labels();
label->set_name("chart");
label->set_value(chart);
label = timeseries->add_labels();
label->set_name("family");
label->set_value(family);
if(dimension) {
label = timeseries->add_labels();
label->set_name("dimension");
label->set_value(dimension);
}
label = timeseries->add_labels();
label->set_name("instance");
label->set_value(instance);
sample = timeseries->add_samples();
sample->set_value(value);
sample->set_timestamp(timestamp);
}
size_t get_write_request_size(){
size_t size = (size_t)snappy::MaxCompressedLength(write_request->ByteSize());
return (size < INT_MAX)?size:0;
}
int pack_write_request(char *buffer, size_t *size) {
std::string uncompressed_write_request;
if(write_request->SerializeToString(&uncompressed_write_request) == false) return 1;
snappy::RawCompress(uncompressed_write_request.data(), uncompressed_write_request.size(), buffer, size);
return 0;
}
void protocol_buffers_shutdown() {
google::protobuf::ShutdownProtobufLibrary();
}