|
| 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 | +template <class T> |
| 19 | +using ScopeConfigurator = std::function<T(const InstrumentationScope &)>; |
| 20 | + |
| 21 | +template <typename T> |
| 22 | +class ScopeConfiguratorBuilder |
| 23 | +{ |
| 24 | +public: |
| 25 | + /** |
| 26 | + * Constructor for a builder object that cam be used to create a scope configurator. A minimally |
| 27 | + * configured builder would build a ScopeConfigurator that applies the default_scope_config to |
| 28 | + * every instrumentation scope. |
| 29 | + * @param default_scope_config The default scope config that the built configurator should fall |
| 30 | + * back on. |
| 31 | + */ |
| 32 | + explicit ScopeConfiguratorBuilder(T default_scope_config) noexcept |
| 33 | + : default_scope_config_(default_scope_config) |
| 34 | + {} |
| 35 | + |
| 36 | + /** |
| 37 | + * Allows the user to pass a generic function that evaluates an instrumentation scope through a |
| 38 | + * boolean check. If the check passes, the provided config is applied. Conditions are evaluated in |
| 39 | + * order. |
| 40 | + * @param scope_matcher a function that returns true if the scope being evaluated matches the |
| 41 | + * criteria defined by the function. |
| 42 | + * @param scope_config the scope configuration to return for the matched scope. |
| 43 | + * @return this |
| 44 | + */ |
| 45 | + ScopeConfiguratorBuilder<T> AddCondition( |
| 46 | + std::function<bool(const InstrumentationScope &)> scope_matcher, |
| 47 | + T scope_config) |
| 48 | + { |
| 49 | + conditions_.push_back(ScopeConfiguratorBuilder::Condition(scope_matcher, scope_config)); |
| 50 | + return this; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * A convenience condition that specifically matches the scope name of the scope being evaluated. |
| 55 | + * If the scope name matches to the provided string, then the provided scope configuration is |
| 56 | + * applied to the scope. |
| 57 | + * @param scope_name The scope name to which the config needs to be applied. |
| 58 | + * @param scope_config The scope config for the matching scopes. |
| 59 | + * @return this |
| 60 | + */ |
| 61 | + ScopeConfiguratorBuilder<T> AddConditionNameEquals(nostd::string_view scope_name, T scope_config) |
| 62 | + { |
| 63 | + auto name_equals_matcher = [scope_name](const InstrumentationScope &scope_info) { |
| 64 | + return scope_info.GetName() == scope_name; |
| 65 | + }; |
| 66 | + conditions_.push_back(ScopeConfiguratorBuilder::Condition(name_equals_matcher, scope_config)); |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Constructs the scope configurator object that can be used to retrieve scope config depending on |
| 72 | + * the instrumentation scope. |
| 73 | + * @return a configured scope configurator. |
| 74 | + */ |
| 75 | + ScopeConfigurator<T> Build() |
| 76 | + { |
| 77 | + if (conditions_.size() == 0) |
| 78 | + { |
| 79 | + return [default_scope_config_ = this->default_scope_config_](const InstrumentationScope &) { |
| 80 | + return default_scope_config_; |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + // Return a configurator that processes all the conditions |
| 85 | + return [conditions_ = this->conditions_, default_scope_config_ = this->default_scope_config_]( |
| 86 | + const InstrumentationScope &scope_info) { |
| 87 | + for (Condition condition : conditions_) |
| 88 | + { |
| 89 | + if (condition.scope_matcher(scope_info)) |
| 90 | + { |
| 91 | + return condition.scope_config; |
| 92 | + } |
| 93 | + } |
| 94 | + return default_scope_config_; |
| 95 | + }; |
| 96 | + } |
| 97 | + |
| 98 | +private: |
| 99 | + /** |
| 100 | + * An internal struct to encapsulate 'conditions' that can be applied to a |
| 101 | + * ScopeConfiguratorBuilder. The applied conditions influence the behavior of the generatred |
| 102 | + * ScopeConfigurator. |
| 103 | + */ |
| 104 | + struct Condition |
| 105 | + { |
| 106 | + std::function<bool(const InstrumentationScope &)> scope_matcher; |
| 107 | + T scope_config; |
| 108 | + }; |
| 109 | + |
| 110 | + T default_scope_config_; |
| 111 | + std::vector<Condition> conditions_; |
| 112 | +}; |
| 113 | +} // namespace instrumentationscope |
| 114 | +} // namespace sdk |
| 115 | +OPENTELEMETRY_END_NAMESPACE |
0 commit comments