summaryrefslogtreecommitdiffstats
path: root/src/tracer.cpp
blob: 52f2f46e39b9ae9528c57a204a7d60c7f63c0304 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <opentracing/noop.h>
#include <opentracing/tracer.h>

#include <mutex>

namespace opentracing {
BEGIN_OPENTRACING_ABI_NAMESPACE
namespace {
class TracerRegistry {
 public:
  static TracerRegistry& instance() noexcept {
    static TracerRegistry result;
    return result;
  }

  static std::shared_ptr<Tracer> RegisterTracer(
      std::shared_ptr<Tracer>& tracer) noexcept {
    std::lock_guard<std::mutex> lock_guard{mutex_};
    is_registered_ = true;
    tracer_.swap(tracer);
    return tracer;
  }

  static std::shared_ptr<Tracer> tracer() noexcept {
    std::lock_guard<std::mutex> lock_guard{mutex_};
    return tracer_;
  }

  static bool is_registered() noexcept {
    std::lock_guard<std::mutex> lock_guard{mutex_};
    return is_registered_;
  }

 private:
  static std::mutex mutex_;
  static bool is_registered_;
  static std::shared_ptr<Tracer> tracer_;
};

std::mutex TracerRegistry::mutex_;

bool TracerRegistry::is_registered_{false};

std::shared_ptr<Tracer> TracerRegistry::tracer_{MakeNoopTracer()};
}  // namespace

std::shared_ptr<Tracer> Tracer::Global() noexcept {
  return TracerRegistry::instance().tracer();
}

std::shared_ptr<Tracer> Tracer::InitGlobal(
    std::shared_ptr<Tracer> tracer) noexcept {
  return TracerRegistry::instance().RegisterTracer(tracer);
}

bool Tracer::IsGlobalTracerRegistered() noexcept {
  return TracerRegistry::instance().is_registered();
}
END_OPENTRACING_ABI_NAMESPACE
}  // namespace opentracing