Skip to content

Commit

Permalink
HPCC-32691 Add optional timestamp arg to createClientSpan()
Browse files Browse the repository at this point in the history
Signed-off-by: M Kelly <[email protected]>
  • Loading branch information
mckellyln committed Sep 19, 2024
1 parent 63c81b6 commit 8afcaa6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
15 changes: 10 additions & 5 deletions system/jlib/jtrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ class CSpan : public CInterfaceOf<ISpan>
return spanID.get();
}

ISpan * createClientSpan(const char * name) override;
ISpan * createClientSpan(const char * name, const SpanTimeStamp * spanStartTimeStamp = nullptr) override;
ISpan * createInternalSpan(const char * name) override;

virtual void endSpan() final override
Expand Down Expand Up @@ -918,7 +918,7 @@ class CNullSpan final : public CInterfaceOf<ISpan>
virtual const char* queryCallerId() const override { return nullptr; }
virtual const char* queryLocalId() const override { return nullptr; }

virtual ISpan * createClientSpan(const char * name) override { return getNullSpan(); }
virtual ISpan * createClientSpan(const char * name, const SpanTimeStamp * spanStartTimeStamp = nullptr) override { return getNullSpan(); }
virtual ISpan * createInternalSpan(const char * name) override { return getNullSpan(); }

private:
Expand Down Expand Up @@ -1001,9 +1001,14 @@ class CInternalSpan : public CChildSpan
class CClientSpan : public CChildSpan
{
public:
CClientSpan(const char * spanName, CSpan * parent)
CClientSpan(const char * spanName, CSpan * parent, const SpanTimeStamp * spanStartTimeStamp = nullptr)
: CChildSpan(spanName, parent)
{
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);
}
Expand All @@ -1015,9 +1020,9 @@ class CClientSpan : public CChildSpan
}
};

ISpan * CSpan::createClientSpan(const char * name)
ISpan * CSpan::createClientSpan(const char * name, const SpanTimeStamp * spanStartTimeStamp)
{
return new CClientSpan(name, this);
return new CClientSpan(name, this, spanStartTimeStamp);
}

ISpan * CSpan::createInternalSpan(const char * name)
Expand Down
48 changes: 24 additions & 24 deletions system/jlib/jtrace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,29 @@ struct SpanError
void setError(const char * _errorMessage, int _errorCode) { errorMessage = _errorMessage; errorCode = _errorCode; }
};

struct SpanTimeStamp
{
std::chrono::nanoseconds steadyClockTime = std::chrono::nanoseconds::zero();
std::chrono::nanoseconds systemClockTime = std::chrono::nanoseconds::zero();

void now()
{
systemClockTime = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch());
steadyClockTime = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch());
}

void setMSTickTime(const unsigned int msTickTime)
{
systemClockTime = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch() - std::chrono::milliseconds(msTick() - msTickTime));
steadyClockTime = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch() - std::chrono::milliseconds(msTick() - msTickTime));
}

bool isInitialized() const
{
return systemClockTime != std::chrono::nanoseconds::zero();
}
};

interface ISpan : extends IInterface
{
virtual void setSpanAttribute(const char * key, const char * val) = 0;
Expand All @@ -127,7 +150,7 @@ interface ISpan : extends IInterface
virtual const char * queryTraceId() const = 0;
virtual const char * querySpanId() const = 0;

virtual ISpan * createClientSpan(const char * name) = 0;
virtual ISpan * createClientSpan(const char * name, const SpanTimeStamp * spanStartTimeStamp = nullptr) = 0;
virtual ISpan * createInternalSpan(const char * name) = 0;

//Old-style global/caller/local id interface functions
Expand Down Expand Up @@ -159,29 +182,6 @@ class jlib_decl OwnedSpanScope
extern jlib_decl IProperties * getClientHeaders(const ISpan * span);
extern jlib_decl IProperties * getSpanContext(const ISpan * span);

struct SpanTimeStamp
{
std::chrono::nanoseconds steadyClockTime = std::chrono::nanoseconds::zero();
std::chrono::nanoseconds systemClockTime = std::chrono::nanoseconds::zero();

void now()
{
systemClockTime = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch());
steadyClockTime = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch());
}

void setMSTickTime(const unsigned int msTickTime)
{
systemClockTime = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch() - std::chrono::milliseconds(msTick() - msTickTime));
steadyClockTime = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch() - std::chrono::milliseconds(msTick() - msTickTime));
}

bool isInitialized() const
{
return systemClockTime != std::chrono::nanoseconds::zero();
}
};

interface ITraceManager : extends IInterface
{
virtual ISpan * createServerSpan(const char * name, const IProperties * httpHeaders, const SpanTimeStamp * spanStartTimeStamp, SpanFlags flags = SpanFlags::None) const = 0;
Expand Down

0 comments on commit 8afcaa6

Please sign in to comment.