Skip to content

Commit 6e15bc3

Browse files
committed
TracerProvider uses TracerConfig as per spec
1 parent 73df243 commit 6e15bc3

File tree

9 files changed

+135
-39
lines changed

9 files changed

+135
-39
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include <functional>
7+
#include "instrumentation_scope.h"
8+
#include "opentelemetry/version.h"
9+
10+
OPENTELEMETRY_BEGIN_NAMESPACE
11+
namespace sdk
12+
{
13+
namespace instrumentationscope
14+
{
15+
template <typename T>
16+
class ScopeConfigurator
17+
{
18+
public:
19+
static nostd::unique_ptr<ScopeConfigurator> Create(
20+
std::function<T(const InstrumentationScope &)> scope_configurator)
21+
{
22+
return nostd::unique_ptr<ScopeConfigurator>(new ScopeConfigurator(scope_configurator));
23+
}
24+
25+
T ComputeConfig(const InstrumentationScope &instrumentation_scope)
26+
{
27+
return scope_configurator_(instrumentation_scope);
28+
}
29+
30+
private:
31+
explicit ScopeConfigurator(
32+
const std::function<T(const InstrumentationScope &)> scope_configurator)
33+
: scope_configurator_(scope_configurator)
34+
{}
35+
const std::function<T(const InstrumentationScope &)> scope_configurator_;
36+
};
37+
} // namespace instrumentationscope
38+
} // namespace sdk
39+
OPENTELEMETRY_END_NAMESPACE

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#include "opentelemetry/sdk/trace/sampler.h"
1616
#include "opentelemetry/sdk/trace/tracer_config.h"
1717
#include "opentelemetry/sdk/trace/tracer_context.h"
18+
#include "opentelemetry/trace/noop.h"
1819
#include "opentelemetry/trace/span.h"
19-
#include "opentelemetry/trace/span_context_kv_iterable.h"
2020
#include "opentelemetry/trace/span_startoptions.h"
2121
#include "opentelemetry/trace/tracer.h"
2222
#include "opentelemetry/version.h"
@@ -108,6 +108,7 @@ class Tracer final : public opentelemetry::trace::Tracer,
108108
std::shared_ptr<InstrumentationScope> instrumentation_scope_;
109109
std::shared_ptr<TracerContext> context_;
110110
TracerConfig tracer_config_;
111+
static const std::shared_ptr<opentelemetry::trace::NoopTracer> kNoopTracer;
111112
};
112113
} // namespace trace
113114
} // namespace sdk

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#pragma once
55

6+
#include "opentelemetry/sdk/instrumentationscope/scope_configurator.h"
67
#include "opentelemetry/version.h"
78

89
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -25,12 +26,14 @@ class TracerConfig
2526
static TracerConfig Disabled();
2627
static TracerConfig Enabled();
2728
static TracerConfig Default();
29+
static const instrumentationscope::ScopeConfigurator<TracerConfig> &DefaultConfigurator();
2830

2931
private:
3032
explicit TracerConfig(const bool disabled = false) : disabled_(disabled) {}
3133
bool disabled_;
32-
static TracerConfig kDefaultConfig;
33-
static TracerConfig kDisabledConfig;
34+
static const TracerConfig kDefaultConfig;
35+
static const TracerConfig kDisabledConfig;
36+
static const instrumentationscope::ScopeConfigurator<TracerConfig> kDefaultTracerConfigurator;
3437
};
3538
} // namespace trace
3639
} // namespace sdk

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

+11-1
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
@@ -21,6 +23,8 @@ namespace sdk
2123
namespace trace
2224
{
2325

26+
using namespace opentelemetry::sdk::instrumentationscope;
27+
2428
/**
2529
* A class which stores the TracerProvider context.
2630
*
@@ -43,7 +47,10 @@ class TracerContext
4347
opentelemetry::sdk::resource::Resource::Create({}),
4448
std::unique_ptr<Sampler> sampler = std::unique_ptr<AlwaysOnSampler>(new AlwaysOnSampler),
4549
std::unique_ptr<IdGenerator> id_generator =
46-
std::unique_ptr<IdGenerator>(new RandomIdGenerator())) noexcept;
50+
std::unique_ptr<IdGenerator>(new RandomIdGenerator()),
51+
std::unique_ptr<ScopeConfigurator<TracerConfig>> tracer_configurator =
52+
std::make_unique<ScopeConfigurator<TracerConfig>>(
53+
TracerConfig::DefaultConfigurator())) noexcept;
4754

4855
virtual ~TracerContext() = default;
4956

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

87+
ScopeConfigurator<TracerConfig> &GetTracerConfigurator() const noexcept;
88+
8089
/**
8190
* Obtain the Id Generator associated with this tracer context.
8291
* @return The ID Generator for this tracer context.
@@ -100,6 +109,7 @@ class TracerContext
100109
std::unique_ptr<Sampler> sampler_;
101110
std::unique_ptr<IdGenerator> id_generator_;
102111
std::unique_ptr<SpanProcessor> processor_;
112+
std::unique_ptr<ScopeConfigurator<TracerConfig>> tracer_configurator_;
103113
};
104114

105115
} // namespace trace

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

+16-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "opentelemetry/nostd/shared_ptr.h"
1111
#include "opentelemetry/nostd/string_view.h"
12+
#include "opentelemetry/sdk/instrumentationscope/scope_configurator.h"
1213
#include "opentelemetry/sdk/resource/resource.h"
1314
#include "opentelemetry/sdk/trace/id_generator.h"
1415
#include "opentelemetry/sdk/trace/processor.h"
@@ -27,6 +28,8 @@ namespace sdk
2728
namespace trace
2829
{
2930

31+
using namespace opentelemetry::sdk::instrumentationscope;
32+
3033
class OPENTELEMETRY_EXPORT TracerProvider final : public opentelemetry::trace::TracerProvider
3134
{
3235
public:
@@ -39,30 +42,30 @@ class OPENTELEMETRY_EXPORT TracerProvider final : public opentelemetry::trace::T
3942
* not be a nullptr.
4043
* @param id_generator The custom id generator for this tracer provider. This must
4144
* not be a nullptr
45+
* @param tracer_configurator Provides access to a function that computes the TracerConfig for
46+
* Tracers provided by this TracerProvider.
4247
*/
4348
explicit TracerProvider(
4449
std::unique_ptr<SpanProcessor> processor,
4550
const opentelemetry::sdk::resource::Resource &resource =
4651
opentelemetry::sdk::resource::Resource::Create({}),
4752
std::unique_ptr<Sampler> sampler = std::unique_ptr<AlwaysOnSampler>(new AlwaysOnSampler),
4853
std::unique_ptr<IdGenerator> id_generator =
49-
std::unique_ptr<IdGenerator>(new RandomIdGenerator())) noexcept;
54+
std::unique_ptr<IdGenerator>(new RandomIdGenerator()),
55+
std::unique_ptr<ScopeConfigurator<TracerConfig>> tracer_configurator =
56+
std::make_unique<ScopeConfigurator<TracerConfig>>(
57+
TracerConfig::DefaultConfigurator())) noexcept;
5058

5159
explicit TracerProvider(
5260
std::vector<std::unique_ptr<SpanProcessor>> &&processors,
5361
const opentelemetry::sdk::resource::Resource &resource =
5462
opentelemetry::sdk::resource::Resource::Create({}),
5563
std::unique_ptr<Sampler> sampler = std::unique_ptr<AlwaysOnSampler>(new AlwaysOnSampler),
5664
std::unique_ptr<IdGenerator> id_generator =
57-
std::unique_ptr<IdGenerator>(new RandomIdGenerator())) noexcept;
58-
59-
// explicit TracerProvider(
60-
// std::vector<std::unique_ptr<SpanProcessor>> &&processors,
61-
// const opentelemetry::sdk::resource::Resource &resource =
62-
// opentelemetry::sdk::resource::Resource::Create({}),
63-
// std::unique_ptr<Sampler> sampler = std::unique_ptr<AlwaysOnSampler>(new AlwaysOnSampler),
64-
// std::unique_ptr<IdGenerator> id_generator =
65-
// std::unique_ptr<IdGenerator>(new RandomIdGenerator())) noexcept;
65+
std::unique_ptr<IdGenerator>(new RandomIdGenerator()),
66+
std::unique_ptr<ScopeConfigurator<TracerConfig>> tracer_configurator =
67+
std::make_unique<ScopeConfigurator<TracerConfig>>(
68+
TracerConfig::DefaultConfigurator())) noexcept;
6669

6770
/**
6871
* Initialize a new tracer provider with a specified context
@@ -121,6 +124,9 @@ class OPENTELEMETRY_EXPORT TracerProvider final : public opentelemetry::trace::T
121124
std::vector<std::shared_ptr<Tracer>> tracers_;
122125
std::shared_ptr<TracerContext> context_;
123126
std::mutex lock_;
127+
128+
// private helper to get TracerConfig from InstrumentationScope using tracer configurator.
129+
TracerConfig GetTracerConfig(const InstrumentationScope &instrumentation_scope) const;
124130
};
125131
} // namespace trace
126132
} // namespace sdk

sdk/src/trace/tracer.cc

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include <stdint.h>
55
#include <chrono>
6-
#include <map>
76
#include <new>
87
#include <utility>
98

@@ -21,7 +20,6 @@
2120
#include "opentelemetry/trace/noop.h"
2221
#include "opentelemetry/trace/span.h"
2322
#include "opentelemetry/trace/span_context.h"
24-
#include "opentelemetry/trace/span_context_kv_iterable.h"
2523
#include "opentelemetry/trace/span_id.h"
2624
#include "opentelemetry/trace/span_startoptions.h"
2725
#include "opentelemetry/trace/trace_flags.h"
@@ -36,6 +34,8 @@ namespace sdk
3634
{
3735
namespace trace
3836
{
37+
const std::shared_ptr<opentelemetry::trace::NoopTracer> Tracer::kNoopTracer =
38+
std::make_shared<opentelemetry::trace::NoopTracer>();
3939

4040
Tracer::Tracer(std::shared_ptr<TracerContext> context,
4141
std::unique_ptr<InstrumentationScope> instrumentation_scope,
@@ -54,6 +54,10 @@ nostd::shared_ptr<opentelemetry::trace::Span> Tracer::StartSpan(
5454
opentelemetry::trace::SpanContext parent_context = GetCurrentSpan()->GetContext();
5555
if (nostd::holds_alternative<opentelemetry::trace::SpanContext>(options.parent))
5656
{
57+
if (!tracer_config_.IsEnabled())
58+
{
59+
return kNoopTracer->StartSpan(name, attributes, links, options);
60+
}
5761
auto span_context = nostd::get<opentelemetry::trace::SpanContext>(options.parent);
5862
if (span_context.IsValid())
5963
{

sdk/src/trace/tracer_config.cc

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
//
2-
// Created by sharmapranav on 11/8/24.
3-
//
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
43

54
#include "opentelemetry/sdk/trace/tracer_config.h"
65

@@ -10,8 +9,13 @@ namespace sdk
109
namespace trace
1110
{
1211

13-
TracerConfig TracerConfig::kDefaultConfig = TracerConfig();
14-
TracerConfig TracerConfig::kDisabledConfig = TracerConfig(true);
12+
const TracerConfig TracerConfig::kDefaultConfig = TracerConfig();
13+
const TracerConfig TracerConfig::kDisabledConfig = TracerConfig(true);
14+
15+
const instrumentationscope::ScopeConfigurator<TracerConfig>
16+
TracerConfig::kDefaultTracerConfigurator =
17+
*instrumentationscope::ScopeConfigurator<TracerConfig>::Create(
18+
[](const instrumentationscope::InstrumentationScope &) { return Default(); });
1519

1620
TracerConfig TracerConfig::Disabled()
1721
{
@@ -28,6 +32,11 @@ TracerConfig TracerConfig::Default()
2832
return kDefaultConfig;
2933
}
3034

35+
const instrumentationscope::ScopeConfigurator<TracerConfig> &TracerConfig::DefaultConfigurator()
36+
{
37+
return kDefaultTracerConfigurator;
38+
}
39+
3140
bool TracerConfig::IsEnabled() const noexcept
3241
{
3342
return !disabled_;

sdk/src/trace/tracer_context.cc

+13-5
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ namespace trace
2121
{
2222
namespace resource = opentelemetry::sdk::resource;
2323

24-
TracerContext::TracerContext(std::vector<std::unique_ptr<SpanProcessor>> &&processors,
25-
const resource::Resource &resource,
26-
std::unique_ptr<Sampler> sampler,
27-
std::unique_ptr<IdGenerator> id_generator) noexcept
24+
TracerContext::TracerContext(
25+
std::vector<std::unique_ptr<SpanProcessor>> &&processors,
26+
const resource::Resource &resource,
27+
std::unique_ptr<Sampler> sampler,
28+
std::unique_ptr<IdGenerator> id_generator,
29+
std::unique_ptr<ScopeConfigurator<TracerConfig>> tracer_configurator) noexcept
2830
: resource_(resource),
2931
sampler_(std::move(sampler)),
3032
id_generator_(std::move(id_generator)),
31-
processor_(std::unique_ptr<SpanProcessor>(new MultiSpanProcessor(std::move(processors))))
33+
processor_(std::unique_ptr<SpanProcessor>(new MultiSpanProcessor(std::move(processors)))),
34+
tracer_configurator_(std::move(tracer_configurator))
3235
{}
3336

3437
Sampler &TracerContext::GetSampler() const noexcept
@@ -41,6 +44,11 @@ const resource::Resource &TracerContext::GetResource() const noexcept
4144
return resource_;
4245
}
4346

47+
ScopeConfigurator<TracerConfig> &TracerContext::GetTracerConfigurator() const noexcept
48+
{
49+
return *tracer_configurator_;
50+
}
51+
4452
opentelemetry::sdk::trace::IdGenerator &TracerContext::GetIdGenerator() const noexcept
4553
{
4654
return *id_generator_;

sdk/src/trace/tracer_provider.cc

+29-13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "opentelemetry/sdk/trace/processor.h"
1717
#include "opentelemetry/sdk/trace/sampler.h"
1818
#include "opentelemetry/sdk/trace/tracer.h"
19+
#include "opentelemetry/sdk/trace/tracer_config.h"
1920
#include "opentelemetry/sdk/trace/tracer_context.h"
2021
#include "opentelemetry/sdk/trace/tracer_provider.h"
2122
#include "opentelemetry/trace/tracer.h"
@@ -35,24 +36,30 @@ TracerProvider::TracerProvider(std::unique_ptr<TracerContext> context) noexcept
3536
OTEL_INTERNAL_LOG_DEBUG("[TracerProvider] TracerProvider created.");
3637
}
3738

38-
TracerProvider::TracerProvider(std::unique_ptr<SpanProcessor> processor,
39-
const resource::Resource &resource,
40-
std::unique_ptr<Sampler> sampler,
41-
std::unique_ptr<IdGenerator> id_generator) noexcept
39+
TracerProvider::TracerProvider(
40+
std::unique_ptr<SpanProcessor> processor,
41+
const resource::Resource &resource,
42+
std::unique_ptr<Sampler> sampler,
43+
std::unique_ptr<IdGenerator> id_generator,
44+
std::unique_ptr<ScopeConfigurator<TracerConfig>> tracer_configurator) noexcept
4245
{
4346
std::vector<std::unique_ptr<SpanProcessor>> processors;
4447
processors.push_back(std::move(processor));
45-
context_ = std::make_shared<TracerContext>(std::move(processors), resource, std::move(sampler),
46-
std::move(id_generator));
48+
context_ =
49+
std::make_shared<TracerContext>(std::move(processors), resource, std::move(sampler),
50+
std::move(id_generator), std::move(tracer_configurator));
4751
}
4852

49-
TracerProvider::TracerProvider(std::vector<std::unique_ptr<SpanProcessor>> &&processors,
50-
const resource::Resource &resource,
51-
std::unique_ptr<Sampler> sampler,
52-
std::unique_ptr<IdGenerator> id_generator) noexcept
53+
TracerProvider::TracerProvider(
54+
std::vector<std::unique_ptr<SpanProcessor>> &&processors,
55+
const resource::Resource &resource,
56+
std::unique_ptr<Sampler> sampler,
57+
std::unique_ptr<IdGenerator> id_generator,
58+
std::unique_ptr<ScopeConfigurator<TracerConfig>> tracer_configurator) noexcept
5359
{
54-
context_ = std::make_shared<TracerContext>(std::move(processors), resource, std::move(sampler),
55-
std::move(id_generator));
60+
context_ =
61+
std::make_shared<TracerContext>(std::move(processors), resource, std::move(sampler),
62+
std::move(id_generator), std::move(tracer_configurator));
5663
}
5764

5865
TracerProvider::~TracerProvider()
@@ -107,8 +114,11 @@ nostd::shared_ptr<trace_api::Tracer> TracerProvider::GetTracer(
107114
instrumentationscope::InstrumentationScopeAttributes attrs_map(attributes);
108115
auto scope =
109116
instrumentationscope::InstrumentationScope::Create(name, version, schema_url, attrs_map);
117+
const instrumentationscope::InstrumentationScope &scope_reference =
118+
instrumentationscope::InstrumentationScope(*scope);
119+
auto tracer_config = context_->GetTracerConfigurator().ComputeConfig(scope_reference);
110120

111-
auto tracer = std::shared_ptr<Tracer>(new Tracer(context_, std::move(scope)));
121+
auto tracer = std::shared_ptr<Tracer>(new Tracer(context_, std::move(scope), tracer_config));
112122
tracers_.push_back(tracer);
113123
return nostd::shared_ptr<trace_api::Tracer>{tracer};
114124
}
@@ -133,6 +143,12 @@ bool TracerProvider::ForceFlush(std::chrono::microseconds timeout) noexcept
133143
return context_->ForceFlush(timeout);
134144
}
135145

146+
TracerConfig TracerProvider::GetTracerConfig(
147+
const InstrumentationScope &instrumentation_scope) const
148+
{
149+
return context_->GetTracerConfigurator().ComputeConfig(instrumentation_scope);
150+
}
151+
136152
} // namespace trace
137153
} // namespace sdk
138154
OPENTELEMETRY_END_NAMESPACE

0 commit comments

Comments
 (0)