-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathrecordable.cc
261 lines (237 loc) · 8.45 KB
/
recordable.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#include "opentelemetry/exporters/zipkin/recordable.h"
#include "opentelemetry/sdk/resource/resource.h"
#include "opentelemetry/sdk/resource/semantic_conventions.h"
#include <map>
#include <string>
OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace zipkin
{
using namespace opentelemetry::sdk::resource;
namespace trace_api = opentelemetry::trace;
namespace common = opentelemetry::common;
namespace sdk = opentelemetry::sdk;
// constexpr needs keys to be constexpr, const is next best to use.
static const std::map<trace_api::SpanKind, std::string> kSpanKindMap = {
{trace_api::SpanKind::kClient, "CLIENT"},
{trace_api::SpanKind::kServer, "SERVER"},
{trace_api::SpanKind::kConsumer, "CONSUMER"},
{trace_api::SpanKind::kProducer, "PRODUCER"},
};
//
// See `attribute_value.h` for details.
//
const int kAttributeValueSize = 16;
void Recordable::SetIdentity(const trace_api::SpanContext &span_context,
trace_api::SpanId parent_span_id) noexcept
{
char trace_id_lower_base16[trace::TraceId::kSize * 2] = {0};
span_context.trace_id().ToLowerBase16(trace_id_lower_base16);
char span_id_lower_base16[trace::SpanId::kSize * 2] = {0};
span_context.span_id().ToLowerBase16(span_id_lower_base16);
if (parent_span_id.IsValid())
{
char parent_span_id_lower_base16[trace::SpanId::kSize * 2] = {0};
parent_span_id.ToLowerBase16(parent_span_id_lower_base16);
span_["parentId"] = std::string(parent_span_id_lower_base16, 16);
}
span_["id"] = std::string(span_id_lower_base16, 16);
span_["traceId"] = std::string(trace_id_lower_base16, 32);
}
void PopulateAttribute(nlohmann::json &attribute,
nostd::string_view key,
const common::AttributeValue &value)
{
// Assert size of variant to ensure that this method gets updated if the variant
// definition changes
static_assert(nostd::variant_size<common::AttributeValue>::value == kAttributeValueSize,
"AttributeValue contains unknown type");
if (nostd::holds_alternative<bool>(value))
{
attribute[key.data()] = nostd::get<bool>(value);
}
else if (nostd::holds_alternative<int>(value))
{
attribute[key.data()] = nostd::get<int>(value);
}
else if (nostd::holds_alternative<int64_t>(value))
{
attribute[key.data()] = nostd::get<int64_t>(value);
}
else if (nostd::holds_alternative<unsigned int>(value))
{
attribute[key.data()] = nostd::get<unsigned int>(value);
}
else if (nostd::holds_alternative<uint64_t>(value))
{
attribute[key.data()] = nostd::get<uint64_t>(value);
}
else if (nostd::holds_alternative<double>(value))
{
attribute[key.data()] = nostd::get<double>(value);
}
else if (nostd::holds_alternative<const char *>(value))
{
attribute[key.data()] = nostd::get<const char *>(value);
}
else if (nostd::holds_alternative<nostd::string_view>(value))
{
attribute[key.data()] = nostd::string_view(nostd::get<nostd::string_view>(value).data(),
nostd::get<nostd::string_view>(value).size());
}
else if (nostd::holds_alternative<nostd::span<const uint8_t>>(value))
{
attribute[key.data()] = {};
for (const auto &val : nostd::get<nostd::span<const uint8_t>>(value))
{
attribute[key.data()].push_back(val);
}
}
else if (nostd::holds_alternative<nostd::span<const bool>>(value))
{
attribute[key.data()] = {};
for (const auto &val : nostd::get<nostd::span<const bool>>(value))
{
attribute[key.data()].push_back(val);
}
}
else if (nostd::holds_alternative<nostd::span<const int>>(value))
{
attribute[key.data()] = {};
for (const auto &val : nostd::get<nostd::span<const int>>(value))
{
attribute[key.data()].push_back(val);
}
}
else if (nostd::holds_alternative<nostd::span<const int64_t>>(value))
{
attribute[key.data()] = {};
for (const auto &val : nostd::get<nostd::span<const int64_t>>(value))
{
attribute[key.data()].push_back(val);
}
}
else if (nostd::holds_alternative<nostd::span<const unsigned int>>(value))
{
attribute[key.data()] = {};
for (const auto &val : nostd::get<nostd::span<const unsigned int>>(value))
{
attribute[key.data()].push_back(val);
}
}
else if (nostd::holds_alternative<nostd::span<const uint64_t>>(value))
{
attribute[key.data()] = {};
for (const auto &val : nostd::get<nostd::span<const uint64_t>>(value))
{
attribute[key.data()].push_back(val);
}
}
else if (nostd::holds_alternative<nostd::span<const double>>(value))
{
attribute[key.data()] = {};
for (const auto &val : nostd::get<nostd::span<const double>>(value))
{
attribute[key.data()].push_back(val);
}
}
else if (nostd::holds_alternative<nostd::span<const nostd::string_view>>(value))
{
attribute[key.data()] = {};
for (const auto &val : nostd::get<nostd::span<const nostd::string_view>>(value))
{
attribute[key.data()].push_back(std::string(val.data(), val.size()));
}
}
}
void Recordable::SetAttribute(nostd::string_view key, const common::AttributeValue &value) noexcept
{
if (!span_.contains("tags"))
{
span_["tags"] = nlohmann::json::object();
}
PopulateAttribute(span_["tags"], key, value);
}
void Recordable::AddEvent(nostd::string_view name,
common::SystemTimestamp timestamp,
const common::KeyValueIterable &attributes) noexcept
{
nlohmann::json attrs = nlohmann::json::object(); // empty object
attributes.ForEachKeyValue([&](nostd::string_view key, common::AttributeValue value) noexcept {
PopulateAttribute(attrs, key, value);
return true;
});
nlohmann::json annotation = {{"value", nlohmann::json::object({{name.data(), attrs}}).dump()},
{"timestamp", std::chrono::duration_cast<std::chrono::microseconds>(
timestamp.time_since_epoch())
.count()}};
if (!span_.contains("annotations"))
{
span_["annotations"] = nlohmann::json::array();
}
span_["annotations"].push_back(annotation);
}
void Recordable::AddLink(const trace_api::SpanContext & /* span_context */,
const common::KeyValueIterable & /* attributes */) noexcept
{
// TODO: Currently not supported by specs:
// https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/sdk_exporters/zipkin.md
}
void Recordable::SetStatus(trace::StatusCode code, nostd::string_view description) noexcept
{
if (code != trace::StatusCode::kUnset)
{
span_["tags"]["otel.status_code"] = code;
if (code == trace::StatusCode::kError)
{
span_["tags"]["error"] = description;
}
}
}
void Recordable::SetName(nostd::string_view name) noexcept
{
span_["name"] = name.data();
}
void Recordable::SetTraceFlags(opentelemetry::trace::TraceFlags /* flags */) noexcept
{
// TODO: Currently not supported by specs:
// https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/sdk_exporters/zipkin.md
}
void Recordable::SetResource(const sdk::resource::Resource &resource) noexcept
{
// only service.name attribute is supported by specs as of now.
auto attributes = resource.GetAttributes();
if (attributes.find(SemanticConventions::kServiceName) != attributes.end())
{
service_name_ = nostd::get<std::string>(attributes[SemanticConventions::kServiceName]);
}
}
void Recordable::SetStartTime(common::SystemTimestamp start_time) noexcept
{
span_["timestamp"] =
std::chrono::duration_cast<std::chrono::microseconds>(start_time.time_since_epoch()).count();
}
void Recordable::SetDuration(std::chrono::nanoseconds duration) noexcept
{
span_["duration"] = std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
}
void Recordable::SetSpanKind(trace_api::SpanKind span_kind) noexcept
{
auto span_iter = kSpanKindMap.find(span_kind);
if (span_iter != kSpanKindMap.end())
{
span_["kind"] = span_iter->second;
}
}
void Recordable::SetInstrumentationScope(
const sdk::instrumentationscope::InstrumentationScope &instrumentation_scope) noexcept
{
span_["tags"]["otel.library.name"] = instrumentation_scope.GetName();
span_["tags"]["otel.library.version"] = instrumentation_scope.GetVersion();
}
} // namespace zipkin
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE