summaryrefslogtreecommitdiffstats
path: root/contrib/libs/opentelemetry-cpp/sdk/src/trace/samplers/parent.cc
blob: e77db2e4aece2b97e6e659e71bfd7f84584d479f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <memory>
#include <string>

#include "opentelemetry/common/key_value_iterable.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/sdk/trace/sampler.h"
#include "opentelemetry/sdk/trace/samplers/parent.h"
#include "opentelemetry/trace/span_context.h"
#include "opentelemetry/trace/span_metadata.h"
#include "opentelemetry/trace/trace_id.h"
#include "opentelemetry/version.h"

namespace trace_api = opentelemetry::trace;

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace trace
{
ParentBasedSampler::ParentBasedSampler(
    const std::shared_ptr<Sampler> &root_sampler,
    const std::shared_ptr<Sampler> &remote_parent_sampled_sampler,
    const std::shared_ptr<Sampler> &remote_parent_nonsampled_sampler,
    const std::shared_ptr<Sampler> &local_parent_sampled_sampler,
    const std::shared_ptr<Sampler> &local_parent_nonsampled_sampler) noexcept
    : root_sampler_(root_sampler),
      remote_parent_sampled_sampler_(remote_parent_sampled_sampler),
      remote_parent_nonsampled_sampler_(remote_parent_nonsampled_sampler),
      local_parent_sampled_sampler_(local_parent_sampled_sampler),
      local_parent_nonsampled_sampler_(local_parent_nonsampled_sampler),
      description_("ParentBased{" + std::string{root_sampler->GetDescription()} + "}")
{}

SamplingResult ParentBasedSampler::ShouldSample(
    const trace_api::SpanContext &parent_context,
    trace_api::TraceId trace_id,
    nostd::string_view name,
    trace_api::SpanKind span_kind,
    const opentelemetry::common::KeyValueIterable &attributes,
    const trace_api::SpanContextKeyValueIterable &links) noexcept
{
  if (!parent_context.IsValid())
  {
    // If no parent (root span) exists returns the result of the root_sampler
    return root_sampler_->ShouldSample(parent_context, trace_id, name, span_kind, attributes,
                                       links);
  }

  // If parent exists:
  if (parent_context.IsSampled())
  {
    if (parent_context.IsRemote())
    {
      return remote_parent_sampled_sampler_->ShouldSample(parent_context, trace_id, name, span_kind,
                                                          attributes, links);
    }
    return local_parent_sampled_sampler_->ShouldSample(parent_context, trace_id, name, span_kind,
                                                       attributes, links);
  }

  // Parent is not sampled
  if (parent_context.IsRemote())
  {
    return remote_parent_nonsampled_sampler_->ShouldSample(parent_context, trace_id, name,
                                                           span_kind, attributes, links);
  }
  return local_parent_nonsampled_sampler_->ShouldSample(parent_context, trace_id, name, span_kind,
                                                        attributes, links);
}

nostd::string_view ParentBasedSampler::GetDescription() const noexcept
{
  return description_;
}

}  // namespace trace
}  // namespace sdk
OPENTELEMETRY_END_NAMESPACE