Skip to content

Commit

Permalink
HPCC-30403 Provid ECL based API for Tracing
Browse files Browse the repository at this point in the history
- Declares several ecl calls to logging library
- Defines getTraceSpanHeader, getTraceStateHeader
- Defines getTraceID, getSpanID, setSpanAttribute

Signed-off-by: Rodrigo Pastrana <[email protected]>
  • Loading branch information
rpastrana committed Nov 21, 2023
1 parent cef0bf9 commit e706bab
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 1 deletion.
8 changes: 8 additions & 0 deletions common/thorhelper/thorcommon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,14 @@ class CStatsContextLogger : public CSimpleInterfaceOf<IContextLogger>
{
return ::getClientHeaders(activeSpan);
}
virtual IProperties * getSpanContext() const override
{
return ::getSpanContext(activeSpan);
}
virtual void setSpanAttribute(const char *name, const char *value) const override
{
return ::setSpanAttribute(activeSpan, name, value);
}
virtual const char *queryGlobalId() const override
{
return activeSpan->queryGlobalId();
Expand Down
31 changes: 31 additions & 0 deletions ecllibrary/std/system/Log.ecl
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,35 @@ EXPORT generateGloballyUniqueId() := lib_logging.Logging.generateGloballyUniqueI

EXPORT getElapsedMs() := lib_logging.Logging.getElapsedMs();

/*
* Gets the active span's traceID.
*
* Returns the the unique Id.
*/
EXPORT getTraceID() := lib_logging.Logging.getTraceID();
/*
* Gets the active spanID.
*
* Returns the the unique Id.
*/
EXPORT getSpanID() := lib_logging.Logging.getSpanID();
/*
* Gets OpenTelemetry formatted trace span header.
* Should be provided to remote services for trace propagation as 'traceparent'.
*
* Returns the the unique Id.
*/
EXPORT getTraceSpanHeader() := lib_logging.Logging.getTraceSpanHeader();
/*
* Gets OpenTelemetry formatted trace state header.
* Should be provided to remote services for trace propagation as 'tracestate'.
*
* Returns the the unique Id.
*/
EXPORT getTraceStateHeader() := lib_logging.Logging.getTraceStateHeader();
/*
* Adds the name/value pair to the active span as an attribute.
*/
EXPORT setSpanAttribute(string name, string value) := lib_logging.Logging.setSpanAttribute(name, value);

END;
56 changes: 55 additions & 1 deletion plugins/logging/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "logging.hpp"
#include "jlog.hpp"

#define LOGGING_VERSION "LOGGING 1.0.1"
#define LOGGING_VERSION "LOGGING 1.0.2"
static const char * compatibleVersions[] = {
"LOGGING 1.0.0 [66aec3fb4911ceda247c99d6a2a5944c]", // linux version
LOGGING_VERSION,
Expand All @@ -42,6 +42,11 @@ static const char * EclDefinition =
" varstring getCallerId() : c,context,entrypoint='logGetCallerId'; \n"
" varstring generateGloballyUniqueId() : c,entrypoint='logGenerateGloballyUniqueId'; \n"
" unsigned4 getElapsedMs() : c,context,entrypoint='logGetElapsedMs'; \n"
" varstring getTraceSpanHeader() : c,context,entrypoint='getTraceSpanHeader'; \n"
" varstring getTraceStateHeader() : c,context,entrypoint='getTraceStateHeader'; \n"
" varstring getTraceID() : c,context,entrypoint='getTraceID'; \n"
" varstring getSpanID() : c,context,entrypoint='getSpanID'; \n"
" setSpanAttribute(const string name, const string value) : c,context,action,entrypoint='setSpanAttribute'; \n"
"END;";

LOGGING_API bool getECLPluginDefinition(ECLPluginDefinitionBlock *pb)
Expand Down Expand Up @@ -117,3 +122,52 @@ LOGGING_API unsigned int LOGGING_CALL logGetElapsedMs(ICodeContext *ctx)
{
return ctx->getElapsedMs();
}

LOGGING_API char * LOGGING_CALL getTraceID(ICodeContext *ctx)
{
StringBuffer ret;
Owned<IProperties> httpHeaders = ctx->queryContextLogger().getSpanContext();
if (httpHeaders)
ret.set(httpHeaders->queryProp("traceID"));

return ret.detach();
}

LOGGING_API char * LOGGING_CALL getSpanID(ICodeContext *ctx)
{
StringBuffer ret;
Owned<IProperties> httpHeaders = ctx->queryContextLogger().getSpanContext();
if (httpHeaders)
ret.set(httpHeaders->queryProp("spanID"));

return ret.detach();
}

LOGGING_API char * LOGGING_CALL getTraceSpanHeader(ICodeContext *ctx)
{
StringBuffer ret;
Owned<IProperties> httpHeaders = ctx->queryContextLogger().getClientHeaders();
if (httpHeaders)
ret.set(httpHeaders->queryProp("traceparent"));

return ret.detach();
}

LOGGING_API char * LOGGING_CALL getTraceStateHeader(ICodeContext *ctx)
{
StringBuffer ret;
Owned<IProperties> httpHeaders = ctx->queryContextLogger().getClientHeaders();
if (httpHeaders)
ret.set(httpHeaders->queryProp("tracestate"));

return ret.detach();
}

LOGGING_API void LOGGING_CALL setSpanAttribute(ICodeContext *ctx,
unsigned nameLen, const char * name, unsigned valueLen, const char * value)
{
StringBuffer attName(nameLen, name);
StringBuffer attVal(valueLen, value);

ctx->queryContextLogger().setSpanAttribute(attName.str(), attVal.str());
}
5 changes: 5 additions & 0 deletions plugins/logging/logging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ LOGGING_API char * LOGGING_CALL logGetCallerId(ICodeContext *ctx);
LOGGING_API char * LOGGING_CALL logGetLocalId(ICodeContext *ctx);
LOGGING_API char * LOGGING_CALL logGenerateGloballyUniqueId();
LOGGING_API unsigned int LOGGING_CALL logGetElapsedMs(ICodeContext *ctx);
LOGGING_API char * LOGGING_CALL getTraceSpanHeader(ICodeContext *ctx);
LOGGING_API char * LOGGING_CALL getTraceStateHeader(ICodeContext *ctx);
LOGGING_API char * LOGGING_CALL getTraceID(ICodeContext *ctx);
LOGGING_API char * LOGGING_CALL getSpanID(ICodeContext *ctx);
LOGGING_API void LOGGING_CALL setSpanAttribute(ICodeContext *ctx, unsigned nameLen, const char * name, unsigned valueLen, const char * value);

}

Expand Down
8 changes: 8 additions & 0 deletions roxie/ccd/ccd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,14 @@ class ContextLogger : implements IRoxieContextLogger, public CInterface
{
return ::getClientHeaders(activeSpan);
}
virtual IProperties * getSpanContext() const override
{
return ::getSpanContext(activeSpan);
}
virtual void setSpanAttribute(const char *name, const char *value) const override
{
return ::setSpanAttribute(activeSpan, name, value);
}
virtual const char *queryGlobalId() const override
{
return activeSpan->queryGlobalId();
Expand Down
8 changes: 8 additions & 0 deletions roxie/ccd/ccdcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,14 @@ class CRoxieContextBase : implements IRoxieAgentContext, implements ICodeContext
{
return logctx.getClientHeaders();
}
virtual IProperties * getSpanContext() const override
{
return logctx.getSpanContext();
}
virtual void setSpanAttribute(const char *name, const char *value) const override
{
return logctx.setSpanAttribute(name, value);
}
virtual const char *queryGlobalId() const override
{
return logctx.queryGlobalId();
Expand Down
16 changes: 16 additions & 0 deletions roxie/ccd/ccdserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@ class IndirectAgentContext : implements IRoxieAgentContext, public CInterface
{
return ctx->getClientHeaders();
}
virtual IProperties * getSpanContext() const override
{
return ctx->getSpanContext();
}
virtual void setSpanAttribute(const char *name, const char *value) const override
{
return ctx->setSpanAttribute(name, value);
}
virtual const char *queryGlobalId() const
{
return ctx->queryGlobalId();
Expand Down Expand Up @@ -1362,6 +1370,14 @@ class CRoxieServerActivity : implements CInterfaceOf<IRoxieServerActivity>, impl
return ctx->getClientHeaders();
return nullptr;
}
virtual IProperties * getSpanContext() const override
{
return ctx->getSpanContext();
}
virtual void setSpanAttribute(const char *name, const char *value) const override
{
return ctx->setSpanAttribute(name, value);
}
virtual const char *queryGlobalId() const override
{
return ctx ? ctx->queryGlobalId() : nullptr;
Expand Down
8 changes: 8 additions & 0 deletions system/jlib/jlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2876,6 +2876,14 @@ class DummyLogCtx : implements IContextLogger
{
return ::getClientHeaders(activeSpan);
}
virtual IProperties * getSpanContext() const override
{
return ::getSpanContext(activeSpan);
}
virtual void setSpanAttribute(const char *name, const char *value) const override
{
return ::setSpanAttribute(activeSpan, name, value);
}
virtual const char *queryGlobalId() const override
{
return activeSpan->queryGlobalId();
Expand Down
2 changes: 2 additions & 0 deletions system/jlib/jlog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,9 @@ interface jlib_decl IContextLogger : extends IInterface
virtual const char *queryCallerId() const = 0;
virtual const CRuntimeStatisticCollection & queryStats() const = 0;
virtual void setActiveSpan(ISpan * span) = 0;
virtual IProperties * getSpanContext() const = 0;
virtual IProperties * getClientHeaders() const = 0;
virtual void setSpanAttribute(const char *name, const char *value) const = 0;
virtual void recordStatistics(IStatisticGatherer &progress) const = 0;
virtual const LogMsgJobInfo & queryJob() const { return unknownJob; }
};
Expand Down
12 changes: 12 additions & 0 deletions system/jlib/jtrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,18 @@ IProperties * getClientHeaders(const ISpan * span)
return headers.getClear();
}

void setSpanAttribute(ISpan * span, const char * name, const char * val)
{
span->setSpanAttribute(name, val);
}

IProperties * getSpanContext(const ISpan * span)
{
Owned<IProperties> headers = createProperties(true);
span->getSpanContext(headers, false);
return headers.getClear();
}

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

class CTraceManager : implements ITraceManager, public CInterface
Expand Down
2 changes: 2 additions & 0 deletions system/jlib/jtrace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ interface ISpan : extends IInterface
};

extern jlib_decl IProperties * getClientHeaders(const ISpan * span);
extern jlib_decl IProperties * getSpanContext(const ISpan * span);
extern jlib_decl void setSpanAttribute(ISpan * span, const char * name, const char * val);

interface ITraceManager : extends IInterface
{
Expand Down

0 comments on commit e706bab

Please sign in to comment.