Skip to content

Commit

Permalink
HPCC-31347 Jtrace JLog::export should honor noexcept
Browse files Browse the repository at this point in the history
- Catches all exceptions thrown in noexcept fn
- Reports export failure if exception caught

Signed-off-by: Rodrigo Pastrana <[email protected]>
  • Loading branch information
rpastrana committed Feb 27, 2024
1 parent 68e0dbd commit 2b8925b
Showing 1 changed file with 59 additions and 52 deletions.
111 changes: 59 additions & 52 deletions system/jlib/jtrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,76 +180,83 @@ class JLogSpanExporter final : public opentelemetry::sdk::trace::SpanExporter
opentelemetry::sdk::common::ExportResult Export(
const nostd::span<std::unique_ptr<opentelemetry::sdk::trace::Recordable>> &recordables) noexcept override
{
if (isShutDown())
return opentelemetry::sdk::common::ExportResult::kFailure;

for (auto &recordable : recordables)
try
{
//Casting the recordable object to the type of the object that was previously created by
//JLogSpanExporter::MakeRecordable() -
auto span = std::unique_ptr<opentelemetry::sdk::trace::SpanData>(
static_cast<opentelemetry::sdk::trace::SpanData *>(recordable.release()));
if (isShutDown())
return opentelemetry::sdk::common::ExportResult::kFailure;

if (span != nullptr)
for (auto &recordable : recordables)
{
char traceID[32] = {0};
char spanID[16] = {0};
//Casting the recordable object to the type of the object that was previously created by
//JLogSpanExporter::MakeRecordable() -
auto span = std::unique_ptr<opentelemetry::sdk::trace::SpanData>(
static_cast<opentelemetry::sdk::trace::SpanData *>(recordable.release()));

span->GetTraceId().ToLowerBase16(traceID);
span->GetSpanId().ToLowerBase16(spanID);
if (span != nullptr)
{
char traceID[32] = {0};
char spanID[16] = {0};

StringBuffer out("{ \"type\": \"span\""); //for simple identification in log scraping
out.appendf(", \"name\": \"%s\"", span->GetName().data());
out.append(", \"trace_id\": \"").append(32, traceID).append("\"");
out.append(", \"span_id\": \"").append(16, spanID).append("\"");
out.appendf(", \"start\": %lld", (long long)(span->GetStartTime().time_since_epoch()).count());
out.appendf(", \"duration\": %lld", (long long)span->GetDuration().count());
span->GetTraceId().ToLowerBase16(traceID);
span->GetSpanId().ToLowerBase16(spanID);

if (hasMask(logFlags, SpanLogFlags::LogParentInfo))
{
if (span->GetParentSpanId().IsValid())
StringBuffer out("{ \"type\": \"span\""); //for simple identification in log scraping
out.appendf(", \"name\": \"%s\"", span->GetName().data());
out.append(", \"trace_id\": \"").append(32, traceID).append("\"");
out.append(", \"span_id\": \"").append(16, spanID).append("\"");
out.appendf(", \"start\": %lld", (long long)(span->GetStartTime().time_since_epoch()).count());
out.appendf(", \"duration\": %lld", (long long)span->GetDuration().count());

if (hasMask(logFlags, SpanLogFlags::LogParentInfo))
{
char parentSpanID[16] = {0};
span->GetParentSpanId().ToLowerBase16(parentSpanID);
out.append(", \"parent_span_id\": \"").append(16, parentSpanID).append("\"");
if (span->GetParentSpanId().IsValid())
{
char parentSpanID[16] = {0};
span->GetParentSpanId().ToLowerBase16(parentSpanID);
out.append(", \"parent_span_id\": \"").append(16, parentSpanID).append("\"");
}

std::string traceStatestr = span->GetSpanContext().trace_state()->ToHeader();
if (!traceStatestr.empty())
out.appendf(", \"trace_state\": \"%s\"", traceStatestr.c_str());
}

std::string traceStatestr = span->GetSpanContext().trace_state()->ToHeader();
if (!traceStatestr.empty())
out.appendf(", \"trace_state\": \"%s\"", traceStatestr.c_str());
}

if (hasMask(logFlags, SpanLogFlags::LogSpanDetails))
{
out.appendf(", \"status\": \"%s\"", spanStatusToString(span->GetStatus()));
out.appendf(", \"kind\": \"%s\"", spanKindToString(span->GetSpanKind()));
const char * description = span->GetDescription().data();
if (!isEmptyString(description))
if (hasMask(logFlags, SpanLogFlags::LogSpanDetails))
{
StringBuffer encoded;
encodeJSON(encoded, description);
out.appendf(", \"description\": \"%s\"", encoded.str());
out.appendf(", \"status\": \"%s\"", spanStatusToString(span->GetStatus()));
out.appendf(", \"kind\": \"%s\"", spanKindToString(span->GetSpanKind()));
const char * description = span->GetDescription().data();
if (!isEmptyString(description))
{
StringBuffer encoded;
encodeJSON(encoded, description);
out.appendf(", \"description\": \"%s\"", encoded.str());
}
printInstrumentationScope(out, span->GetInstrumentationScope());
}
printInstrumentationScope(out, span->GetInstrumentationScope());
}

if (hasMask(logFlags, SpanLogFlags::LogAttributes))
printAttributes(out, span->GetAttributes());
if (hasMask(logFlags, SpanLogFlags::LogAttributes))
printAttributes(out, span->GetAttributes());

if (hasMask(logFlags, SpanLogFlags::LogEvents))
printEvents(out, span->GetEvents());
if (hasMask(logFlags, SpanLogFlags::LogEvents))
printEvents(out, span->GetEvents());

if (hasMask(logFlags, SpanLogFlags::LogLinks))
printLinks(out, span->GetLinks());
if (hasMask(logFlags, SpanLogFlags::LogLinks))
printLinks(out, span->GetLinks());

if (hasMask(logFlags, SpanLogFlags::LogResources))
printResources(out, span->GetResource());
if (hasMask(logFlags, SpanLogFlags::LogResources))
printResources(out, span->GetResource());

out.append(" }");
LOG(MCmonitorEvent, "%s",out.str());
out.append(" }");
LOG(MCmonitorEvent, "%s",out.str());
}
}
return opentelemetry::sdk::common::ExportResult::kSuccess;
}
catch(...) //Interface does not allow throwing exceptions
{
return opentelemetry::sdk::common::ExportResult::kFailure;
}
return opentelemetry::sdk::common::ExportResult::kSuccess;
}

/**
Expand Down

0 comments on commit 2b8925b

Please sign in to comment.