Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion exporters/ostream/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ target_include_directories(
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>")

target_link_libraries(opentelemetry_exporter_ostream_span
PUBLIC opentelemetry_trace)
PUBLIC opentelemetry_trace
nlohmann_json::nlohmann_json)

if(nlohmann_json_clone)
add_dependencies(opentelemetry_exporter_otlp_http_client
nlohmann_json::nlohmann_json)
endif()

install(
TARGETS opentelemetry_exporter_ostream_span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
#include "opentelemetry/version.h"

#include <iostream>
#include <list>
#include <map>
#include <sstream>

#include "nlohmann/json.hpp"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
Expand All @@ -30,7 +33,7 @@ class OStreamSpanExporter final : public opentelemetry::sdk::trace::SpanExporter
* export() function will send span data into.
* The default ostream is set to stdout
*/
explicit OStreamSpanExporter(std::ostream &sout = std::cout) noexcept;
explicit OStreamSpanExporter(std::ostream &sout = std::cout, bool isJson = false) noexcept;

std::unique_ptr<opentelemetry::sdk::trace::Recordable> MakeRecordable() noexcept override;

Expand All @@ -43,6 +46,7 @@ class OStreamSpanExporter final : public opentelemetry::sdk::trace::SpanExporter

private:
std::ostream &sout_;
bool isJson_;
bool is_shutdown_ = false;
mutable opentelemetry::common::SpinLockMutex lock_;
bool isShutdown() const noexcept;
Expand Down Expand Up @@ -112,7 +116,30 @@ class OStreamSpanExporter final : public opentelemetry::sdk::trace::SpanExporter
#endif
}

// various format helpers
std::string formatTraceId(opentelemetry::trace::TraceId trace_id);

std::string formatSpanId(opentelemetry::trace::SpanId span_id);

// various json helpers
nlohmann::basic_json<nlohmann::ordered_map> formatContext(const opentelemetry::trace::SpanContext &context);

nlohmann::basic_json<nlohmann::ordered_map> formatAttributes(const std::unordered_map<std::string,
opentelemetry::sdk::common::OwnedAttributeValue> &attributes);

std::list<nlohmann::basic_json<nlohmann::ordered_map>> formatEvents(const std::vector<opentelemetry::sdk::trace::SpanDataEvent> &events);

std::list<nlohmann::basic_json<nlohmann::ordered_map>> formatLinks(const std::vector<opentelemetry::sdk::trace::SpanDataLink> &links);

void PopulateAttribute(nostd::string_view key,
const opentelemetry::sdk::common::OwnedAttributeValue &value,
nlohmann::basic_json<nlohmann::ordered_map> &attributes);

// various print helpers
void printSpanJson(const std::unique_ptr<opentelemetry::sdk::trace::SpanData> &span) noexcept;

void printSpanText(const std::unique_ptr<opentelemetry::sdk::trace::SpanData> &span) noexcept;

void printAttributes(
const std::unordered_map<std::string, opentelemetry::sdk::common::OwnedAttributeValue> &map,
const std::string prefix = "\n\t");
Expand Down
Loading