Skip to content

Commit

Permalink
allow using filter_path for /stats
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasr committed Jul 13, 2023
1 parent 07c4ab8 commit 29df57d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ Scraping `/_nodes/stats` subsystem thread_pool + fs paths metric
$ docker run --network=host -it vinted/elasticsearch_exporter --elasticsearch_url=http://IP:PORT --exporter_metrics_enabled="nodes_stats=true" --elasticsearch_path_parameters="nodes_stats=thread_pool,fs"
```

Scraping `/stats` for `total.indexing` and `total.search` metrics only

```
$ docker run --network=host -it vinted/elasticsearch_exporter --elasticsearch_url=http://IP:PORT --exporter_metrics_enabled="stats=true" --elasticsearch_query_filter_path="stats=indices.*.total.indexing,indices.*.total.search"
```

```shell
$ curl -s http://127.0.0.1:9222
Expand Down
5 changes: 5 additions & 0 deletions src/bin/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ pub struct Opts {
#[clap(long = "elasticsearch_query_fields", default_value = "")]
pub elasticsearch_query_fields: HashMapVec,

/// Elasticsearch query ?filter_path= for /stats. Comma-separated list or
/// wildcard expressions of paths to include in the statistics.
#[clap(long = "elasticsearch_query_filter_path", default_value = "")]
pub elasticsearch_query_filter_path: HashMapVec,

/// Exporter default metrics lifeimte interval in seconds
#[clap(
long = "exporter_metrics_lifetime_default_interval",
Expand Down
1 change: 1 addition & 0 deletions src/bin/elasticsearch_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
elasticsearch_url: opts.elasticsearch_url.clone(),
elasticsearch_global_timeout: *opts.elasticsearch_global_timeout,
elasticsearch_query_fields: opts.elasticsearch_query_fields.0.clone(),
elasticsearch_query_filter_path: opts.elasticsearch_query_filter_path.0.clone(),
elasticsearch_subsystem_timeouts: opts.elasticsearch_subsystem_timeouts.0.clone(),
elasticsearch_path_parameters: opts.elasticsearch_path_parameters.0.clone(),

Expand Down
5 changes: 5 additions & 0 deletions src/metrics/_stats/_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub(crate) const SUBSYSTEM: &str = "stats";

async fn metrics(exporter: &Exporter) -> Result<Vec<Metrics>, elasticsearch::Error> {
let fields = exporter.options().query_fields_for_subsystem(SUBSYSTEM);
let filter_path = exporter.options().query_filter_path_for_subsystem(SUBSYSTEM);

let indices = exporter.client().indices();

Expand All @@ -17,6 +18,10 @@ async fn metrics(exporter: &Exporter) -> Result<Vec<Metrics>, elasticsearch::Err
indices_stats = indices_stats.fields(&fields);
}

if !filter_path.is_empty() {
indices_stats = indices_stats.filter_path(&filter_path)
}

let response = indices_stats.send().await?;

let values = response
Expand Down
17 changes: 17 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub struct ExporterOptions {
/// Elasticsearch /_nodes/stats fields comma-separated list or
/// wildcard expressions of fields to include in the statistics.
pub elasticsearch_query_fields: CollectionLabels,
/// Elasticsearch /stats filter_path. Comma-separated list or
/// wildcard expressions of paths to include in the statistics.
pub elasticsearch_query_filter_path: CollectionLabels,
/// Exporter timeout for subsystems, in case subsystem timeout is not defined
/// default global timeout is used
pub elasticsearch_subsystem_timeouts: ExporterPollIntervals,
Expand Down Expand Up @@ -71,6 +74,14 @@ impl ExporterOptions {
.unwrap_or_default()
}

/// ?filter_path= parameters for subsystems
pub fn query_filter_path_for_subsystem(&self, subsystem: &'static str) -> Vec<&str> {
self.elasticsearch_query_filter_path
.get(subsystem)
.map(|params| params.iter().map(AsRef::as_ref).collect::<Vec<&str>>())
.unwrap_or_default()
}

/// Path parameters for subsystems
pub fn path_parameters_for_subsystem(&self, subsystem: &'static str) -> Vec<&str> {
self.elasticsearch_path_parameters
Expand Down Expand Up @@ -220,6 +231,12 @@ impl fmt::Display for ExporterOptions {
&self.elasticsearch_query_fields,
);

collection_labels_to_string(
&mut output,
"elasticsearch_query_filter_path",
&self.elasticsearch_query_filter_path,
);

poll_duration_to_string(
&mut output,
"elasticsearch_subsystem_timeouts",
Expand Down

0 comments on commit 29df57d

Please sign in to comment.