Skip to content

Commit fabc4a0

Browse files
authored
Merge branch 'main' into fix_lifetime_in_log_record
2 parents 156c229 + 52a80b5 commit fabc4a0

19 files changed

+658
-34
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Increment the:
1515

1616
## [Unreleased]
1717

18+
* [SDK] Add tracer scope configurator
19+
[#3137](https://github.com/open-telemetry/opentelemetry-cpp/pull/3137)
20+
1821
## [1.19 2025-01-22]
1922

2023
* [PROMETHEUS_EXPORTER] Fix default for emitting otel_scope attributes
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
#include <functional>
6+
7+
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
8+
#include "opentelemetry/version.h"
9+
10+
OPENTELEMETRY_BEGIN_NAMESPACE
11+
namespace sdk
12+
{
13+
namespace instrumentationscope
14+
{
15+
/**
16+
* A scope configurator is a function that returns the scope config for a given instrumentation
17+
* scope.
18+
*/
19+
template <typename T>
20+
class ScopeConfigurator
21+
{
22+
public:
23+
/**
24+
* A builder class for the ScopeConfigurator that facilitates the creation of ScopeConfigurators.
25+
*/
26+
class Builder
27+
{
28+
public:
29+
/**
30+
* Constructor for a builder object that cam be used to create a scope configurator. A minimally
31+
* configured builder would build a ScopeConfigurator that applies the default_scope_config to
32+
* every instrumentation scope.
33+
* @param default_scope_config The default scope config that the built configurator should fall
34+
* back on.
35+
*/
36+
explicit Builder(T default_scope_config) noexcept : default_scope_config_(default_scope_config)
37+
{}
38+
39+
/**
40+
* Allows the user to pass a generic function that evaluates an instrumentation scope through a
41+
* boolean check. If the check passes, the provided config is applied. Conditions are evaluated
42+
* in order.
43+
* @param scope_matcher a function that returns true if the scope being evaluated matches the
44+
* criteria defined by the function.
45+
* @param scope_config the scope configuration to return for the matched scope.
46+
* @return this
47+
*/
48+
Builder &AddCondition(std::function<bool(const InstrumentationScope &)> scope_matcher,
49+
T scope_config)
50+
{
51+
conditions_.emplace_back(scope_matcher, scope_config);
52+
return *this;
53+
}
54+
55+
/**
56+
* A convenience condition that specifically matches the scope name of the scope being
57+
* evaluated. If the scope name matches to the provided string, then the provided scope
58+
* configuration is applied to the scope.
59+
* @param scope_name The scope name to which the config needs to be applied.
60+
* @param scope_config The scope config for the matching scopes.
61+
* @return this
62+
*/
63+
Builder &AddConditionNameEquals(nostd::string_view scope_name, T scope_config)
64+
{
65+
std::function<bool(const InstrumentationScope &)> name_equals_matcher =
66+
[scope_name = std::string(scope_name)](const InstrumentationScope &scope_info) {
67+
return scope_info.GetName() == scope_name;
68+
};
69+
conditions_.emplace_back(name_equals_matcher, scope_config);
70+
return *this;
71+
}
72+
73+
/**
74+
* Constructs the scope configurator object that can be used to retrieve scope config depending
75+
* on the instrumentation scope.
76+
* @return a configured scope configurator.
77+
*/
78+
ScopeConfigurator<T> Build() const
79+
{
80+
if (conditions_.size() == 0)
81+
{
82+
return ScopeConfigurator<T>(
83+
[default_scope_config_ = this->default_scope_config_](const InstrumentationScope &) {
84+
return default_scope_config_;
85+
});
86+
}
87+
88+
// Return a configurator that processes all the conditions
89+
return ScopeConfigurator<T>(
90+
[conditions_ = this->conditions_, default_scope_config_ = this->default_scope_config_](
91+
const InstrumentationScope &scope_info) {
92+
for (Condition condition : conditions_)
93+
{
94+
if (condition.scope_matcher(scope_info))
95+
{
96+
return condition.scope_config;
97+
}
98+
}
99+
return default_scope_config_;
100+
});
101+
}
102+
103+
private:
104+
/**
105+
* An internal struct to encapsulate 'conditions' that can be applied to a
106+
* ScopeConfiguratorBuilder. The applied conditions influence the behavior of the generated
107+
* ScopeConfigurator.
108+
*/
109+
struct Condition
110+
{
111+
std::function<bool(const InstrumentationScope &)> scope_matcher;
112+
T scope_config;
113+
114+
Condition(const std::function<bool(const InstrumentationScope &)> &matcher, const T &config)
115+
: scope_matcher(matcher), scope_config(config)
116+
{}
117+
};
118+
119+
T default_scope_config_;
120+
std::vector<Condition> conditions_;
121+
};
122+
123+
// Public methods for ScopeConfigurator
124+
125+
/**
126+
* Invokes the underlying configurator function to get a valid scope configuration.
127+
* @param scope_info The InstrumentationScope containing scope information for which configuration
128+
* needs to be retrieved.
129+
*/
130+
T ComputeConfig(const InstrumentationScope &scope_info) const
131+
{
132+
return this->configurator_(scope_info);
133+
}
134+
135+
private:
136+
// Prevent direct initialization of ScopeConfigurator objects.
137+
explicit ScopeConfigurator(std::function<T(const InstrumentationScope &)> configurator)
138+
: configurator_(configurator)
139+
{}
140+
141+
std::function<T(const InstrumentationScope &)> configurator_;
142+
};
143+
} // namespace instrumentationscope
144+
} // namespace sdk
145+
OPENTELEMETRY_END_NAMESPACE

sdk/include/opentelemetry/sdk/trace/tracer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
#include "opentelemetry/sdk/trace/id_generator.h"
1414
#include "opentelemetry/sdk/trace/processor.h"
1515
#include "opentelemetry/sdk/trace/sampler.h"
16+
#include "opentelemetry/sdk/trace/tracer_config.h"
1617
#include "opentelemetry/sdk/trace/tracer_context.h"
18+
#include "opentelemetry/trace/noop.h"
1719
#include "opentelemetry/trace/span.h"
1820
#include "opentelemetry/trace/span_context_kv_iterable.h"
1921
#include "opentelemetry/trace/span_startoptions.h"
@@ -105,6 +107,8 @@ class Tracer final : public opentelemetry::trace::Tracer,
105107
// tracer-context.
106108
std::shared_ptr<InstrumentationScope> instrumentation_scope_;
107109
std::shared_ptr<TracerContext> context_;
110+
TracerConfig tracer_config_;
111+
static const std::shared_ptr<opentelemetry::trace::NoopTracer> kNoopTracer;
108112
};
109113
} // namespace trace
110114
} // namespace sdk
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include "opentelemetry/version.h"
7+
8+
OPENTELEMETRY_BEGIN_NAMESPACE
9+
namespace sdk
10+
{
11+
namespace trace
12+
{
13+
/**
14+
* TracerConfig defines various configurable aspects of a Tracer's behavior.
15+
* This class should not be used directly to configure a Tracer's behavior, instead a
16+
* ScopeConfigurator should be used to compute the desired TracerConfig which can then be used to
17+
* configure a Tracer.
18+
*/
19+
class TracerConfig
20+
{
21+
public:
22+
bool operator==(const TracerConfig &other) const noexcept;
23+
24+
/**
25+
* Returns if the Tracer is enabled or disabled. Tracers are enabled by default.
26+
* @return a boolean indicating if the Tracer is enabled. Defaults to true.
27+
*/
28+
bool IsEnabled() const noexcept;
29+
30+
/**
31+
* Returns a TracerConfig that represents a disabled Tracer. A disabled tracer behaves like a
32+
* no-op tracer.
33+
* @return a static constant TracerConfig that represents a disabled tracer.
34+
*/
35+
static TracerConfig Disabled();
36+
37+
/**
38+
* Returns a TracerConfig that represents an enabled Tracer.
39+
* @return a static constant TracerConfig that represents an enabled tracer.
40+
*/
41+
static TracerConfig Enabled();
42+
43+
/**
44+
* Returns a TracerConfig that represents a Tracer configured with the default behavior.
45+
* The default behavior is guided by the OpenTelemetry specification.
46+
* @return a static constant TracerConfig that represents a tracer configured with default
47+
* behavior.
48+
*/
49+
static TracerConfig Default();
50+
51+
private:
52+
explicit TracerConfig(const bool disabled = false) : disabled_(disabled) {}
53+
bool disabled_;
54+
static const TracerConfig kDefaultConfig;
55+
static const TracerConfig kDisabledConfig;
56+
};
57+
} // namespace trace
58+
} // namespace sdk
59+
OPENTELEMETRY_END_NAMESPACE

sdk/include/opentelemetry/sdk/trace/tracer_context.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
#include <memory>
88
#include <vector>
99

10+
#include "opentelemetry/sdk/instrumentationscope/scope_configurator.h"
1011
#include "opentelemetry/sdk/resource/resource.h"
1112
#include "opentelemetry/sdk/trace/id_generator.h"
1213
#include "opentelemetry/sdk/trace/processor.h"
1314
#include "opentelemetry/sdk/trace/random_id_generator.h"
1415
#include "opentelemetry/sdk/trace/sampler.h"
1516
#include "opentelemetry/sdk/trace/samplers/always_on.h"
17+
#include "opentelemetry/sdk/trace/tracer_config.h"
1618
#include "opentelemetry/version.h"
1719

1820
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -43,7 +45,12 @@ class TracerContext
4345
opentelemetry::sdk::resource::Resource::Create({}),
4446
std::unique_ptr<Sampler> sampler = std::unique_ptr<AlwaysOnSampler>(new AlwaysOnSampler),
4547
std::unique_ptr<IdGenerator> id_generator =
46-
std::unique_ptr<IdGenerator>(new RandomIdGenerator())) noexcept;
48+
std::unique_ptr<IdGenerator>(new RandomIdGenerator()),
49+
std::unique_ptr<instrumentationscope::ScopeConfigurator<TracerConfig>> tracer_configurator =
50+
std::make_unique<instrumentationscope::ScopeConfigurator<TracerConfig>>(
51+
instrumentationscope::ScopeConfigurator<TracerConfig>::Builder(
52+
TracerConfig::Default())
53+
.Build())) noexcept;
4754

4855
virtual ~TracerContext() = default;
4956

@@ -77,6 +84,13 @@ class TracerContext
7784
*/
7885
const opentelemetry::sdk::resource::Resource &GetResource() const noexcept;
7986

87+
/**
88+
* Obtain the ScopeConfigurator with this tracer context.
89+
* @return The ScopeConfigurator for this tracer context.
90+
*/
91+
const instrumentationscope::ScopeConfigurator<TracerConfig> &GetTracerConfigurator()
92+
const noexcept;
93+
8094
/**
8195
* Obtain the Id Generator associated with this tracer context.
8296
* @return The ID Generator for this tracer context.
@@ -100,6 +114,7 @@ class TracerContext
100114
std::unique_ptr<Sampler> sampler_;
101115
std::unique_ptr<IdGenerator> id_generator_;
102116
std::unique_ptr<SpanProcessor> processor_;
117+
std::unique_ptr<instrumentationscope::ScopeConfigurator<TracerConfig>> tracer_configurator_;
103118
};
104119

105120
} // namespace trace

sdk/include/opentelemetry/sdk/trace/tracer_context_factory.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ class OPENTELEMETRY_EXPORT TracerContextFactory
5454
const opentelemetry::sdk::resource::Resource &resource,
5555
std::unique_ptr<Sampler> sampler,
5656
std::unique_ptr<IdGenerator> id_generator);
57+
58+
/**
59+
* Create a TracerContext.
60+
*/
61+
static std::unique_ptr<TracerContext> Create(
62+
std::vector<std::unique_ptr<SpanProcessor>> &&processors,
63+
const opentelemetry::sdk::resource::Resource &resource,
64+
std::unique_ptr<Sampler> sampler,
65+
std::unique_ptr<IdGenerator> id_generator,
66+
std::unique_ptr<instrumentationscope::ScopeConfigurator<TracerConfig>> tracer_configurator);
5767
};
5868

5969
} // namespace trace

sdk/include/opentelemetry/sdk/trace/tracer_provider.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,34 @@ class OPENTELEMETRY_EXPORT TracerProvider final : public opentelemetry::trace::T
3939
* not be a nullptr.
4040
* @param id_generator The custom id generator for this tracer provider. This must
4141
* not be a nullptr
42+
* @param tracer_configurator Provides access to a function that computes the TracerConfig for
43+
* Tracers provided by this TracerProvider.
4244
*/
4345
explicit TracerProvider(
4446
std::unique_ptr<SpanProcessor> processor,
4547
const opentelemetry::sdk::resource::Resource &resource =
4648
opentelemetry::sdk::resource::Resource::Create({}),
4749
std::unique_ptr<Sampler> sampler = std::unique_ptr<AlwaysOnSampler>(new AlwaysOnSampler),
4850
std::unique_ptr<IdGenerator> id_generator =
49-
std::unique_ptr<IdGenerator>(new RandomIdGenerator())) noexcept;
51+
std::unique_ptr<IdGenerator>(new RandomIdGenerator()),
52+
std::unique_ptr<instrumentationscope::ScopeConfigurator<TracerConfig>> tracer_configurator =
53+
std::make_unique<instrumentationscope::ScopeConfigurator<TracerConfig>>(
54+
instrumentationscope::ScopeConfigurator<TracerConfig>::Builder(
55+
TracerConfig::Default())
56+
.Build())) noexcept;
5057

5158
explicit TracerProvider(
5259
std::vector<std::unique_ptr<SpanProcessor>> &&processors,
5360
const opentelemetry::sdk::resource::Resource &resource =
5461
opentelemetry::sdk::resource::Resource::Create({}),
5562
std::unique_ptr<Sampler> sampler = std::unique_ptr<AlwaysOnSampler>(new AlwaysOnSampler),
5663
std::unique_ptr<IdGenerator> id_generator =
57-
std::unique_ptr<IdGenerator>(new RandomIdGenerator())) noexcept;
64+
std::unique_ptr<IdGenerator>(new RandomIdGenerator()),
65+
std::unique_ptr<instrumentationscope::ScopeConfigurator<TracerConfig>> tracer_configurator =
66+
std::make_unique<instrumentationscope::ScopeConfigurator<TracerConfig>>(
67+
instrumentationscope::ScopeConfigurator<TracerConfig>::Builder(
68+
TracerConfig::Default())
69+
.Build())) noexcept;
5870

5971
/**
6072
* Initialize a new tracer provider with a specified context

sdk/include/opentelemetry/sdk/trace/tracer_provider_factory.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace trace
2828
class OPENTELEMETRY_EXPORT TracerProviderFactory
2929
{
3030
public:
31-
/* Serie of builders with a single processor. */
31+
/* Series of creator methods with a single processor. */
3232

3333
static std::unique_ptr<opentelemetry::sdk::trace::TracerProvider> Create(
3434
std::unique_ptr<SpanProcessor> processor);
@@ -48,7 +48,14 @@ class OPENTELEMETRY_EXPORT TracerProviderFactory
4848
std::unique_ptr<Sampler> sampler,
4949
std::unique_ptr<IdGenerator> id_generator);
5050

51-
/* Serie of builders with a vector of processor. */
51+
static std::unique_ptr<opentelemetry::sdk::trace::TracerProvider> Create(
52+
std::unique_ptr<SpanProcessor> processor,
53+
const opentelemetry::sdk::resource::Resource &resource,
54+
std::unique_ptr<Sampler> sampler,
55+
std::unique_ptr<IdGenerator> id_generator,
56+
std::unique_ptr<instrumentationscope::ScopeConfigurator<TracerConfig>> tracer_configurator);
57+
58+
/* Series of creator methods with a vector of processors. */
5259

5360
static std::unique_ptr<opentelemetry::sdk::trace::TracerProvider> Create(
5461
std::vector<std::unique_ptr<SpanProcessor>> &&processors);
@@ -68,6 +75,13 @@ class OPENTELEMETRY_EXPORT TracerProviderFactory
6875
std::unique_ptr<Sampler> sampler,
6976
std::unique_ptr<IdGenerator> id_generator);
7077

78+
static std::unique_ptr<opentelemetry::sdk::trace::TracerProvider> Create(
79+
std::vector<std::unique_ptr<SpanProcessor>> &&processors,
80+
const opentelemetry::sdk::resource::Resource &resource,
81+
std::unique_ptr<Sampler> sampler,
82+
std::unique_ptr<IdGenerator> id_generator,
83+
std::unique_ptr<instrumentationscope::ScopeConfigurator<TracerConfig>> tracer_configurator);
84+
7185
/* Create with a tracer context. */
7286

7387
static std::unique_ptr<opentelemetry::sdk::trace::TracerProvider> Create(

sdk/src/trace/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ add_library(
2020
samplers/trace_id_ratio.cc
2121
samplers/trace_id_ratio_factory.cc
2222
random_id_generator.cc
23-
random_id_generator_factory.cc)
23+
random_id_generator_factory.cc
24+
tracer_config.cc)
2425

2526
set_target_properties(opentelemetry_trace PROPERTIES EXPORT_NAME trace)
2627
set_target_version(opentelemetry_trace)

0 commit comments

Comments
 (0)