|
| 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 | + : wrapped_sampler_(wrapped_sampler) {} |
| 32 | + SamplingResult shouldSample(const absl::optional<SpanContext> parent_context, |
| 33 | + const std::string& trace_id, const std::string& name, |
| 34 | + OTelSpanKind spankind, |
| 35 | + OptRef<const Tracing::TraceContext> trace_context, |
| 36 | + const std::vector<SpanContext>& links) override; |
| 37 | + std::string getDescription() const override; |
| 38 | + |
| 39 | +private: |
| 40 | + SamplerSharedPtr wrapped_sampler_; |
| 41 | +}; |
| 42 | + |
| 43 | +} // namespace OpenTelemetry |
| 44 | +} // namespace Tracers |
| 45 | +} // namespace Extensions |
| 46 | +} // namespace Envoy |
0 commit comments