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

//! # Vinted 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,
    trivial_numeric_casts,
    unused_extern_crates,
    unused_import_braces,
    unused_qualifications,
    unused_results,
    trivial_numeric_casts,
    unreachable_pub,
    unused_import_braces,
    unused_qualifications,
    unused_results,
    deprecated,
    unconditional_recursion,
    unknown_lints,
    unreachable_code,
    unused_mut
)]

#[macro_use]
extern crate prometheus;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
#[macro_use]
extern crate serde_derive;
use elasticsearch::http::transport::{SingleNodeConnectionPool, TransportBuilder};
use elasticsearch::Elasticsearch;
use std::collections::{BTreeMap, HashMap};
use std::sync::Arc;
use std::time::Duration;

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

/// Exporter metrics
mod exporter_metrics;

mod options;
pub use options::ExporterOptions;

/// Reserved labels
pub mod reserved;

/// Cluster metadata
pub mod metadata;

pub(crate) mod metrics;

const NAMESPACE: &str = "elasticsearch";

/// 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,
}

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
    }

    /// 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 transport = TransportBuilder::new(connection_pool)
            .timeout(options.elasticsearch_global_timeout)
            .build()?;

        let client = Elasticsearch::new(transport);
        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());

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

    /// 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()));
        }
    };
}