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
#![feature(hash_extract_if)]

//! # Proper Elasticsearch exporter
#![deny(
    warnings,
    bad_style,
    dead_code,
    improper_ctypes,
    non_shorthand_field_patterns,
    no_mangle_generic_items,
    overflowing_literals,
    path_statements,
    patterns_in_fns_without_body,
    private_bounds,
    private_interfaces,
    unconditional_recursion,
    unused,
    unused_allocation,
    unused_comparisons,
    unused_parens,
    while_true,
    missing_debug_implementations,
    missing_docs,
    trivial_casts,
    unused_extern_crates,
    unused_qualifications,
    trivial_numeric_casts,
    unreachable_pub,
    unused_import_braces,
    unused_results,
    deprecated,
    unknown_lints,
    unreachable_code,
    unused_mut
)]

#[macro_use]
extern crate log;
#[macro_use]
extern crate serde_derive;
use elasticsearch::cert::{Certificate, CertificateValidation};
use elasticsearch::http::transport::{SingleNodeConnectionPool, TransportBuilder};
use elasticsearch::Elasticsearch;
use prometheus::{default_registry, HistogramOpts, HistogramVec, IntGaugeVec, Opts};
use std::collections::{BTreeMap, HashMap};
use std::fs::File;
use std::io::Read;
use std::sync::Arc;
use std::time::Duration;

/// Generic collector of Elasticsearch metrics
pub mod collection;
/// Metric
pub mod metric;

mod options;
pub use options::{CertificateValidationOptions, ExporterOptions};

/// Reserved labels
pub mod reserved;

/// Cluster metadata
pub mod metadata;

pub(crate) mod metrics;

/// Labels type with ordered keys
pub type Labels = BTreeMap<String, String>;

/// Collection labels
pub type CollectionLabels = BTreeMap<String, Vec<String>>;

/// Exporter polling intervals
pub type ExporterPollIntervals = HashMap<String, Duration>;

/// Exporter metrics switch ON/OFF
pub type ExporterMetricsSwitch = BTreeMap<String, bool>;

/// Elasticsearch exporter
#[derive(Debug, Clone)]
pub struct Exporter(Arc<Inner>);

#[derive(Debug)]
struct Inner {
    /// Name of Elasticsearch cluster exporter is working
    cluster_name: String,
    /// Elasticsearch client instance
    client: Elasticsearch,
    /// Exporter options
    options: ExporterOptions,
    /// Constant exporter labels, e.g.: cluster
    const_labels: HashMap<String, String>,

    /// Node ID to node name map for adding extra metadata labels
    /// {"U-WnGaTpRxucgde3miiDWw": "m1-supernode.example.com"}
    nodes_metadata: metadata::IdToMetadata,

    // Exporter metrics
    metrics: ExporterMetrics,
}

/// Global metrics for Elasticsearch exporter
#[derive(Debug)]
pub struct ExporterMetrics {
    /// Subsystem request histogram
    subsystem_request_histogram: HistogramVec,
    /// Cluster health status
    cluster_health_status: IntGaugeVec,
}

impl Exporter {
    /// Elasticsearch client instance
    pub fn client(&self) -> &Elasticsearch {
        &self.0.client
    }

    /// Elasticsearch cluster name
    pub fn cluster_name(&self) -> &str {
        &self.0.cluster_name
    }

    /// Exporter options
    pub fn options(&self) -> &ExporterOptions {
        &self.0.options
    }

    /// Exporter options
    pub fn const_labels(&self) -> HashMap<String, String> {
        self.0.const_labels.clone()
    }

    /// Node ID to node name map for adding extra metadata labels
    /// {"U-WnGaTpRxucgde3miiDWw": "m1-supernode.example.com"}
    pub fn nodes_metadata(&self) -> &metadata::IdToMetadata {
        &self.0.nodes_metadata
    }

    /// Exporter metrics
    pub fn metrics(&self) -> &ExporterMetrics {
        &self.0.metrics
    }

    /// Spawn exporter
    pub async fn new(options: ExporterOptions) -> Result<Self, Box<dyn std::error::Error>> {
        let connection_pool = SingleNodeConnectionPool::new(options.elasticsearch_url.clone());

        let mut transport =
            TransportBuilder::new(connection_pool).timeout(options.elasticsearch_global_timeout);

        let load_cert = || -> Result<Certificate, elasticsearch::Error> {
            if let Some(ref cert_path) = options.elasticsearch_certificate_path {
                let mut buf = Vec::new();
                let _ = File::open(cert_path)?.read_to_end(&mut buf)?;
                Certificate::from_pem(&buf)
            } else {
                panic!("Please provide --elasticsearch_certificate_path=CERTIFICATE_PATH flag");
            }
        };

        match options.elasticsearch_certificate_validation {
            Some(CertificateValidationOptions::Full) => {
                let cert = load_cert()?;
                transport = transport.cert_validation(CertificateValidation::Full(cert));
            }
            Some(CertificateValidationOptions::Partial) => {
                let cert = load_cert()?;
                transport = transport.cert_validation(CertificateValidation::Certificate(cert));
            }
            Some(CertificateValidationOptions::None) => {
                transport = transport.cert_validation(CertificateValidation::None);
            }
            None => {}
        }

        let client = Elasticsearch::new(transport.build()?);
        info!("Elasticsearch: ping");
        let _ = client.ping().send().await?;

        let nodes_metadata = if options.enable_metadata_refresh() {
            metadata::node_data::build(&client).await?
        } else {
            info!("Skip metadata refresh");
            // This will generate empty map
            Default::default()
        };

        let cluster_name = metadata::cluster_name(&client).await?;

        let mut const_labels = HashMap::new();
        let _ = const_labels.insert("cluster".into(), cluster_name.clone());

        let metrics = ExporterMetrics {
            subsystem_request_histogram: HistogramVec::new(
                HistogramOpts::new(
                    "subsystem_request_duration_seconds",
                    "The Elasticsearch subsystem request latencies in seconds.",
                )
                .namespace(options.exporter_metrics_namespace.as_str()),
                &["subsystem", "cluster"],
            )
            .expect("valid histogram vec metric"),

            cluster_health_status: IntGaugeVec::new(
                Opts::new(
                    "cluster_health_status",
                    "Whether all primary and replica shards are allocated.",
                )
                .namespace(options.exporter_metrics_namespace.as_str()),
                &["cluster", "color"],
            )
            .expect("valid prometheus metric"),
        };

        default_registry().register(Box::new(metrics.cluster_health_status.clone()))?;
        default_registry().register(Box::new(metrics.subsystem_request_histogram.clone()))?;

        Ok(Self(Arc::new(Inner {
            cluster_name,
            client,
            options,
            const_labels,
            nodes_metadata,
            metrics,
        })))
    }

    /// Spawn collectors
    pub async fn spawn(self) {
        self.spawn_cat();
        self.spawn_cluster();
        self.spawn_nodes();
        self.spawn_stats();

        if self.options().enable_metadata_refresh() {
            #[allow(clippy::let_underscore_future)]
            let _ = tokio::spawn(metadata::node_data::poll(self));
        }
    }

    fn spawn_cluster(&self) {
        use metrics::_cluster::*;

        is_metric_enabled!(self.clone(), health);
    }

    fn spawn_stats(&self) {
        use metrics::_stats::*;

        is_metric_enabled!(self.clone(), _all);
    }

    fn spawn_nodes(&self) {
        use metrics::_nodes::*;

        is_metric_enabled!(self.clone(), usage);
        is_metric_enabled!(self.clone(), stats);
        is_metric_enabled!(self.clone(), info);
    }

    // =^.^=
    // /_cat/allocation
    // /_cat/shards
    // /_cat/indices
    // /_cat/segments
    // /_cat/nodes
    // /_cat/recovery
    // /_cat/health
    // /_cat/pending_tasks
    // /_cat/aliases
    // /_cat/thread_pool
    // /_cat/plugins
    // /_cat/fielddata
    // /_cat/nodeattrs
    // /_cat/repositories
    // /_cat/templates
    // /_cat/transforms
    fn spawn_cat(&self) {
        use metrics::_cat::*;

        is_metric_enabled!(self.clone(), allocation);
        is_metric_enabled!(self.clone(), shards);
        is_metric_enabled!(self.clone(), indices);
        is_metric_enabled!(self.clone(), segments);
        is_metric_enabled!(self.clone(), nodes);
        is_metric_enabled!(self.clone(), recovery);
        is_metric_enabled!(self.clone(), health);
        is_metric_enabled!(self.clone(), pending_tasks);
        is_metric_enabled!(self.clone(), aliases);
        is_metric_enabled!(self.clone(), thread_pool);
        is_metric_enabled!(self.clone(), plugins);
        is_metric_enabled!(self.clone(), fielddata);
        is_metric_enabled!(self.clone(), nodeattrs);
        is_metric_enabled!(self.clone(), repositories);
        is_metric_enabled!(self.clone(), templates);
        is_metric_enabled!(self.clone(), transforms);
    }

    pub(crate) fn random_delay() -> u64 {
        oorandom::Rand64::new(292).rand_range(150..800)
    }
}

/// Convenience macro to poll metrics
#[macro_export]
macro_rules! is_metric_enabled {
    ($exporter:expr, $metric:ident) => {
        if $exporter.options().is_metric_enabled($metric::SUBSYSTEM) {
            #[allow(clippy::let_underscore_future)]
            let _ = tokio::spawn($metric::poll($exporter.clone()));
        }
    };
}