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 // Created by adi ENZEL on 8/21/19.
22 #ifndef E2_OPENTRACING_H
23 #define E2_OPENTRACING_H
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
32 struct RICCarrierWriter : opentracing::TextMapWriter {
33 explicit RICCarrierWriter(
34 std::unordered_map<std::string, std::string>& data_)
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;
46 auto was_successful = data.emplace(key, value);
47 if (was_successful.second) {
48 // Use a default constructed opentracing::expected<void> to indicate
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));
59 std::unordered_map<std::string, std::string>& data;
62 struct RICCarrierReader : opentracing::TextMapReader {
63 explicit RICCarrierReader(
64 const std::unordered_map<std::string, std::string>& data_)
67 using F = std::function<opentracing::expected<void>(
68 opentracing::string_view, opentracing::string_view)>;
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;
81 // Indicate successful iteration.
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);
92 return opentracing::string_view{iter->second};
95 const std::unordered_map<std::string, std::string>& data;
100 #endif //E2_OPENTRACING_H