Coverage Report

Created: 2023-11-12 09:30

/proc/self/cwd/source/common/tracing/tracer_manager_impl.cc
Line
Count
Source (jump to first uncovered line)
1
#include "source/common/tracing/tracer_manager_impl.h"
2
3
#include "source/common/config/utility.h"
4
5
namespace Envoy {
6
namespace Tracing {
7
8
SINGLETON_MANAGER_REGISTRATION(tracer_manager);
9
10
TracerManagerImpl::TracerManagerImpl(Server::Configuration::TracerFactoryContextPtr factory_context)
11
5.35k
    : factory_context_(std::move(factory_context)) {}
12
13
TracerSharedPtr
14
876
TracerManagerImpl::getOrCreateTracer(const envoy::config::trace::v3::Tracing_Http* config) {
15
876
  if (!config) {
16
845
    return null_tracer_;
17
845
  }
18
19
31
  const auto cache_key = MessageUtil::hash(*config);
20
31
  const auto it = tracers_.find(cache_key);
21
31
  if (it != tracers_.end()) {
22
0
    auto tracer = it->second.lock();
23
0
    if (tracer) { // Tracer might have been released since it's a weak reference
24
0
      return tracer;
25
0
    }
26
0
  }
27
28
  // Free memory held by expired weak references.
29
  //
30
  // Given that:
31
  //
32
  // * Tracer is obtained only once per listener lifecycle
33
  // * in a typical case, all listeners will have identical tracing configuration and, consequently,
34
  //   will share the same Tracer instance
35
  // * amount of memory held by an expired weak reference is minimal
36
  //
37
  // it seems reasonable to avoid introducing an external sweeper and only reclaim memory at
38
  // the moment when a new Tracer instance is about to be created.
39
31
  removeExpiredCacheEntries();
40
41
  // Initialize a new tracer.
42
31
  ENVOY_LOG(info, "instantiating a new tracer: {}", config->name());
43
44
  // Now see if there is a factory that will accept the config.
45
31
  auto& factory =
46
31
      Envoy::Config::Utility::getAndCheckFactory<Server::Configuration::TracerFactory>(*config);
47
31
  ProtobufTypes::MessagePtr message = Envoy::Config::Utility::translateToFactoryConfig(
48
31
      *config, factory_context_->messageValidationVisitor(), factory);
49
50
31
  TracerSharedPtr tracer =
51
31
      std::make_shared<Tracing::TracerImpl>(factory.createTracerDriver(*message, *factory_context_),
52
31
                                            factory_context_->serverFactoryContext().localInfo());
53
31
  tracers_.emplace(cache_key, tracer); // cache a weak reference
54
31
  return tracer;
55
31
}
56
57
31
void TracerManagerImpl::removeExpiredCacheEntries() {
58
31
  absl::erase_if(tracers_, [](const std::pair<const std::size_t, std::weak_ptr<Tracer>>& entry) {
59
0
    return entry.second.expired();
60
0
  });
61
31
}
62
63
std::shared_ptr<TracerManager>
64
6.61k
TracerManagerImpl::singleton(Server::Configuration::FactoryContext& context) {
65
6.61k
  return context.singletonManager().getTyped<Tracing::TracerManagerImpl>(
66
6.61k
      SINGLETON_MANAGER_REGISTERED_NAME(tracer_manager), [&context] {
67
5.35k
        return std::make_shared<Tracing::TracerManagerImpl>(
68
5.35k
            std::make_unique<Tracing::TracerFactoryContextImpl>(
69
5.35k
                context.getServerFactoryContext(), context.messageValidationVisitor()));
70
5.35k
      });
71
6.61k
}
72
73
} // namespace Tracing
74
} // namespace Envoy