Skip to main content

confluent_kafka/common/metrics/
metrics_reporter.rs

1// Copyright 2025 Confluent Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! A plugin interface to allow listening as new metrics are created so they can
16//! be reported (`org.apache.kafka.common.metrics.MetricsReporter`).
17
18use std::sync::Arc;
19
20use crate::common::metrics::KafkaMetric;
21
22/// A plugin interface to allow things to listen as new metrics are created so
23/// they can be reported.
24///
25/// Implement `ClusterResourceListener` to receive cluster metadata once it's
26/// available. Please see the class documentation for `ClusterResourceListener`
27/// for more information.
28///
29/// # Translation notes
30///
31/// WARNING: implementing `ClusterResourceListener` delivers no cluster
32/// metadata: no client takes a reporter, as `metric.reporters` is class-valued.
33// Java's `MetricsReporter extends Reconfigurable, AutoCloseable` and (via
34// `JmxReporter`) bridges to JMX, which is JVM-only and out of scope. Every
35// method has a no-op default, so a do-nothing reporter needs an empty `impl`.
36#[doc(alias = "org.apache.kafka.common.metrics.MetricsReporter")]
37pub trait MetricsReporter: Send + Sync {
38    /// This is called when the reporter is first registered to initially register
39    /// all existing metrics.
40    ///
41    /// # Arguments
42    ///
43    /// - `metrics` - All currently existing metrics
44    #[doc(alias = "org.apache.kafka.common.metrics.MetricsReporter#init")]
45    fn init(&self, metrics: &[Arc<KafkaMetric>]) {
46        let _ = metrics;
47    }
48
49    /// This is called whenever a metric is updated or added.
50    ///
51    /// # Arguments
52    ///
53    /// - `metric` - The metric that has been added or changed
54    #[doc(alias = "org.apache.kafka.common.metrics.MetricsReporter#metricChange")]
55    fn metric_change(&self, metric: &Arc<KafkaMetric>) {
56        let _ = metric;
57    }
58
59    /// This is called whenever a metric is removed.
60    ///
61    /// # Arguments
62    ///
63    /// - `metric` - The metric that has been removed
64    #[doc(alias = "org.apache.kafka.common.metrics.MetricsReporter#metricRemoval")]
65    fn metric_removal(&self, metric: &Arc<KafkaMetric>) {
66        let _ = metric;
67    }
68
69    /// Called when the metrics repository is closed.
70    #[doc(alias = "org.apache.kafka.common.metrics.MetricsReporter#close")]
71    fn close(&self) {}
72}