diff --git a/system/jlib/jtrace.cpp b/system/jlib/jtrace.cpp index c27b33b1b9a..a297fee6a4e 100644 --- a/system/jlib/jtrace.cpp +++ b/system/jlib/jtrace.cpp @@ -47,6 +47,7 @@ #include "jmisc.hpp" #include "jtrace.hpp" #include "lnuid.h" +#include namespace context = opentelemetry::context; namespace nostd = opentelemetry::nostd; @@ -275,27 +276,35 @@ class JLogSpanExporter final : public opentelemetry::sdk::trace::SpanExporter if (map.size() == 0) return; - out.appendf(", \"%s\": {", attsContainerName); - - bool first = true; - for (const auto &kv : map) + try { - if (!first) - out.append(","); - else - first = false; + out.appendf(", \"%s\": {", attsContainerName); - std::ostringstream attsOS; //used to exploit OTel convenience functions for printing attribute values - opentelemetry::exporter::ostream_common::print_value(kv.second, attsOS); - std::string val = attsOS.str(); - if (val.size() > 0) + bool first = true; + for (const auto &kv : map) { - StringBuffer encoded; - encodeJSON(encoded, val.c_str()); - out.appendf("\"%s\": \"%s\"", kv.first.c_str(), encoded.str()); + if (!first) + out.append(","); + else + first = false; + + std::ostringstream attsOS; //used to exploit OTel convenience functions for printing attribute values + opentelemetry::exporter::ostream_common::print_value(kv.second, attsOS); + std::string val = attsOS.str(); + if (val.size() > 0) + { + StringBuffer encoded; + encodeJSON(encoded, val.c_str()); + out.appendf("\"%s\": \"%s\"", kv.first.c_str(), encoded.str()); + } } + out.append(" }"); + + } + catch(const std::bad_variant_access & e) + { + ERRLOG("Could not export span %s: %s", attsContainerName, e.what()); } - out.append(" }"); } static void printEvents(StringBuffer & out, const std::vector &events) @@ -352,18 +361,16 @@ class JLogSpanExporter final : public opentelemetry::sdk::trace::SpanExporter static void printResources(StringBuffer & out, const opentelemetry::sdk::resource::Resource &resources) { - auto attributes = resources.GetAttributes(); - if (attributes.size()) - printAttributes(out, attributes, "resources"); + if (resources.GetAttributes().size()) + printAttributes(out, resources.GetAttributes(), "resources"); } static void printInstrumentationScope(StringBuffer & out, const opentelemetry::sdk::instrumentationscope::InstrumentationScope &instrumentation_scope) { out.appendf(", \"instrumented_library\": \"%s\"", instrumentation_scope.GetName().c_str()); - auto version = instrumentation_scope.GetVersion(); - if (version.size()) - out.appendf("-").append(version.c_str()); + if (instrumentation_scope.GetVersion().size()) + out.appendf("-").append(instrumentation_scope.GetVersion().c_str()); } private: