Coverage Report

Created: 2023-11-12 09:30

/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
   */
56
  explicit Tracer(const std::string& collector_cluster, const std::string& collector_reference_host,
57
                  const datadog::tracing::TracerConfig& config,
58
                  Upstream::ClusterManager& cluster_manager, Stats::Scope& scope,
59
                  ThreadLocal::SlotAllocator& thread_local_slot_allocator);
60
61
  struct ThreadLocalTracer : public ThreadLocal::ThreadLocalObject {
62
    /**
63
     * Create a thread local tracer configured using the specified `config`.
64
     */
65
    explicit ThreadLocalTracer(const datadog::tracing::FinalizedTracerConfig& config);
66
67
    /**
68
     * Create a null (no-op) thread local tracer.
69
     */
70
0
    ThreadLocalTracer() = default;
71
72
    datadog::tracing::Optional<datadog::tracing::Tracer> tracer;
73
  };
74
75
  // Tracing::Driver
76
77
  /**
78
   * Create a Datadog span from the specified \p trace_context, and having the
79
   * specified \p operation_name, \p stream_info and \p tracing_decision.
80
   * If this tracer encountered an error during initialization, then return a
81
   * \c Tracing::NullSpan instead.
82
   * @param config this parameter is ignored
83
   * @param trace_context possibly contains information about an existing trace
84
   * that the returned span will be a part of; otherwise, the returned span is
85
   * the root of a new trace
86
   * @param stream_info contains information about the stream.
87
   * @param operation_name the Datadog "resource name" to associate with the span.
88
   * See comments in the implementation for more information.
89
   * @param tracing_decision the sampling decision made in advance by Envoy for
90
   * this trace. If the decision is to drop the trace, then this tracer will
91
   * honor that decision. If the decision is to keep the trace, then this tracer
92
   * will apply its own sampling logic, which might keep or drop the trace.
93
   */
94
  Tracing::SpanPtr startSpan(const Tracing::Config& config, Tracing::TraceContext& trace_context,
95
                             const StreamInfo::StreamInfo& stream_info,
96
                             const std::string& operation_name,
97
                             Tracing::Decision tracing_decision) override;
98
99
private:
100
  datadog::tracing::Span extract_or_create_span(datadog::tracing::Tracer& tracer,
101
                                                const datadog::tracing::SpanConfig& span_config,
102
                                                const datadog::tracing::DictReader& reader);
103
104
  TracerStats tracer_stats_;
105
  ThreadLocal::TypedSlotPtr<ThreadLocalTracer> thread_local_slot_;
106
};
107
108
} // namespace Datadog
109
} // namespace Tracers
110
} // namespace Extensions
111
} // namespace Envoy