Skip to content

Commit c1c9d2b

Browse files
santigimenotrevnorris
authored andcommitted
agents: use OTLP Summary for percentile metrics
PR-URL: #180 Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-by: Trevor Norris <[email protected]>
1 parent fc930fb commit c1c9d2b

File tree

3 files changed

+102
-8
lines changed

3 files changed

+102
-8
lines changed

agents/otlp/src/otlp_common.cc

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "otlp_common.h"
22
// NOLINTNEXTLINE(build/c++11)
33
#include <chrono>
4+
#include <unordered_map>
45
#include "asserts-cpp/asserts.h"
56
#include "env-inl.h"
67
#include "nlohmann/json.hpp"
@@ -106,6 +107,28 @@ static void add_gauge(std::vector<MetricData>& metrics,
106107
metrics.push_back(metric_data);
107108
}
108109

110+
// NOLINTNEXTLINE(runtime/references)
111+
static void add_summary(std::vector<MetricData>& metrics,
112+
const time_point& start,
113+
const time_point& end,
114+
const char* name,
115+
const char* unit,
116+
InstrumentValueType type,
117+
std::unordered_map<double, ValueType>&& values,
118+
PointAttributes attrs = {}) {
119+
opentelemetry::sdk::metrics::SummaryPointData summary_point_data{};
120+
summary_point_data.quantile_values_ = std::move(values);
121+
MetricData metric_data{
122+
InstrumentDescriptor{
123+
name, "", unit, InstrumentType::kSummary, type },
124+
AggregationTemporality::kUnspecified,
125+
SystemTimestamp{ start },
126+
SystemTimestamp{ end },
127+
std::vector<PointDataAttributes>{{ attrs, summary_point_data }}
128+
};
129+
metrics.push_back(metric_data);
130+
}
131+
109132
InstrumentationScope* GetScope() {
110133
static std::unique_ptr<InstrumentationScope> scope =
111134
InstrumentationScope::Create("nsolid", NODE_VERSION "+ns" NSOLID_VERSION);
@@ -257,6 +280,43 @@ void fill_env_metrics(std::vector<MetricData>& metrics,
257280
}
258281
NSOLID_ENV_METRICS_NUMBERS(V)
259282
#undef V
283+
284+
// Add the summary metrics separately.
285+
add_summary(metrics,
286+
process_start,
287+
end,
288+
"gc_dur",
289+
kNSUSecs,
290+
InstrumentValueType::kDouble,
291+
{{ 0.5, stor.gc_dur_us_median },
292+
{ 0.99, stor.gc_dur_us99_ptile }},
293+
attrs);
294+
add_summary(metrics,
295+
process_start,
296+
end,
297+
"dns",
298+
kNSMSecs,
299+
InstrumentValueType::kDouble,
300+
{{ 0.5, stor.dns_median }, { 0.99, stor.dns99_ptile }},
301+
attrs);
302+
add_summary(metrics,
303+
process_start,
304+
end,
305+
"http_client",
306+
kNSMSecs,
307+
InstrumentValueType::kDouble,
308+
{{ 0.5, stor.http_client99_ptile },
309+
{ 0.99, stor.http_client_median }},
310+
attrs);
311+
add_summary(metrics,
312+
process_start,
313+
end,
314+
"http_server",
315+
kNSMSecs,
316+
InstrumentValueType::kDouble,
317+
{{ 0.5, stor.http_server_median },
318+
{ 0.99, stor.http_server99_ptile }},
319+
attrs);
260320
}
261321

262322
void fill_log_recordable(LogsRecordable* recordable,

test/agents/test-otlp-grpc-metrics.mjs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ if (process.argv[2] === 'child') {
117117
['loop_avg_tasks', undefined, 'asDouble', 'gauge'],
118118
['loop_estimated_lag', 'ms', 'asDouble', 'gauge'],
119119
['loop_idle_percent', undefined, 'asDouble', 'gauge'],
120+
['gc_dur', 'us', 'asDouble', 'summary'],
121+
['dns', 'ms', 'asDouble', 'summary'],
122+
['http_client', 'ms', 'asDouble', 'summary'],
123+
['http_server', 'ms', 'asDouble', 'summary'],
120124
];
121125

122126
// The format of the metrics is as follows:
@@ -490,10 +494,23 @@ if (process.argv[2] === 'child') {
490494
const time = BigInt(dataPoint.timeUnixNano);
491495
assert.ok(time);
492496
assert.ok(time > startTime);
493-
if (type === 'asInt') {
494-
validateNumber(parseInt(dataPoint[type], 10), `${name}.${type}`);
495-
} else { // asDouble
496-
validateNumber(dataPoint[type], `${name}.${type}`);
497+
if (aggregation !== 'summary') {
498+
if (type === 'asInt') {
499+
validateNumber(parseInt(dataPoint[type], 10), `${name}.${type}`);
500+
} else { // asDouble
501+
validateNumber(dataPoint[type], `${name}.${type}`);
502+
}
503+
} else {
504+
validateArray(dataPoint.quantileValues, `${name}.quantileValues`);
505+
assert.strictEqual(dataPoint.quantileValues.length, 2);
506+
assert.strictEqual(dataPoint.quantileValues[0].quantile, 0.99);
507+
assert.strictEqual(dataPoint.quantileValues[1].quantile, 0.5);
508+
if (dataPoint.quantileValues[0].value) {
509+
validateNumber(dataPoint.quantileValues[0].value, `${name}.quantileValues[0].value`);
510+
}
511+
if (dataPoint.quantileValues[1].value) {
512+
validateNumber(dataPoint.quantileValues[1].value, `${name}.quantileValues[1].value`);
513+
}
497514
}
498515

499516
expected.splice(expectedIndex, 1);

test/agents/test-otlp-metrics.mjs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ if (process.argv[2] === 'child') {
117117
['loop_avg_tasks', undefined, 'asDouble', 'gauge'],
118118
['loop_estimated_lag', 'ms', 'asDouble', 'gauge'],
119119
['loop_idle_percent', undefined, 'asDouble', 'gauge'],
120+
['gc_dur', 'us', 'asDouble', 'summary'],
121+
['dns', 'ms', 'asDouble', 'summary'],
122+
['http_client', 'ms', 'asDouble', 'summary'],
123+
['http_server', 'ms', 'asDouble', 'summary'],
120124
];
121125

122126
// The format of the metrics is as follows:
@@ -490,10 +494,23 @@ if (process.argv[2] === 'child') {
490494
const time = BigInt(dataPoint.timeUnixNano);
491495
assert.ok(time);
492496
assert.ok(time > startTime);
493-
if (type === 'asInt') {
494-
validateNumber(parseInt(dataPoint[type], 10), `${name}.${type}`);
495-
} else { // asDouble
496-
validateNumber(dataPoint[type], `${name}.${type}`);
497+
if (aggregation !== 'summary') {
498+
if (type === 'asInt') {
499+
validateNumber(parseInt(dataPoint[type], 10), `${name}.${type}`);
500+
} else { // asDouble
501+
validateNumber(dataPoint[type], `${name}.${type}`);
502+
}
503+
} else {
504+
validateArray(dataPoint.quantileValues, `${name}.quantileValues`);
505+
assert.strictEqual(dataPoint.quantileValues.length, 2);
506+
assert.strictEqual(dataPoint.quantileValues[0].quantile, 0.99);
507+
assert.strictEqual(dataPoint.quantileValues[1].quantile, 0.5);
508+
if (dataPoint.quantileValues[0].value) {
509+
validateNumber(dataPoint.quantileValues[0].value, `${name}.quantileValues[0].value`);
510+
}
511+
if (dataPoint.quantileValues[1].value) {
512+
validateNumber(dataPoint.quantileValues[1].value, `${name}.quantileValues[1].value`);
513+
}
497514
}
498515

499516
expected.splice(expectedIndex, 1);

0 commit comments

Comments
 (0)