-
Notifications
You must be signed in to change notification settings - Fork 438
/
span_exporter.cc
183 lines (161 loc) · 5.7 KB
/
span_exporter.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#include "opentelemetry/exporters/ostream/span_exporter.h"
#include "opentelemetry/exporters/ostream/common_utils.h"
#include <iostream>
#include <mutex>
#include "opentelemetry/sdk_config.h"
namespace nostd = opentelemetry::nostd;
namespace trace_sdk = opentelemetry::sdk::trace;
namespace trace_api = opentelemetry::trace;
namespace sdkcommon = opentelemetry::sdk::common;
OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace trace
{
std::ostream &operator<<(std::ostream &os, trace_api::SpanKind span_kind)
{
switch (span_kind)
{
case trace_api::SpanKind::kClient:
return os << "Client";
case trace_api::SpanKind::kInternal:
return os << "Internal";
case trace_api::SpanKind::kServer:
return os << "Server";
case trace_api::SpanKind::kProducer:
return os << "Producer";
case trace_api::SpanKind::kConsumer:
return os << "Consumer";
};
return os << "";
}
OStreamSpanExporter::OStreamSpanExporter(std::ostream &sout) noexcept : sout_(sout) {}
std::unique_ptr<trace_sdk::Recordable> OStreamSpanExporter::MakeRecordable() noexcept
{
return std::unique_ptr<trace_sdk::Recordable>(new trace_sdk::SpanData);
}
sdk::common::ExportResult OStreamSpanExporter::Export(
const nostd::span<std::unique_ptr<trace_sdk::Recordable>> &spans) noexcept
{
if (isShutdown())
{
OTEL_INTERNAL_LOG_ERROR("[Ostream Trace Exporter] Exporting "
<< spans.size() << " span(s) failed, exporter is shutdown");
return sdk::common::ExportResult::kFailure;
}
for (auto &recordable : spans)
{
auto span = std::unique_ptr<trace_sdk::SpanData>(
static_cast<trace_sdk::SpanData *>(recordable.release()));
if (span != nullptr)
{
char trace_id[32] = {0};
char span_id[16] = {0};
char parent_span_id[16] = {0};
span->GetTraceId().ToLowerBase16(trace_id);
span->GetSpanId().ToLowerBase16(span_id);
span->GetParentSpanId().ToLowerBase16(parent_span_id);
sout_ << "{"
<< "\n name : " << span->GetName()
<< "\n trace_id : " << std::string(trace_id, 32)
<< "\n span_id : " << std::string(span_id, 16)
<< "\n tracestate : " << span->GetSpanContext().trace_state()->ToHeader()
<< "\n parent_span_id: " << std::string(parent_span_id, 16)
<< "\n start : " << span->GetStartTime().time_since_epoch().count()
<< "\n duration : " << span->GetDuration().count()
<< "\n description : " << span->GetDescription()
<< "\n span kind : " << span->GetSpanKind()
<< "\n status : " << statusMap[int(span->GetStatus())]
<< "\n attributes : ";
printAttributes(span->GetAttributes());
sout_ << "\n events : ";
printEvents(span->GetEvents());
sout_ << "\n links : ";
printLinks(span->GetLinks());
sout_ << "\n resources : ";
printResources(span->GetResource());
sout_ << "\n instr-lib : ";
printInstrumentationScope(span->GetInstrumentationScope());
sout_ << "\n}\n";
}
}
return sdk::common::ExportResult::kSuccess;
}
bool OStreamSpanExporter::ForceFlush(std::chrono::microseconds /* timeout */) noexcept
{
sout_.flush();
return true;
}
bool OStreamSpanExporter::Shutdown(std::chrono::microseconds /* timeout */) noexcept
{
const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_);
is_shutdown_ = true;
return true;
}
bool OStreamSpanExporter::isShutdown() const noexcept
{
const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_);
return is_shutdown_;
}
void OStreamSpanExporter::printAttributes(
const std::unordered_map<std::string, sdkcommon::OwnedAttributeValue> &map,
const std::string prefix)
{
for (const auto &kv : map)
{
sout_ << prefix << kv.first << ": ";
opentelemetry::exporter::ostream_common::print_value(kv.second, sout_);
}
}
void OStreamSpanExporter::printEvents(const std::vector<trace_sdk::SpanDataEvent> &events)
{
for (const auto &event : events)
{
sout_ << "\n\t{"
<< "\n\t name : " << event.GetName()
<< "\n\t timestamp : " << event.GetTimestamp().time_since_epoch().count()
<< "\n\t attributes : ";
printAttributes(event.GetAttributes(), "\n\t\t");
sout_ << "\n\t}";
}
}
void OStreamSpanExporter::printLinks(const std::vector<trace_sdk::SpanDataLink> &links)
{
for (const auto &link : links)
{
char trace_id[32] = {0};
char span_id[16] = {0};
link.GetSpanContext().trace_id().ToLowerBase16(trace_id);
link.GetSpanContext().span_id().ToLowerBase16(span_id);
sout_ << "\n\t{"
<< "\n\t trace_id : " << std::string(trace_id, 32)
<< "\n\t span_id : " << std::string(span_id, 16)
<< "\n\t tracestate : " << link.GetSpanContext().trace_state()->ToHeader()
<< "\n\t attributes : ";
printAttributes(link.GetAttributes(), "\n\t\t");
sout_ << "\n\t}";
}
}
void OStreamSpanExporter::printResources(const opentelemetry::sdk::resource::Resource &resources)
{
auto attributes = resources.GetAttributes();
if (attributes.size())
{
printAttributes(attributes, "\n\t");
}
}
void OStreamSpanExporter::printInstrumentationScope(
const opentelemetry::sdk::instrumentationscope::InstrumentationScope &instrumentation_scope)
{
sout_ << instrumentation_scope.GetName();
auto version = instrumentation_scope.GetVersion();
if (version.size())
{
sout_ << "-" << version;
}
}
} // namespace trace
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE