Skip to content

Commit

Permalink
agents: move text metrics calculation to common
Browse files Browse the repository at this point in the history
The `Resource` is now updated in `fill_proc_metrics()` so there's no
need for every agent to do it.

PR-URL: #198
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
santigimeno committed Oct 22, 2024
1 parent 1c2215d commit 69b16ce
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
15 changes: 14 additions & 1 deletion 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,8 @@ 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) {
time_point end{
duration_cast<time_point::duration>(
milliseconds(static_cast<uint64_t>(stor.timestamp)))};
Expand Down Expand Up @@ -230,6 +232,17 @@ 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)
Expand Down
3 changes: 2 additions & 1 deletion agents/otlp/src/otlp_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ 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);

void fill_env_metrics(std::vector<opentelemetry::sdk::metrics::MetricData>&,
const ThreadMetrics::MetricsStor& stor);
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

0 comments on commit 69b16ce

Please sign in to comment.