1
#pragma once
2

            
3
#include <memory>
4

            
5
#include "envoy/extensions/tracers/opentelemetry/samplers/v3/dynatrace_sampler.pb.h"
6
#include "envoy/server/factory_context.h"
7

            
8
#include "source/common/common/logger.h"
9
#include "source/common/config/datasource.h"
10
#include "source/extensions/tracers/opentelemetry/samplers/dynatrace/sampler_config_provider.h"
11
#include "source/extensions/tracers/opentelemetry/samplers/dynatrace/sampling_controller.h"
12
#include "source/extensions/tracers/opentelemetry/samplers/dynatrace/stream_summary.h"
13
#include "source/extensions/tracers/opentelemetry/samplers/sampler.h"
14

            
15
#include "absl/synchronization/mutex.h"
16

            
17
namespace Envoy {
18
namespace Extensions {
19
namespace Tracers {
20
namespace OpenTelemetry {
21

            
22
/**
23
 * @brief A Dynatrace specific sampler.
24
 *
25
 * For sampling, the requests are categorized based on the http method and the http path.
26
 * A sampling multiplicity is calculated for every request category based on the
27
 * number of requests. A Dynatrace specific tag is added to the http tracestate header.
28
 */
29
class DynatraceSampler : public Sampler, Logger::Loggable<Logger::Id::tracing> {
30
public:
31
  DynatraceSampler(
32
      const envoy::extensions::tracers::opentelemetry::samplers::v3::DynatraceSamplerConfig& config,
33
      Server::Configuration::TracerFactoryContext& context,
34
      SamplerConfigProviderPtr sampler_config_provider);
35

            
36
  /** @see Sampler#shouldSample */
37
  SamplingResult shouldSample(const StreamInfo::StreamInfo& stream_info,
38
                              const absl::optional<SpanContext> parent_context,
39
                              const std::string& trace_id, const std::string& name,
40
                              OTelSpanKind spankind,
41
                              OptRef<const Tracing::TraceContext> trace_context,
42
                              const std::vector<SpanContext>& links) override;
43

            
44
  std::string getDescription() const override;
45

            
46
private:
47
  std::string dt_tracestate_key_; // used as key in the http tracestate header
48
  Event::TimerPtr timer_;         // used to periodically calculate sampling multiplicity
49
  SamplingController sampling_controller_;
50
};
51

            
52
} // namespace OpenTelemetry
53
} // namespace Tracers
54
} // namespace Extensions
55
} // namespace Envoy