Skip to content

Commit

Permalink
Add Ostream log exporter and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Karen Xu committed Dec 13, 2020
1 parent ec6b6c7 commit f7fb574
Show file tree
Hide file tree
Showing 13 changed files with 441 additions and 67 deletions.
24 changes: 24 additions & 0 deletions exporters/ostream/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
package(default_visibility = ["//visibility:public"])

cc_library(
name = "ostream_log_exporter",
srcs = [
"src/log_exporter.cc",
],
hdrs = [
"include/opentelemetry/exporters/ostream/log_exporter.h",
],
strip_include_prefix = "include",
deps = [
"//sdk/src/logs",
"//sdk/src/trace",
],
)

cc_test(
name = "ostream_log_test",
srcs = ["test/ostream_log_test.cc"],
deps = [
":ostream_log_exporter",
"@com_google_googletest//:gtest_main",
],
)

cc_library(
name = "ostream_metrics_exporter",
srcs = [
Expand Down
20 changes: 12 additions & 8 deletions exporters/ostream/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
include_directories(include)

add_library(opentelemetry_exporter_ostream_logs src/log_exporter.cc)
add_library(opentelemetry_exporter_ostream_metrics src/metrics_exporter.cc)
add_library(opentelemetry_exporter_ostream_span src/span_exporter.cc)

if(BUILD_TESTING)
add_executable(ostream_metrics_test test/ostream_metrics_test.cc)
add_executable(ostream_span_test test/ostream_span_test.cc)
add_executable(ostream_log_test test/ostream_log_test.cc)

target_link_libraries(
ostream_span_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}
Expand All @@ -15,12 +17,14 @@ if(BUILD_TESTING)
ostream_metrics_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}
opentelemetry_exporter_ostream_metrics)

gtest_add_tests(
TARGET ostream_metrics_test
TEST_PREFIX exporter.
TEST_LIST ostream_metrics_test)
gtest_add_tests(
TARGET ostream_span_test
TEST_PREFIX exporter.
TEST_LIST ostream_span_test)
target_link_libraries(
ostream_log_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}
opentelemetry_exporter_ostream_logs opentelemetry_logs)

gtest_add_tests(TARGET ostream_log_test TEST_PREFIX exporter. TEST_LIST
ostream_log_test)
gtest_add_tests(TARGET ostream_metrics_test TEST_PREFIX exporter. TEST_LIST
ostream_metrics_test)
gtest_add_tests(TARGET ostream_span_test TEST_PREFIX exporter. TEST_LIST
ostream_span_test)
endif() # BUILD_TESTING
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

// #include "nlohmann/json.hpp"
#include "opentelemetry/logs/log_record.h"
#include "opentelemetry/nostd/type_traits.h"
#include "opentelemetry/sdk/logs/exporter.h"
#include "opentelemetry/version.h" // needed?

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

namespace nostd = opentelemetry::nostd;
namespace sdklogs = opentelemetry::sdk::logs;

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace logs
{
/**
* The OStreamLogExporter exports logs through an ostream (default set to std::cout)
*/
class OStreamLogExporter final : public sdklogs::LogExporter
{
public:
/**
* Create an OStreamLogExporter. This constructor takes in a reference to an ostream that the
* export() function will send log data into.
* The default ostream is set to stdout
*/
explicit OStreamLogExporter(std::ostream &sout = std::cout) noexcept;
sdklogs::ExportResult Export(const nostd::span<std::shared_ptr<opentelemetry::logs::LogRecord>> &
records) noexcept override;

bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;

private:
std::ostream &sout_;
bool isShutdown_ = false;
bool firstLog = true;
};
} // namespace logs
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
62 changes: 62 additions & 0 deletions exporters/ostream/src/log_exporter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "opentelemetry/exporters/ostream/log_exporter.h"

#include <iostream>

namespace nostd = opentelemetry::nostd;
namespace sdklogs = opentelemetry::sdk::logs;

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace logs
{
OStreamLogExporter::OStreamLogExporter(std::ostream &sout) noexcept : sout_(sout) {}

sdklogs::ExportResult OStreamLogExporter::Export(
const nostd::span<std::shared_ptr<opentelemetry::logs::LogRecord>> & records) noexcept
{
if (isShutdown_)
{
return sdklogs::ExportResult::kFailure;
}

for (auto &record : records)
{
// Convert trace, spanid, traceflags into string convertable representation
char trace_id[32] = {0};
record->trace_id.ToLowerBase16(trace_id);

char span_id[16] = {0};
record->span_id.ToLowerBase16(span_id);

char trace_flags[2] = {0};
record->trace_flags.ToLowerBase16(trace_flags);

// Print out each field of the log record

sout_ << "{\n"
<< " timestamp : " << record->timestamp.time_since_epoch().count() << "\n"
<< " severity : " << static_cast<int>(record->severity) << "\n"
<< " name : " << record->name << "\n"
<< " body : " << record->body
<< "\n"
// << " resource : " << record->resource << "\n"
// << " attributes : " << record->attributes << "\n"
<< " trace_id : " << std::string(trace_id, 32) << "\n"
<< " span_id : " << std::string(span_id, 16) << "\n"
<< " trace_flags : " << std::string(trace_flags, 2) << "\n"
<< "}\n";
}

return sdklogs::ExportResult::kSuccess;
}

bool OStreamLogExporter::Shutdown(std::chrono::microseconds timeout) noexcept
{
isShutdown_ = true;
return true;
}

} // namespace logs
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Loading

0 comments on commit f7fb574

Please sign in to comment.