klag-exporter 0.1.22

High-performance Kafka consumer group lag exporter with offset and time lag metrics
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
use crate::error::{KlagError, Result};
use regex::Regex;
use serde::Deserialize;
use std::collections::HashMap;
use std::path::Path;
use std::time::Duration;

#[derive(Debug, Deserialize, Clone)]
pub struct Config {
    pub exporter: ExporterConfig,
    pub clusters: Vec<ClusterConfig>,
}

#[derive(Debug, Deserialize, Clone)]
pub struct ExporterConfig {
    #[serde(with = "humantime_serde", default = "default_poll_interval")]
    pub poll_interval: Duration,
    #[serde(default = "default_http_port")]
    pub http_port: u16,
    #[serde(default = "default_http_host")]
    pub http_host: String,
    #[serde(default = "default_granularity")]
    pub granularity: Granularity,
    #[serde(default)]
    pub timestamp_sampling: TimestampSamplingConfig,
    #[serde(default)]
    pub otel: OtelConfig,
    #[serde(default)]
    pub leadership: LeadershipConfig,
    #[serde(default)]
    pub performance: PerformanceConfig,
}

#[derive(Debug, Deserialize, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum Granularity {
    Topic,
    Partition,
}

#[derive(Debug, Deserialize, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum TimestampSamplingMode {
    /// Read the actual message at the committed offset via a pooled
    /// BaseConsumer and use its produce timestamp. Exact, but the pool
    /// occupies meaningful resident memory and creates per-cycle FFI
    /// churn on large clusters.
    Message,
    /// Estimate time lag from the observed rate of change of high
    /// watermarks. Pure CPU, no consumer pool, no FFI. Accuracy depends
    /// on steady producer rate across the history window — good enough
    /// for lag alerting, not appropriate for audit-grade timestamp
    /// tracking.
    Rate,
}

#[derive(Debug, Deserialize, Clone)]
pub struct TimestampSamplingConfig {
    #[serde(default = "default_true")]
    pub enabled: bool,
    /// How the exporter derives the "seconds of lag" metric. Default is
    /// `rate` — much cheaper on large clusters and scales to thousands
    /// of partitions without a consumer pool. Set to `message` to restore
    /// the pre-Tier-3 behavior (read the actual message at the committed
    /// offset and subtract its produce timestamp from "now").
    #[serde(default = "default_timestamp_sampling_mode")]
    pub mode: TimestampSamplingMode,
    /// Cache TTL for the message-mode sampler. Ignored in `rate` mode.
    #[serde(with = "humantime_serde", default = "default_cache_ttl")]
    pub cache_ttl: Duration,
    /// Maximum concurrent message fetches in `message` mode. Ignored in
    /// `rate` mode (rate mode does no I/O).
    #[serde(default = "default_max_concurrent_fetches")]
    pub max_concurrent_fetches: usize,
    /// `rate` mode: maximum number of (time, high_watermark) samples
    /// retained per partition. Larger = smoother rate estimate, more
    /// memory. Default 5.
    #[serde(default = "default_rate_history_samples")]
    pub rate_history_samples: usize,
    /// `rate` mode: samples older than this are evicted. Default 10
    /// minutes. On long poll intervals (> a few minutes) raise this so
    /// the ring buffer has enough points to compute a stable rate.
    #[serde(with = "humantime_serde", default = "default_rate_history_max_age")]
    pub rate_history_max_age: Duration,
    /// `rate` mode: producers observed to be running below this rate are
    /// treated as idle and time-lag is reported as missing rather than
    /// an unreliable division. Default 0.01 msg/sec.
    #[serde(default = "default_rate_min_msgs_per_sec")]
    pub rate_min_msgs_per_sec: f64,
}

/// Performance tuning configuration for large clusters.
/// These settings control parallelism and timeouts for Kafka operations.
#[derive(Debug, Deserialize, Clone)]
pub struct PerformanceConfig {
    /// Timeout for individual Kafka API operations (metadata, watermarks, etc.)
    #[serde(with = "humantime_serde", default = "default_kafka_timeout")]
    pub kafka_timeout: Duration,
    /// Timeout for fetching committed offsets per consumer group
    #[serde(with = "humantime_serde", default = "default_offset_fetch_timeout")]
    pub offset_fetch_timeout: Duration,
    /// Maximum number of consumer groups to fetch offsets for in parallel
    #[serde(default = "default_max_concurrent_groups")]
    pub max_concurrent_groups: usize,
    /// **Deprecated** since the batched Admin API replaced the per-partition
    /// watermark fan-out: watermarks are now fetched in two batched
    /// `ListOffsets` calls regardless of partition count, so a per-partition
    /// concurrency cap has no effect. Kept in the schema so existing configs
    /// continue to parse; emits a one-time startup INFO if set to a
    /// non-default value. Will be removed in a future release.
    #[serde(default = "default_max_concurrent_watermarks")]
    pub max_concurrent_watermarks: usize,
    /// Number of collection cycles between Kafka client recycling.
    /// Recycling destroys and recreates internal librdkafka clients to release
    /// accumulated metadata that librdkafka never frees on its own.
    /// Set to 0 to disable. Default: 50 (~25 min at 30s poll interval).
    #[serde(default = "default_client_recycle_interval")]
    pub client_recycle_interval: u64,
    /// Maximum size of the Tokio blocking-thread pool used for librdkafka FFI
    /// calls. The default Tokio limit is 512; each thread holds a native
    /// stack (2–8 MB depending on platform), so the worst-case virtual
    /// memory footprint is significant on large clusters. 64 is ample for
    /// this exporter: the hot path's concurrent blocking calls are bounded
    /// by `max_concurrent_groups` + a few for watermark / compacted-topic /
    /// timestamp-sampling FFI calls. Raise this if you set
    /// `max_concurrent_groups` above ~50.
    #[serde(default = "default_max_blocking_threads")]
    pub max_blocking_threads: usize,
    /// How long to cache each topic's `cleanup.policy` in memory.
    /// `cleanup.policy` rarely changes after topic creation, so caching it
    /// for a long time saves a per-cycle `DescribeConfigs` RPC over the
    /// monitored topic set. Only topics new to the cache (or whose entry
    /// has expired) get queried on a given cycle.
    #[serde(
        with = "humantime_serde",
        default = "default_compacted_topics_cache_ttl"
    )]
    pub compacted_topics_cache_ttl: Duration,
    /// How long to cache the derived "monitored partitions + monitored topics"
    /// set from cluster metadata. A cycle with a fresh cache entry skips the
    /// `fetch_metadata` call and the regex filtering pass over every topic
    /// name — significant savings on clusters with thousands of topics.
    /// New topics (or partition additions) become visible at most this long
    /// after they appear on the cluster. Set to 0 to disable the cache
    /// (refresh every cycle).
    #[serde(with = "humantime_serde", default = "default_metadata_cache_ttl")]
    pub metadata_cache_ttl: Duration,
    /// How long to cache the list of consumer groups on the cluster.
    /// `list_consumer_groups` is one RPC per collection cycle; caching
    /// it shaves off one round trip whenever the set of groups is stable
    /// (the common case). Newly-created consumer groups become visible
    /// at most this long after they appear. Set to 0 to disable.
    #[serde(
        with = "humantime_serde",
        default = "default_consumer_groups_cache_ttl"
    )]
    pub consumer_groups_cache_ttl: Duration,
}

#[derive(Debug, Deserialize, Clone)]
pub struct OtelConfig {
    #[serde(default)]
    pub enabled: bool,
    #[serde(default = "default_otel_endpoint")]
    pub endpoint: String,
    #[serde(with = "humantime_serde", default = "default_export_interval")]
    pub export_interval: Duration,
}

/// Configuration for leader election in high availability deployments.
#[derive(Debug, Deserialize, Clone)]
pub struct LeadershipConfig {
    /// Enable leader election. When disabled (default), runs in single-instance mode.
    #[serde(default)]
    pub enabled: bool,
    /// Leadership provider type. Currently only "kubernetes" is supported.
    #[serde(default = "default_leadership_provider")]
    pub provider: LeadershipProvider,
    /// Name of the Kubernetes Lease resource.
    #[serde(default = "default_lease_name")]
    pub lease_name: String,
    /// Namespace for the Lease resource. Supports env var substitution.
    #[serde(default = "default_lease_namespace")]
    pub lease_namespace: String,
    /// Identity of this instance. Defaults to HOSTNAME or POD_NAME env var.
    #[allow(dead_code)] // Used by kubernetes feature
    pub identity: Option<String>,
    /// Duration the lease is valid in seconds.
    #[serde(default = "default_lease_duration")]
    #[allow(dead_code)] // Used by kubernetes feature
    pub lease_duration_secs: u32,
    /// Grace period for lease renewal in seconds. Must be less than lease_duration.
    #[serde(default = "default_grace_period")]
    #[allow(dead_code)] // Used by kubernetes feature
    pub grace_period_secs: u32,
}

#[derive(Debug, Deserialize, Clone, Copy, PartialEq, Eq, Default)]
#[serde(rename_all = "lowercase")]
pub enum LeadershipProvider {
    #[default]
    Kubernetes,
}

#[derive(Debug, Deserialize, Clone)]
pub struct ClusterConfig {
    pub name: String,
    pub bootstrap_servers: String,
    #[serde(default = "default_whitelist")]
    pub group_whitelist: Vec<String>,
    #[serde(default)]
    pub group_blacklist: Vec<String>,
    #[serde(default = "default_whitelist")]
    pub topic_whitelist: Vec<String>,
    #[serde(default = "default_topic_blacklist")]
    pub topic_blacklist: Vec<String>,
    #[serde(default)]
    pub consumer_properties: HashMap<String, String>,
    #[serde(default)]
    pub labels: HashMap<String, String>,
}

fn default_poll_interval() -> Duration {
    Duration::from_secs(30)
}

fn default_http_port() -> u16 {
    8000
}

fn default_http_host() -> String {
    "0.0.0.0".to_string()
}

fn default_granularity() -> Granularity {
    Granularity::Topic
}

fn default_true() -> bool {
    true
}

fn default_cache_ttl() -> Duration {
    Duration::from_secs(60)
}

fn default_max_concurrent_fetches() -> usize {
    5
}

fn default_timestamp_sampling_mode() -> TimestampSamplingMode {
    TimestampSamplingMode::Rate
}

fn default_rate_history_samples() -> usize {
    5
}

fn default_rate_history_max_age() -> Duration {
    Duration::from_secs(600)
}

fn default_rate_min_msgs_per_sec() -> f64 {
    0.01
}

fn default_kafka_timeout() -> Duration {
    Duration::from_secs(30)
}

fn default_offset_fetch_timeout() -> Duration {
    Duration::from_secs(10)
}

fn default_max_concurrent_groups() -> usize {
    10
}

fn default_max_concurrent_watermarks() -> usize {
    50
}

fn default_client_recycle_interval() -> u64 {
    50
}

fn default_max_blocking_threads() -> usize {
    64
}

fn default_compacted_topics_cache_ttl() -> Duration {
    Duration::from_secs(3600)
}

fn default_metadata_cache_ttl() -> Duration {
    Duration::from_secs(300)
}

fn default_consumer_groups_cache_ttl() -> Duration {
    Duration::from_secs(60)
}

fn default_otel_endpoint() -> String {
    "http://localhost:4317".to_string()
}

fn default_export_interval() -> Duration {
    Duration::from_secs(60)
}

fn default_leadership_provider() -> LeadershipProvider {
    LeadershipProvider::Kubernetes
}

fn default_lease_name() -> String {
    "klag-exporter".to_string()
}

fn default_lease_namespace() -> String {
    "default".to_string()
}

fn default_lease_duration() -> u32 {
    15
}

fn default_grace_period() -> u32 {
    5
}

fn default_whitelist() -> Vec<String> {
    vec![".*".to_string()]
}

fn default_topic_blacklist() -> Vec<String> {
    vec!["__.*".to_string()]
}

impl Default for TimestampSamplingConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            mode: default_timestamp_sampling_mode(),
            cache_ttl: default_cache_ttl(),
            max_concurrent_fetches: default_max_concurrent_fetches(),
            rate_history_samples: default_rate_history_samples(),
            rate_history_max_age: default_rate_history_max_age(),
            rate_min_msgs_per_sec: default_rate_min_msgs_per_sec(),
        }
    }
}

impl Default for OtelConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            endpoint: default_otel_endpoint(),
            export_interval: default_export_interval(),
        }
    }
}

impl Default for LeadershipConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            provider: default_leadership_provider(),
            lease_name: default_lease_name(),
            lease_namespace: default_lease_namespace(),
            identity: None,
            lease_duration_secs: default_lease_duration(),
            grace_period_secs: default_grace_period(),
        }
    }
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            kafka_timeout: default_kafka_timeout(),
            offset_fetch_timeout: default_offset_fetch_timeout(),
            max_concurrent_groups: default_max_concurrent_groups(),
            max_concurrent_watermarks: default_max_concurrent_watermarks(),
            client_recycle_interval: default_client_recycle_interval(),
            max_blocking_threads: default_max_blocking_threads(),
            compacted_topics_cache_ttl: default_compacted_topics_cache_ttl(),
            metadata_cache_ttl: default_metadata_cache_ttl(),
            consumer_groups_cache_ttl: default_consumer_groups_cache_ttl(),
        }
    }
}

impl Config {
    pub fn load(path: Option<&str>) -> Result<Config> {
        let config_path = path.unwrap_or("config.toml");

        if !Path::new(config_path).exists() {
            return Err(KlagError::Config(format!(
                "Configuration file not found: {}",
                config_path
            )));
        }

        let content = std::fs::read_to_string(config_path)?;
        let content = Self::substitute_env_vars(&content);

        let config: Config = toml::from_str(&content)
            .map_err(|e| KlagError::Config(format!("TOML parse error: {}", e)))?;

        config.validate()?;
        Ok(config)
    }

    fn substitute_env_vars(content: &str) -> String {
        // Supports:
        // - ${VAR} - replaced with env var value, empty string if not set
        // - ${VAR:-default} - replaced with env var value, or "default" if not set
        // - ${?VAR} - replaced with env var value if set, empty string if not set (same as ${VAR})
        let re = Regex::new(r"\$\{\??([^}:-]+)(?::-([^}]*))?\}").unwrap();
        re.replace_all(content, |caps: &regex::Captures| {
            let var_name = &caps[1];
            let default_value = caps.get(2).map(|m| m.as_str()).unwrap_or("");
            std::env::var(var_name).unwrap_or_else(|_| default_value.to_string())
        })
        .to_string()
    }

    pub fn validate(&self) -> Result<()> {
        if self.clusters.is_empty() {
            return Err(KlagError::Config(
                "At least one cluster must be configured".to_string(),
            ));
        }

        for cluster in &self.clusters {
            cluster.validate()?;
        }

        // Validate performance config
        if self.exporter.performance.max_concurrent_groups == 0 {
            return Err(KlagError::Config(
                "performance.max_concurrent_groups must be at least 1".to_string(),
            ));
        }
        if self.exporter.performance.max_concurrent_watermarks == 0 {
            return Err(KlagError::Config(
                "performance.max_concurrent_watermarks must be at least 1".to_string(),
            ));
        }

        // Timestamp-sampling validation. Only enforce the constraints that
        // actually matter for the configured mode so rate-mode users aren't
        // forced to tune message-mode knobs (and vice versa).
        let ts = &self.exporter.timestamp_sampling;
        if ts.enabled && ts.mode == TimestampSamplingMode::Message && ts.max_concurrent_fetches == 0
        {
            return Err(KlagError::Config(
                "timestamp_sampling.max_concurrent_fetches must be >= 1 when mode = 'message'"
                    .to_string(),
            ));
        }
        if ts.enabled && ts.mode == TimestampSamplingMode::Rate {
            if ts.rate_history_samples < 2 {
                return Err(KlagError::Config(
                    "timestamp_sampling.rate_history_samples must be >= 2 (need two samples \
                     to compute a rate)"
                        .to_string(),
                ));
            }
            if !ts.rate_min_msgs_per_sec.is_finite() || ts.rate_min_msgs_per_sec < 0.0 {
                return Err(KlagError::Config(format!(
                    "timestamp_sampling.rate_min_msgs_per_sec ({}) must be finite and >= 0",
                    ts.rate_min_msgs_per_sec
                )));
            }
        }
        // The blocking-thread pool must be able to hold every concurrent FFI
        // call our hot path spawns simultaneously. `max_concurrent_groups`
        // is the dominant consumer; the timestamp sampler adds up to
        // `max_concurrent_fetches` more — but only when sampling is enabled
        // AND in `message` mode. Rate mode does no I/O and consumes no
        // blocking threads. If the pool is too small, tasks queue behind
        // each other and we effectively serialize — silently undoing the
        // Tier 1 win.
        let sampler_uses_blocking_threads = self.exporter.timestamp_sampling.enabled
            && matches!(
                self.exporter.timestamp_sampling.mode,
                TimestampSamplingMode::Message
            );
        let sampler_contribution = if sampler_uses_blocking_threads {
            self.exporter.timestamp_sampling.max_concurrent_fetches
        } else {
            0
        };
        let min_needed = self.exporter.performance.max_concurrent_groups + sampler_contribution + 4; // small headroom for watermark / compacted-topic / metadata FFI
        if self.exporter.performance.max_blocking_threads < min_needed {
            let sampling_state = match (
                self.exporter.timestamp_sampling.enabled,
                self.exporter.timestamp_sampling.mode,
            ) {
                (false, _) => "disabled",
                (true, TimestampSamplingMode::Rate) => "rate (no FFI)",
                (true, TimestampSamplingMode::Message) => "message",
            };
            return Err(KlagError::Config(format!(
                "performance.max_blocking_threads ({}) must be >= max_concurrent_groups ({}) + \
                 timestamp_sampling.max_concurrent_fetches ({}, sampling {}) + 4 = {}",
                self.exporter.performance.max_blocking_threads,
                self.exporter.performance.max_concurrent_groups,
                sampler_contribution,
                sampling_state,
                min_needed,
            )));
        }

        Ok(())
    }
}

impl ClusterConfig {
    pub fn validate(&self) -> Result<()> {
        if self.name.is_empty() {
            return Err(KlagError::Config(
                "Cluster name cannot be empty".to_string(),
            ));
        }

        if self.bootstrap_servers.is_empty() {
            return Err(KlagError::Config(format!(
                "Cluster '{}': bootstrap_servers cannot be empty",
                self.name
            )));
        }

        self.compile_filters()?;
        Ok(())
    }

    pub fn compile_filters(&self) -> Result<CompiledFilters> {
        let group_whitelist = self
            .group_whitelist
            .iter()
            .map(|p| Regex::new(p))
            .collect::<std::result::Result<Vec<_>, _>>()?;

        let group_blacklist = self
            .group_blacklist
            .iter()
            .map(|p| Regex::new(p))
            .collect::<std::result::Result<Vec<_>, _>>()?;

        let topic_whitelist = self
            .topic_whitelist
            .iter()
            .map(|p| Regex::new(p))
            .collect::<std::result::Result<Vec<_>, _>>()?;

        let topic_blacklist = self
            .topic_blacklist
            .iter()
            .map(|p| Regex::new(p))
            .collect::<std::result::Result<Vec<_>, _>>()?;

        Ok(CompiledFilters {
            group_whitelist,
            group_blacklist,
            topic_whitelist,
            topic_blacklist,
        })
    }
}

#[derive(Debug, Clone)]
pub struct CompiledFilters {
    pub group_whitelist: Vec<Regex>,
    pub group_blacklist: Vec<Regex>,
    pub topic_whitelist: Vec<Regex>,
    pub topic_blacklist: Vec<Regex>,
}

impl CompiledFilters {
    pub fn matches_group(&self, group: &str) -> bool {
        let matches_whitelist = self.group_whitelist.iter().any(|r| r.is_match(group));
        let matches_blacklist = self.group_blacklist.iter().any(|r| r.is_match(group));
        matches_whitelist && !matches_blacklist
    }

    pub fn matches_topic(&self, topic: &str) -> bool {
        let matches_whitelist = self.topic_whitelist.iter().any(|r| r.is_match(topic));
        let matches_blacklist = self.topic_blacklist.iter().any(|r| r.is_match(topic));
        matches_whitelist && !matches_blacklist
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_config_loads_from_file() {
        let config_content = r#"
[exporter]
poll_interval = "30s"
http_port = 8000

[[clusters]]
name = "test"
bootstrap_servers = "localhost:9092"
"#;

        let mut file = NamedTempFile::new().unwrap();
        file.write_all(config_content.as_bytes()).unwrap();

        let config = Config::load(Some(file.path().to_str().unwrap())).unwrap();
        assert_eq!(config.exporter.poll_interval, Duration::from_secs(30));
        assert_eq!(config.exporter.http_port, 8000);
        assert_eq!(config.clusters.len(), 1);
        assert_eq!(config.clusters[0].name, "test");
    }

    #[test]
    fn test_config_env_override() {
        std::env::set_var("TEST_KAFKA_USER", "myuser");

        let config_content = r#"
[exporter]
poll_interval = "30s"

[[clusters]]
name = "test"
bootstrap_servers = "localhost:9092"

[clusters.consumer_properties]
"sasl.username" = "${TEST_KAFKA_USER}"
"#;

        let mut file = NamedTempFile::new().unwrap();
        file.write_all(config_content.as_bytes()).unwrap();

        let config = Config::load(Some(file.path().to_str().unwrap())).unwrap();
        assert_eq!(
            config.clusters[0].consumer_properties.get("sasl.username"),
            Some(&"myuser".to_string())
        );

        std::env::remove_var("TEST_KAFKA_USER");
    }

    #[test]
    fn test_config_env_with_default() {
        // Ensure env var is NOT set
        std::env::remove_var("TEST_NONEXISTENT_VAR");

        let config_content = r#"
[exporter]
poll_interval = "30s"

[[clusters]]
name = "test"
bootstrap_servers = "${TEST_NONEXISTENT_VAR:-localhost:9092}"
"#;

        let mut file = NamedTempFile::new().unwrap();
        file.write_all(config_content.as_bytes()).unwrap();

        let config = Config::load(Some(file.path().to_str().unwrap())).unwrap();
        // Should use default value since env var is not set
        assert_eq!(config.clusters[0].bootstrap_servers, "localhost:9092");
    }

    #[test]
    fn test_config_env_override_default() {
        std::env::set_var("TEST_BOOTSTRAP", "kafka:29092");

        let config_content = r#"
[exporter]
poll_interval = "30s"

[[clusters]]
name = "test"
bootstrap_servers = "${TEST_BOOTSTRAP:-localhost:9092}"
"#;

        let mut file = NamedTempFile::new().unwrap();
        file.write_all(config_content.as_bytes()).unwrap();

        let config = Config::load(Some(file.path().to_str().unwrap())).unwrap();
        // Should use env var value instead of default
        assert_eq!(config.clusters[0].bootstrap_servers, "kafka:29092");

        std::env::remove_var("TEST_BOOTSTRAP");
    }

    #[test]
    fn test_config_validates_bootstrap_servers() {
        let config_content = r#"
[exporter]
poll_interval = "30s"

[[clusters]]
name = "test"
bootstrap_servers = ""
"#;

        let mut file = NamedTempFile::new().unwrap();
        file.write_all(config_content.as_bytes()).unwrap();

        let result = Config::load(Some(file.path().to_str().unwrap()));
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("bootstrap_servers cannot be empty"));
    }

    #[test]
    fn test_regex_filter_whitelist_match() {
        let filters = CompiledFilters {
            group_whitelist: vec![Regex::new("^my-group.*").unwrap()],
            group_blacklist: vec![],
            topic_whitelist: vec![Regex::new(".*").unwrap()],
            topic_blacklist: vec![],
        };

        assert!(filters.matches_group("my-group-1"));
        assert!(filters.matches_group("my-group-2"));
        assert!(!filters.matches_group("other-group"));
    }

    #[test]
    fn test_regex_filter_blacklist_reject() {
        let filters = CompiledFilters {
            group_whitelist: vec![Regex::new(".*").unwrap()],
            group_blacklist: vec![Regex::new("^internal-.*").unwrap()],
            topic_whitelist: vec![Regex::new(".*").unwrap()],
            topic_blacklist: vec![Regex::new("^__.*").unwrap()],
        };

        assert!(filters.matches_group("my-group"));
        assert!(!filters.matches_group("internal-group"));
        assert!(filters.matches_topic("my-topic"));
        assert!(!filters.matches_topic("__consumer_offsets"));
    }

    #[test]
    fn test_default_config_values() {
        let config_content = r#"
[exporter]

[[clusters]]
name = "test"
bootstrap_servers = "localhost:9092"
"#;

        let mut file = NamedTempFile::new().unwrap();
        file.write_all(config_content.as_bytes()).unwrap();

        let config = Config::load(Some(file.path().to_str().unwrap())).unwrap();
        assert_eq!(config.exporter.poll_interval, Duration::from_secs(30));
        assert_eq!(config.exporter.http_port, 8000);
        assert_eq!(config.exporter.http_host, "0.0.0.0");
        assert_eq!(config.exporter.granularity, Granularity::Topic);
        assert!(config.exporter.timestamp_sampling.enabled);
        assert_eq!(
            config.exporter.timestamp_sampling.mode,
            TimestampSamplingMode::Rate,
            "default mode should be rate (Tier 3)"
        );
        assert!(!config.exporter.otel.enabled);
        // Performance defaults
        assert_eq!(
            config.exporter.performance.kafka_timeout,
            Duration::from_secs(30)
        );
        assert_eq!(
            config.exporter.performance.offset_fetch_timeout,
            Duration::from_secs(10)
        );
        assert_eq!(config.exporter.performance.max_concurrent_groups, 10);
        assert_eq!(config.exporter.performance.max_concurrent_watermarks, 50);
        assert_eq!(config.exporter.performance.client_recycle_interval, 50);
        assert_eq!(config.exporter.performance.max_blocking_threads, 64);
        assert_eq!(
            config.exporter.performance.compacted_topics_cache_ttl,
            Duration::from_secs(3600)
        );
        assert_eq!(
            config.exporter.performance.metadata_cache_ttl,
            Duration::from_secs(300)
        );
        assert_eq!(
            config.exporter.performance.consumer_groups_cache_ttl,
            Duration::from_secs(60)
        );
    }

    #[test]
    fn test_performance_config_custom_values() {
        let config_content = r#"
[exporter]
poll_interval = "60s"

[exporter.performance]
kafka_timeout = "15s"
offset_fetch_timeout = "5s"
max_concurrent_groups = 20
max_concurrent_watermarks = 100
client_recycle_interval = 0

[[clusters]]
name = "test"
bootstrap_servers = "localhost:9092"
"#;

        let mut file = NamedTempFile::new().unwrap();
        file.write_all(config_content.as_bytes()).unwrap();

        let config = Config::load(Some(file.path().to_str().unwrap())).unwrap();
        assert_eq!(
            config.exporter.performance.kafka_timeout,
            Duration::from_secs(15)
        );
        assert_eq!(
            config.exporter.performance.offset_fetch_timeout,
            Duration::from_secs(5)
        );
        assert_eq!(config.exporter.performance.max_concurrent_groups, 20);
        assert_eq!(config.exporter.performance.max_concurrent_watermarks, 100);
        assert_eq!(config.exporter.performance.client_recycle_interval, 0);
    }

    #[test]
    fn test_max_blocking_threads_rejected_when_too_small() {
        // Message mode uses blocking threads. Default max_concurrent_groups=10,
        // max_concurrent_fetches=5, so minimum = 10 + 5 + 4 = 19. 16 must fail.
        let config_content = r#"
[exporter]

[exporter.timestamp_sampling]
mode = "message"

[exporter.performance]
max_blocking_threads = 16

[[clusters]]
name = "test"
bootstrap_servers = "localhost:9092"
"#;
        let mut file = NamedTempFile::new().unwrap();
        file.write_all(config_content.as_bytes()).unwrap();

        let err = Config::load(Some(file.path().to_str().unwrap())).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("max_blocking_threads (16)"),
            "unexpected error: {msg}"
        );
        assert!(msg.contains("= 19"), "unexpected error: {msg}");
    }

    #[test]
    fn test_message_mode_rejects_zero_concurrent_fetches() {
        let config_content = r#"
[exporter]

[exporter.timestamp_sampling]
mode = "message"
max_concurrent_fetches = 0

[[clusters]]
name = "test"
bootstrap_servers = "localhost:9092"
"#;
        let mut file = NamedTempFile::new().unwrap();
        file.write_all(config_content.as_bytes()).unwrap();
        let err = Config::load(Some(file.path().to_str().unwrap())).unwrap_err();
        assert!(
            err.to_string()
                .contains("max_concurrent_fetches must be >= 1"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn test_rate_mode_rejects_one_history_sample() {
        let config_content = r#"
[exporter]

[exporter.timestamp_sampling]
rate_history_samples = 1

[[clusters]]
name = "test"
bootstrap_servers = "localhost:9092"
"#;
        let mut file = NamedTempFile::new().unwrap();
        file.write_all(config_content.as_bytes()).unwrap();
        let err = Config::load(Some(file.path().to_str().unwrap())).unwrap_err();
        assert!(
            err.to_string()
                .contains("rate_history_samples must be >= 2"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn test_rate_mode_rejects_negative_min_rate() {
        let config_content = r#"
[exporter]

[exporter.timestamp_sampling]
rate_min_msgs_per_sec = -1.0

[[clusters]]
name = "test"
bootstrap_servers = "localhost:9092"
"#;
        let mut file = NamedTempFile::new().unwrap();
        file.write_all(config_content.as_bytes()).unwrap();
        let err = Config::load(Some(file.path().to_str().unwrap())).unwrap_err();
        assert!(
            err.to_string().contains("rate_min_msgs_per_sec")
                && err.to_string().contains("must be finite and >= 0"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn test_max_blocking_threads_sampler_excluded_when_disabled() {
        // Sampling disabled → sampler_contribution = 0. Required min becomes
        // max_concurrent_groups (10 default) + 4 = 14. 16 must be accepted
        // (would have been rejected with the old validation that always
        // counted max_concurrent_fetches = 5).
        let config_content = r#"
[exporter]

[exporter.timestamp_sampling]
enabled = false

[exporter.performance]
max_blocking_threads = 16

[[clusters]]
name = "test"
bootstrap_servers = "localhost:9092"
"#;
        let mut file = NamedTempFile::new().unwrap();
        file.write_all(config_content.as_bytes()).unwrap();

        let config = Config::load(Some(file.path().to_str().unwrap()))
            .expect("should accept 16 when sampling is disabled");
        assert_eq!(config.exporter.performance.max_blocking_threads, 16);
    }

    #[test]
    fn test_max_blocking_threads_custom_value_accepted() {
        let config_content = r#"
[exporter]

[exporter.performance]
max_concurrent_groups = 30
max_blocking_threads = 128

[exporter.timestamp_sampling]
max_concurrent_fetches = 20

[[clusters]]
name = "test"
bootstrap_servers = "localhost:9092"
"#;
        let mut file = NamedTempFile::new().unwrap();
        file.write_all(config_content.as_bytes()).unwrap();

        let config = Config::load(Some(file.path().to_str().unwrap())).unwrap();
        assert_eq!(config.exporter.performance.max_blocking_threads, 128);
    }

    #[test]
    fn test_performance_config_validates_zero_concurrency() {
        let config_content = r#"
[exporter]
poll_interval = "30s"

[exporter.performance]
max_concurrent_groups = 0

[[clusters]]
name = "test"
bootstrap_servers = "localhost:9092"
"#;

        let mut file = NamedTempFile::new().unwrap();
        file.write_all(config_content.as_bytes()).unwrap();

        let result = Config::load(Some(file.path().to_str().unwrap()));
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("max_concurrent_groups must be at least 1"));
    }

    #[test]
    fn test_performance_config_validates_zero_watermarks_concurrency() {
        let config_content = r#"
[exporter]
poll_interval = "30s"

[exporter.performance]
max_concurrent_watermarks = 0

[[clusters]]
name = "test"
bootstrap_servers = "localhost:9092"
"#;

        let mut file = NamedTempFile::new().unwrap();
        file.write_all(config_content.as_bytes()).unwrap();

        let result = Config::load(Some(file.path().to_str().unwrap()));
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("max_concurrent_watermarks must be at least 1"));
    }
}