From 6932ce578774a9fb2a95bc3adb480f7008688a0d Mon Sep 17 00:00:00 2001 From: Rodrigo Pastrana Date: Thu, 7 Nov 2024 17:11:19 -0500 Subject: [PATCH] HPCC-32967 Avoid Otel GetGlobalProp spinlock - Avoids frequent call to GetGlobalProp - Adds static filescope-wide propegator - Does not set globalprop Signed-off-by: Rodrigo Pastrana --- system/jlib/jtrace.cpp | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/system/jlib/jtrace.cpp b/system/jlib/jtrace.cpp index ce6f7f68fd5..45a5bd91957 100644 --- a/system/jlib/jtrace.cpp +++ b/system/jlib/jtrace.cpp @@ -481,7 +481,7 @@ class CHPCCHttpTextMapCarrier : public opentelemetry::context::propagation::Text }; //--------------------------------------------------------------------------------------------------------------------- - +using namespace opentelemetry::v1::context::propagation; class CTraceManager : implements ITraceManager, public CInterface { private: @@ -530,6 +530,12 @@ class CTraceManager : implements ITraceManager, public CInterface static Singleton theTraceManager; +// thePropagator can be used be regardless of whether tracing is enabled or not. +// Can be used to injects Context into or extract it from carriers that travel in-band +// across process boundaries. Encoding is expected to conform to the HTTP Header Field semantics. +// Reference this propagator rather than fetching globalpropagator +static nostd::shared_ptr thePropagator = nostd::shared_ptr(new opentelemetry::trace::propagation::HttpTraceContext()); + //What is the global trace manager? Only valid to call within this module from spans //Which can only be created if the trace manager has been initialized static inline CTraceManager & queryInternalTraceManager() { return *theTraceManager.query(); } @@ -631,13 +637,11 @@ class CSpan : public CInterfaceOf { assertex(carrier); - auto propagator = opentelemetry::context::propagation::GlobalTextMapPropagator::GetGlobalPropagator(); - opentelemetry::context::Context emptyCtx; auto spanCtx = SetSpan(emptyCtx, span); //and have the propagator inject the ctx into carrier - propagator->Inject(*carrier, spanCtx); + thePropagator->Inject(*carrier, spanCtx); if (!isEmptyString(queryGlobalId())) carrier->Set(kGlobalIdHttpHeaderName, queryGlobalId()); @@ -1037,18 +1041,16 @@ class CServerSpan : public CSpan hpccCallerId.set(httpHeaders->queryProp(kLegacyCallerIdHttpHeaderName)); const CHPCCHttpTextMapCarrier carrier(httpHeaders); - auto globalPropegator = context::propagation::GlobalTextMapPropagator::GetGlobalPropagator(); //avoiding getCurrent: https://github.com/open-telemetry/opentelemetry-cpp/issues/1467 opentelemetry::context::Context empty; auto wrappedSpanCtx = SetSpan(empty, span); - opentelemetry::v1::context::Context runtimeCtx = globalPropegator->Extract(carrier, wrappedSpanCtx); + opentelemetry::v1::context::Context runtimeCtx = thePropagator->Extract(carrier, wrappedSpanCtx); opentelemetry::v1::nostd::shared_ptr remoteParentSpan = opentelemetry::trace::GetSpan(runtimeCtx); if (remoteParentSpan != nullptr && remoteParentSpan->GetContext().IsValid()) { remoteParentSpanCtx = remoteParentSpan->GetContext(); opts.parent = remoteParentSpanCtx; - } } } @@ -1429,15 +1431,6 @@ void CTraceManager::initTracer(const IPropertyTree * traceConfig) optAlwaysCreateTraceIds = traceConfig->getPropBool("@alwaysCreateTraceIds", optAlwaysCreateTraceIds); } - // The global propagator should be set regardless of whether tracing is enabled or not. - // Injects Context into and extracts it from carriers that travel in-band - // across process boundaries. Encoding is expected to conform to the HTTP - // Header Field semantics. - // Values are often encoded as RPC/HTTP request headers. - opentelemetry::context::propagation::GlobalTextMapPropagator::SetGlobalPropagator( - opentelemetry::nostd::shared_ptr( - new opentelemetry::trace::propagation::HttpTraceContext())); - } catch (IException * e) {