Line data Source code
1 : #include <utility>
2 :
3 : #ifndef SOCIAL_NETWORK_MICROSERVICES_TRACING_H
4 : #define SOCIAL_NETWORK_MICROSERVICES_TRACING_H
5 :
6 : #include <string>
7 : #include <yaml-cpp/yaml.h>
8 : #include <jaegertracing/Tracer.h>
9 :
10 : #include <opentracing/propagation.h>
11 : #include <string>
12 : #include <map>
13 : #include "logger.h"
14 :
15 : namespace social_network {
16 :
17 : using opentracing::expected;
18 : using opentracing::string_view;
19 :
20 76784 : class TextMapReader : public opentracing::TextMapReader {
21 : public:
22 76743 : explicit TextMapReader(const std::map<std::string, std::string> &text_map)
23 76743 : : _text_map(text_map) {}
24 :
25 76800 : expected<void> ForeachKey(
26 : std::function<expected<void>(string_view key, string_view value)> f)
27 : const override {
28 153562 : for (const auto& key_value : _text_map) {
29 153545 : auto result = f(key_value.first, key_value.second);
30 76776 : if (!result) return result;
31 : }
32 76776 : return {};
33 : }
34 :
35 : private:
36 : const std::map<std::string, std::string>& _text_map;
37 : };
38 :
39 76772 : class TextMapWriter : public opentracing::TextMapWriter {
40 : public:
41 76763 : explicit TextMapWriter(std::map<std::string, std::string> &text_map)
42 76763 : : _text_map(text_map) {}
43 :
44 76705 : expected<void> Set(string_view key, string_view value) const override {
45 76705 : _text_map[key] = value;
46 76764 : return {};
47 : }
48 :
49 : private:
50 : std::map<std::string, std::string>& _text_map;
51 : };
52 :
53 1 : void SetUpTracer(
54 : const std::string &config_file_path,
55 : const std::string &service) {
56 2 : auto configYAML = YAML::LoadFile(config_file_path);
57 :
58 : // Enable local Jaeger agent, by prepending the service name to the default
59 : // Jaeger agent's hostname
60 : // configYAML["reporter"]["localAgentHostPort"] = service + "-" +
61 : // configYAML["reporter"]["localAgentHostPort"].as<std::string>();
62 :
63 2 : auto config = jaegertracing::Config::parse(configYAML);
64 :
65 1 : bool r = false;
66 3 : while (!r) {
67 : try
68 : {
69 : auto tracer = jaegertracing::Tracer::make(
70 2 : service, config, jaegertracing::logging::consoleLogger());
71 1 : r = true;
72 : opentracing::Tracer::InitGlobal(
73 1 : std::static_pointer_cast<opentracing::Tracer>(tracer));
74 : }
75 0 : catch(...)
76 : {
77 0 : LOG(error) << "Failed to connect to jaeger, retrying ...";
78 0 : sleep(1);
79 : }
80 : }
81 :
82 :
83 1 : }
84 :
85 :
86 : } //namespace social_network
87 :
88 : #endif //SOCIAL_NETWORK_MICROSERVICES_TRACING_H
|