1
#pragma once
2

            
3
#include <memory>
4
#include <utility>
5
#include <vector>
6

            
7
#include "envoy/config/core/v3/http_service.pb.h"
8
#include "envoy/config/core/v3/http_uri.pb.h"
9
#include "envoy/extensions/tracers/opentelemetry/samplers/v3/dynatrace_sampler.pb.h"
10
#include "envoy/http/async_client.h"
11
#include "envoy/http/message.h"
12
#include "envoy/server/tracer_config.h"
13

            
14
#include "source/common/http/async_client_impl.h"
15
#include "source/common/http/async_client_utility.h"
16
#include "source/common/http/headers.h"
17
#include "source/common/http/message_impl.h"
18
#include "source/common/http/utility.h"
19
#include "source/extensions/tracers/opentelemetry/samplers/dynatrace/sampler_config.h"
20

            
21
namespace Envoy {
22
namespace Extensions {
23
namespace Tracers {
24
namespace OpenTelemetry {
25

            
26
/**
27
 * @brief The Dynatrace sampling configuration provider.
28
 *
29
 * The configuration provider obtains sampling configuration from the Dynatrace API
30
 * in order to dynamically tune up/down the sampling rate of spans.
31
 */
32
class SamplerConfigProvider {
33
public:
34
44
  virtual ~SamplerConfigProvider() = default;
35

            
36
  /**
37
   * @brief Get the Dynatrace Sampler configuration.
38
   *
39
   * @return const SamplerConfig&
40
   */
41
  virtual const SamplerConfig& getSamplerConfig() const = 0;
42
};
43

            
44
class SamplerConfigProviderImpl : public SamplerConfigProvider,
45
                                  public Logger::Loggable<Logger::Id::tracing>,
46
                                  public Http::AsyncClient::Callbacks {
47
public:
48
  SamplerConfigProviderImpl(
49
      Server::Configuration::TracerFactoryContext& context,
50
      const envoy::extensions::tracers::opentelemetry::samplers::v3::DynatraceSamplerConfig&
51
          config);
52

            
53
  void onSuccess(const Http::AsyncClient::Request& request,
54
                 Http::ResponseMessagePtr&& response) override;
55

            
56
  void onFailure(const Http::AsyncClient::Request& request,
57
                 Http::AsyncClient::FailureReason reason) override;
58

            
59
  void onBeforeFinalizeUpstreamSpan(Envoy::Tracing::Span& /*span*/,
60
1
                                    const Http::ResponseHeaderMap* /*response_headers*/) override {
61
1
  };
62

            
63
  const SamplerConfig& getSamplerConfig() const override;
64

            
65
  ~SamplerConfigProviderImpl() override;
66

            
67
private:
68
  Event::TimerPtr timer_;
69
  Upstream::ClusterManager& cluster_manager_;
70
  envoy::config::core::v3::HttpUri http_uri_;
71
  std::vector<std::pair<const Http::LowerCaseString, const std::string>> parsed_headers_to_add_;
72
  Http::AsyncClient::Request* active_request_{};
73
  SamplerConfig sampler_config_;
74
};
75

            
76
using SamplerConfigProviderPtr = std::unique_ptr<SamplerConfigProvider>;
77

            
78
} // namespace OpenTelemetry
79
} // namespace Tracers
80
} // namespace Extensions
81
} // namespace Envoy