From fd0178c66a40761d6c1c523180c811e834f6fcf0 Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Tue, 12 Nov 2024 23:34:29 +0000 Subject: [PATCH] TracerProvider uses TracerConfig as per spec --- .../instrumentationscope/scope_configurator.h | 39 +++++++++++++++++ sdk/include/opentelemetry/sdk/trace/tracer.h | 3 +- .../opentelemetry/sdk/trace/tracer_config.h | 9 ++-- .../opentelemetry/sdk/trace/tracer_context.h | 12 +++++- .../opentelemetry/sdk/trace/tracer_provider.h | 26 +++++++----- sdk/src/trace/CMakeLists.txt | 3 +- sdk/src/trace/tracer.cc | 8 +++- sdk/src/trace/tracer_config.cc | 21 +++++++--- sdk/src/trace/tracer_context.cc | 18 +++++--- sdk/src/trace/tracer_provider.cc | 42 +++++++++++++------ 10 files changed, 138 insertions(+), 43 deletions(-) create mode 100644 sdk/include/opentelemetry/sdk/instrumentationscope/scope_configurator.h diff --git a/sdk/include/opentelemetry/sdk/instrumentationscope/scope_configurator.h b/sdk/include/opentelemetry/sdk/instrumentationscope/scope_configurator.h new file mode 100644 index 0000000000..6ffac33bca --- /dev/null +++ b/sdk/include/opentelemetry/sdk/instrumentationscope/scope_configurator.h @@ -0,0 +1,39 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include +#include "instrumentation_scope.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace instrumentationscope +{ +template +class ScopeConfigurator +{ +public: + static nostd::unique_ptr Create( + std::function scope_configurator) + { + return nostd::unique_ptr(new ScopeConfigurator(scope_configurator)); + } + + T ComputeConfig(const InstrumentationScope &instrumentation_scope) + { + return scope_configurator_(instrumentation_scope); + } + +private: + explicit ScopeConfigurator( + const std::function scope_configurator) + : scope_configurator_(scope_configurator) + {} + const std::function scope_configurator_; +}; +} // namespace instrumentationscope +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/trace/tracer.h b/sdk/include/opentelemetry/sdk/trace/tracer.h index 7f8f11ecb3..77c1fd0861 100644 --- a/sdk/include/opentelemetry/sdk/trace/tracer.h +++ b/sdk/include/opentelemetry/sdk/trace/tracer.h @@ -15,8 +15,8 @@ #include "opentelemetry/sdk/trace/sampler.h" #include "opentelemetry/sdk/trace/tracer_config.h" #include "opentelemetry/sdk/trace/tracer_context.h" +#include "opentelemetry/trace/noop.h" #include "opentelemetry/trace/span.h" -#include "opentelemetry/trace/span_context_kv_iterable.h" #include "opentelemetry/trace/span_startoptions.h" #include "opentelemetry/trace/tracer.h" #include "opentelemetry/version.h" @@ -108,6 +108,7 @@ class Tracer final : public opentelemetry::trace::Tracer, std::shared_ptr instrumentation_scope_; std::shared_ptr context_; TracerConfig tracer_config_; + static const std::shared_ptr kNoopTracer; }; } // namespace trace } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/trace/tracer_config.h b/sdk/include/opentelemetry/sdk/trace/tracer_config.h index ae8b2248f7..eb8f4a80c3 100644 --- a/sdk/include/opentelemetry/sdk/trace/tracer_config.h +++ b/sdk/include/opentelemetry/sdk/trace/tracer_config.h @@ -3,6 +3,7 @@ #pragma once +#include "opentelemetry/sdk/instrumentationscope/scope_configurator.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -25,13 +26,15 @@ class TracerConfig static TracerConfig Disabled(); static TracerConfig Enabled(); static TracerConfig Default(); + static const instrumentationscope::ScopeConfigurator &DefaultConfigurator(); private: explicit TracerConfig(const bool disabled = false) : disabled_(disabled) {} bool disabled_; - static TracerConfig kDefaultConfig; - static TracerConfig kDisabledConfig; + static const TracerConfig kDefaultConfig; + static const TracerConfig kDisabledConfig; + static const instrumentationscope::ScopeConfigurator kDefaultTracerConfigurator; }; } // namespace trace } // namespace sdk -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/trace/tracer_context.h b/sdk/include/opentelemetry/sdk/trace/tracer_context.h index e1b2cb25d2..b49d45444f 100644 --- a/sdk/include/opentelemetry/sdk/trace/tracer_context.h +++ b/sdk/include/opentelemetry/sdk/trace/tracer_context.h @@ -7,12 +7,14 @@ #include #include +#include "opentelemetry/sdk/instrumentationscope/scope_configurator.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/trace/id_generator.h" #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/sdk/trace/random_id_generator.h" #include "opentelemetry/sdk/trace/sampler.h" #include "opentelemetry/sdk/trace/samplers/always_on.h" +#include "opentelemetry/sdk/trace/tracer_config.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -21,6 +23,8 @@ namespace sdk namespace trace { +using namespace opentelemetry::sdk::instrumentationscope; + /** * A class which stores the TracerProvider context. * @@ -43,7 +47,10 @@ class TracerContext opentelemetry::sdk::resource::Resource::Create({}), std::unique_ptr sampler = std::unique_ptr(new AlwaysOnSampler), std::unique_ptr id_generator = - std::unique_ptr(new RandomIdGenerator())) noexcept; + std::unique_ptr(new RandomIdGenerator()), + std::unique_ptr> tracer_configurator = + std::make_unique>( + TracerConfig::DefaultConfigurator())) noexcept; virtual ~TracerContext() = default; @@ -77,6 +84,8 @@ class TracerContext */ const opentelemetry::sdk::resource::Resource &GetResource() const noexcept; + ScopeConfigurator &GetTracerConfigurator() const noexcept; + /** * Obtain the Id Generator associated with this tracer context. * @return The ID Generator for this tracer context. @@ -100,6 +109,7 @@ class TracerContext std::unique_ptr sampler_; std::unique_ptr id_generator_; std::unique_ptr processor_; + std::unique_ptr> tracer_configurator_; }; } // namespace trace diff --git a/sdk/include/opentelemetry/sdk/trace/tracer_provider.h b/sdk/include/opentelemetry/sdk/trace/tracer_provider.h index 55e996ebdf..fe73083584 100644 --- a/sdk/include/opentelemetry/sdk/trace/tracer_provider.h +++ b/sdk/include/opentelemetry/sdk/trace/tracer_provider.h @@ -9,6 +9,7 @@ #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/sdk/instrumentationscope/scope_configurator.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/trace/id_generator.h" #include "opentelemetry/sdk/trace/processor.h" @@ -27,6 +28,8 @@ namespace sdk namespace trace { +using namespace opentelemetry::sdk::instrumentationscope; + class OPENTELEMETRY_EXPORT TracerProvider final : public opentelemetry::trace::TracerProvider { public: @@ -39,6 +42,8 @@ class OPENTELEMETRY_EXPORT TracerProvider final : public opentelemetry::trace::T * not be a nullptr. * @param id_generator The custom id generator for this tracer provider. This must * not be a nullptr + * @param tracer_configurator Provides access to a function that computes the TracerConfig for + * Tracers provided by this TracerProvider. */ explicit TracerProvider( std::unique_ptr processor, @@ -46,7 +51,10 @@ class OPENTELEMETRY_EXPORT TracerProvider final : public opentelemetry::trace::T opentelemetry::sdk::resource::Resource::Create({}), std::unique_ptr sampler = std::unique_ptr(new AlwaysOnSampler), std::unique_ptr id_generator = - std::unique_ptr(new RandomIdGenerator())) noexcept; + std::unique_ptr(new RandomIdGenerator()), + std::unique_ptr> tracer_configurator = + std::make_unique>( + TracerConfig::DefaultConfigurator())) noexcept; explicit TracerProvider( std::vector> &&processors, @@ -54,15 +62,10 @@ class OPENTELEMETRY_EXPORT TracerProvider final : public opentelemetry::trace::T opentelemetry::sdk::resource::Resource::Create({}), std::unique_ptr sampler = std::unique_ptr(new AlwaysOnSampler), std::unique_ptr id_generator = - std::unique_ptr(new RandomIdGenerator())) noexcept; - - // explicit TracerProvider( - // std::vector> &&processors, - // const opentelemetry::sdk::resource::Resource &resource = - // opentelemetry::sdk::resource::Resource::Create({}), - // std::unique_ptr sampler = std::unique_ptr(new AlwaysOnSampler), - // std::unique_ptr id_generator = - // std::unique_ptr(new RandomIdGenerator())) noexcept; + std::unique_ptr(new RandomIdGenerator()), + std::unique_ptr> tracer_configurator = + std::make_unique>( + TracerConfig::DefaultConfigurator())) noexcept; /** * Initialize a new tracer provider with a specified context @@ -121,6 +124,9 @@ class OPENTELEMETRY_EXPORT TracerProvider final : public opentelemetry::trace::T std::vector> tracers_; std::shared_ptr context_; std::mutex lock_; + + // private helper to get TracerConfig from InstrumentationScope using tracer configurator. + TracerConfig GetTracerConfig(const InstrumentationScope &instrumentation_scope) const; }; } // namespace trace } // namespace sdk diff --git a/sdk/src/trace/CMakeLists.txt b/sdk/src/trace/CMakeLists.txt index ec975fd2b7..b99c2ca42c 100644 --- a/sdk/src/trace/CMakeLists.txt +++ b/sdk/src/trace/CMakeLists.txt @@ -21,8 +21,7 @@ add_library( samplers/trace_id_ratio_factory.cc random_id_generator.cc random_id_generator_factory.cc - tracer_config.cc -) + tracer_config.cc) set_target_properties(opentelemetry_trace PROPERTIES EXPORT_NAME trace) set_target_version(opentelemetry_trace) diff --git a/sdk/src/trace/tracer.cc b/sdk/src/trace/tracer.cc index a803dc6880..d4c6507839 100644 --- a/sdk/src/trace/tracer.cc +++ b/sdk/src/trace/tracer.cc @@ -3,7 +3,6 @@ #include #include -#include #include #include @@ -21,7 +20,6 @@ #include "opentelemetry/trace/noop.h" #include "opentelemetry/trace/span.h" #include "opentelemetry/trace/span_context.h" -#include "opentelemetry/trace/span_context_kv_iterable.h" #include "opentelemetry/trace/span_id.h" #include "opentelemetry/trace/span_startoptions.h" #include "opentelemetry/trace/trace_flags.h" @@ -36,6 +34,8 @@ namespace sdk { namespace trace { +const std::shared_ptr Tracer::kNoopTracer = + std::make_shared(); Tracer::Tracer(std::shared_ptr context, std::unique_ptr instrumentation_scope, @@ -54,6 +54,10 @@ nostd::shared_ptr Tracer::StartSpan( opentelemetry::trace::SpanContext parent_context = GetCurrentSpan()->GetContext(); if (nostd::holds_alternative(options.parent)) { + if (!tracer_config_.IsEnabled()) + { + return kNoopTracer->StartSpan(name, attributes, links, options); + } auto span_context = nostd::get(options.parent); if (span_context.IsValid()) { diff --git a/sdk/src/trace/tracer_config.cc b/sdk/src/trace/tracer_config.cc index 3f66c2670c..461e6c935c 100644 --- a/sdk/src/trace/tracer_config.cc +++ b/sdk/src/trace/tracer_config.cc @@ -1,6 +1,5 @@ -// -// Created by sharmapranav on 11/8/24. -// +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 #include "opentelemetry/sdk/trace/tracer_config.h" @@ -10,8 +9,13 @@ namespace sdk namespace trace { -TracerConfig TracerConfig::kDefaultConfig = TracerConfig(); -TracerConfig TracerConfig::kDisabledConfig = TracerConfig(true); +const TracerConfig TracerConfig::kDefaultConfig = TracerConfig(); +const TracerConfig TracerConfig::kDisabledConfig = TracerConfig(true); + +const instrumentationscope::ScopeConfigurator + TracerConfig::kDefaultTracerConfigurator = + *instrumentationscope::ScopeConfigurator::Create( + [](const instrumentationscope::InstrumentationScope &) { return Default(); }); TracerConfig TracerConfig::Disabled() { @@ -28,10 +32,15 @@ TracerConfig TracerConfig::Default() return kDefaultConfig; } +const instrumentationscope::ScopeConfigurator &TracerConfig::DefaultConfigurator() +{ + return kDefaultTracerConfigurator; +} + bool TracerConfig::IsEnabled() const noexcept { return !disabled_; } } // namespace trace } // namespace sdk -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/trace/tracer_context.cc b/sdk/src/trace/tracer_context.cc index 65300d6f22..138abb8377 100644 --- a/sdk/src/trace/tracer_context.cc +++ b/sdk/src/trace/tracer_context.cc @@ -21,14 +21,17 @@ namespace trace { namespace resource = opentelemetry::sdk::resource; -TracerContext::TracerContext(std::vector> &&processors, - const resource::Resource &resource, - std::unique_ptr sampler, - std::unique_ptr id_generator) noexcept +TracerContext::TracerContext( + std::vector> &&processors, + const resource::Resource &resource, + std::unique_ptr sampler, + std::unique_ptr id_generator, + std::unique_ptr> tracer_configurator) noexcept : resource_(resource), sampler_(std::move(sampler)), id_generator_(std::move(id_generator)), - processor_(std::unique_ptr(new MultiSpanProcessor(std::move(processors)))) + processor_(std::unique_ptr(new MultiSpanProcessor(std::move(processors)))), + tracer_configurator_(std::move(tracer_configurator)) {} Sampler &TracerContext::GetSampler() const noexcept @@ -41,6 +44,11 @@ const resource::Resource &TracerContext::GetResource() const noexcept return resource_; } +ScopeConfigurator &TracerContext::GetTracerConfigurator() const noexcept +{ + return *tracer_configurator_; +} + opentelemetry::sdk::trace::IdGenerator &TracerContext::GetIdGenerator() const noexcept { return *id_generator_; diff --git a/sdk/src/trace/tracer_provider.cc b/sdk/src/trace/tracer_provider.cc index bda0ab2928..5824120422 100644 --- a/sdk/src/trace/tracer_provider.cc +++ b/sdk/src/trace/tracer_provider.cc @@ -16,6 +16,7 @@ #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/sdk/trace/sampler.h" #include "opentelemetry/sdk/trace/tracer.h" +#include "opentelemetry/sdk/trace/tracer_config.h" #include "opentelemetry/sdk/trace/tracer_context.h" #include "opentelemetry/sdk/trace/tracer_provider.h" #include "opentelemetry/trace/tracer.h" @@ -35,24 +36,30 @@ TracerProvider::TracerProvider(std::unique_ptr context) noexcept OTEL_INTERNAL_LOG_DEBUG("[TracerProvider] TracerProvider created."); } -TracerProvider::TracerProvider(std::unique_ptr processor, - const resource::Resource &resource, - std::unique_ptr sampler, - std::unique_ptr id_generator) noexcept +TracerProvider::TracerProvider( + std::unique_ptr processor, + const resource::Resource &resource, + std::unique_ptr sampler, + std::unique_ptr id_generator, + std::unique_ptr> tracer_configurator) noexcept { std::vector> processors; processors.push_back(std::move(processor)); - context_ = std::make_shared(std::move(processors), resource, std::move(sampler), - std::move(id_generator)); + context_ = + std::make_shared(std::move(processors), resource, std::move(sampler), + std::move(id_generator), std::move(tracer_configurator)); } -TracerProvider::TracerProvider(std::vector> &&processors, - const resource::Resource &resource, - std::unique_ptr sampler, - std::unique_ptr id_generator) noexcept +TracerProvider::TracerProvider( + std::vector> &&processors, + const resource::Resource &resource, + std::unique_ptr sampler, + std::unique_ptr id_generator, + std::unique_ptr> tracer_configurator) noexcept { - context_ = std::make_shared(std::move(processors), resource, std::move(sampler), - std::move(id_generator)); + context_ = + std::make_shared(std::move(processors), resource, std::move(sampler), + std::move(id_generator), std::move(tracer_configurator)); } TracerProvider::~TracerProvider() @@ -107,8 +114,11 @@ nostd::shared_ptr TracerProvider::GetTracer( instrumentationscope::InstrumentationScopeAttributes attrs_map(attributes); auto scope = instrumentationscope::InstrumentationScope::Create(name, version, schema_url, attrs_map); + const instrumentationscope::InstrumentationScope &scope_reference = + instrumentationscope::InstrumentationScope(*scope); + auto tracer_config = context_->GetTracerConfigurator().ComputeConfig(scope_reference); - auto tracer = std::shared_ptr(new Tracer(context_, std::move(scope))); + auto tracer = std::shared_ptr(new Tracer(context_, std::move(scope), tracer_config)); tracers_.push_back(tracer); return nostd::shared_ptr{tracer}; } @@ -133,6 +143,12 @@ bool TracerProvider::ForceFlush(std::chrono::microseconds timeout) noexcept return context_->ForceFlush(timeout); } +TracerConfig TracerProvider::GetTracerConfig( + const InstrumentationScope &instrumentation_scope) const +{ + return context_->GetTracerConfigurator().ComputeConfig(instrumentation_scope); +} + } // namespace trace } // namespace sdk OPENTELEMETRY_END_NAMESPACE