Skip to main content

relay_kafka/
statsd.rs

1//! Kafka producer metrics.
2//!
3//! ## Producer Name Tag
4//!
5//! Many metrics include a `producer_name` tag to identify the deployment or application instance
6//! producing messages. This tag is derived using the following fallback strategy:
7//!
8//! 1. **`client.id`** - If explicitly configured in Kafka config parameters
9//! 2. **Config name** - The name of the secondary Kafka config (e.g., "profiles_cluster", "cluster1")  
10//! 3. **`"unknown"`** - Final fallback for the default Kafka configuration
11//!
12//! Example configurations:
13//! ```yaml
14//! # Explicit client.id (highest priority)
15//! secondary_kafka_configs:
16//!   profiles_cluster:
17//!     - name: "client.id"
18//!       value: "relay-profiles-prod"
19//! # Results in: producer_name="relay-profiles-prod"
20//!
21//! # Config name fallback
22//! secondary_kafka_configs:
23//!   cluster1:
24//!     - name: "bootstrap.servers"
25//!       value: "kafka:9092"
26//! # Results in: producer_name="cluster1"
27//!
28//! # Default config fallback
29//! kafka:
30//!   - name: "bootstrap.servers"
31//!     value: "kafka:9092"
32//! # Results in: producer_name="unknown"
33//! ```
34
35use relay_statsd::{CounterMetric, DistributionMetric};
36
37pub enum KafkaCounters {
38    /// Number of messages that were written to the wrong partition because of configured rate limits.
39    ///
40    /// Each topic in Relay can optionally be configured with a per-partition-key rate limit. This
41    /// rate limit does not drop messages, but instead disables semantic partitioning. Everytime
42    /// this happens for a message, this counter is incremented.
43    ///
44    /// This metric is tagged with:
45    /// - `topic`: The Kafka topic being produced to.
46    /// - `variant`: The Kafka message variant.
47    /// - `producer_name`: The configured producer name/deployment identifier.
48    ProducerPartitionKeyRateLimit,
49
50    /// Number of messages successfully produced to Kafka brokers.
51    ///
52    /// This metric is tagged with:
53    /// - `topic`: The Kafka topic produced to.
54    /// - `producer_name`: The configured producer name/deployment identifier.
55    ProcessingMessageProduced,
56}
57
58impl CounterMetric for KafkaCounters {
59    fn name(&self) -> &'static str {
60        match self {
61            Self::ProducerPartitionKeyRateLimit => "producer.partition_key.rate_limit",
62            Self::ProcessingMessageProduced => "processing.event.produced",
63        }
64    }
65}
66
67pub enum KafkaDistributions {
68    /// Size of emitted kafka message in bytes.
69    ///
70    /// This metric is tagged with:
71    /// - `topic`: The Kafka topic being produced to.
72    /// - `variant`: The Kafka message variant.
73    /// - `producer_name`: The configured producer name/deployment identifier.
74    KafkaMessageSize,
75}
76
77impl DistributionMetric for KafkaDistributions {
78    fn name(&self) -> &'static str {
79        match self {
80            Self::KafkaMessageSize => "kafka.message_size",
81        }
82    }
83}