Skip to content

Commit 5f6a95f

Browse files
committed
Add scope_configurator_builder for better class structure
1 parent 31c73a3 commit 5f6a95f

File tree

11 files changed

+142
-39
lines changed

11 files changed

+142
-39
lines changed

sdk/include/opentelemetry/sdk/instrumentationscope/instrumentation_scope.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,6 @@ class InstrumentationScope
158158

159159
InstrumentationScopeAttributes attributes_;
160160
};
161-
162-
template <class T>
163-
using ScopeConfigurator = std::function<T(const InstrumentationScope &)>;
164161
} // namespace instrumentationscope
165162
} // namespace sdk
166163

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
template <class T>
19+
using ScopeConfigurator = std::function<T(const InstrumentationScope &)>;
20+
21+
template <typename T>
22+
class ScopeConfiguratorBuilder
23+
{
24+
public:
25+
/**
26+
* Constructor for a builder object that cam be used to create a scope configurator. A minimally
27+
* configured builder would build a ScopeConfigurator that applies the default_scope_config to
28+
* every instrumentation scope.
29+
* @param default_scope_config The default scope config that the built configurator should fall
30+
* back on.
31+
*/
32+
explicit ScopeConfiguratorBuilder(T default_scope_config) noexcept
33+
: default_scope_config_(default_scope_config)
34+
{}
35+
36+
/**
37+
* Allows the user to pass a generic function that evaluates an instrumentation scope through a
38+
* boolean check. If the check passes, the provided config is applied. Conditions are evaluated in
39+
* order.
40+
* @param scope_matcher a function that returns true if the scope being evaluated matches the
41+
* criteria defined by the function.
42+
* @param scope_config the scope configuration to return for the matched scope.
43+
* @return this
44+
*/
45+
ScopeConfiguratorBuilder<T> AddCondition(
46+
std::function<bool(const InstrumentationScope &)> scope_matcher,
47+
T scope_config)
48+
{
49+
conditions_.push_back(ScopeConfiguratorBuilder::Condition(scope_matcher, scope_config));
50+
return this;
51+
}
52+
53+
/**
54+
* A convenience condition that specifically matches the scope name of the scope being evaluated.
55+
* If the scope name matches to the provided string, then the provided scope configuration is
56+
* applied to the scope.
57+
* @param scope_name The scope name to which the config needs to be applied.
58+
* @param scope_config The scope config for the matching scopes.
59+
* @return this
60+
*/
61+
ScopeConfiguratorBuilder<T> AddConditionNameEquals(nostd::string_view scope_name, T scope_config)
62+
{
63+
auto name_equals_matcher = [scope_name](const InstrumentationScope &scope_info) {
64+
return scope_info.GetName() == scope_name;
65+
};
66+
conditions_.push_back(ScopeConfiguratorBuilder::Condition(name_equals_matcher, scope_config));
67+
return this;
68+
}
69+
70+
/**
71+
* Constructs the scope configurator object that can be used to retrieve scope config depending on
72+
* the instrumentation scope.
73+
* @return a configured scope configurator.
74+
*/
75+
ScopeConfigurator<T> Build()
76+
{
77+
if (conditions_.size() == 0)
78+
{
79+
return [default_scope_config_ = this->default_scope_config_](const InstrumentationScope &) {
80+
return default_scope_config_;
81+
};
82+
}
83+
84+
// Return a configurator that processes all the conditions
85+
return [conditions_ = this->conditions_, default_scope_config_ = this->default_scope_config_](
86+
const InstrumentationScope &scope_info) {
87+
for (Condition condition : conditions_)
88+
{
89+
if (condition.scope_matcher(scope_info))
90+
{
91+
return condition.scope_config;
92+
}
93+
}
94+
return default_scope_config_;
95+
};
96+
}
97+
98+
private:
99+
/**
100+
* An internal struct to encapsulate 'conditions' that can be applied to a
101+
* ScopeConfiguratorBuilder. The applied conditions influence the behavior of the generatred
102+
* ScopeConfigurator.
103+
*/
104+
struct Condition
105+
{
106+
std::function<bool(const InstrumentationScope &)> scope_matcher;
107+
T scope_config;
108+
};
109+
110+
T default_scope_config_;
111+
std::vector<Condition> conditions_;
112+
};
113+
} // namespace instrumentationscope
114+
} // namespace sdk
115+
OPENTELEMETRY_END_NAMESPACE

sdk/include/opentelemetry/sdk/trace/tracer_config.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,11 @@ class TracerConfig
4949
*/
5050
static TracerConfig Default();
5151

52-
/**
53-
* Returns a ScopeConfigurator that can be used to get the default tracer configurator.
54-
* A tracer configurator computes TracerConfig based on the InstrumentationScope. The default
55-
* tracer configurator unconditionally computes the default TracerConfig for all scopes.
56-
*
57-
* @return a static constant TracerConfig that represents a tracer configurator with default
58-
* behavior.
59-
*/
60-
static const instrumentationscope::ScopeConfigurator<TracerConfig> &DefaultConfigurator();
61-
6252
private:
6353
explicit TracerConfig(const bool disabled = false) : disabled_(disabled) {}
6454
bool disabled_;
6555
static const TracerConfig kDefaultConfig;
6656
static const TracerConfig kDisabledConfig;
67-
static const instrumentationscope::ScopeConfigurator<TracerConfig> kDefaultTracerConfigurator;
6857
};
6958
} // namespace trace
7059
} // namespace sdk

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <memory>
88
#include <vector>
99

10+
#include "opentelemetry/sdk/instrumentationscope/scope_configurator_builder.h"
1011
#include "opentelemetry/sdk/resource/resource.h"
1112
#include "opentelemetry/sdk/trace/id_generator.h"
1213
#include "opentelemetry/sdk/trace/processor.h"
@@ -22,8 +23,6 @@ namespace sdk
2223
namespace trace
2324
{
2425

25-
using namespace opentelemetry::sdk::instrumentationscope;
26-
2726
/**
2827
* A class which stores the TracerProvider context.
2928
*
@@ -49,7 +48,8 @@ class TracerContext
4948
std::unique_ptr<IdGenerator>(new RandomIdGenerator()),
5049
std::unique_ptr<instrumentationscope::ScopeConfigurator<TracerConfig>> tracer_configurator =
5150
std::make_unique<instrumentationscope::ScopeConfigurator<TracerConfig>>(
52-
TracerConfig::DefaultConfigurator())) noexcept;
51+
instrumentationscope::ScopeConfiguratorBuilder<TracerConfig>(TracerConfig::Default())
52+
.Build())) noexcept;
5353

5454
virtual ~TracerContext() = default;
5555

@@ -83,7 +83,12 @@ class TracerContext
8383
*/
8484
const opentelemetry::sdk::resource::Resource &GetResource() const noexcept;
8585

86-
instrumentationscope::ScopeConfigurator<TracerConfig> &GetTracerConfigurator() const noexcept;
86+
/**
87+
* Obtain the ScopeConfigurator with this tracer context.
88+
* @return The ScopeConfigurator for this tracer context.
89+
*/
90+
const instrumentationscope::ScopeConfigurator<TracerConfig> &GetTracerConfigurator()
91+
const noexcept;
8792

8893
/**
8994
* Obtain the Id Generator associated with this tracer context.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ namespace sdk
2727
namespace trace
2828
{
2929

30-
using namespace opentelemetry::sdk::instrumentationscope;
31-
3230
class OPENTELEMETRY_EXPORT TracerProvider final : public opentelemetry::trace::TracerProvider
3331
{
3432
public:
@@ -53,7 +51,8 @@ class OPENTELEMETRY_EXPORT TracerProvider final : public opentelemetry::trace::T
5351
std::unique_ptr<IdGenerator>(new RandomIdGenerator()),
5452
std::unique_ptr<instrumentationscope::ScopeConfigurator<TracerConfig>> tracer_configurator =
5553
std::make_unique<instrumentationscope::ScopeConfigurator<TracerConfig>>(
56-
TracerConfig::DefaultConfigurator())) noexcept;
54+
instrumentationscope::ScopeConfiguratorBuilder<TracerConfig>(TracerConfig::Default())
55+
.Build())) noexcept;
5756

5857
explicit TracerProvider(
5958
std::vector<std::unique_ptr<SpanProcessor>> &&processors,
@@ -64,7 +63,8 @@ class OPENTELEMETRY_EXPORT TracerProvider final : public opentelemetry::trace::T
6463
std::unique_ptr<IdGenerator>(new RandomIdGenerator()),
6564
std::unique_ptr<instrumentationscope::ScopeConfigurator<TracerConfig>> tracer_configurator =
6665
std::make_unique<instrumentationscope::ScopeConfigurator<TracerConfig>>(
67-
TracerConfig::DefaultConfigurator())) noexcept;
66+
instrumentationscope::ScopeConfiguratorBuilder<TracerConfig>(TracerConfig::Default())
67+
.Build())) noexcept;
6868

6969
/**
7070
* Initialize a new tracer provider with a specified context

sdk/src/trace/tracer_config.cc

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ namespace trace
1313
const TracerConfig TracerConfig::kDefaultConfig = TracerConfig();
1414
const TracerConfig TracerConfig::kDisabledConfig = TracerConfig(true);
1515

16-
const instrumentationscope::ScopeConfigurator<TracerConfig>
17-
TracerConfig::kDefaultTracerConfigurator =
18-
[](const instrumentationscope::InstrumentationScope &) { return Default(); };
19-
2016
TracerConfig TracerConfig::Disabled()
2117
{
2218
return kDisabledConfig;
@@ -32,11 +28,6 @@ TracerConfig TracerConfig::Default()
3228
return kDefaultConfig;
3329
}
3430

35-
const instrumentationscope::ScopeConfigurator<TracerConfig> &TracerConfig::DefaultConfigurator()
36-
{
37-
return kDefaultTracerConfigurator;
38-
}
39-
4031
bool TracerConfig::IsEnabled() const noexcept
4132
{
4233
return !disabled_;

sdk/src/trace/tracer_context.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const resource::Resource &TracerContext::GetResource() const noexcept
4646
return resource_;
4747
}
4848

49-
instrumentationscope::ScopeConfigurator<TracerConfig> &TracerContext::GetTracerConfigurator()
49+
const instrumentationscope::ScopeConfigurator<TracerConfig> &TracerContext::GetTracerConfigurator()
5050
const noexcept
5151
{
5252
return *tracer_configurator_;

sdk/src/trace/tracer_context_factory.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ std::unique_ptr<TracerContext> TracerContextFactory::Create(
5555
{
5656
auto tracer_configurator =
5757
std::make_unique<instrumentationscope::ScopeConfigurator<TracerConfig>>(
58-
TracerConfig::DefaultConfigurator());
58+
instrumentationscope::ScopeConfiguratorBuilder<TracerConfig>(TracerConfig::Default())
59+
.Build());
5960
return Create(std::move(processors), resource, std::move(sampler), std::move(id_generator),
6061
std::move(tracer_configurator));
6162
}

sdk/src/trace/tracer_provider_factory.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ std::unique_ptr<opentelemetry::sdk::trace::TracerProvider> TracerProviderFactory
5656
{
5757
auto tracer_configurator =
5858
std::make_unique<instrumentationscope::ScopeConfigurator<TracerConfig>>(
59-
TracerConfig::DefaultConfigurator());
59+
instrumentationscope::ScopeConfiguratorBuilder<TracerConfig>(TracerConfig::Default())
60+
.Build());
6061
return Create(std::move(processor), resource, std::move(sampler), std::move(id_generator),
6162
std::move(tracer_configurator));
6263
}
@@ -107,7 +108,8 @@ std::unique_ptr<opentelemetry::sdk::trace::TracerProvider> TracerProviderFactory
107108
{
108109
auto tracer_configurator =
109110
std::make_unique<instrumentationscope::ScopeConfigurator<TracerConfig>>(
110-
TracerConfig::DefaultConfigurator());
111+
instrumentationscope::ScopeConfiguratorBuilder<TracerConfig>(TracerConfig::Default())
112+
.Build());
111113
return Create(std::move(processors), resource, std::move(sampler), std::move(id_generator),
112114
std::move(tracer_configurator));
113115
}

sdk/test/trace/tracer_config_test.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "opentelemetry/common/attribute_value.h"
1111
#include "opentelemetry/nostd/string_view.h"
1212
#include "opentelemetry/nostd/unique_ptr.h"
13-
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
13+
#include "opentelemetry/sdk/instrumentationscope/scope_configurator_builder.h"
1414

1515
namespace trace_sdk = opentelemetry::sdk::trace;
1616
namespace instrumentation_scope = opentelemetry::sdk::instrumentationscope;
@@ -76,7 +76,9 @@ TEST_P(DefaultTracerConfiguratorTestFixture, VerifyDefaultConfiguratorBehavior)
7676
{
7777
instrumentation_scope::InstrumentationScope *scope = GetParam();
7878
instrumentation_scope::ScopeConfigurator<trace_sdk::TracerConfig> default_configurator =
79-
trace_sdk::TracerConfig::DefaultConfigurator();
79+
instrumentation_scope::ScopeConfiguratorBuilder<trace_sdk::TracerConfig>(
80+
trace_sdk::TracerConfig::Default())
81+
.Build();
8082

8183
ASSERT_EQ(default_configurator(*scope), trace_sdk::TracerConfig::Default());
8284
}

0 commit comments

Comments
 (0)