fiddler 4.9.1

Data Stream processor written in rust
Documentation
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
//! ClickHouse metrics backend.
//!
//! This module provides a metrics implementation that sends metrics
//! to a ClickHouse database.
//!
//! # Configuration
//!
//! ```yaml
//! metrics:
//!   clickhouse:
//!     url: "http://localhost:8123"        # Required: ClickHouse HTTP endpoint
//!     database: "fiddler"                 # Optional: Database name (default: "fiddler")
//!     table: "metrics"                    # Optional: Table name (default: "metrics")
//!     username: "default"                 # Optional: Username for authentication
//!     password: ""                        # Optional: Password for authentication
//!     include:                            # Optional: list of metrics to include (all if not set)
//!       - total_received
//!       - throughput_per_sec
//!     exclude:                            # Optional: list of metrics to exclude (none if not set)
//!       - stale_entries_removed
//!     dimensions:                         # Optional: additional dimensions for all metrics
//!       - name: "environment"
//!         value: "production"
//!     batch_size: 100                     # Optional: Number of metrics to batch (default: 100)
//!     flush_interval_ms: 5000             # Optional: Max time between flushes in ms (default: 5000)
//!     create_table: true                  # Optional: Auto-create table if not exists (default: true)
//!     ttl_days: 0                         # Optional: TTL for metrics in days (default: 0 = no expiration)
//! ```

use crate::config::register_plugin;
use crate::config::ItemType;
use crate::config::{ConfigSpec, ExecutionType};
use crate::modules::metrics::ALL_METRICS;
use crate::Error;
use crate::{Closer, MetricEntry, Metrics};
use async_trait::async_trait;
use chrono::Utc;
use fiddler_macros::fiddler_registration_func;
use flume::{bounded, Sender};
use serde::Deserialize;
use serde_yaml::Value;
use std::collections::HashSet;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::oneshot;
use tracing::{debug, error, warn};

const DEFAULT_DATABASE: &str = "fiddler";
const DEFAULT_TABLE: &str = "metrics";
const DEFAULT_BATCH_SIZE: usize = 100;
const DEFAULT_FLUSH_INTERVAL_MS: u64 = 5000;
const DEFAULT_TTL_DAYS: u32 = 0;
// Buffer size is 10x batch size to handle bursts while batching provides efficiency
const CHANNEL_BUFFER_SIZE: usize = 1000;

/// ClickHouse dimension configuration.
#[derive(Debug, Deserialize, Clone, Default)]
pub struct DimensionConfig {
    /// Dimension name (will be used as column name).
    pub name: String,
    /// Dimension value.
    pub value: String,
}

/// ClickHouse-specific configuration options.
#[derive(Debug, Deserialize, Clone)]
pub struct ClickHouseConfig {
    /// ClickHouse HTTP endpoint URL (required).
    pub url: String,
    /// Database name (default: "fiddler").
    #[serde(default = "default_database")]
    pub database: String,
    /// Table name (default: "metrics").
    #[serde(default = "default_table")]
    pub table: String,
    /// Username for authentication.
    pub username: Option<String>,
    /// Password for authentication.
    pub password: Option<String>,
    /// List of metric names to include. If empty or not set, all metrics are included.
    #[serde(default)]
    pub include: Vec<String>,
    /// List of metric names to exclude. Applied after include filter.
    #[serde(default)]
    pub exclude: Vec<String>,
    /// Additional dimensions to add to all metrics.
    #[serde(default)]
    pub dimensions: Vec<DimensionConfig>,
    /// Number of metrics to batch before sending (default: 100).
    #[serde(default = "default_batch_size")]
    pub batch_size: usize,
    /// Maximum time between flushes in milliseconds (default: 5000).
    #[serde(default = "default_flush_interval_ms")]
    pub flush_interval_ms: u64,
    /// Whether to auto-create the table if it doesn't exist (default: true).
    #[serde(default = "default_create_table")]
    pub create_table: bool,
    /// TTL (time-to-live) for metrics in days. Set to 0 to disable TTL (default: 0 = no expiration).
    #[serde(default = "default_ttl_days")]
    pub ttl_days: u32,
}

fn default_database() -> String {
    DEFAULT_DATABASE.to_string()
}

fn default_table() -> String {
    DEFAULT_TABLE.to_string()
}

fn default_batch_size() -> usize {
    DEFAULT_BATCH_SIZE
}

fn default_flush_interval_ms() -> u64 {
    DEFAULT_FLUSH_INTERVAL_MS
}

fn default_create_table() -> bool {
    true
}

fn default_ttl_days() -> u32 {
    DEFAULT_TTL_DAYS
}

/// Validate a ClickHouse identifier (database, table, column name).
/// Identifiers must contain only alphanumeric characters and underscores,
/// and must start with a letter or underscore.
fn validate_identifier(identifier: &str) -> Result<(), Error> {
    if identifier.is_empty() {
        return Err(Error::Validation("Identifier cannot be empty".into()));
    }

    let first_char = identifier.chars().next().unwrap_or('0');
    if !first_char.is_alphabetic() && first_char != '_' {
        return Err(Error::Validation(format!(
            "Invalid identifier '{}': must start with letter or underscore",
            identifier
        )));
    }

    if !identifier.chars().all(|c| c.is_alphanumeric() || c == '_') {
        return Err(Error::Validation(format!(
            "Invalid identifier '{}': must contain only alphanumeric characters and underscores",
            identifier
        )));
    }

    Ok(())
}

/// Internal message type for the background publisher task.
enum PublisherMessage {
    Metric(MetricEntry),
    Shutdown,
}

/// ClickHouse HTTP client wrapper.
struct ClickHouseClient {
    client: reqwest::Client,
    url: String,
    database: String,
    table: String,
    username: Option<String>,
    password: Option<String>,
    dimensions: Vec<DimensionConfig>,
    include_set: HashSet<String>,
    exclude_set: HashSet<String>,
    table_created: AtomicBool,
    create_table_enabled: bool,
    ttl_days: u32,
}

impl ClickHouseClient {
    fn new(
        config: &ClickHouseConfig,
        include_set: HashSet<String>,
        exclude_set: HashSet<String>,
    ) -> Result<Self, Error> {
        // Validate identifiers to prevent SQL injection
        validate_identifier(&config.database)?;
        validate_identifier(&config.table)?;
        for dim in &config.dimensions {
            validate_identifier(&dim.name)?;
        }

        // Build HTTP client with proper configuration
        let client = reqwest::Client::builder()
            .pool_max_idle_per_host(2) // Keep connections alive for reuse
            .pool_idle_timeout(Duration::from_secs(90))
            .timeout(Duration::from_secs(30)) // Request timeout
            .connect_timeout(Duration::from_secs(10)) // Connection timeout
            .build()
            .map_err(|e| Error::ExecutionError(format!("Failed to build HTTP client: {}", e)))?;

        Ok(Self {
            client,
            url: config.url.clone(),
            database: config.database.clone(),
            table: config.table.clone(),
            username: config.username.clone(),
            password: config.password.clone(),
            dimensions: config.dimensions.clone(),
            include_set,
            exclude_set,
            table_created: AtomicBool::new(false),
            create_table_enabled: config.create_table,
            ttl_days: config.ttl_days,
        })
    }

    /// Execute a query against ClickHouse.
    async fn execute_query(&self, query: &str) -> Result<(), Error> {
        let url = format!("{}/?database={}", self.url, self.database);

        let mut request = self.client.post(&url).body(query.to_string());

        if let (Some(username), Some(password)) = (&self.username, &self.password) {
            request = request.basic_auth(username, Some(password));
        } else if let Some(username) = &self.username {
            request = request.basic_auth(username, None::<&str>);
        }

        let response = request
            .send()
            .await
            .map_err(|e| Error::ExecutionError(format!("ClickHouse request failed: {}", e)))?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            return Err(Error::ExecutionError(format!(
                "ClickHouse query failed with status {}: {}",
                status, body
            )));
        }

        Ok(())
    }

    /// Create the metrics table if it doesn't exist.
    async fn create_table_if_not_exists(&self) -> Result<(), Error> {
        // Build dimension columns
        let dimension_columns: String = self
            .dimensions
            .iter()
            .map(|d| format!("    {} String", d.name))
            .collect::<Vec<_>>()
            .join(",\n");

        let dimension_part = if dimension_columns.is_empty() {
            String::new()
        } else {
            format!(",\n{}", dimension_columns)
        };

        // Build TTL clause
        let ttl_clause = if self.ttl_days > 0 {
            format!(
                "\nTTL toDateTime(timestamp) + INTERVAL {} DAY",
                self.ttl_days
            )
        } else {
            String::new()
        };

        let create_query = format!(
            r#"CREATE TABLE IF NOT EXISTS {}.{} (
    timestamp DateTime64(3),
    metric_name String,
    metric_value Float64{}
) ENGINE = MergeTree()
ORDER BY (timestamp, metric_name){}"#,
            self.database, self.table, dimension_part, ttl_clause
        );

        self.execute_query(&create_query).await
    }

    /// Insert metrics into ClickHouse.
    async fn insert_metrics(&self, metrics: &[MetricEntry]) -> Result<(), Error> {
        if metrics.is_empty() {
            return Ok(());
        }

        // Retry table creation on first insert if it failed initially
        if self.create_table_enabled && !self.table_created.load(Ordering::Relaxed) {
            if self.create_table_if_not_exists().await.is_ok() {
                self.table_created.store(true, Ordering::Relaxed);
            }
        }

        // Build dimension column names
        let dimension_cols: String = self
            .dimensions
            .iter()
            .map(|d| format!(", {}", d.name))
            .collect();

        // Build the INSERT statement header
        let insert_header = format!(
            "INSERT INTO {}.{} (timestamp, metric_name, metric_value{}) VALUES ",
            self.database, self.table, dimension_cols
        );

        // Build dimension values once (same for all rows)
        let dimension_values: String = if self.dimensions.is_empty() {
            String::new()
        } else {
            self.dimensions
                .iter()
                .map(|d| format!(", '{}'", escape_string(&d.value)))
                .collect()
        };

        // Generate timestamp once per batch (all metrics in a batch represent the same snapshot)
        let timestamp = Utc::now().timestamp_millis();

        // Pre-allocate with estimated capacity (16 metric fields per entry)
        let estimated_rows = metrics.len() * 16;
        let mut rows = Vec::with_capacity(estimated_rows);

        for metric in metrics {
            self.add_metric_row(
                &mut rows,
                timestamp,
                "total_received",
                metric.total_received as f64,
                &dimension_values,
            );
            self.add_metric_row(
                &mut rows,
                timestamp,
                "total_completed",
                metric.total_completed as f64,
                &dimension_values,
            );
            self.add_metric_row(
                &mut rows,
                timestamp,
                "total_process_errors",
                metric.total_process_errors as f64,
                &dimension_values,
            );
            self.add_metric_row(
                &mut rows,
                timestamp,
                "total_output_errors",
                metric.total_output_errors as f64,
                &dimension_values,
            );
            self.add_metric_row(
                &mut rows,
                timestamp,
                "total_filtered",
                metric.total_filtered as f64,
                &dimension_values,
            );
            self.add_metric_row(
                &mut rows,
                timestamp,
                "streams_started",
                metric.streams_started as f64,
                &dimension_values,
            );
            self.add_metric_row(
                &mut rows,
                timestamp,
                "streams_completed",
                metric.streams_completed as f64,
                &dimension_values,
            );
            self.add_metric_row(
                &mut rows,
                timestamp,
                "duplicates_rejected",
                metric.duplicates_rejected as f64,
                &dimension_values,
            );
            self.add_metric_row(
                &mut rows,
                timestamp,
                "stale_entries_removed",
                metric.stale_entries_removed as f64,
                &dimension_values,
            );
            self.add_metric_row(
                &mut rows,
                timestamp,
                "in_flight",
                metric.in_flight as f64,
                &dimension_values,
            );
            self.add_metric_row(
                &mut rows,
                timestamp,
                "throughput_per_sec",
                metric.throughput_per_sec,
                &dimension_values,
            );
            self.add_metric_row(
                &mut rows,
                timestamp,
                "input_bytes",
                metric.input_bytes as f64,
                &dimension_values,
            );
            self.add_metric_row(
                &mut rows,
                timestamp,
                "output_bytes",
                metric.output_bytes as f64,
                &dimension_values,
            );
            self.add_metric_row(
                &mut rows,
                timestamp,
                "bytes_per_sec",
                metric.bytes_per_sec,
                &dimension_values,
            );
            self.add_metric_row(
                &mut rows,
                timestamp,
                "latency_avg_ms",
                metric.latency_avg_ms,
                &dimension_values,
            );
            self.add_metric_row(
                &mut rows,
                timestamp,
                "latency_min_ms",
                metric.latency_min_ms,
                &dimension_values,
            );
            self.add_metric_row(
                &mut rows,
                timestamp,
                "latency_max_ms",
                metric.latency_max_ms,
                &dimension_values,
            );
            // System metrics - only emit if collected
            if let Some(cpu) = metric.cpu_usage_percent {
                self.add_metric_row(
                    &mut rows,
                    timestamp,
                    "cpu_usage_percent",
                    cpu as f64,
                    &dimension_values,
                );
            }
            if let Some(mem_used) = metric.memory_used_bytes {
                self.add_metric_row(
                    &mut rows,
                    timestamp,
                    "memory_used_bytes",
                    mem_used as f64,
                    &dimension_values,
                );
            }
            if let Some(mem_total) = metric.memory_total_bytes {
                self.add_metric_row(
                    &mut rows,
                    timestamp,
                    "memory_total_bytes",
                    mem_total as f64,
                    &dimension_values,
                );
            }
        }

        if rows.is_empty() {
            return Ok(());
        }

        let query = format!("{}{}", insert_header, rows.join(", "));
        self.execute_query(&query).await
    }

    fn add_metric_row(
        &self,
        rows: &mut Vec<String>,
        timestamp: i64,
        metric_name: &str,
        value: f64,
        dimension_values: &str,
    ) {
        if self.should_include(metric_name) {
            rows.push(format!(
                "(fromUnixTimestamp64Milli({}), '{}', {}{})",
                timestamp, metric_name, value, dimension_values
            ));
        }
    }

    fn should_include(&self, metric_name: &str) -> bool {
        self.include_set.contains(metric_name) && !self.exclude_set.contains(metric_name)
    }
}

/// Escape a string for ClickHouse SQL.
fn escape_string(s: &str) -> String {
    s.replace('\\', "\\\\").replace('\'', "\\'")
}

/// ClickHouse metrics backend.
///
/// Records metrics by sending them to ClickHouse asynchronously.
/// Uses a bounded channel with a background task for non-blocking operation.
/// Supports batching and periodic flushing for efficiency.
pub struct ClickHouseMetrics {
    sender: Sender<PublisherMessage>,
    include_set: HashSet<String>,
    exclude_set: HashSet<String>,
    shutdown_complete: Option<oneshot::Receiver<()>>,
}

impl ClickHouseMetrics {
    /// Creates a new ClickHouse metrics instance from configuration.
    pub async fn new(config: Value) -> Result<Self, Error> {
        let ch_config: ClickHouseConfig = serde_yaml::from_value(config)?;

        // Build include/exclude sets
        let include_set: HashSet<String> = if ch_config.include.is_empty() {
            ALL_METRICS.iter().map(|s| s.to_string()).collect()
        } else {
            ch_config.include.iter().cloned().collect()
        };

        let exclude_set: HashSet<String> = ch_config.exclude.iter().cloned().collect();

        let client = Arc::new(ClickHouseClient::new(
            &ch_config,
            include_set.clone(),
            exclude_set.clone(),
        )?);

        // Create table if configured
        if ch_config.create_table {
            match client.create_table_if_not_exists().await {
                Ok(()) => {
                    client.table_created.store(true, Ordering::Relaxed);
                }
                Err(e) => {
                    warn!(error = %e, "Failed to create ClickHouse table, will retry on first insert");
                }
            }
        }

        // Create channel for async publishing
        let (sender, receiver) = bounded::<PublisherMessage>(CHANNEL_BUFFER_SIZE);

        // Create shutdown completion channel
        let (shutdown_tx, shutdown_rx) = oneshot::channel();

        let batch_size = ch_config.batch_size;
        let flush_interval = Duration::from_millis(ch_config.flush_interval_ms);

        // Spawn background task for publishing metrics
        tokio::spawn(async move {
            let mut batch: Vec<MetricEntry> = Vec::with_capacity(batch_size);
            let mut last_flush = std::time::Instant::now();

            loop {
                // Calculate remaining time until next flush
                let elapsed = last_flush.elapsed();
                let remaining = if elapsed >= flush_interval {
                    Duration::from_millis(1) // Use minimal timeout if overdue
                } else {
                    flush_interval - elapsed
                };

                let timeout = tokio::time::timeout(remaining, receiver.recv_async());

                match timeout.await {
                    Ok(Ok(PublisherMessage::Metric(metric))) => {
                        batch.push(metric);

                        // Flush if batch is full
                        if batch.len() >= batch_size {
                            if let Err(e) = client.insert_metrics(&batch).await {
                                error!(error = %e, "Failed to insert metrics to ClickHouse");
                            }
                            batch.clear();
                            last_flush = std::time::Instant::now();
                        }
                    }
                    Err(_) => {
                        // Timeout - flush on periodic interval
                        if !batch.is_empty() {
                            if let Err(e) = client.insert_metrics(&batch).await {
                                error!(error = %e, "Failed to insert metrics to ClickHouse");
                            }
                            batch.clear();
                        }
                        last_flush = std::time::Instant::now();
                    }
                    Ok(Ok(PublisherMessage::Shutdown)) => {
                        // Final flush before shutdown
                        if !batch.is_empty() {
                            if let Err(e) = client.insert_metrics(&batch).await {
                                error!(error = %e, "Failed to insert final metrics to ClickHouse");
                            }
                        }
                        debug!("ClickHouse metrics publisher task exiting");
                        let _ = shutdown_tx.send(());
                        break;
                    }
                    Ok(Err(_)) => {
                        // Channel closed unexpectedly
                        if !batch.is_empty() {
                            if let Err(e) = client.insert_metrics(&batch).await {
                                error!(error = %e, "Failed to insert final metrics to ClickHouse");
                            }
                        }
                        warn!("ClickHouse metrics publisher channel closed unexpectedly");
                        break;
                    }
                }
            }
        });

        debug!(
            url = %ch_config.url,
            database = %ch_config.database,
            table = %ch_config.table,
            "ClickHouse metrics backend initialized"
        );

        Ok(Self {
            sender,
            include_set,
            exclude_set,
            shutdown_complete: Some(shutdown_rx),
        })
    }

    /// Check if a metric should be included based on include/exclude filters.
    fn should_include(&self, metric_name: &str) -> bool {
        self.include_set.contains(metric_name) && !self.exclude_set.contains(metric_name)
    }
}

#[async_trait]
impl Metrics for ClickHouseMetrics {
    fn record(&mut self, metric: MetricEntry) {
        // Check if any metrics would be included
        let has_metrics = ALL_METRICS.iter().any(|name| self.should_include(name));

        if !has_metrics {
            return;
        }

        // Non-blocking send - warn if channel is full
        if let Err(e) = self.sender.try_send(PublisherMessage::Metric(metric)) {
            warn!(
                error = %e,
                buffer_size = CHANNEL_BUFFER_SIZE,
                "ClickHouse metrics channel full, dropping metric. \
                 Consider increasing batch_size, reducing flush_interval_ms, \
                 or checking ClickHouse connectivity"
            );
        }
    }
}

#[async_trait]
impl Closer for ClickHouseMetrics {
    async fn close(&mut self) -> Result<(), Error> {
        debug!("ClickHouse metrics backend closing");

        // Send shutdown message to flush remaining metrics
        if let Err(e) = self.sender.send_async(PublisherMessage::Shutdown).await {
            warn!(error = %e, "Failed to send shutdown message");
            return Ok(());
        }

        // Wait for background task to complete with a timeout
        if let Some(rx) = self.shutdown_complete.take() {
            match tokio::time::timeout(Duration::from_secs(5), rx).await {
                Ok(Ok(())) => debug!("ClickHouse publisher shutdown complete"),
                Ok(Err(_)) => warn!("ClickHouse publisher shutdown channel closed unexpectedly"),
                Err(_) => warn!("ClickHouse publisher shutdown timed out after 5s"),
            }
        }

        Ok(())
    }
}

#[fiddler_registration_func]
fn create_clickhouse(conf: Value) -> Result<ExecutionType, Error> {
    Ok(ExecutionType::Metrics(Box::new(
        ClickHouseMetrics::new(conf).await?,
    )))
}

/// Registers the ClickHouse metrics plugin.
pub(crate) fn register_clickhouse() -> Result<(), Error> {
    let config = r#"type: object
required:
  - url
properties:
  url:
    type: string
    description: "ClickHouse HTTP endpoint URL"
  database:
    type: string
    default: "fiddler"
    description: "Database name"
  table:
    type: string
    default: "metrics"
    description: "Table name"
  username:
    type: string
    description: "Username for authentication"
  password:
    type: string
    description: "Password for authentication"
  include:
    type: array
    items:
      type: string
    description: "List of metric names to include"
  exclude:
    type: array
    items:
      type: string
    description: "List of metric names to exclude"
  dimensions:
    type: array
    items:
      type: object
      properties:
        name:
          type: string
        value:
          type: string
      required:
        - name
        - value
    description: "Additional dimensions for all metrics"
  batch_size:
    type: integer
    default: 100
    description: "Number of metrics to batch before sending"
  flush_interval_ms:
    type: integer
    default: 5000
    description: "Maximum time between flushes in milliseconds"
  create_table:
    type: boolean
    default: true
    description: "Auto-create table if not exists"
  ttl_days:
    type: integer
    default: 0
    description: "TTL for metrics in days (0 = no expiration)"
"#;
    let conf_spec = ConfigSpec::from_schema(config)?;

    register_plugin(
        "clickhouse".into(),
        ItemType::Metrics,
        conf_spec,
        create_clickhouse,
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_values() {
        assert_eq!(default_database(), "fiddler");
        assert_eq!(default_table(), "metrics");
        assert_eq!(default_batch_size(), 100);
        assert_eq!(default_flush_interval_ms(), 5000);
        assert_eq!(default_ttl_days(), 0);
        assert!(default_create_table());
    }

    #[test]
    fn test_config_deserialization() {
        let yaml = r#"
url: "http://localhost:8123"
database: "test_db"
table: "test_metrics"
username: "user"
password: "pass"
include:
  - total_received
  - throughput_per_sec
exclude:
  - stale_entries_removed
dimensions:
  - name: environment
    value: production
batch_size: 50
flush_interval_ms: 1000
create_table: false
ttl_days: 7
"#;
        let config: ClickHouseConfig = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(config.url, "http://localhost:8123");
        assert_eq!(config.database, "test_db");
        assert_eq!(config.table, "test_metrics");
        assert_eq!(config.username, Some("user".to_string()));
        assert_eq!(config.password, Some("pass".to_string()));
        assert_eq!(config.include.len(), 2);
        assert_eq!(config.exclude.len(), 1);
        assert_eq!(config.dimensions.len(), 1);
        assert_eq!(config.batch_size, 50);
        assert_eq!(config.flush_interval_ms, 1000);
        assert_eq!(config.ttl_days, 7);
        assert!(!config.create_table);
    }

    #[test]
    fn test_config_defaults() {
        let yaml = r#"url: "http://localhost:8123""#;
        let config: ClickHouseConfig = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(config.url, "http://localhost:8123");
        assert_eq!(config.database, "fiddler");
        assert_eq!(config.table, "metrics");
        assert!(config.username.is_none());
        assert!(config.password.is_none());
        assert!(config.include.is_empty());
        assert!(config.exclude.is_empty());
        assert!(config.dimensions.is_empty());
        assert_eq!(config.batch_size, 100);
        assert_eq!(config.flush_interval_ms, 5000);
        assert_eq!(config.ttl_days, 0);
        assert!(config.create_table);
    }

    #[test]
    fn test_escape_string() {
        assert_eq!(escape_string("hello"), "hello");
        assert_eq!(escape_string("it's"), "it\\'s");
        assert_eq!(escape_string("back\\slash"), "back\\\\slash");
        assert_eq!(escape_string("it's a \\test"), "it\\'s a \\\\test");
        assert_eq!(escape_string(""), "");
        assert_eq!(escape_string("'"), "\\'");
        assert_eq!(escape_string("\\"), "\\\\");
    }

    #[test]
    fn test_validate_identifier_valid() {
        assert!(validate_identifier("fiddler").is_ok());
        assert!(validate_identifier("my_database").is_ok());
        assert!(validate_identifier("_private").is_ok());
        assert!(validate_identifier("Table123").is_ok());
        assert!(validate_identifier("a").is_ok());
    }

    #[test]
    fn test_validate_identifier_invalid() {
        assert!(validate_identifier("").is_err());
        assert!(validate_identifier("123invalid").is_err());
        assert!(validate_identifier("invalid-name").is_err());
        assert!(validate_identifier("invalid.name").is_err());
        assert!(validate_identifier("invalid name").is_err());
        assert!(validate_identifier("invalid;name").is_err());
    }

    #[test]
    fn test_register_clickhouse() {
        let result = register_clickhouse();
        // May return DuplicateRegisteredName if already registered by another test
        assert!(result.is_ok() || matches!(result, Err(crate::Error::DuplicateRegisteredName(_))));
    }

    #[test]
    fn test_should_include_all_metrics() {
        let include_set: HashSet<String> = ALL_METRICS.iter().map(|s| s.to_string()).collect();
        let exclude_set: HashSet<String> = HashSet::new();

        let config = ClickHouseConfig {
            url: "http://localhost:8123".to_string(),
            database: default_database(),
            table: default_table(),
            username: None,
            password: None,
            include: vec![],
            exclude: vec![],
            dimensions: vec![],
            batch_size: default_batch_size(),
            flush_interval_ms: default_flush_interval_ms(),
            create_table: false,
            ttl_days: default_ttl_days(),
        };

        let client = ClickHouseClient::new(&config, include_set, exclude_set).unwrap();

        for metric in ALL_METRICS {
            assert!(client.should_include(metric));
        }
    }

    #[test]
    fn test_should_include_with_filter() {
        let include_set: HashSet<String> = vec![
            "total_received".to_string(),
            "throughput_per_sec".to_string(),
        ]
        .into_iter()
        .collect();
        let exclude_set: HashSet<String> = HashSet::new();

        let config = ClickHouseConfig {
            url: "http://localhost:8123".to_string(),
            database: default_database(),
            table: default_table(),
            username: None,
            password: None,
            include: vec![],
            exclude: vec![],
            dimensions: vec![],
            batch_size: default_batch_size(),
            flush_interval_ms: default_flush_interval_ms(),
            create_table: false,
            ttl_days: default_ttl_days(),
        };

        let client = ClickHouseClient::new(&config, include_set, exclude_set).unwrap();

        assert!(client.should_include("total_received"));
        assert!(client.should_include("throughput_per_sec"));
        assert!(!client.should_include("total_completed"));
        assert!(!client.should_include("in_flight"));
    }

    #[test]
    fn test_should_include_with_exclude() {
        let include_set: HashSet<String> = ALL_METRICS.iter().map(|s| s.to_string()).collect();
        let exclude_set: HashSet<String> = vec!["stale_entries_removed".to_string()]
            .into_iter()
            .collect();

        let config = ClickHouseConfig {
            url: "http://localhost:8123".to_string(),
            database: default_database(),
            table: default_table(),
            username: None,
            password: None,
            include: vec![],
            exclude: vec![],
            dimensions: vec![],
            batch_size: default_batch_size(),
            flush_interval_ms: default_flush_interval_ms(),
            create_table: false,
            ttl_days: default_ttl_days(),
        };

        let client = ClickHouseClient::new(&config, include_set, exclude_set).unwrap();

        assert!(client.should_include("total_received"));
        assert!(!client.should_include("stale_entries_removed"));
    }

    #[test]
    fn test_invalid_database_name() {
        let include_set: HashSet<String> = ALL_METRICS.iter().map(|s| s.to_string()).collect();
        let exclude_set: HashSet<String> = HashSet::new();

        let config = ClickHouseConfig {
            url: "http://localhost:8123".to_string(),
            database: "invalid-db".to_string(), // Contains hyphen
            table: default_table(),
            username: None,
            password: None,
            include: vec![],
            exclude: vec![],
            dimensions: vec![],
            batch_size: default_batch_size(),
            flush_interval_ms: default_flush_interval_ms(),
            create_table: false,
            ttl_days: default_ttl_days(),
        };

        let result = ClickHouseClient::new(&config, include_set, exclude_set);
        assert!(result.is_err());
    }

    #[test]
    fn test_invalid_dimension_name() {
        let include_set: HashSet<String> = ALL_METRICS.iter().map(|s| s.to_string()).collect();
        let exclude_set: HashSet<String> = HashSet::new();

        let config = ClickHouseConfig {
            url: "http://localhost:8123".to_string(),
            database: default_database(),
            table: default_table(),
            username: None,
            password: None,
            include: vec![],
            exclude: vec![],
            dimensions: vec![DimensionConfig {
                name: "invalid.name".to_string(),
                value: "value".to_string(),
            }],
            batch_size: default_batch_size(),
            flush_interval_ms: default_flush_interval_ms(),
            create_table: false,
            ttl_days: default_ttl_days(),
        };

        let result = ClickHouseClient::new(&config, include_set, exclude_set);
        assert!(result.is_err());
    }
}