Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK] Add tracer scope configurator #3137

Open
wants to merge 23 commits into
base: main
Choose a base branch
from

Conversation

psx95
Copy link
Contributor

@psx95 psx95 commented Nov 12, 2024

Re #2641

Adds scope configurator for Tracers.

Changes

  • Updates the Trace SDK to add support for TracerConfigurator as per the spec changes.
  • Adds a TracerConfig class that can be used to configure a tracer's behavior as per spec.

Some optional, good-to-have convenience functions were recommended by the spec to accommodate common use-cases - these have not been added in this PR.

See the TracerConfigurator spec for these recommendations. They should be added in a future PR.

For significant contributions please make sure you have completed the following items:

  • CHANGELOG.md updated for non-trivial changes
  • Unit tests have been added
  • Changes in public API reviewed

Copy link

netlify bot commented Nov 12, 2024

Deploy Preview for opentelemetry-cpp-api-docs canceled.

Name Link
🔨 Latest commit 2329279
🔍 Latest deploy log https://app.netlify.com/sites/opentelemetry-cpp-api-docs/deploys/673ba561a4a91f00082fc899

Copy link

codecov bot commented Nov 12, 2024

Codecov Report

Attention: Patch coverage is 86.04651% with 6 lines in your changes missing coverage. Please review.

Project coverage is 87.81%. Comparing base (a388e87) to head (2329279).

Files with missing lines Patch % Lines
sdk/src/trace/tracer_provider_factory.cc 50.00% 6 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3137      +/-   ##
==========================================
- Coverage   87.83%   87.81%   -0.02%     
==========================================
  Files         195      197       +2     
  Lines        6146     6176      +30     
==========================================
+ Hits         5398     5423      +25     
- Misses        748      753       +5     
Files with missing lines Coverage Δ
...y/sdk/instrumentationscope/instrumentation_scope.h 100.00% <ø> (ø)
sdk/include/opentelemetry/sdk/trace/tracer.h 100.00% <ø> (ø)
...dk/include/opentelemetry/sdk/trace/tracer_config.h 100.00% <100.00%> (ø)
...k/include/opentelemetry/sdk/trace/tracer_context.h 100.00% <ø> (ø)
sdk/src/trace/tracer.cc 83.34% <100.00%> (+0.88%) ⬆️
sdk/src/trace/tracer_config.cc 100.00% <100.00%> (ø)
sdk/src/trace/tracer_context.cc 84.00% <100.00%> (+2.19%) ⬆️
sdk/src/trace/tracer_provider.cc 89.14% <100.00%> (ø)
sdk/src/trace/tracer_provider_factory.cc 46.16% <50.00%> (+1.33%) ⬆️
---- 🚨 Try these New Features:

@psx95 psx95 force-pushed the add-scope-config branch 2 times, most recently from fee7f7c to 15274c0 Compare November 15, 2024 00:09
@psx95 psx95 changed the title [WIP] Add scope config [WIP] Add tracer scope configurator Nov 16, 2024
@psx95 psx95 force-pushed the add-scope-config branch 8 times, most recently from 402ba99 to 93950bf Compare November 18, 2024 19:13
@psx95 psx95 changed the title [WIP] Add tracer scope configurator [SDK] Add tracer scope configurator Nov 18, 2024
@psx95 psx95 marked this pull request as ready for review November 18, 2024 21:09
@psx95 psx95 requested a review from a team as a code owner November 18, 2024 21:09
@marcalff marcalff self-requested a review November 18, 2024 21:46
@@ -103,8 +105,10 @@ class Tracer final : public opentelemetry::trace::Tracer,
private:
// order of declaration is important here - instrumentation scope should destroy after
// tracer-context.
std::unique_ptr<TracerConfig> tracer_config_;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace with:

TracerConfig tracer_config_;

There is no need to allocate memory and use a unique pointer, the tracer can own it's own configuration directly.

Comment on lines +41 to +42
const std::shared_ptr<opentelemetry::trace::NoopTracer> Tracer::kNoopTracer =
std::make_shared<opentelemetry::trace::NoopTracer>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a shared_ptr ?

This memory will never be reclaimed.

This should be enough:

opentelemetry::trace::NoopTracer kNoopTracer();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to avoid making multiple copies of NoopTracer, but looking at it again, shared_ptr might actually have more overhead due to ref counts.

Comment on lines +52 to +60
/**
* Returns a ScopeConfigurator that can be used to get the default tracer configurator.
* A tracer configurator computes TracerConfig based on the InstrumentationScope. The default
* tracer configurator unconditionally computes the default TracerConfig for all scopes.
*
* @return a static constant TracerConfig that represents a tracer configurator with default
* behavior.
*/
static const instrumentationscope::ScopeConfigurator<TracerConfig> &DefaultConfigurator();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To move in a separate class.

A TracerConfig represents a configuration.

This class should not know how / when / by whom a configuration is created, and should not even know of ScopeConfigurator.

bool disabled_;
static const TracerConfig kDefaultConfig;
static const TracerConfig kDisabledConfig;
static const instrumentationscope::ScopeConfigurator<TracerConfig> kDefaultTracerConfigurator;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove kDefaultTracerConfigurator.

Comment on lines +162 to +163
template <class T>
using ScopeConfigurator = std::function<T(const InstrumentationScope &)>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of comments.

(1)

ScopeConfigurator should be an abstract class, with a virtual configure() method.

This will help to define different kinds of configurators later, including the various builtins helpers.

(2)

One of the configurators is the always enabled one, so implement a AlwaysOnScopeConfigurator sub class.

Have a static AlwaysOnScopeConfigurator::Create() method, to return an instance.

(3)

Likewise, AlwaysOffScopeConfigurator::Create()

(4)

All this should be in dedicated headers, so instrumentation_scope.h should be unchanged.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I had a class before, but removed since it was easily replaceable by a function. I was expecting to put the built-in helpers in a static utility class, but this approach might look better.

I will give this a try.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the mean time, the spec configuration repository is defining the yaml bits for this:

Instead of "AlwaysOn" / "AlwaysOff", the following is probably better:

class ScopeConfigurator;
class DefaultScopeConfigurator : public ScopeConfigurator;

and later

class NamedScopeConfigurator : public ScopeConfigurator; (for name matching)

Comment on lines +50 to +52
std::unique_ptr<instrumentationscope::ScopeConfigurator<TracerConfig>> tracer_configurator =
std::make_unique<instrumentationscope::ScopeConfigurator<TracerConfig>>(
TracerConfig::DefaultConfigurator())) noexcept;
Copy link
Member

@marcalff marcalff Nov 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace with (assuming the default is enabled):

tracer_configurator = AlwaysOnScopeConfigurator::Create();

Likewise in other places.

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

instrumentationscope::ScopeConfigurator<TracerConfig> &GetTracerConfigurator() const noexcept;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type should probably be a const reference, not just a reference.

Comment on lines +61 to +64
if (!tracer_config_->IsEnabled())
{
return kNoopTracer->StartSpan(name, attributes, links, options);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my understanding, this should be moved earlier, not done only for the SpanContext alternative.

If the tracer is disabled, it is disabled for all types of spans.

static const instrumentationscope::ScopeConfigurator<TracerConfig> &DefaultConfigurator();

private:
explicit TracerConfig(const bool disabled = false) : disabled_(disabled) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use default values = false.

The choice of the default value should not be nested deep in the code, but bubble up in the call stack, up to where the decision is made to use a default.

This will be in the tracer provider factory, when no scope configurator is provided.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a private constructor, so it cannot be called externally and the value of the default is dictated by the spec.

Copy link
Member

@marcalff marcalff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch.

This is a very good start, see comments to refine the structure around TracerConfig + ScopeConfigurator.

@psx95
Copy link
Contributor Author

psx95 commented Nov 21, 2024

Thanks for the detailed review, I will go over the suggestions and make necessary changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants