Skip to content

Commit 9e8e4b7

Browse files
committed
otel-metricfilter-fn
1 parent d19eb32 commit 9e8e4b7

File tree

15 files changed

+604
-23
lines changed

15 files changed

+604
-23
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Increment the:
2121
* [SDK] Fix instrumentation scope attributes evaluated in equal method
2222
[#3214](https://github.com/open-telemetry/opentelemetry-cpp/pull/3214)
2323

24+
* [SDK] Implement spec: MetricFilter
25+
[#3235](https://github.com/open-telemetry/opentelemetry-cpp/pull/3235)
26+
2427
* [EXPORTER] Fix scope attributes missing from otlp traces metrics
2528
[#3185](https://github.com/open-telemetry/opentelemetry-cpp/pull/3185)
2629

sdk/BUILD

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

sdk/include/opentelemetry/sdk/metrics/export/metric_producer.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
#include <utility>
77
#include <vector>
88

9+
#include "absl/types/optional.h"
910
#include "opentelemetry/nostd/function_ref.h"
1011
#include "opentelemetry/nostd/variant.h"
1112
#include "opentelemetry/sdk/metrics/data/metric_data.h"
13+
#include "opentelemetry/sdk/metrics/export/metric_filter.h"
1214
#include "opentelemetry/version.h"
1315

1416
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -80,7 +82,8 @@ struct ResourceMetrics
8082
class MetricProducer
8183
{
8284
public:
83-
MetricProducer() = default;
85+
MetricProducer(absl::optional<MetricFilter> metric_filter = absl::nullopt)
86+
: metric_filter_(metric_filter) {};
8487
virtual ~MetricProducer() = default;
8588

8689
MetricProducer(const MetricProducer &) = delete;
@@ -107,6 +110,8 @@ class MetricProducer
107110
* partial failure.
108111
*/
109112
virtual Result Produce() noexcept = 0;
113+
114+
absl::optional<MetricFilter> metric_filter_;
110115
};
111116

112117
} // namespace metrics

sdk/include/opentelemetry/sdk/metrics/meter_context.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
#include <memory>
99
#include <vector>
1010

11+
#include "absl/types/optional.h"
1112
#include "opentelemetry/common/spin_lock_mutex.h"
1213
#include "opentelemetry/common/timestamp.h"
1314
#include "opentelemetry/nostd/function_ref.h"
1415
#include "opentelemetry/nostd/span.h"
1516
#include "opentelemetry/nostd/string_view.h"
17+
#include "opentelemetry/sdk/metrics/export/metric_filter.h"
1618
#include "opentelemetry/sdk/metrics/metric_reader.h"
1719
#include "opentelemetry/sdk/metrics/state/metric_collector.h"
1820
#include "opentelemetry/sdk/metrics/view/instrument_selector.h"
@@ -96,14 +98,20 @@ class MeterContext : public std::enable_shared_from_this<MeterContext>
9698
opentelemetry::common::SystemTimestamp GetSDKStartTime() noexcept;
9799

98100
/**
99-
* Attaches a metric reader to list of configured readers for this Meter context.
100-
* @param reader The metric reader for this meter context. This
101-
* must not be a nullptr.
101+
* Create a MetricCollector from a MetricReader using this MeterContext and add it to the list of
102+
* configured collectors.
103+
* @param reader The MetricReader for which a MetricCollector is to be created. This must not be a
104+
* nullptr.
105+
* @param metric_filter The optional MetricFilter used when creating the MetricCollector.
106+
* @return The MetricCollector created.
102107
*
103108
* Note: This reader may not receive any in-flight meter data, but will get newly created meter
104-
* data. Note: This method is not thread safe, and should ideally be called from main thread.
109+
* data.
110+
* Note: This method is not thread safe, and should ideally be called from main thread.
105111
*/
106-
void AddMetricReader(std::shared_ptr<MetricReader> reader) noexcept;
112+
std::shared_ptr<MetricCollector> AddMetricReader(
113+
std::shared_ptr<MetricReader> reader,
114+
absl::optional<MetricFilter> metric_filter = absl::nullopt) noexcept;
107115

108116
/**
109117
* Attaches a View to list of configured Views for this Meter context.

sdk/include/opentelemetry/sdk/metrics/meter_provider.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
#include <memory>
88
#include <mutex>
99

10+
#include "absl/types/optional.h"
1011
#include "opentelemetry/metrics/meter.h"
1112
#include "opentelemetry/metrics/meter_provider.h"
1213
#include "opentelemetry/nostd/shared_ptr.h"
1314
#include "opentelemetry/nostd/string_view.h"
15+
#include "opentelemetry/sdk/metrics/export/metric_filter.h"
1416
#include "opentelemetry/sdk/metrics/meter_context.h"
1517
#include "opentelemetry/sdk/metrics/metric_reader.h"
1618
#include "opentelemetry/sdk/metrics/view/instrument_selector.h"
@@ -79,14 +81,20 @@ class OPENTELEMETRY_EXPORT MeterProvider final : public opentelemetry::metrics::
7981
const sdk::resource::Resource &GetResource() const noexcept;
8082

8183
/**
82-
* Attaches a metric reader to list of configured readers for this Meter providers.
83-
* @param reader The metric reader for this meter provider. This
84-
* must not be a nullptr.
84+
* Create a MetricCollector from a MetricReader using the MeterContext of this MeterProvider and
85+
* add it to the list of configured collectors.
86+
* @param reader The MetricReader for which a MetricCollector is to be created. This must not be a
87+
* nullptr.
88+
* @param metric_filter The optional MetricFilter used when creating the MetricCollector.
89+
* @return The MetricCollector created.
8590
*
8691
* Note: This reader may not receive any in-flight meter data, but will get newly created meter
87-
* data. Note: This method is not thread safe, and should ideally be called from main thread.
92+
* data.
93+
* Note: This method is not thread safe, and should ideally be called from main thread.
8894
*/
89-
void AddMetricReader(std::shared_ptr<MetricReader> reader) noexcept;
95+
std::shared_ptr<MetricCollector> AddMetricReader(
96+
std::shared_ptr<MetricReader> reader,
97+
absl::optional<MetricFilter> metric_filter = absl::nullopt) noexcept;
9098

9199
/**
92100
* Attaches a View to list of configured Views for this Meter provider.

sdk/include/opentelemetry/sdk/metrics/state/metric_collector.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
#include <chrono>
77
#include <memory>
88

9+
#include "absl/types/optional.h"
910
#include "opentelemetry/nostd/function_ref.h"
11+
#include "opentelemetry/sdk/metrics/export/metric_filter.h"
1012
#include "opentelemetry/sdk/metrics/export/metric_producer.h"
1113
#include "opentelemetry/sdk/metrics/instruments.h"
1214
#include "opentelemetry/sdk/metrics/metric_reader.h"
@@ -40,7 +42,9 @@ class CollectorHandle
4042
class MetricCollector : public MetricProducer, public CollectorHandle
4143
{
4244
public:
43-
MetricCollector(MeterContext *context, std::shared_ptr<MetricReader> metric_reader);
45+
MetricCollector(MeterContext *context,
46+
std::shared_ptr<MetricReader> metric_reader,
47+
absl::optional<MetricFilter> metric_filter = absl::nullopt);
4448

4549
~MetricCollector() override = default;
4650

@@ -62,6 +66,7 @@ class MetricCollector : public MetricProducer, public CollectorHandle
6266
private:
6367
MeterContext *meter_context_;
6468
std::shared_ptr<MetricReader> metric_reader_;
69+
absl::optional<MetricFilter> metric_filter_;
6570
};
6671
} // namespace metrics
6772
} // namespace sdk

sdk/src/metrics/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ cc_library(
1313
"//sdk/src/common:global_log_handler",
1414
"//sdk/src/common:random",
1515
"//sdk/src/resource",
16+
"@com_google_absl//absl/types:optional",
1617
],
1718
)

sdk/src/metrics/meter_context.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <utility>
1111
#include <vector>
1212

13+
#include "absl/types/optional.h"
1314
#include "opentelemetry/common/spin_lock_mutex.h"
1415
#include "opentelemetry/common/timestamp.h"
1516
#include "opentelemetry/nostd/function_ref.h"
@@ -79,10 +80,14 @@ opentelemetry::common::SystemTimestamp MeterContext::GetSDKStartTime() noexcept
7980
return sdk_start_ts_;
8081
}
8182

82-
void MeterContext::AddMetricReader(std::shared_ptr<MetricReader> reader) noexcept
83+
std::shared_ptr<MetricCollector> MeterContext::AddMetricReader(
84+
std::shared_ptr<MetricReader> reader,
85+
absl::optional<MetricFilter> metric_filter) noexcept
8386
{
84-
auto collector = std::shared_ptr<MetricCollector>{new MetricCollector(this, std::move(reader))};
87+
auto collector =
88+
std::shared_ptr<MetricCollector>{new MetricCollector(this, std::move(reader), metric_filter)};
8589
collectors_.push_back(collector);
90+
return collector;
8691
}
8792

8893
void MeterContext::AddView(std::unique_ptr<InstrumentSelector> instrument_selector,

sdk/src/metrics/meter_provider.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
#include <mutex>
66
#include <utility>
77

8+
#include "absl/types/optional.h"
89
#include "opentelemetry/common/key_value_iterable.h"
910
#include "opentelemetry/metrics/meter.h"
1011
#include "opentelemetry/nostd/shared_ptr.h"
1112
#include "opentelemetry/nostd/span.h"
1213
#include "opentelemetry/nostd/string_view.h"
1314
#include "opentelemetry/sdk/common/global_log_handler.h"
1415
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
16+
#include "opentelemetry/sdk/metrics/export/metric_filter.h"
17+
#include "opentelemetry/sdk/metrics/export/metric_producer.h"
1518
#include "opentelemetry/sdk/metrics/meter.h"
1619
#include "opentelemetry/sdk/metrics/meter_context.h"
1720
#include "opentelemetry/sdk/metrics/meter_provider.h"
@@ -107,9 +110,11 @@ const resource::Resource &MeterProvider::GetResource() const noexcept
107110
return context_->GetResource();
108111
}
109112

110-
void MeterProvider::AddMetricReader(std::shared_ptr<MetricReader> reader) noexcept
113+
std::shared_ptr<MetricCollector> MeterProvider::AddMetricReader(
114+
std::shared_ptr<MetricReader> reader,
115+
absl::optional<MetricFilter> metric_filter) noexcept
111116
{
112-
context_->AddMetricReader(std::move(reader));
117+
return context_->AddMetricReader(std::move(reader), metric_filter);
113118
}
114119

115120
void MeterProvider::AddView(std::unique_ptr<InstrumentSelector> instrument_selector,

0 commit comments

Comments
 (0)