Sync from Azure to LF
[ric-plt/e2.git] / RIC-E2-TERMINATION / openTracing.h
1 /*
2  * Copyright 2019 AT&T Intellectual Property
3  * Copyright 2019 Nokia
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 //
19 // Created by adi ENZEL on 8/21/19.
20 //
21
22 #ifndef E2_OPENTRACING_H
23 #define E2_OPENTRACING_H
24
25 #include <iostream>
26 #include <string>
27 #include <tracelibcpp/tracelibcpp.hpp>
28 #include <opentracing/tracer.h>
29 #include <opentracing/propagation.h>
30 #include <nlohmann/json.hpp>  // use nlohmann json library as an example
31
32 struct RICCarrierWriter : opentracing::TextMapWriter {
33     explicit RICCarrierWriter(
34             std::unordered_map<std::string, std::string>& data_)
35             : data{data_} {}
36
37     opentracing::expected<void> Set(
38             opentracing::string_view key,
39             opentracing::string_view value) const override {
40         // OpenTracing uses opentracing::expected for error handling. This closely
41         // follows the expected proposal for the C++ Standard Library. See
42         //    http://open-std.org/JTC1/SC22/WG21/docs/papers/2017/p0323r3.pdf
43         // for more background.
44         opentracing::expected<void> result;
45
46         auto was_successful = data.emplace(key, value);
47         if (was_successful.second) {
48             // Use a default constructed opentracing::expected<void> to indicate
49             // success.
50             return result;
51         } else {
52             // `key` clashes with existing data, so the span context can't be encoded
53             // successfully; set opentracing::expected<void> to an std::error_code.
54             return opentracing::make_unexpected(
55                     std::make_error_code(std::errc::not_supported));
56         }
57     }
58
59     std::unordered_map<std::string, std::string>& data;
60 };
61
62 struct RICCarrierReader : opentracing::TextMapReader {
63     explicit RICCarrierReader(
64             const std::unordered_map<std::string, std::string>& data_)
65             : data{data_} {}
66
67     using F = std::function<opentracing::expected<void>(
68             opentracing::string_view, opentracing::string_view)>;
69
70     opentracing::expected<void> ForeachKey(F f) const override {
71         // Iterate through all key-value pairs, the tracer will use the relevant keys
72         // to extract a span context.
73         for (auto& key_value : data) {
74             auto was_successful = f(key_value.first, key_value.second);
75             if (!was_successful) {
76                 // If the callback returns and unexpected value, bail out of the loop.
77                 return was_successful;
78             }
79         }
80
81         // Indicate successful iteration.
82         return {};
83     }
84
85     // Optional, define TextMapReader::LookupKey to allow for faster extraction.
86     opentracing::expected<opentracing::string_view> LookupKey(
87             opentracing::string_view key) const override {
88         auto iter = data.find(key);
89         if (iter != data.end()) {
90             return opentracing::make_unexpected(opentracing::key_not_found_error);
91         }
92         return opentracing::string_view{iter->second};
93     }
94
95     const std::unordered_map<std::string, std::string>& data;
96 };
97
98
99
100 #endif //E2_OPENTRACING_H