Coverage Report

Created: 2024-09-19 09:45

/proc/self/cwd/source/extensions/tracers/datadog/tracer.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include <optional>
4
#include <string>
5
6
#include "envoy/thread_local/thread_local.h"
7
#include "envoy/tracing/trace_driver.h"
8
9
#include "source/common/common/logger.h"
10
#include "source/extensions/tracers/datadog/tracer_stats.h"
11
12
#include "datadog/tracer.h"
13
14
namespace datadog {
15
namespace tracing {
16
17
class DictReader;
18
class FinalizedTracerConfig;
19
class Span;
20
struct SpanConfig;
21
struct TracerConfig;
22
23
} // namespace tracing
24
} // namespace datadog
25
26
namespace Envoy {
27
namespace Extensions {
28
namespace Tracers {
29
namespace Datadog {
30
31
/**
32
 * Entry point for Datadog tracing. This class accepts configuration and runtime
33
 * dependencies, and installs a thread-local instance of itself from which
34
 * traces can be created.
35
 */
36
class Tracer : public Tracing::Driver, private Logger::Loggable<Logger::Id::tracing> {
37
public:
38
  /**
39
   * Create a \c Tracer that produces traces according to the specified \p config
40
   * and sends those traces to the specified \p collector_cluster, uses the
41
   * specified \p collector_reference_host as the "Host" header, uses the
42
   * specified \p cluster_manager to send requests to the collector, uses the
43
   * specified \p scope to keep track of usage statistics, and uses the specified
44
   * \p thread_local_slot_allocator to register this instance with the current
45
   * worker thread.
46
   * @param collector_cluster name of the cluster to which traces will be sent
47
   * @param collector_reference_host value of the "Host" header in requests sent
48
   * to the \p collector_cluster.
49
   * @param config Datadog tracer configuration
50
   * @param cluster_manager cluster manager from which the thread local
51
   * cluster is retrieved in order to send HTTP request containing traces
52
   * @param scope statistics scope from which \c TracerStats can be created
53
   * @param thread_local_slot_allocator slot allocator for installing a
54
   * thread-local instance of the tracer
55
   * @param time_source clocks used for calculating HTTP request timeouts
56
   */
57
  explicit Tracer(const std::string& collector_cluster, const std::string& collector_reference_host,
58
                  const datadog::tracing::TracerConfig& config,
59
                  Upstream::ClusterManager& cluster_manager, Stats::Scope& scope,
60
                  ThreadLocal::SlotAllocator& thread_local_slot_allocator, TimeSource& time_source);
61
62
  struct ThreadLocalTracer : public ThreadLocal::ThreadLocalObject {
63
    /**
64
     * Create a thread local tracer configured using the specified `config`.
65
     */
66
    explicit ThreadLocalTracer(const datadog::tracing::FinalizedTracerConfig& config);
67
68
    /**
69
     * Create a null (no-op) thread local tracer.
70
     */
71
0
    ThreadLocalTracer() = default;
72
73
    datadog::tracing::Optional<datadog::tracing::Tracer> tracer;
74
  };
75
76
  // Tracing::Driver
77
78
  /**
79
   * Create a Datadog span from the specified \p trace_context, and having the
80
   * specified \p operation_name, \p stream_info and \p tracing_decision.
81
   * If this tracer encountered an error during initialization, then return a
82
   * \c Tracing::NullSpan instead.
83
   * @param config this parameter is ignored
84
   * @param trace_context possibly contains information about an existing trace
85
   * that the returned span will be a part of; otherwise, the returned span is
86
   * the root of a new trace
87
   * @param stream_info contains information about the stream.
88
   * @param operation_name the Datadog "resource name" to associate with the span.
89
   * See comments in the implementation for more information.
90
   * @param tracing_decision the sampling decision made in advance by Envoy for
91
   * this trace. If the decision is to drop the trace, then this tracer will
92
   * honor that decision. If the decision is to keep the trace, then this tracer
93
   * will apply its own sampling logic, which might keep or drop the trace.
94
   */
95
  Tracing::SpanPtr startSpan(const Tracing::Config& config, Tracing::TraceContext& trace_context,
96
                             const StreamInfo::StreamInfo& stream_info,
97
                             const std::string& operation_name,
98
                             Tracing::Decision tracing_decision) override;
99
100
private:
101
  datadog::tracing::Span extractOrCreateSpan(datadog::tracing::Tracer& tracer,
102
                                             const datadog::tracing::SpanConfig& span_config,
103
                                             const datadog::tracing::DictReader& reader);
104
105
  TracerStats tracer_stats_;
106
  ThreadLocal::TypedSlotPtr<ThreadLocalTracer> thread_local_slot_;
107
};
108
109
} // namespace Datadog
110
} // namespace Tracers
111
} // namespace Extensions
112
} // namespace Envoy