]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/exporters/otlp/src/otlp_recordable_utils.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / exporters / otlp / src / otlp_recordable_utils.cc
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include "opentelemetry/exporters/otlp/otlp_recordable_utils.h"
5
6 #include "opentelemetry/exporters/otlp/protobuf_include_prefix.h"
7
8 #include "opentelemetry/proto/logs/v1/logs.pb.h"
9 #include "opentelemetry/proto/trace/v1/trace.pb.h"
10
11 #include "opentelemetry/exporters/otlp/protobuf_include_suffix.h"
12
13 #include "opentelemetry/exporters/otlp/otlp_log_recordable.h"
14 #include "opentelemetry/exporters/otlp/otlp_recordable.h"
15
16 #include <list>
17 #include <unordered_map>
18
19 namespace nostd = opentelemetry::nostd;
20
21 OPENTELEMETRY_BEGIN_NAMESPACE
22 namespace exporter
23 {
24 namespace otlp
25 {
26
27 namespace
28 {
29 struct InstrumentationLibraryPointerHasher
30 {
31 std::size_t operator()(const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
32 *instrumentation) const noexcept
33 {
34 return instrumentation->HashCode();
35 }
36 };
37
38 struct InstrumentationLibraryPointerEqual
39 {
40 std::size_t operator()(
41 const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *left,
42 const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *right)
43 const noexcept
44 {
45 return *left == *right;
46 }
47 };
48 } // namespace
49
50 //
51 // See `attribute_value.h` for details.
52 //
53 const int kAttributeValueSize = 16;
54 const int kOwnedAttributeValueSize = 15;
55
56 void OtlpRecordableUtils::PopulateAttribute(
57 opentelemetry::proto::common::v1::KeyValue *attribute,
58 nostd::string_view key,
59 const opentelemetry::common::AttributeValue &value) noexcept
60 {
61 if (nullptr == attribute)
62 {
63 return;
64 }
65
66 // Assert size of variant to ensure that this method gets updated if the variant
67 // definition changes
68 static_assert(
69 nostd::variant_size<opentelemetry::common::AttributeValue>::value == kAttributeValueSize,
70 "AttributeValue contains unknown type");
71
72 attribute->set_key(key.data(), key.size());
73
74 if (nostd::holds_alternative<bool>(value))
75 {
76 attribute->mutable_value()->set_bool_value(nostd::get<bool>(value));
77 }
78 else if (nostd::holds_alternative<int>(value))
79 {
80 attribute->mutable_value()->set_int_value(nostd::get<int>(value));
81 }
82 else if (nostd::holds_alternative<int64_t>(value))
83 {
84 attribute->mutable_value()->set_int_value(nostd::get<int64_t>(value));
85 }
86 else if (nostd::holds_alternative<unsigned int>(value))
87 {
88 attribute->mutable_value()->set_int_value(nostd::get<unsigned int>(value));
89 }
90 else if (nostd::holds_alternative<uint64_t>(value))
91 {
92 attribute->mutable_value()->set_int_value(nostd::get<uint64_t>(value));
93 }
94 else if (nostd::holds_alternative<double>(value))
95 {
96 attribute->mutable_value()->set_double_value(nostd::get<double>(value));
97 }
98 else if (nostd::holds_alternative<const char *>(value))
99 {
100 attribute->mutable_value()->set_string_value(nostd::get<const char *>(value));
101 }
102 else if (nostd::holds_alternative<nostd::string_view>(value))
103 {
104 attribute->mutable_value()->set_string_value(nostd::get<nostd::string_view>(value).data(),
105 nostd::get<nostd::string_view>(value).size());
106 }
107 else if (nostd::holds_alternative<nostd::span<const uint8_t>>(value))
108 {
109 for (const auto &val : nostd::get<nostd::span<const uint8_t>>(value))
110 {
111 attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val);
112 }
113 }
114 else if (nostd::holds_alternative<nostd::span<const bool>>(value))
115 {
116 for (const auto &val : nostd::get<nostd::span<const bool>>(value))
117 {
118 attribute->mutable_value()->mutable_array_value()->add_values()->set_bool_value(val);
119 }
120 }
121 else if (nostd::holds_alternative<nostd::span<const int>>(value))
122 {
123 for (const auto &val : nostd::get<nostd::span<const int>>(value))
124 {
125 attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val);
126 }
127 }
128 else if (nostd::holds_alternative<nostd::span<const int64_t>>(value))
129 {
130 for (const auto &val : nostd::get<nostd::span<const int64_t>>(value))
131 {
132 attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val);
133 }
134 }
135 else if (nostd::holds_alternative<nostd::span<const unsigned int>>(value))
136 {
137 for (const auto &val : nostd::get<nostd::span<const unsigned int>>(value))
138 {
139 attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val);
140 }
141 }
142 else if (nostd::holds_alternative<nostd::span<const uint64_t>>(value))
143 {
144 for (const auto &val : nostd::get<nostd::span<const uint64_t>>(value))
145 {
146 attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val);
147 }
148 }
149 else if (nostd::holds_alternative<nostd::span<const double>>(value))
150 {
151 for (const auto &val : nostd::get<nostd::span<const double>>(value))
152 {
153 attribute->mutable_value()->mutable_array_value()->add_values()->set_double_value(val);
154 }
155 }
156 else if (nostd::holds_alternative<nostd::span<const nostd::string_view>>(value))
157 {
158 for (const auto &val : nostd::get<nostd::span<const nostd::string_view>>(value))
159 {
160 attribute->mutable_value()->mutable_array_value()->add_values()->set_string_value(val.data(),
161 val.size());
162 }
163 }
164 }
165
166 /** Maps from C++ attribute into OTLP proto attribute. */
167 void OtlpRecordableUtils::PopulateAttribute(
168 opentelemetry::proto::common::v1::KeyValue *attribute,
169 nostd::string_view key,
170 const opentelemetry::sdk::common::OwnedAttributeValue &value) noexcept
171 {
172 if (nullptr == attribute)
173 {
174 return;
175 }
176
177 // Assert size of variant to ensure that this method gets updated if the variant
178 // definition changes
179 static_assert(nostd::variant_size<opentelemetry::sdk::common::OwnedAttributeValue>::value ==
180 kOwnedAttributeValueSize,
181 "OwnedAttributeValue contains unknown type");
182
183 attribute->set_key(key.data(), key.size());
184
185 if (nostd::holds_alternative<bool>(value))
186 {
187 attribute->mutable_value()->set_bool_value(nostd::get<bool>(value));
188 }
189 else if (nostd::holds_alternative<int32_t>(value))
190 {
191 attribute->mutable_value()->set_int_value(nostd::get<int32_t>(value));
192 }
193 else if (nostd::holds_alternative<int64_t>(value))
194 {
195 attribute->mutable_value()->set_int_value(nostd::get<int64_t>(value));
196 }
197 else if (nostd::holds_alternative<uint32_t>(value))
198 {
199 attribute->mutable_value()->set_int_value(nostd::get<uint32_t>(value));
200 }
201 else if (nostd::holds_alternative<uint64_t>(value))
202 {
203 attribute->mutable_value()->set_int_value(nostd::get<uint64_t>(value));
204 }
205 else if (nostd::holds_alternative<double>(value))
206 {
207 attribute->mutable_value()->set_double_value(nostd::get<double>(value));
208 }
209 else if (nostd::holds_alternative<std::string>(value))
210 {
211 attribute->mutable_value()->set_string_value(nostd::get<std::string>(value));
212 }
213 else if (nostd::holds_alternative<std::vector<bool>>(value))
214 {
215 for (const auto &val : nostd::get<std::vector<bool>>(value))
216 {
217 attribute->mutable_value()->mutable_array_value()->add_values()->set_bool_value(val);
218 }
219 }
220 else if (nostd::holds_alternative<std::vector<int32_t>>(value))
221 {
222 for (const auto &val : nostd::get<std::vector<int32_t>>(value))
223 {
224 attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val);
225 }
226 }
227 else if (nostd::holds_alternative<std::vector<uint32_t>>(value))
228 {
229 for (const auto &val : nostd::get<std::vector<uint32_t>>(value))
230 {
231 attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val);
232 }
233 }
234 else if (nostd::holds_alternative<std::vector<int64_t>>(value))
235 {
236 for (const auto &val : nostd::get<std::vector<int64_t>>(value))
237 {
238 attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val);
239 }
240 }
241 else if (nostd::holds_alternative<std::vector<uint64_t>>(value))
242 {
243 for (const auto &val : nostd::get<std::vector<uint64_t>>(value))
244 {
245 attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val);
246 }
247 }
248 else if (nostd::holds_alternative<std::vector<double>>(value))
249 {
250 for (const auto &val : nostd::get<std::vector<double>>(value))
251 {
252 attribute->mutable_value()->mutable_array_value()->add_values()->set_double_value(val);
253 }
254 }
255 else if (nostd::holds_alternative<std::vector<std::string>>(value))
256 {
257 for (const auto &val : nostd::get<std::vector<std::string>>(value))
258 {
259 attribute->mutable_value()->mutable_array_value()->add_values()->set_string_value(val);
260 }
261 }
262 }
263
264 void OtlpRecordableUtils::PopulateAttribute(
265 opentelemetry::proto::resource::v1::Resource *proto,
266 const opentelemetry::sdk::resource::Resource &resource) noexcept
267 {
268 if (nullptr == proto)
269 {
270 return;
271 }
272
273 for (const auto &kv : resource.GetAttributes())
274 {
275 OtlpRecordableUtils::PopulateAttribute(proto->add_attributes(), kv.first, kv.second);
276 }
277 }
278
279 void OtlpRecordableUtils::PopulateRequest(
280 const nostd::span<std::unique_ptr<opentelemetry::sdk::trace::Recordable>> &spans,
281 proto::collector::trace::v1::ExportTraceServiceRequest *request) noexcept
282 {
283 if (nullptr == request)
284 {
285 return;
286 }
287
288 for (auto &recordable : spans)
289 {
290 auto rec = std::unique_ptr<OtlpRecordable>(static_cast<OtlpRecordable *>(recordable.release()));
291 auto resource_span = request->add_resource_spans();
292 auto instrumentation_lib = resource_span->add_instrumentation_library_spans();
293
294 *instrumentation_lib->add_spans() = std::move(rec->span());
295 *instrumentation_lib->mutable_instrumentation_library() = rec->GetProtoInstrumentationLibrary();
296
297 instrumentation_lib->set_schema_url(rec->GetInstrumentationLibrarySchemaURL());
298
299 *resource_span->mutable_resource() = rec->ProtoResource();
300 resource_span->set_schema_url(rec->GetResourceSchemaURL());
301 }
302 }
303
304 #ifdef ENABLE_LOGS_PREVIEW
305 void OtlpRecordableUtils::PopulateRequest(
306 const nostd::span<std::unique_ptr<opentelemetry::sdk::logs::Recordable>> &logs,
307 proto::collector::logs::v1::ExportLogsServiceRequest *request) noexcept
308 {
309 if (nullptr == request)
310 {
311 return;
312 }
313
314 using logs_index_by_instrumentation_type =
315 std::unordered_map<const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *,
316 std::list<std::unique_ptr<OtlpLogRecordable>>,
317 InstrumentationLibraryPointerHasher, InstrumentationLibraryPointerEqual>;
318 std::unordered_map<const opentelemetry::sdk::resource::Resource *,
319 logs_index_by_instrumentation_type>
320 logs_index_by_resource;
321
322 for (auto &recordable : logs)
323 {
324 auto rec =
325 std::unique_ptr<OtlpLogRecordable>(static_cast<OtlpLogRecordable *>(recordable.release()));
326 auto instrumentation = &rec->GetInstrumentationLibrary();
327 auto resource = &rec->GetResource();
328
329 logs_index_by_resource[resource][instrumentation].emplace_back(std::move(rec));
330 }
331
332 for (auto &input_resource_log : logs_index_by_resource)
333 {
334 auto output_resource_log = request->add_resource_logs();
335 for (auto &input_scope_log : input_resource_log.second)
336 {
337 auto output_scope_log = output_resource_log->add_scope_logs();
338 for (auto &input_log_record : input_scope_log.second)
339 {
340 if (!output_resource_log->has_resource())
341 {
342 *output_resource_log->mutable_resource() = input_log_record->ProtoResource();
343 output_resource_log->set_schema_url(input_resource_log.first->GetSchemaURL());
344 }
345
346 if (!output_scope_log->has_scope())
347 {
348 output_scope_log->mutable_scope()->set_name(input_scope_log.first->GetName());
349 output_scope_log->mutable_scope()->set_version(input_scope_log.first->GetVersion());
350 output_scope_log->set_schema_url(input_scope_log.first->GetSchemaURL());
351 }
352
353 *output_scope_log->add_log_records() = std::move(input_log_record->log_record());
354 }
355 }
356 }
357 }
358 #endif
359
360 } // namespace otlp
361 } // namespace exporter
362 OPENTELEMETRY_END_NAMESPACE