2 * Copyright 2019 AT&T Intellectual Property
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20 * platform project (RICP).
25 // Created by adi ENZEL on 8/21/19.
28 #ifndef E2_OPENTRACING_H
29 #define E2_OPENTRACING_H
33 #include <tracelibcpp/tracelibcpp.hpp>
34 #include <opentracing/tracer.h>
35 #include <opentracing/propagation.h>
36 #include <nlohmann/json.hpp> // use nlohmann json library as an example
38 struct RICCarrierWriter : opentracing::TextMapWriter {
39 explicit RICCarrierWriter(
40 std::unordered_map<std::string, std::string>& data_)
43 opentracing::expected<void> Set(
44 opentracing::string_view key,
45 opentracing::string_view value) const override {
46 // OpenTracing uses opentracing::expected for error handling. This closely
47 // follows the expected proposal for the C++ Standard Library. See
48 // http://open-std.org/JTC1/SC22/WG21/docs/papers/2017/p0323r3.pdf
49 // for more background.
50 opentracing::expected<void> result;
52 auto was_successful = data.emplace(key, value);
53 if (was_successful.second) {
54 // Use a default constructed opentracing::expected<void> to indicate
58 // `key` clashes with existing data, so the span context can't be encoded
59 // successfully; set opentracing::expected<void> to an std::error_code.
60 return opentracing::make_unexpected(
61 std::make_error_code(std::errc::not_supported));
65 std::unordered_map<std::string, std::string>& data;
68 struct RICCarrierReader : opentracing::TextMapReader {
69 explicit RICCarrierReader(
70 const std::unordered_map<std::string, std::string>& data_)
73 using F = std::function<opentracing::expected<void>(
74 opentracing::string_view, opentracing::string_view)>;
76 opentracing::expected<void> ForeachKey(F f) const override {
77 // Iterate through all key-value pairs, the tracer will use the relevant keys
78 // to extract a span context.
79 for (auto& key_value : data) {
80 auto was_successful = f(key_value.first, key_value.second);
81 if (!was_successful) {
82 // If the callback returns and unexpected value, bail out of the loop.
83 return was_successful;
87 // Indicate successful iteration.
91 // Optional, define TextMapReader::LookupKey to allow for faster extraction.
92 opentracing::expected<opentracing::string_view> LookupKey(
93 opentracing::string_view key) const override {
94 auto iter = data.find(key);
95 if (iter != data.end()) {
96 return opentracing::make_unexpected(opentracing::key_not_found_error);
98 return opentracing::string_view{iter->second};
101 const std::unordered_map<std::string, std::string>& data;
106 #endif //E2_OPENTRACING_H