LCOV - code coverage report
Current view: top level - src - tracing.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 23 26 88.5 %
Date: 2025-11-04 01:11:44 Functions: 7 9 77.8 %

          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         200 : class TextMapReader : public opentracing::TextMapReader {
      21             :  public:
      22         200 :   explicit TextMapReader(const std::map<std::string, std::string> &text_map)
      23         200 :       : _text_map(text_map) {}
      24             : 
      25         200 :   expected<void> ForeachKey(
      26             :       std::function<expected<void>(string_view key, string_view value)> f)
      27             :   const override {
      28         400 :     for (const auto& key_value : _text_map) {
      29         400 :       auto result = f(key_value.first, key_value.second);
      30         200 :       if (!result) return result;
      31             :     }
      32         200 :     return {};
      33             :   }
      34             : 
      35             :  private:
      36             :   const std::map<std::string, std::string>& _text_map;
      37             : };
      38             : 
      39         200 : class TextMapWriter : public opentracing::TextMapWriter {
      40             :  public:
      41         200 :   explicit TextMapWriter(std::map<std::string, std::string> &text_map)
      42         200 :     : _text_map(text_map) {}
      43             : 
      44         200 :   expected<void> Set(string_view key, string_view value) const override {
      45         200 :     _text_map[key] = value;
      46         200 :     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

Generated by: LCOV version 1.12