1
#pragma once
2

            
3
#include <memory>
4

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

            
8
#include "source/common/common/logger.h"
9
#include "source/extensions/tracers/opentelemetry/samplers/sampler.h"
10

            
11
namespace Envoy {
12
namespace Extensions {
13
namespace Tracers {
14
namespace OpenTelemetry {
15

            
16
/**
17
 * This is a sampler decorator. If the parent context is empty or doesn't have a valid traceId,
18
 * the ParentBasedSampler will delegate the decision to the wrapped sampler.
19
 * Otherwise, it will decide based on the sampled flag of the parent context:
20
 * if parent_context->sampled -> return RecordAndSample
21
 * else -> return Decision::Drop
22
 * Check https://opentelemetry.io/docs/specs/otel/trace/sdk/#parentbased for the official docs and
23
 * https://github.com/open-telemetry/opentelemetry-cpp/blob/eb2b9753ea2df64079e07d40489388ea1b323108/sdk/src/trace/samplers/parent.cc#L30
24
 * for an official implementation
25
 */
26
class ParentBasedSampler : public Sampler, Logger::Loggable<Logger::Id::tracing> {
27
public:
28
  explicit ParentBasedSampler(const Protobuf::Message& /*config*/,
29
                              Server::Configuration::TracerFactoryContext& /*context*/,
30
                              SamplerSharedPtr wrapped_sampler)
31
122
      : wrapped_sampler_(wrapped_sampler) {}
32
  SamplingResult shouldSample(const StreamInfo::StreamInfo& stream_info,
33
                              const absl::optional<SpanContext> parent_context,
34
                              const std::string& trace_id, const std::string& name,
35
                              OTelSpanKind spankind,
36
                              OptRef<const Tracing::TraceContext> trace_context,
37
                              const std::vector<SpanContext>& links) override;
38
  std::string getDescription() const override;
39

            
40
private:
41
  SamplerSharedPtr wrapped_sampler_;
42
};
43

            
44
} // namespace OpenTelemetry
45
} // namespace Tracers
46
} // namespace Extensions
47
} // namespace Envoy