From a359ecbf88962591aef81aa9cf2ea656689ebd8d Mon Sep 17 00:00:00 2001 From: Santiago Gimeno Date: Wed, 21 Aug 2024 23:44:33 +0200 Subject: [PATCH] agents: fix process_start calculation 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: https://github.com/nodesource/nsolid/pull/169 Reviewed-by: Trevor Norris --- agents/otlp/src/otlp_common.cc | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/agents/otlp/src/otlp_common.cc b/agents/otlp/src/otlp_common.cc index 0ae3ef3cb4..9b3deed1f7 100644 --- a/agents/otlp/src/otlp_common.cc +++ b/agents/otlp/src/otlp_common.cc @@ -1,6 +1,8 @@ #include "otlp_common.h" // NOLINTNEXTLINE(build/c++11) #include +// NOLINTNEXTLINE(build/c++11) +#include #include "asserts-cpp/asserts.h" #include "env-inl.h" #include "nlohmann/json.hpp" @@ -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( - microseconds(static_cast( - 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( + microseconds(static_cast( + performance::performance_process_start_timestamp)))); + }); + return start; +} static std::vector discarded_metrics = { "thread_id", "timestamp" @@ -169,7 +179,7 @@ void fill_proc_metrics(std::vector& metrics, case MetricsType::ECounter: \ { \ add_counter(metrics, \ - process_start, \ + process_start(), \ end, \ #CName, \ Unit, \ @@ -179,7 +189,7 @@ void fill_proc_metrics(std::vector& 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: \ @@ -228,7 +238,7 @@ void fill_env_metrics(std::vector& metrics, case MetricsType::ECounter: \ { \ add_counter(metrics, \ - process_start, \ + process_start(), \ end, \ #CName, \ Unit, \ @@ -240,7 +250,7 @@ void fill_env_metrics(std::vector& metrics, case MetricsType::EGauge: \ { \ add_gauge(metrics, \ - process_start, \ + process_start(), \ end, \ #CName, \ Unit, \