Skip to content

Commit

Permalink
HPCC-32967 Avoid Otel GetGlobalProp spinlock
Browse files Browse the repository at this point in the history
- Avoids frequent call to GetGlobalProp
- Adds filescope-wide propegator
- Does not set globalprop

Signed-off-by: Rodrigo Pastrana <[email protected]>
  • Loading branch information
rpastrana committed Nov 7, 2024
1 parent 77ce2e1 commit 17aa0c2
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions system/jlib/jtrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ class CHPCCHttpTextMapCarrier : public opentelemetry::context::propagation::Text
};

//---------------------------------------------------------------------------------------------------------------------

using namespace opentelemetry::v1::context::propagation;
class CTraceManager : implements ITraceManager, public CInterface
{
private:
Expand Down Expand Up @@ -535,6 +535,12 @@ class CTraceManager : implements ITraceManager, public CInterface

static Singleton<CTraceManager> 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
nostd::shared_ptr<TextMapPropagator> thePropagator = nostd::shared_ptr<TextMapPropagator>(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(); }
Expand Down Expand Up @@ -636,13 +642,11 @@ class CSpan : public CInterfaceOf<ISpan>
{
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());
Expand Down Expand Up @@ -1055,18 +1059,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<opentelemetry::v1::trace::Span> remoteParentSpan = opentelemetry::trace::GetSpan(runtimeCtx);

if (remoteParentSpan != nullptr && remoteParentSpan->GetContext().IsValid())
{
remoteParentSpanCtx = remoteParentSpan->GetContext();
opts.parent = remoteParentSpanCtx;

}
}
}
Expand Down Expand Up @@ -1447,15 +1449,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<opentelemetry::context::propagation::TextMapPropagator>(
new opentelemetry::trace::propagation::HttpTraceContext()));

}
catch (IException * e)
{
Expand Down

0 comments on commit 17aa0c2

Please sign in to comment.