blob: 1f313daa754b7c67f6546d22a177fd0a2ad20c36 [file] [log] [blame]
ss412g1a79bdf2019-10-24 12:03:05 +03001/*
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
nm755n2e268142019-11-28 16:40:23 +000018/*
19 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20 * platform project (RICP).
21 */
22
23
ss412g1a79bdf2019-10-24 12:03:05 +030024//
25// Created by adi ENZEL on 8/21/19.
26//
27
28#ifndef E2_OPENTRACING_H
29#define E2_OPENTRACING_H
30
31#include <iostream>
32#include <string>
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
37
38struct RICCarrierWriter : opentracing::TextMapWriter {
39 explicit RICCarrierWriter(
40 std::unordered_map<std::string, std::string>& data_)
41 : data{data_} {}
42
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;
51
52 auto was_successful = data.emplace(key, value);
53 if (was_successful.second) {
54 // Use a default constructed opentracing::expected<void> to indicate
55 // success.
56 return result;
57 } else {
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));
62 }
63 }
64
65 std::unordered_map<std::string, std::string>& data;
66};
67
68struct RICCarrierReader : opentracing::TextMapReader {
69 explicit RICCarrierReader(
70 const std::unordered_map<std::string, std::string>& data_)
71 : data{data_} {}
72
73 using F = std::function<opentracing::expected<void>(
74 opentracing::string_view, opentracing::string_view)>;
75
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;
84 }
85 }
86
87 // Indicate successful iteration.
88 return {};
89 }
90
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);
97 }
98 return opentracing::string_view{iter->second};
99 }
100
101 const std::unordered_map<std::string, std::string>& data;
102};
103
104
105
106#endif //E2_OPENTRACING_H