-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOtelMetrics.cpp
More file actions
116 lines (91 loc) · 3.7 KB
/
OtelMetrics.cpp
File metadata and controls
116 lines (91 loc) · 3.7 KB
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
#include "OtelMetrics.h"
namespace OTel {
// Helper: merge default + per-call labels into a datapoint attributes array
static void addPointAttributes(JsonArray& attrArray,
const std::map<String, String>& callLabels) {
// Defaults first
for (const auto& kv : defaultMetricLabels()) {
JsonObject a = attrArray.add<JsonObject>();
a["key"] = kv.first;
a["value"].to<JsonObject>()["stringValue"] = kv.second;
}
// Then per-call (override by reusing key downstream in the stack)
for (const auto& kv : callLabels) {
JsonObject a = attrArray.add<JsonObject>();
a["key"] = kv.first;
a["value"].to<JsonObject>()["stringValue"] = kv.second;
}
}
static void addCommonResource(JsonObject& resource) {
JsonArray rattrs = resource["attributes"].to<JsonArray>();
buildResourceAttributes(rattrs, defaultServiceName(), defaultServiceInstanceId(), defaultHostName());
}
static void addCommonScope(JsonObject& scope) {
scope["name"] = metricsScopeConfig().scopeName;
scope["version"] = metricsScopeConfig().scopeVersion;
}
// ----------------- GAUGE -----------------
void Metrics::buildAndSendGauge(const String& name, double value,
const String& unit,
const std::map<String,String>& labels)
{
JsonDocument doc;
JsonArray resourceMetrics = doc["resourceMetrics"].to<JsonArray>();
JsonObject rm = resourceMetrics.add<JsonObject>();
// resource with attributes (service.name, etc.)
JsonObject resource = rm["resource"].to<JsonObject>();
addCommonResource(resource);
// scope
JsonObject sm = rm["scopeMetrics"].to<JsonArray>().add<JsonObject>();
JsonObject scope = sm["scope"].to<JsonObject>();
addCommonScope(scope);
// metric
JsonArray metrics = sm["metrics"].to<JsonArray>();
JsonObject metric = metrics.add<JsonObject>();
metric["name"] = name;
metric["unit"] = unit;
metric["type"] = "gauge";
JsonObject gauge = metric["gauge"].to<JsonObject>();
JsonArray dps = gauge["dataPoints"].to<JsonArray>();
JsonObject dp = dps.add<JsonObject>();
dp["timeUnixNano"] = u64ToStr(nowUnixNano());
dp["asDouble"] = value;
JsonArray attrs = dp["attributes"].to<JsonArray>();
addPointAttributes(attrs, labels);
OTelSender::sendJson("/v1/metrics", doc);
}
// ----------------- SUM -------------------
void Metrics::buildAndSendSum(const String& name, double value,
bool isMonotonic,
const String& temporality,
const String& unit,
const std::map<String,String>& labels)
{
JsonDocument doc;
JsonArray resourceMetrics = doc["resourceMetrics"].to<JsonArray>();
JsonObject rm = resourceMetrics.add<JsonObject>();
// resource with attributes
JsonObject resource = rm["resource"].to<JsonObject>();
addCommonResource(resource);
// scope
JsonObject sm = rm["scopeMetrics"].to<JsonArray>().add<JsonObject>();
JsonObject scope = sm["scope"].to<JsonObject>();
addCommonScope(scope);
// metric
JsonArray metrics = sm["metrics"].to<JsonArray>();
JsonObject metric = metrics.add<JsonObject>();
metric["name"] = name;
metric["unit"] = unit;
metric["type"] = "sum";
JsonObject sum = metric["sum"].to<JsonObject>();
sum["isMonotonic"] = isMonotonic;
sum["aggregationTemporality"] = temporality; // "DELTA" or "CUMULATIVE"
JsonArray dps = sum["dataPoints"].to<JsonArray>();
JsonObject dp = dps.add<JsonObject>();
dp["timeUnixNano"] = u64ToStr(nowUnixNano());
dp["asDouble"] = value;
JsonArray attrs = dp["attributes"].to<JsonArray>();
addPointAttributes(attrs, labels);
OTelSender::sendJson("/v1/metrics", doc);
}
} // namespace OTel