Skip to content

Commit

Permalink
agents: fix process_start calculation
Browse files Browse the repository at this point in the history
Apparently, in v18.x `performance::performance_process_start_timestamp`
is set later than in v20.x. Create a run-once function which calculcates
it the first time is needed.

PR-URL: #169
Reviewed-by: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
santigimeno authored and trevnorris committed Aug 23, 2024
1 parent aea98a5 commit 919491b
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions agents/otlp/src/otlp_common.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "otlp_common.h"
// NOLINTNEXTLINE(build/c++11)
#include <chrono>
// NOLINTNEXTLINE(build/c++11)
#include <mutex>
#include "asserts-cpp/asserts.h"
#include "env-inl.h"
#include "nlohmann/json.hpp"
Expand Down Expand Up @@ -55,9 +57,17 @@ namespace otlp {
static const size_t kTraceIdSize = 32;
static const size_t kSpanIdSize = 16;

static time_point process_start(duration_cast<time_point::duration>(
microseconds(static_cast<uint64_t>(
performance::performance_process_start_timestamp))));
static std::once_flag process_start_flag;

static time_point process_start() {
static time_point start;
std::call_once(process_start_flag, []() {
start = time_point(duration_cast<time_point::duration>(
microseconds(static_cast<uint64_t>(
performance::performance_process_start_timestamp))));
});
return start;
}

static std::vector<std::string> discarded_metrics = {
"thread_id", "timestamp"
Expand Down Expand Up @@ -169,7 +179,7 @@ void fill_proc_metrics(std::vector<MetricData>& metrics,
case MetricsType::ECounter: \
{ \
add_counter(metrics, \
process_start, \
process_start(), \
end, \
#CName, \
Unit, \
Expand All @@ -179,7 +189,7 @@ void fill_proc_metrics(std::vector<MetricData>& metrics,
break; \
case MetricsType::EGauge: \
{ \
add_gauge(metrics, process_start, end, #CName, Unit, type, value); \
add_gauge(metrics, process_start(), end, #CName, Unit, type, value); \
} \
break; \
default: \
Expand Down Expand Up @@ -228,7 +238,7 @@ void fill_env_metrics(std::vector<MetricData>& metrics,
case MetricsType::ECounter: \
{ \
add_counter(metrics, \
process_start, \
process_start(), \
end, \
#CName, \
Unit, \
Expand All @@ -240,7 +250,7 @@ void fill_env_metrics(std::vector<MetricData>& metrics,
case MetricsType::EGauge: \
{ \
add_gauge(metrics, \
process_start, \
process_start(), \
end, \
#CName, \
Unit, \
Expand Down

0 comments on commit 919491b

Please sign in to comment.