forked from open-telemetry/opentelemetry-cpp
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Karen Xu
committed
Dec 13, 2020
1 parent
ec6b6c7
commit f7fb574
Showing
13 changed files
with
441 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
exporters/ostream/include/opentelemetry/exporters/ostream/log_exporter.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.