From 8aebdecab2fd0805d4f3b2a8e844e54223054f2f Mon Sep 17 00:00:00 2001 From: M Kelly Date: Thu, 19 Sep 2024 19:01:31 -0400 Subject: [PATCH] HPCC-32691 Add optional timestamp arg to createClientSpan() 2 Signed-off-by: M Kelly --- system/jlib/jtrace.cpp | 26 +++++++++++++------------- system/jlib/jtrace.hpp | 2 +- testing/unittests/jlibtests.cpp | 31 +++++++++++++++++++++++++++---- 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/system/jlib/jtrace.cpp b/system/jlib/jtrace.cpp index 8ead5b470ca..69c4f2d0baf 100644 --- a/system/jlib/jtrace.cpp +++ b/system/jlib/jtrace.cpp @@ -558,7 +558,7 @@ class CSpan : public CInterfaceOf } ISpan * createClientSpan(const char * name, const SpanTimeStamp * spanStartTimeStamp = nullptr) override; - ISpan * createInternalSpan(const char * name) override; + ISpan * createInternalSpan(const char * name, const SpanTimeStamp * spanStartTimeStamp = nullptr) override; virtual void endSpan() final override { @@ -919,7 +919,7 @@ class CNullSpan final : public CInterfaceOf virtual const char* queryLocalId() const override { return nullptr; } virtual ISpan * createClientSpan(const char * name, const SpanTimeStamp * spanStartTimeStamp = nullptr) override { return getNullSpan(); } - virtual ISpan * createInternalSpan(const char * name) override { return getNullSpan(); } + virtual ISpan * createInternalSpan(const char * name, const SpanTimeStamp * spanStartTimeStamp = nullptr) override { return getNullSpan(); } private: CNullSpan(const CNullSpan&) = delete; @@ -930,9 +930,14 @@ class CNullSpan final : public CInterfaceOf class CChildSpan : public CSpan { protected: - CChildSpan(const char * spanName, CSpan * parent) + CChildSpan(const char * spanName, CSpan * parent, const SpanTimeStamp *spanStartTimeStamp = nullptr) : CSpan(spanName), localParentSpan(parent) { + if (spanStartTimeStamp && spanStartTimeStamp->isInitialized()) + { + opts.start_system_time = opentelemetry::common::SystemTimestamp(spanStartTimeStamp->systemClockTime); + opts.start_steady_time = opentelemetry::common::SteadyTimestamp(spanStartTimeStamp->steadyClockTime); + } injectlocalParentSpan(localParentSpan); } @@ -984,8 +989,8 @@ class CChildSpan : public CSpan class CInternalSpan : public CChildSpan { public: - CInternalSpan(const char * spanName, CSpan * parent) - : CChildSpan(spanName, parent) + CInternalSpan(const char * spanName, CSpan * parent, const SpanTimeStamp * spanStartTimeStamp = nullptr) + : CChildSpan(spanName, parent, spanStartTimeStamp) { opts.kind = opentelemetry::trace::SpanKind::kInternal; init(SpanFlags::None); @@ -1002,13 +1007,8 @@ class CClientSpan : public CChildSpan { public: CClientSpan(const char * spanName, CSpan * parent, const SpanTimeStamp * spanStartTimeStamp = nullptr) - : CChildSpan(spanName, parent) + : CChildSpan(spanName, parent, spanStartTimeStamp) { - if (spanStartTimeStamp && spanStartTimeStamp->isInitialized()) - { - opts.start_system_time = opentelemetry::common::SystemTimestamp(spanStartTimeStamp->systemClockTime); - opts.start_steady_time = opentelemetry::common::SteadyTimestamp(spanStartTimeStamp->steadyClockTime); - } opts.kind = opentelemetry::trace::SpanKind::kClient; init(SpanFlags::None); } @@ -1025,9 +1025,9 @@ ISpan * CSpan::createClientSpan(const char * name, const SpanTimeStamp * spanSta return new CClientSpan(name, this, spanStartTimeStamp); } -ISpan * CSpan::createInternalSpan(const char * name) +ISpan * CSpan::createInternalSpan(const char * name, const SpanTimeStamp * spanStartTimeStamp) { - return new CInternalSpan(name, this); + return new CInternalSpan(name, this, spanStartTimeStamp); } class CServerSpan : public CSpan diff --git a/system/jlib/jtrace.hpp b/system/jlib/jtrace.hpp index f0541534910..7f1e7d9431b 100644 --- a/system/jlib/jtrace.hpp +++ b/system/jlib/jtrace.hpp @@ -151,7 +151,7 @@ interface ISpan : extends IInterface virtual const char * querySpanId() const = 0; virtual ISpan * createClientSpan(const char * name, const SpanTimeStamp * spanStartTimeStamp = nullptr) = 0; - virtual ISpan * createInternalSpan(const char * name) = 0; + virtual ISpan * createInternalSpan(const char * name, const SpanTimeStamp * spanStartTimeStamp = nullptr) = 0; //Old-style global/caller/local id interface functions virtual const char* queryGlobalId() const = 0; diff --git a/testing/unittests/jlibtests.cpp b/testing/unittests/jlibtests.cpp index 2bf6f831ee4..bc80645682d 100644 --- a/testing/unittests/jlibtests.cpp +++ b/testing/unittests/jlibtests.cpp @@ -59,6 +59,7 @@ class JlibTraceTest : public CppUnit::TestFixture CPPUNIT_TEST(testPropegatedServerSpan); CPPUNIT_TEST(testInvalidPropegatedServerSpan); CPPUNIT_TEST(testInternalSpan); + CPPUNIT_TEST(testClientSpan); CPPUNIT_TEST(testMultiNestedSpanTraceOutput); CPPUNIT_TEST(testNullSpan); CPPUNIT_TEST(testClientSpanGlobalID); @@ -482,11 +483,33 @@ class JlibTraceTest : public CppUnit::TestFixture CPPUNIT_ASSERT_MESSAGE("Mismatched localParentSpanID detected", strsame(serverSpanID, retrievedSpanCtxAttributes->queryProp("localParentSpanID"))); - CPPUNIT_ASSERT_EQUAL_MESSAGE("Unexpected missing remoteParentID detected", true, - retrievedSpanCtxAttributes->hasProp("remoteParentID")); + CPPUNIT_ASSERT_EQUAL_MESSAGE("Unexpected GlobalID detected", false, + retrievedSpanCtxAttributes->hasProp(kGlobalIdHttpHeaderName)); + CPPUNIT_ASSERT_EQUAL_MESSAGE("Unexpected CallerID detected", false, + retrievedSpanCtxAttributes->hasProp(kCallerIdHttpHeaderName)); - CPPUNIT_ASSERT_MESSAGE("Unexpected CallerID detected", - strsame(serverTraceID, retrievedSpanCtxAttributes->queryProp("remoteParentID"))); + CPPUNIT_ASSERT_EQUAL_MESSAGE("Unexpected Declared Parent SpanID detected", false, + retrievedSpanCtxAttributes->hasProp("remoteParentSpanID")); + + CPPUNIT_ASSERT_EQUAL_MESSAGE("Unexpected empty TraceID detected", false, isEmptyString(retrievedSpanCtxAttributes->queryProp("traceID"))); + CPPUNIT_ASSERT_EQUAL_MESSAGE("Unexpected empty SpanID detected", false, isEmptyString(retrievedSpanCtxAttributes->queryProp("spanID"))); + } + } + + { + unsigned startMs = msTick() - 1234; + SpanTimeStamp SpanTS; + SpanTS.setMSTickTime(startMs); + OwnedSpanScope clientSpan = serverSpan->createClientSpan("clientSpan", &SpanTS); + //retrieve clientSpan context with the intent to propogate otel and HPCC context + { + Owned retrievedSpanCtxAttributes = createProperties(); + clientSpan->getSpanContext(retrievedSpanCtxAttributes); + CPPUNIT_ASSERT_EQUAL_MESSAGE("Unexpected missing localParentSpanID detected", true, + retrievedSpanCtxAttributes->hasProp("localParentSpanID")); + + CPPUNIT_ASSERT_MESSAGE("Mismatched localParentSpanID detected", + strsame(serverSpanID, retrievedSpanCtxAttributes->queryProp("localParentSpanID"))); CPPUNIT_ASSERT_EQUAL_MESSAGE("Unexpected GlobalID detected", false, retrievedSpanCtxAttributes->hasProp(kGlobalIdHttpHeaderName));