-
Notifications
You must be signed in to change notification settings - Fork 438
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
psx95
wants to merge
26
commits into
open-telemetry:main
Choose a base branch
from
psx95:add-scope-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
ae5cdf4
Add tracer_config as per spec
psx95 9117eff
Update formatting
psx95 127311b
TracerProvider uses TracerConfig as per spec
psx95 20cb4e9
Fix iwyu warnings generated
psx95 df36a54
Fix formatting
psx95 a80800e
Replace scope_configurator class with typedef
psx95 694f9c0
Update factories to accept TracerConfigurator
psx95 f815c3e
Fix IWYU warnings
psx95 9b89786
Add operator overload to check for equality
psx95 541c0eb
Add tests for TracerConfig
psx95 a67ea85
Fix formatting
psx95 bcd6e94
Fix memory leak issue in test
psx95 2e2afd4
Replace typedef with type alias
psx95 4de93cb
Infer trace_config from the context
psx95 cfd9730
Add tests for Tracer
psx95 47acb3f
Fix iwyu warnings
psx95 f7dde19
Add unit test for custom configurator
psx95 d87997d
Add documentation for the tracer_config.h
psx95 2f1ee38
Update Changelog
psx95 31c73a3
Address comments in tracer: fix logic
psx95 5f6a95f
Add scope_configurator_builder for better class structure
psx95 501fa47
ScopeConfigurator: make builder inner class
psx95 c2bf6f9
Fix iwyu warnings
psx95 47dd040
Add unit test for condition ordering
psx95 98ca2c3
Remove accidental changes from instrumentation_scope.h
psx95 be16f38
Merge branch 'main' into add-scope-config
psx95 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
sdk/include/opentelemetry/sdk/instrumentationscope/scope_configurator.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
#include <functional> | ||
|
||
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h" | ||
#include "opentelemetry/version.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace instrumentationscope | ||
{ | ||
/** | ||
* A scope configurator is a function that returns the scope config for a given instrumentation | ||
* scope. | ||
*/ | ||
template <typename T> | ||
class ScopeConfigurator | ||
{ | ||
public: | ||
/** | ||
* A builder class for the ScopeConfigurator that facilitates the creation of ScopeConfigurators. | ||
*/ | ||
class Builder | ||
{ | ||
public: | ||
/** | ||
* Constructor for a builder object that cam be used to create a scope configurator. A minimally | ||
* configured builder would build a ScopeConfigurator that applies the default_scope_config to | ||
* every instrumentation scope. | ||
* @param default_scope_config The default scope config that the built configurator should fall | ||
* back on. | ||
*/ | ||
explicit Builder(T default_scope_config) noexcept : default_scope_config_(default_scope_config) | ||
{} | ||
|
||
/** | ||
* Allows the user to pass a generic function that evaluates an instrumentation scope through a | ||
* boolean check. If the check passes, the provided config is applied. Conditions are evaluated | ||
* in order. | ||
* @param scope_matcher a function that returns true if the scope being evaluated matches the | ||
* criteria defined by the function. | ||
* @param scope_config the scope configuration to return for the matched scope. | ||
* @return this | ||
*/ | ||
Builder AddCondition(std::function<bool(const InstrumentationScope &)> scope_matcher, | ||
T scope_config) | ||
{ | ||
conditions_.push_back(Condition{scope_matcher, scope_config}); | ||
return *this; | ||
} | ||
|
||
/** | ||
* A convenience condition that specifically matches the scope name of the scope being | ||
* evaluated. If the scope name matches to the provided string, then the provided scope | ||
* configuration is applied to the scope. | ||
* @param scope_name The scope name to which the config needs to be applied. | ||
* @param scope_config The scope config for the matching scopes. | ||
* @return this | ||
*/ | ||
Builder AddConditionNameEquals(nostd::string_view scope_name, T scope_config) | ||
{ | ||
std::function<bool(const InstrumentationScope &)> name_equals_matcher = | ||
[scope_name](const InstrumentationScope &scope_info) { | ||
return scope_info.GetName() == scope_name; | ||
}; | ||
conditions_.push_back(Condition{name_equals_matcher, scope_config}); | ||
return *this; | ||
} | ||
|
||
/** | ||
* Constructs the scope configurator object that can be used to retrieve scope config depending | ||
* on the instrumentation scope. | ||
* @return a configured scope configurator. | ||
*/ | ||
ScopeConfigurator<T> Build() | ||
{ | ||
if (conditions_.size() == 0) | ||
{ | ||
return ScopeConfigurator<T>( | ||
[default_scope_config_ = this->default_scope_config_](const InstrumentationScope &) { | ||
return default_scope_config_; | ||
}); | ||
} | ||
|
||
// Return a configurator that processes all the conditions | ||
return ScopeConfigurator<T>( | ||
[conditions_ = this->conditions_, default_scope_config_ = this->default_scope_config_]( | ||
const InstrumentationScope &scope_info) { | ||
for (Condition condition : conditions_) | ||
{ | ||
if (condition.scope_matcher(scope_info)) | ||
{ | ||
return condition.scope_config; | ||
} | ||
} | ||
return default_scope_config_; | ||
}); | ||
} | ||
|
||
private: | ||
/** | ||
* An internal struct to encapsulate 'conditions' that can be applied to a | ||
* ScopeConfiguratorBuilder. The applied conditions influence the behavior of the generatred | ||
* ScopeConfigurator. | ||
*/ | ||
struct Condition | ||
{ | ||
std::function<bool(const InstrumentationScope &)> scope_matcher; | ||
T scope_config; | ||
}; | ||
|
||
T default_scope_config_; | ||
std::vector<Condition> conditions_; | ||
}; | ||
|
||
// Public methods for ScopeConfigurator | ||
|
||
/** | ||
* Invokes the underlying configurator function to get a valid scope configuration. | ||
* @param scope_info The InstrumentationScope containing scope information for which configuration | ||
* needs to be retrieved. | ||
*/ | ||
T ComputeConfig(const InstrumentationScope &scope_info) const | ||
{ | ||
return this->configurator_(scope_info); | ||
} | ||
|
||
private: | ||
// Prevent direct initialization of ScopeConfigurator objects. | ||
explicit ScopeConfigurator(std::function<T(const InstrumentationScope &)> configurator) | ||
: configurator_(configurator) | ||
{} | ||
|
||
std::function<T(const InstrumentationScope &)> configurator_; | ||
}; | ||
} // namespace instrumentationscope | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include "opentelemetry/version.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace trace | ||
{ | ||
/** | ||
* TracerConfig defines various configurable aspects of a Tracer's behavior. | ||
* This class should not be used directly to configure a Tracer's behavior, instead a | ||
* ScopeConfigurator should be used to compute the desired TracerConfig which can then be used to | ||
* configure a Tracer. | ||
*/ | ||
class TracerConfig | ||
{ | ||
public: | ||
bool operator==(const TracerConfig &other) const noexcept; | ||
|
||
/** | ||
* Returns if the Tracer is enabled or disabled. Tracers are enabled by default. | ||
* @return a boolean indicating if the Tracer is enabled. Defaults to true. | ||
*/ | ||
bool IsEnabled() const noexcept; | ||
|
||
/** | ||
* Returns a TracerConfig that represents a disabled Tracer. A disabled tracer behaves like a | ||
* no-op tracer. | ||
* @return a static constant TracerConfig that represents a disabled tracer. | ||
*/ | ||
static TracerConfig Disabled(); | ||
|
||
/** | ||
* Returns a TracerConfig that represents an enabled Tracer. | ||
* @return a static constant TracerConfig that represents an enabled tracer. | ||
*/ | ||
static TracerConfig Enabled(); | ||
|
||
/** | ||
* Returns a TracerConfig that represents a Tracer configured with the default behavior. | ||
* The default behavior is guided by the OpenTelemetry specification. | ||
* @return a static constant TracerConfig that represents a tracer configured with default | ||
* behavior. | ||
*/ | ||
static TracerConfig Default(); | ||
|
||
private: | ||
explicit TracerConfig(const bool disabled = false) : disabled_(disabled) {} | ||
bool disabled_; | ||
static const TracerConfig kDefaultConfig; | ||
static const TracerConfig kDisabledConfig; | ||
}; | ||
} // namespace trace | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.