Skip to content

Commit

Permalink
TracerProvider uses TracerConfig as per spec
Browse files Browse the repository at this point in the history
  • Loading branch information
psx95 committed Nov 13, 2024
1 parent 73df243 commit fd0178c
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <functional>
#include "instrumentation_scope.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace instrumentationscope
{
template <typename T>
class ScopeConfigurator
{
public:
static nostd::unique_ptr<ScopeConfigurator> Create(
std::function<T(const InstrumentationScope &)> scope_configurator)
{
return nostd::unique_ptr<ScopeConfigurator>(new ScopeConfigurator(scope_configurator));
}

T ComputeConfig(const InstrumentationScope &instrumentation_scope)
{
return scope_configurator_(instrumentation_scope);
}

private:
explicit ScopeConfigurator(
const std::function<T(const InstrumentationScope &)> scope_configurator)
: scope_configurator_(scope_configurator)
{}
const std::function<T(const InstrumentationScope &)> scope_configurator_;
};
} // namespace instrumentationscope
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
3 changes: 2 additions & 1 deletion sdk/include/opentelemetry/sdk/trace/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -108,6 +108,7 @@ class Tracer final : public opentelemetry::trace::Tracer,
std::shared_ptr<InstrumentationScope> instrumentation_scope_;
std::shared_ptr<TracerContext> context_;
TracerConfig tracer_config_;
static const std::shared_ptr<opentelemetry::trace::NoopTracer> kNoopTracer;
};
} // namespace trace
} // namespace sdk
Expand Down
9 changes: 6 additions & 3 deletions sdk/include/opentelemetry/sdk/trace/tracer_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#pragma once

#include "opentelemetry/sdk/instrumentationscope/scope_configurator.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
Expand All @@ -25,13 +26,15 @@ class TracerConfig
static TracerConfig Disabled();
static TracerConfig Enabled();
static TracerConfig Default();
static const instrumentationscope::ScopeConfigurator<TracerConfig> &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<TracerConfig> kDefaultTracerConfigurator;
};
} // namespace trace
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
OPENTELEMETRY_END_NAMESPACE
12 changes: 11 additions & 1 deletion sdk/include/opentelemetry/sdk/trace/tracer_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
#include <memory>
#include <vector>

#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
Expand All @@ -21,6 +23,8 @@ namespace sdk
namespace trace
{

using namespace opentelemetry::sdk::instrumentationscope;

/**
* A class which stores the TracerProvider context.
*
Expand All @@ -43,7 +47,10 @@ class TracerContext
opentelemetry::sdk::resource::Resource::Create({}),
std::unique_ptr<Sampler> sampler = std::unique_ptr<AlwaysOnSampler>(new AlwaysOnSampler),
std::unique_ptr<IdGenerator> id_generator =
std::unique_ptr<IdGenerator>(new RandomIdGenerator())) noexcept;
std::unique_ptr<IdGenerator>(new RandomIdGenerator()),
std::unique_ptr<ScopeConfigurator<TracerConfig>> tracer_configurator =
std::make_unique<ScopeConfigurator<TracerConfig>>(
TracerConfig::DefaultConfigurator())) noexcept;

virtual ~TracerContext() = default;

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

ScopeConfigurator<TracerConfig> &GetTracerConfigurator() const noexcept;

/**
* Obtain the Id Generator associated with this tracer context.
* @return The ID Generator for this tracer context.
Expand All @@ -100,6 +109,7 @@ class TracerContext
std::unique_ptr<Sampler> sampler_;
std::unique_ptr<IdGenerator> id_generator_;
std::unique_ptr<SpanProcessor> processor_;
std::unique_ptr<ScopeConfigurator<TracerConfig>> tracer_configurator_;
};

} // namespace trace
Expand Down
26 changes: 16 additions & 10 deletions sdk/include/opentelemetry/sdk/trace/tracer_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -27,6 +28,8 @@ namespace sdk
namespace trace
{

using namespace opentelemetry::sdk::instrumentationscope;

class OPENTELEMETRY_EXPORT TracerProvider final : public opentelemetry::trace::TracerProvider
{
public:
Expand All @@ -39,30 +42,30 @@ 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<SpanProcessor> processor,
const opentelemetry::sdk::resource::Resource &resource =
opentelemetry::sdk::resource::Resource::Create({}),
std::unique_ptr<Sampler> sampler = std::unique_ptr<AlwaysOnSampler>(new AlwaysOnSampler),
std::unique_ptr<IdGenerator> id_generator =
std::unique_ptr<IdGenerator>(new RandomIdGenerator())) noexcept;
std::unique_ptr<IdGenerator>(new RandomIdGenerator()),
std::unique_ptr<ScopeConfigurator<TracerConfig>> tracer_configurator =
std::make_unique<ScopeConfigurator<TracerConfig>>(
TracerConfig::DefaultConfigurator())) noexcept;

explicit TracerProvider(
std::vector<std::unique_ptr<SpanProcessor>> &&processors,
const opentelemetry::sdk::resource::Resource &resource =
opentelemetry::sdk::resource::Resource::Create({}),
std::unique_ptr<Sampler> sampler = std::unique_ptr<AlwaysOnSampler>(new AlwaysOnSampler),
std::unique_ptr<IdGenerator> id_generator =
std::unique_ptr<IdGenerator>(new RandomIdGenerator())) noexcept;

// explicit TracerProvider(
// std::vector<std::unique_ptr<SpanProcessor>> &&processors,
// const opentelemetry::sdk::resource::Resource &resource =
// opentelemetry::sdk::resource::Resource::Create({}),
// std::unique_ptr<Sampler> sampler = std::unique_ptr<AlwaysOnSampler>(new AlwaysOnSampler),
// std::unique_ptr<IdGenerator> id_generator =
// std::unique_ptr<IdGenerator>(new RandomIdGenerator())) noexcept;
std::unique_ptr<IdGenerator>(new RandomIdGenerator()),
std::unique_ptr<ScopeConfigurator<TracerConfig>> tracer_configurator =
std::make_unique<ScopeConfigurator<TracerConfig>>(
TracerConfig::DefaultConfigurator())) noexcept;

/**
* Initialize a new tracer provider with a specified context
Expand Down Expand Up @@ -121,6 +124,9 @@ class OPENTELEMETRY_EXPORT TracerProvider final : public opentelemetry::trace::T
std::vector<std::shared_ptr<Tracer>> tracers_;
std::shared_ptr<TracerContext> 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
Expand Down
3 changes: 1 addition & 2 deletions sdk/src/trace/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions sdk/src/trace/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <stdint.h>
#include <chrono>
#include <map>
#include <new>
#include <utility>

Expand All @@ -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"
Expand All @@ -36,6 +34,8 @@ namespace sdk
{
namespace trace
{
const std::shared_ptr<opentelemetry::trace::NoopTracer> Tracer::kNoopTracer =
std::make_shared<opentelemetry::trace::NoopTracer>();

Tracer::Tracer(std::shared_ptr<TracerContext> context,
std::unique_ptr<InstrumentationScope> instrumentation_scope,
Expand All @@ -54,6 +54,10 @@ nostd::shared_ptr<opentelemetry::trace::Span> Tracer::StartSpan(
opentelemetry::trace::SpanContext parent_context = GetCurrentSpan()->GetContext();
if (nostd::holds_alternative<opentelemetry::trace::SpanContext>(options.parent))
{
if (!tracer_config_.IsEnabled())
{
return kNoopTracer->StartSpan(name, attributes, links, options);
}
auto span_context = nostd::get<opentelemetry::trace::SpanContext>(options.parent);
if (span_context.IsValid())
{
Expand Down
21 changes: 15 additions & 6 deletions sdk/src/trace/tracer_config.cc
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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>
TracerConfig::kDefaultTracerConfigurator =
*instrumentationscope::ScopeConfigurator<TracerConfig>::Create(
[](const instrumentationscope::InstrumentationScope &) { return Default(); });

TracerConfig TracerConfig::Disabled()
{
Expand All @@ -28,10 +32,15 @@ TracerConfig TracerConfig::Default()
return kDefaultConfig;
}

const instrumentationscope::ScopeConfigurator<TracerConfig> &TracerConfig::DefaultConfigurator()
{
return kDefaultTracerConfigurator;
}

bool TracerConfig::IsEnabled() const noexcept
{
return !disabled_;
}
} // namespace trace
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
OPENTELEMETRY_END_NAMESPACE
18 changes: 13 additions & 5 deletions sdk/src/trace/tracer_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ namespace trace
{
namespace resource = opentelemetry::sdk::resource;

TracerContext::TracerContext(std::vector<std::unique_ptr<SpanProcessor>> &&processors,
const resource::Resource &resource,
std::unique_ptr<Sampler> sampler,
std::unique_ptr<IdGenerator> id_generator) noexcept
TracerContext::TracerContext(
std::vector<std::unique_ptr<SpanProcessor>> &&processors,
const resource::Resource &resource,
std::unique_ptr<Sampler> sampler,
std::unique_ptr<IdGenerator> id_generator,
std::unique_ptr<ScopeConfigurator<TracerConfig>> tracer_configurator) noexcept
: resource_(resource),
sampler_(std::move(sampler)),
id_generator_(std::move(id_generator)),
processor_(std::unique_ptr<SpanProcessor>(new MultiSpanProcessor(std::move(processors))))
processor_(std::unique_ptr<SpanProcessor>(new MultiSpanProcessor(std::move(processors)))),
tracer_configurator_(std::move(tracer_configurator))
{}

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

ScopeConfigurator<TracerConfig> &TracerContext::GetTracerConfigurator() const noexcept
{
return *tracer_configurator_;
}

opentelemetry::sdk::trace::IdGenerator &TracerContext::GetIdGenerator() const noexcept
{
return *id_generator_;
Expand Down
Loading

0 comments on commit fd0178c

Please sign in to comment.