|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#pragma once |
| 5 | +#include <functional> |
| 6 | + |
| 7 | +#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h" |
| 8 | +#include "opentelemetry/version.h" |
| 9 | + |
| 10 | +OPENTELEMETRY_BEGIN_NAMESPACE |
| 11 | +namespace sdk |
| 12 | +{ |
| 13 | +namespace instrumentationscope |
| 14 | +{ |
| 15 | +/** |
| 16 | + * A scope configurator is a function that returns the scope config for a given instrumentation |
| 17 | + * scope. |
| 18 | + */ |
| 19 | +template <typename T> |
| 20 | +class ScopeConfigurator |
| 21 | +{ |
| 22 | +public: |
| 23 | + /** |
| 24 | + * A builder class for the ScopeConfigurator that facilitates the creation of ScopeConfigurators. |
| 25 | + */ |
| 26 | + class Builder |
| 27 | + { |
| 28 | + public: |
| 29 | + /** |
| 30 | + * Constructor for a builder object that cam be used to create a scope configurator. A minimally |
| 31 | + * configured builder would build a ScopeConfigurator that applies the default_scope_config to |
| 32 | + * every instrumentation scope. |
| 33 | + * @param default_scope_config The default scope config that the built configurator should fall |
| 34 | + * back on. |
| 35 | + */ |
| 36 | + explicit Builder(T default_scope_config) noexcept : default_scope_config_(default_scope_config) |
| 37 | + {} |
| 38 | + |
| 39 | + /** |
| 40 | + * Allows the user to pass a generic function that evaluates an instrumentation scope through a |
| 41 | + * boolean check. If the check passes, the provided config is applied. Conditions are evaluated |
| 42 | + * in order. |
| 43 | + * @param scope_matcher a function that returns true if the scope being evaluated matches the |
| 44 | + * criteria defined by the function. |
| 45 | + * @param scope_config the scope configuration to return for the matched scope. |
| 46 | + * @return this |
| 47 | + */ |
| 48 | + Builder &AddCondition(std::function<bool(const InstrumentationScope &)> scope_matcher, |
| 49 | + T scope_config) |
| 50 | + { |
| 51 | + conditions_.emplace_back(scope_matcher, scope_config); |
| 52 | + return *this; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * A convenience condition that specifically matches the scope name of the scope being |
| 57 | + * evaluated. If the scope name matches to the provided string, then the provided scope |
| 58 | + * configuration is applied to the scope. |
| 59 | + * @param scope_name The scope name to which the config needs to be applied. |
| 60 | + * @param scope_config The scope config for the matching scopes. |
| 61 | + * @return this |
| 62 | + */ |
| 63 | + Builder &AddConditionNameEquals(nostd::string_view scope_name, T scope_config) |
| 64 | + { |
| 65 | + std::function<bool(const InstrumentationScope &)> name_equals_matcher = |
| 66 | + [scope_name = std::string(scope_name)](const InstrumentationScope &scope_info) { |
| 67 | + return scope_info.GetName() == scope_name; |
| 68 | + }; |
| 69 | + conditions_.emplace_back(name_equals_matcher, scope_config); |
| 70 | + return *this; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Constructs the scope configurator object that can be used to retrieve scope config depending |
| 75 | + * on the instrumentation scope. |
| 76 | + * @return a configured scope configurator. |
| 77 | + */ |
| 78 | + ScopeConfigurator<T> Build() const |
| 79 | + { |
| 80 | + if (conditions_.size() == 0) |
| 81 | + { |
| 82 | + return ScopeConfigurator<T>( |
| 83 | + [default_scope_config_ = this->default_scope_config_](const InstrumentationScope &) { |
| 84 | + return default_scope_config_; |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + // Return a configurator that processes all the conditions |
| 89 | + return ScopeConfigurator<T>( |
| 90 | + [conditions_ = this->conditions_, default_scope_config_ = this->default_scope_config_]( |
| 91 | + const InstrumentationScope &scope_info) { |
| 92 | + for (Condition condition : conditions_) |
| 93 | + { |
| 94 | + if (condition.scope_matcher(scope_info)) |
| 95 | + { |
| 96 | + return condition.scope_config; |
| 97 | + } |
| 98 | + } |
| 99 | + return default_scope_config_; |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + private: |
| 104 | + /** |
| 105 | + * An internal struct to encapsulate 'conditions' that can be applied to a |
| 106 | + * ScopeConfiguratorBuilder. The applied conditions influence the behavior of the generated |
| 107 | + * ScopeConfigurator. |
| 108 | + */ |
| 109 | + struct Condition |
| 110 | + { |
| 111 | + std::function<bool(const InstrumentationScope &)> scope_matcher; |
| 112 | + T scope_config; |
| 113 | + |
| 114 | + Condition(const std::function<bool(const InstrumentationScope &)> &matcher, const T &config) |
| 115 | + : scope_matcher(matcher), scope_config(config) |
| 116 | + {} |
| 117 | + }; |
| 118 | + |
| 119 | + T default_scope_config_; |
| 120 | + std::vector<Condition> conditions_; |
| 121 | + }; |
| 122 | + |
| 123 | + // Public methods for ScopeConfigurator |
| 124 | + |
| 125 | + /** |
| 126 | + * Invokes the underlying configurator function to get a valid scope configuration. |
| 127 | + * @param scope_info The InstrumentationScope containing scope information for which configuration |
| 128 | + * needs to be retrieved. |
| 129 | + */ |
| 130 | + T ComputeConfig(const InstrumentationScope &scope_info) const |
| 131 | + { |
| 132 | + return this->configurator_(scope_info); |
| 133 | + } |
| 134 | + |
| 135 | +private: |
| 136 | + // Prevent direct initialization of ScopeConfigurator objects. |
| 137 | + explicit ScopeConfigurator(std::function<T(const InstrumentationScope &)> configurator) |
| 138 | + : configurator_(configurator) |
| 139 | + {} |
| 140 | + |
| 141 | + std::function<T(const InstrumentationScope &)> configurator_; |
| 142 | +}; |
| 143 | +} // namespace instrumentationscope |
| 144 | +} // namespace sdk |
| 145 | +OPENTELEMETRY_END_NAMESPACE |
0 commit comments