Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple changes/fixes in preparation for gRPC #198

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions agents/otlp/src/otlp_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ using opentelemetry::trace::SpanKind;
using opentelemetry::trace::TraceFlags;
using opentelemetry::trace::TraceId;
using opentelemetry::trace::propagation::detail::HexToBinary;
using opentelemetry::v1::trace::SemanticConventions::kProcessOwner;
using opentelemetry::v1::trace::SemanticConventions::kThreadId;
using opentelemetry::v1::trace::SemanticConventions::kThreadName;

Expand Down Expand Up @@ -179,7 +180,9 @@ Resource* UpdateResource(ResourceAttributes&& attrs) {

// NOLINTNEXTLINE(runtime/references)
void fill_proc_metrics(std::vector<MetricData>& metrics,
const ProcessMetrics::MetricsStor& stor) {
const ProcessMetrics::MetricsStor& stor,
const ProcessMetrics::MetricsStor& prev_stor,
bool use_snake_case) {
time_point end{
duration_cast<time_point::duration>(
milliseconds(static_cast<uint64_t>(stor.timestamp)))};
Expand Down Expand Up @@ -211,15 +214,21 @@ void fill_proc_metrics(std::vector<MetricData>& metrics,
add_counter(metrics, \
process_start, \
end, \
#CName, \
use_snake_case ? #CName : #JSName, \
Unit, \
type, \
value); \
} \
break; \
case MetricsType::EGauge: \
{ \
add_gauge(metrics, process_start, end, #CName, Unit, type, value); \
add_gauge(metrics, \
process_start, \
end, \
use_snake_case ? #CName : #JSName, \
Unit, \
type, \
value); \
} \
break; \
default: \
Expand All @@ -230,11 +239,23 @@ void fill_proc_metrics(std::vector<MetricData>& metrics,
NSOLID_PROCESS_METRICS_UINT64(V)
NSOLID_PROCESS_METRICS_DOUBLE(V)
#undef V

// Update Resource if needed:
// Check if 'user' or 'title' are different from the previous metrics.
if (prev_stor.user != stor.user || prev_stor.title != stor.title) {
ResourceAttributes attrs = {
{ kProcessOwner, stor.user },
{ "process.title", stor.title },
};

USE(UpdateResource(std::move(attrs)));
}
}

// NOLINTNEXTLINE(runtime/references)
void fill_env_metrics(std::vector<MetricData>& metrics,
const ThreadMetrics::MetricsStor& stor) {
const ThreadMetrics::MetricsStor& stor,
bool use_snake_case) {
time_point end{
duration_cast<time_point::duration>(
milliseconds(static_cast<uint64_t>(stor.timestamp)))};
Expand Down Expand Up @@ -271,7 +292,7 @@ void fill_env_metrics(std::vector<MetricData>& metrics,
add_counter(metrics, \
process_start, \
end, \
#CName, \
use_snake_case ? #CName : #JSName, \
Unit, \
type, \
value, \
Expand All @@ -283,7 +304,7 @@ void fill_env_metrics(std::vector<MetricData>& metrics,
add_gauge(metrics, \
process_start, \
end, \
#CName, \
use_snake_case ? #CName : #JSName, \
Unit, \
type, \
value, \
Expand All @@ -301,7 +322,7 @@ NSOLID_ENV_METRICS_NUMBERS(V)
add_summary(metrics,
process_start,
end,
"gc_dur",
use_snake_case ? "gc_dur_us" : "gcDurUs",
kNSUSecs,
InstrumentValueType::kDouble,
{{ 0.5, stor.gc_dur_us_median },
Expand All @@ -318,7 +339,7 @@ NSOLID_ENV_METRICS_NUMBERS(V)
add_summary(metrics,
process_start,
end,
"http_client",
use_snake_case ? "http_client" : "httpClient",
kNSMSecs,
InstrumentValueType::kDouble,
{{ 0.5, stor.http_client99_ptile },
Expand All @@ -327,7 +348,7 @@ NSOLID_ENV_METRICS_NUMBERS(V)
add_summary(metrics,
process_start,
end,
"http_server",
use_snake_case ? "http_server" : "httpServer",
kNSMSecs,
InstrumentValueType::kDouble,
{{ 0.5, stor.http_server_median },
Expand Down Expand Up @@ -391,6 +412,7 @@ void fill_recordable(Recordable* recordable, const Tracer::SpanStor& s) {
}

recordable->SetAttribute("thread.id", s.thread_id);
recordable->SetAttribute("nsolid.span_type", s.type);

recordable->SetResource(*GetResource());
}
Expand Down
7 changes: 5 additions & 2 deletions agents/otlp/src/otlp_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ OPENTELEMETRY_NAMESPACE::sdk::resource::Resource* UpdateResource(
OPENTELEMETRY_NAMESPACE::sdk::resource::ResourceAttributes&&);

void fill_proc_metrics(std::vector<opentelemetry::sdk::metrics::MetricData>&,
const ProcessMetrics::MetricsStor& stor);
const ProcessMetrics::MetricsStor& stor,
const ProcessMetrics::MetricsStor& prev_stor,
bool use_snake_case = true);

void fill_env_metrics(std::vector<opentelemetry::sdk::metrics::MetricData>&,
const ThreadMetrics::MetricsStor& stor);
const ThreadMetrics::MetricsStor& stor,
bool use_snake_case = true);

void fill_log_recordable(OPENTELEMETRY_NAMESPACE::sdk::logs::Recordable*,
const LogWriteInfo&);
Expand Down
22 changes: 3 additions & 19 deletions agents/otlp/src/otlp_metrics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,11 @@ using opentelemetry::sdk::metrics::ScopeMetrics;
using opentelemetry::sdk::metrics::SumPointData;
using opentelemetry::sdk::metrics::ValueType;
using opentelemetry::sdk::resource::Resource;
using opentelemetry::sdk::resource::ResourceAttributes;
using opentelemetry::v1::exporter::otlp::GetOtlpDefaultHttpMetricsProtocol;
using opentelemetry::v1::exporter::otlp::OtlpGrpcMetricExporter;
using opentelemetry::v1::exporter::otlp::OtlpGrpcMetricExporterOptions;
using opentelemetry::v1::exporter::otlp::OtlpHttpMetricExporter;
using opentelemetry::v1::exporter::otlp::OtlpHttpMetricExporterOptions;
using opentelemetry::v1::trace::SemanticConventions::kProcessOwner;
using opentelemetry::v1::trace::SemanticConventions::kThreadId;

namespace node {
namespace nsolid {
Expand Down Expand Up @@ -104,23 +101,10 @@ OTLPMetrics::~OTLPMetrics() {
/*virtual*/
void OTLPMetrics::got_proc_metrics(const ProcessMetricsStor& stor,
const ProcessMetricsStor& prev_stor) {
ResourceMetrics data;
Resource* resource;
// Check if 'user' or 'title' are different from the previous metrics
if (prev_stor.user != stor.user || prev_stor.title != stor.title) {
ResourceAttributes attrs = {
{ kProcessOwner, stor.user },
{ "process.title", stor.title },
};

resource = UpdateResource(std::move(attrs));
} else {
resource = GetResource();
}

data.resource_ = resource;
std::vector<MetricData> metrics;
fill_proc_metrics(metrics, stor);
fill_proc_metrics(metrics, stor, prev_stor);
ResourceMetrics data;
data.resource_ = GetResource();
data.scope_metric_data_ = std::vector<ScopeMetrics>{{scope_, metrics}};
auto result = otlp_metric_exporter_->Export(data);
Debug("# ProcessMetrics Exported. Result: %d\n", static_cast<int>(result));
Expand Down
1 change: 0 additions & 1 deletion doc/changelogs/NSOLID_CHANGELOG_V5_NODE_V20.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
* \[[`3913f0e27f`](https://github.com/nodesource/nsolid/commit/3913f0e27f)] - **agents:** refactor ZmqAgent to use ProfileCollector (Santiago Gimeno) [nodesource/nsolid#161](https://github.com/nodesource/nsolid/pull/161)
* \[[`183b115e48`](https://github.com/nodesource/nsolid/commit/183b115e48)] - **agents:** implement ProfileCollector class (Santiago Gimeno) [nodesource/nsolid#161](https://github.com/nodesource/nsolid/pull/161)


## 2024-08-23, Version 20.17.0-nsolid-v5.3.3 'Iron'

### Commits
Expand Down
2 changes: 1 addition & 1 deletion test/agents/test-otlp-grpc-metrics.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ if (process.argv[2] === 'child') {
['loop_avg_tasks', undefined, 'asDouble', 'gauge'],
['loop_estimated_lag', 'ms', 'asDouble', 'gauge'],
['loop_idle_percent', undefined, 'asDouble', 'gauge'],
['gc_dur', 'us', 'asDouble', 'summary'],
['gc_dur_us', 'us', 'asDouble', 'summary'],
['dns', 'ms', 'asDouble', 'summary'],
['http_client', 'ms', 'asDouble', 'summary'],
['http_server', 'ms', 'asDouble', 'summary'],
Expand Down
8 changes: 6 additions & 2 deletions test/agents/test-otlp-grpc-traces.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ if (process.argv[2] === 'child') {
const endTimeUnixNano = BigInt(serverSpan.endTimeUnixNano);
assert.ok(endTimeUnixNano);
validateArray(serverSpan.attributes, 'serverSpan.attributes');
assert.strictEqual(serverSpan.attributes.length, 5);
assert.strictEqual(serverSpan.attributes.length, 6);
assert.strictEqual(serverSpan.attributes[0].key, 'http.method');
assert.strictEqual(serverSpan.attributes[0].value.stringValue, 'GET');
assert.strictEqual(serverSpan.attributes[1].key, 'http.status_code');
Expand All @@ -172,6 +172,8 @@ if (process.argv[2] === 'child') {
`http://127.0.0.1:${port}/`);
assert.strictEqual(serverSpan.attributes[4].key, 'thread.id');
assert.strictEqual(serverSpan.attributes[4].value.intValue, `${threadId}`);
assert.strictEqual(serverSpan.attributes[5].key, 'nsolid.span_type');
assert.strictEqual(serverSpan.attributes[5].value.intValue, '8');

const clientSpan = spans[1];
validateId(serverSpan.traceId, 16);
Expand All @@ -183,7 +185,7 @@ if (process.argv[2] === 'child') {
const endTimeUnixNano2 = BigInt(clientSpan.endTimeUnixNano);
assert.ok(endTimeUnixNano2);
validateArray(clientSpan.attributes, 'clientSpan.attributes');
assert.strictEqual(clientSpan.attributes.length, 5);
assert.strictEqual(clientSpan.attributes.length, 6);
assert.strictEqual(clientSpan.attributes[0].key, 'http.method');
assert.strictEqual(clientSpan.attributes[0].value.stringValue, 'GET');
assert.strictEqual(clientSpan.attributes[1].key, 'http.status_code');
Expand All @@ -195,6 +197,8 @@ if (process.argv[2] === 'child') {
`http://127.0.0.1:${port}/`);
assert.strictEqual(clientSpan.attributes[4].key, 'thread.id');
assert.strictEqual(clientSpan.attributes[4].value.intValue, `${threadId}`);
assert.strictEqual(clientSpan.attributes[5].key, 'nsolid.span_type');
assert.strictEqual(clientSpan.attributes[5].value.intValue, '4');
}

function mergeResourceSpans(data, result) {
Expand Down
2 changes: 1 addition & 1 deletion test/agents/test-otlp-metrics.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ if (process.argv[2] === 'child') {
['loop_avg_tasks', undefined, 'asDouble', 'gauge'],
['loop_estimated_lag', 'ms', 'asDouble', 'gauge'],
['loop_idle_percent', undefined, 'asDouble', 'gauge'],
['gc_dur', 'us', 'asDouble', 'summary'],
['gc_dur_us', 'us', 'asDouble', 'summary'],
['dns', 'ms', 'asDouble', 'summary'],
['http_client', 'ms', 'asDouble', 'summary'],
['http_server', 'ms', 'asDouble', 'summary'],
Expand Down
8 changes: 6 additions & 2 deletions test/agents/test-otlp-traces.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ if (process.argv[2] === 'child') {
const endTimeUnixNano = BigInt(serverSpan.endTimeUnixNano);
assert.ok(endTimeUnixNano);
validateArray(serverSpan.attributes, 'serverSpan.attributes');
assert.strictEqual(serverSpan.attributes.length, 5);
assert.strictEqual(serverSpan.attributes.length, 6);
assert.strictEqual(serverSpan.attributes[0].key, 'http.method');
assert.strictEqual(serverSpan.attributes[0].value.stringValue, 'GET');
assert.strictEqual(serverSpan.attributes[1].key, 'http.status_code');
Expand All @@ -157,6 +157,8 @@ if (process.argv[2] === 'child') {
`http://127.0.0.1:${port}/`);
assert.strictEqual(serverSpan.attributes[4].key, 'thread.id');
assert.strictEqual(serverSpan.attributes[4].value.intValue, `${threadId}`);
assert.strictEqual(serverSpan.attributes[5].key, 'nsolid.span_type');
assert.strictEqual(serverSpan.attributes[5].value.intValue, '8');

const clientSpan = spans[1];
validateString(clientSpan.traceId, 'clientSpan.traceId');
Expand All @@ -168,7 +170,7 @@ if (process.argv[2] === 'child') {
const endTimeUnixNano2 = BigInt(clientSpan.endTimeUnixNano);
assert.ok(endTimeUnixNano2);
validateArray(clientSpan.attributes, 'clientSpan.attributes');
assert.strictEqual(clientSpan.attributes.length, 5);
assert.strictEqual(clientSpan.attributes.length, 6);
assert.strictEqual(clientSpan.attributes[0].key, 'http.method');
assert.strictEqual(clientSpan.attributes[0].value.stringValue, 'GET');
assert.strictEqual(clientSpan.attributes[1].key, 'http.status_code');
Expand All @@ -180,6 +182,8 @@ if (process.argv[2] === 'child') {
`http://127.0.0.1:${port}/`);
assert.strictEqual(clientSpan.attributes[4].key, 'thread.id');
assert.strictEqual(clientSpan.attributes[4].value.intValue, `${threadId}`);
assert.strictEqual(clientSpan.attributes[5].key, 'nsolid.span_type');
assert.strictEqual(clientSpan.attributes[5].value.intValue, '4');
}

function mergeResourceSpans(data, result) {
Expand Down
Loading