|
6 | 6 | #include <cstdint> |
7 | 7 | #include <initializer_list> |
8 | 8 | #include <map> |
| 9 | +#include <memory> |
9 | 10 | #include <string> |
10 | 11 | #include <unordered_map> |
11 | 12 | #include <utility> |
@@ -49,6 +50,8 @@ using OwnedAttributeValue = nostd::variant<bool, |
49 | 50 | std::vector<uint64_t>, |
50 | 51 | std::vector<uint8_t>>; |
51 | 52 |
|
| 53 | +using OwnedAttributeView = nostd::variant<std::vector<nostd::string_view>, std::unique_ptr<bool[]>>; |
| 54 | + |
52 | 55 | enum OwnedAttributeType |
53 | 56 | { |
54 | 57 | kTypeBool, |
@@ -215,6 +218,248 @@ class OrderedAttributeMap : public std::map<std::string, OwnedAttributeValue> |
215 | 218 | AttributeConverter converter_; |
216 | 219 | }; |
217 | 220 |
|
| 221 | +/** |
| 222 | + * Class for storing attributes. |
| 223 | + */ |
| 224 | +struct MixedAttributeMapStorage |
| 225 | +{ |
| 226 | + std::unordered_map<std::string, opentelemetry::common::AttributeValue> attributes; |
| 227 | + AttributeMap owened_attributes; |
| 228 | + std::unordered_map<std::string, OwnedAttributeView> owened_attributes_view; |
| 229 | +}; |
| 230 | + |
| 231 | +/** |
| 232 | + * Set an owned copy (OwnedAttributeValue) and attribute view of a non-owning AttributeValue. |
| 233 | + */ |
| 234 | +class MixedAttributeViewSetter |
| 235 | +{ |
| 236 | +public: |
| 237 | + inline MixedAttributeViewSetter(const nostd::string_view &key, |
| 238 | + MixedAttributeMapStorage &storage, |
| 239 | + AttributeConverter &converter) noexcept |
| 240 | + : key_(&key), storage_(&storage), converter_(&converter) |
| 241 | + {} |
| 242 | + |
| 243 | + void operator()(bool v) |
| 244 | + { |
| 245 | + storage_->owened_attributes[std::string(*key_)] = (*converter_)(v); |
| 246 | + storage_->attributes[std::string(*key_)] = v; |
| 247 | + } |
| 248 | + |
| 249 | + void operator()(int32_t v) |
| 250 | + { |
| 251 | + storage_->owened_attributes[std::string(*key_)] = (*converter_)(v); |
| 252 | + storage_->attributes[std::string(*key_)] = v; |
| 253 | + } |
| 254 | + |
| 255 | + void operator()(uint32_t v) |
| 256 | + { |
| 257 | + storage_->owened_attributes[std::string(*key_)] = (*converter_)(v); |
| 258 | + storage_->attributes[std::string(*key_)] = v; |
| 259 | + } |
| 260 | + |
| 261 | + void operator()(int64_t v) |
| 262 | + { |
| 263 | + storage_->owened_attributes[std::string(*key_)] = (*converter_)(v); |
| 264 | + storage_->attributes[std::string(*key_)] = v; |
| 265 | + } |
| 266 | + |
| 267 | + void operator()(uint64_t v) |
| 268 | + { |
| 269 | + storage_->owened_attributes[std::string(*key_)] = (*converter_)(v); |
| 270 | + storage_->attributes[std::string(*key_)] = v; |
| 271 | + } |
| 272 | + |
| 273 | + void operator()(double v) |
| 274 | + { |
| 275 | + storage_->owened_attributes[std::string(*key_)] = (*converter_)(v); |
| 276 | + storage_->attributes[std::string(*key_)] = v; |
| 277 | + } |
| 278 | + |
| 279 | + void operator()(nostd::string_view v) |
| 280 | + { |
| 281 | + auto &owned_value = storage_->owened_attributes[std::string(*key_)]; |
| 282 | + owned_value = (*converter_)(v); |
| 283 | + storage_->attributes[std::string(*key_)] = nostd::get<std::string>(owned_value); |
| 284 | + } |
| 285 | + |
| 286 | + void operator()(const char *v) |
| 287 | + { |
| 288 | + auto &owned_value = storage_->owened_attributes[std::string(*key_)]; |
| 289 | + owned_value = (*converter_)(v); |
| 290 | + storage_->attributes[std::string(*key_)] = nostd::get<std::string>(owned_value).c_str(); |
| 291 | + } |
| 292 | + |
| 293 | + void operator()(nostd::span<const uint8_t> v) |
| 294 | + { |
| 295 | + auto &owned_value = storage_->owened_attributes[std::string(*key_)]; |
| 296 | + owned_value = (*converter_)(v); |
| 297 | + storage_->attributes[std::string(*key_)] = nostd::get<std::vector<uint8_t>>(owned_value); |
| 298 | + } |
| 299 | + |
| 300 | + void operator()(nostd::span<const bool> v) |
| 301 | + { |
| 302 | + storage_->owened_attributes[std::string(*key_)] = (*converter_)(v); |
| 303 | + if (v.empty()) |
| 304 | + { |
| 305 | + storage_->attributes[std::string(*key_)] = nostd::span<const bool>{}; |
| 306 | + } |
| 307 | + else |
| 308 | + { |
| 309 | + std::unique_ptr<bool[]> owned_view{new bool[v.size()]}; |
| 310 | + for (size_t i = 0; i < v.size(); i++) |
| 311 | + { |
| 312 | + owned_view[i] = v[i]; |
| 313 | + } |
| 314 | + |
| 315 | + storage_->attributes[std::string(*key_)] = |
| 316 | + nostd::span<const bool>{owned_view.get(), v.size()}; |
| 317 | + storage_->owened_attributes_view[std::string(*key_)] = std::move(owned_view); |
| 318 | + } |
| 319 | + } |
| 320 | + |
| 321 | + void operator()(nostd::span<const int32_t> v) |
| 322 | + { |
| 323 | + auto &owned_value = storage_->owened_attributes[std::string(*key_)]; |
| 324 | + owned_value = (*converter_)(v); |
| 325 | + storage_->attributes[std::string(*key_)] = nostd::get<std::vector<int32_t>>(owned_value); |
| 326 | + } |
| 327 | + |
| 328 | + void operator()(nostd::span<const uint32_t> v) |
| 329 | + { |
| 330 | + auto &owned_value = storage_->owened_attributes[std::string(*key_)]; |
| 331 | + owned_value = (*converter_)(v); |
| 332 | + storage_->attributes[std::string(*key_)] = nostd::get<std::vector<uint32_t>>(owned_value); |
| 333 | + } |
| 334 | + |
| 335 | + void operator()(nostd::span<const int64_t> v) |
| 336 | + { |
| 337 | + auto &owned_value = storage_->owened_attributes[std::string(*key_)]; |
| 338 | + owned_value = (*converter_)(v); |
| 339 | + storage_->attributes[std::string(*key_)] = nostd::get<std::vector<int64_t>>(owned_value); |
| 340 | + } |
| 341 | + |
| 342 | + void operator()(nostd::span<const uint64_t> v) |
| 343 | + { |
| 344 | + auto &owned_value = storage_->owened_attributes[std::string(*key_)]; |
| 345 | + owned_value = (*converter_)(v); |
| 346 | + storage_->attributes[std::string(*key_)] = nostd::get<std::vector<uint64_t>>(owned_value); |
| 347 | + } |
| 348 | + |
| 349 | + void operator()(nostd::span<const double> v) |
| 350 | + { |
| 351 | + auto &owned_value = storage_->owened_attributes[std::string(*key_)]; |
| 352 | + owned_value = (*converter_)(v); |
| 353 | + storage_->attributes[std::string(*key_)] = nostd::get<std::vector<double>>(owned_value); |
| 354 | + } |
| 355 | + |
| 356 | + void operator()(nostd::span<const nostd::string_view> v) |
| 357 | + { |
| 358 | + auto &owned_value = storage_->owened_attributes[std::string(*key_)]; |
| 359 | + owned_value = (*converter_)(v); |
| 360 | + |
| 361 | + if (v.empty()) |
| 362 | + { |
| 363 | + storage_->attributes[std::string(*key_)] = nostd::span<const nostd::string_view>{}; |
| 364 | + } |
| 365 | + else |
| 366 | + { |
| 367 | + auto &owned_view = storage_->owened_attributes_view[std::string(*key_)]; |
| 368 | + owned_view = std::vector<nostd::string_view>{}; |
| 369 | + auto &owned_vector = nostd::get<std::vector<nostd::string_view>>(owned_view); |
| 370 | + |
| 371 | + owned_vector.reserve(v.size()); |
| 372 | + for (auto &data : nostd::get<std::vector<std::string>>(owned_value)) |
| 373 | + { |
| 374 | + owned_vector.push_back(data); |
| 375 | + } |
| 376 | + |
| 377 | + storage_->attributes[std::string(*key_)] = |
| 378 | + nostd::span<const nostd::string_view>{owned_vector.data(), owned_vector.size()}; |
| 379 | + } |
| 380 | + } |
| 381 | + |
| 382 | +private: |
| 383 | + const nostd::string_view *key_; |
| 384 | + MixedAttributeMapStorage *storage_; |
| 385 | + AttributeConverter *converter_; |
| 386 | +}; |
| 387 | + |
| 388 | +/** |
| 389 | + * Class for storing attributes and attribute view. |
| 390 | + */ |
| 391 | +class MixedAttributeMap |
| 392 | +{ |
| 393 | +public: |
| 394 | + // Construct empty attribute map |
| 395 | + MixedAttributeMap() {} |
| 396 | + |
| 397 | + // Construct attribute map and populate with attributes |
| 398 | + MixedAttributeMap(const opentelemetry::common::KeyValueIterable &attributes) : MixedAttributeMap() |
| 399 | + { |
| 400 | + attributes.ForEachKeyValue( |
| 401 | + [&](nostd::string_view key, opentelemetry::common::AttributeValue value) noexcept { |
| 402 | + nostd::visit(MixedAttributeViewSetter(key, storage_, converter_), value); |
| 403 | + return true; |
| 404 | + }); |
| 405 | + } |
| 406 | + |
| 407 | + // Construct attribute map and populate with optional attributes |
| 408 | + MixedAttributeMap(const opentelemetry::common::KeyValueIterable *attributes) : MixedAttributeMap() |
| 409 | + { |
| 410 | + if (attributes != nullptr) |
| 411 | + { |
| 412 | + attributes->ForEachKeyValue( |
| 413 | + [&](nostd::string_view key, opentelemetry::common::AttributeValue value) noexcept { |
| 414 | + nostd::visit(MixedAttributeViewSetter(key, storage_, converter_), value); |
| 415 | + return true; |
| 416 | + }); |
| 417 | + } |
| 418 | + } |
| 419 | + |
| 420 | + // Construct map from initializer list by applying `SetAttribute` transform for every attribute |
| 421 | + MixedAttributeMap( |
| 422 | + std::initializer_list<std::pair<nostd::string_view, opentelemetry::common::AttributeValue>> |
| 423 | + attributes) |
| 424 | + : MixedAttributeMap() |
| 425 | + { |
| 426 | + for (auto &kv : attributes) |
| 427 | + { |
| 428 | + nostd::visit(MixedAttributeViewSetter(kv.first, storage_, converter_), kv.second); |
| 429 | + } |
| 430 | + } |
| 431 | + |
| 432 | + // Returns a reference to this map |
| 433 | + const std::unordered_map<std::string, opentelemetry::common::AttributeValue> &GetAttributes() |
| 434 | + const noexcept |
| 435 | + { |
| 436 | + return storage_.attributes; |
| 437 | + } |
| 438 | + |
| 439 | + const AttributeMap &GetOwnedAttributes() const noexcept { return storage_.owened_attributes; } |
| 440 | + |
| 441 | + // Convert non-owning key-value to owning std::string(key) and OwnedAttributeValue(value) |
| 442 | + void SetAttribute(nostd::string_view key, |
| 443 | + const opentelemetry::common::AttributeValue &value) noexcept |
| 444 | + { |
| 445 | + nostd::visit(MixedAttributeViewSetter(key, storage_, converter_), value); |
| 446 | + } |
| 447 | + |
| 448 | + void Reserve(AttributeMap::size_type size) |
| 449 | + { |
| 450 | + storage_.attributes.reserve(size); |
| 451 | + storage_.owened_attributes.reserve(size); |
| 452 | + } |
| 453 | + |
| 454 | + AttributeMap::size_type Size() const noexcept { return storage_.attributes.size(); } |
| 455 | + |
| 456 | + bool Empty() const noexcept { return storage_.attributes.empty(); } |
| 457 | + |
| 458 | +private: |
| 459 | + MixedAttributeMapStorage storage_; |
| 460 | + AttributeConverter converter_; |
| 461 | +}; |
| 462 | + |
218 | 463 | } // namespace common |
219 | 464 | } // namespace sdk |
220 | 465 | OPENTELEMETRY_END_NAMESPACE |
0 commit comments