|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#include <functional> |
| 7 | +#include <memory> |
| 8 | + |
| 9 | +#include "opentelemetry/nostd/string_view.h" |
| 10 | +#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h" |
| 11 | +#include "opentelemetry/sdk/metrics/data/metric_data.h" |
| 12 | +#include "opentelemetry/sdk/metrics/instruments.h" |
| 13 | + |
| 14 | +OPENTELEMETRY_BEGIN_NAMESPACE |
| 15 | +namespace sdk |
| 16 | +{ |
| 17 | +namespace metrics |
| 18 | +{ |
| 19 | + |
| 20 | +/** |
| 21 | + * MetricFilter defines the interface which enables the MetricReader’s |
| 22 | + * registered MetricProducers or the SDK’s MetricProducer to filter aggregated |
| 23 | + * data points (Metric Points) inside its Produce operation. The filtering is |
| 24 | + * done at the MetricProducer for performance reasons. |
| 25 | + * |
| 26 | + * The MetricFilter allows filtering an entire metric stream - dropping or |
| 27 | + * allowing all its attribute sets - by its TestMetric operation, which accepts |
| 28 | + * the metric stream information (scope, name, kind and unit) and returns an |
| 29 | + * enumeration: kAccept, kDrop or kAcceptPartial. If the latter returned, the |
| 30 | + * TestAttributes operation is to be called per attribute set of that metric |
| 31 | + * stream, returning an enumeration determining if the data point for that |
| 32 | + * (metric stream, attributes) pair is to be allowed in the result of the |
| 33 | + * MetricProducer Produce operation. |
| 34 | + */ |
| 35 | +class MetricFilter |
| 36 | +{ |
| 37 | +public: |
| 38 | + enum class MetricFilterResult |
| 39 | + { |
| 40 | + kAccept, |
| 41 | + kDrop, |
| 42 | + kAcceptPartial, |
| 43 | + }; |
| 44 | + |
| 45 | + enum class AttributesFilterResult |
| 46 | + { |
| 47 | + kAccept, |
| 48 | + kDrop, |
| 49 | + }; |
| 50 | + |
| 51 | + using TestMetricFn = std::function<MetricFilterResult( |
| 52 | + const opentelemetry::sdk::instrumentationscope::InstrumentationScope &scope, |
| 53 | + opentelemetry::nostd::string_view name, |
| 54 | + const InstrumentType &type, |
| 55 | + opentelemetry::nostd::string_view unit)>; |
| 56 | + |
| 57 | + using TestAttributesFn = std::function<AttributesFilterResult( |
| 58 | + const opentelemetry::sdk::instrumentationscope::InstrumentationScope &scope, |
| 59 | + opentelemetry::nostd::string_view name, |
| 60 | + const InstrumentType &type, |
| 61 | + opentelemetry::nostd::string_view unit, |
| 62 | + const PointAttributes &attributes)>; |
| 63 | + |
| 64 | + // static |
| 65 | + static std::unique_ptr<MetricFilter> Create(TestMetricFn test_metric_fn, |
| 66 | + TestAttributesFn test_attributes_fn) |
| 67 | + { |
| 68 | + return std::make_unique<MetricFilter>(test_metric_fn, test_attributes_fn); |
| 69 | + } |
| 70 | + |
| 71 | + MetricFilter(TestMetricFn test_metric_fn, TestAttributesFn test_attributes_fn) |
| 72 | + : test_metric_fn_(test_metric_fn), test_attributes_fn_(test_attributes_fn) |
| 73 | + {} |
| 74 | + |
| 75 | + /** |
| 76 | + * TestMetric is called once for every metric stream, in each MetricProducer |
| 77 | + * Produce operation. |
| 78 | + */ |
| 79 | + MetricFilterResult TestMetric( |
| 80 | + const opentelemetry::sdk::instrumentationscope::InstrumentationScope &scope, |
| 81 | + opentelemetry::nostd::string_view name, |
| 82 | + const InstrumentType &type, |
| 83 | + opentelemetry::nostd::string_view unit) |
| 84 | + { |
| 85 | + return test_metric_fn_(scope, name, type, unit); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * TestAttributes determines for a given metric stream and attribute set if |
| 90 | + * it should be allowed or filtered out. |
| 91 | + * |
| 92 | + * This operation should only be called if TestMetric operation returned |
| 93 | + * kAcceptPartial for the given metric stream arguments. |
| 94 | + */ |
| 95 | + AttributesFilterResult TestAttributes( |
| 96 | + const opentelemetry::sdk::instrumentationscope::InstrumentationScope &scope, |
| 97 | + opentelemetry::nostd::string_view name, |
| 98 | + const InstrumentType &type, |
| 99 | + opentelemetry::nostd::string_view unit, |
| 100 | + const PointAttributes &attributes) |
| 101 | + { |
| 102 | + return test_attributes_fn_(scope, name, type, unit, attributes); |
| 103 | + } |
| 104 | + |
| 105 | +private: |
| 106 | + TestMetricFn test_metric_fn_; |
| 107 | + TestAttributesFn test_attributes_fn_; |
| 108 | +}; |
| 109 | + |
| 110 | +} // namespace metrics |
| 111 | +} // namespace sdk |
| 112 | +OPENTELEMETRY_END_NAMESPACE |
0 commit comments