-
Notifications
You must be signed in to change notification settings - Fork 459
Add Prometheus metrics test #5718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rdettai
wants to merge
2
commits into
main
Choose a base branch
from
metrics-integ-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
236 changes: 236 additions & 0 deletions
236
quickwit/quickwit-integration-tests/src/test_utils/prometheus_parser.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
// Copyright 2021-Present Datadog, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::collections::HashMap; | ||
|
||
use regex::Regex; | ||
|
||
#[derive(Debug, PartialEq, Clone)] | ||
pub struct PrometheusMetric { | ||
pub name: String, | ||
pub labels: HashMap<String, String>, | ||
pub metric_value: f64, | ||
} | ||
|
||
/// Parse Prometheus metrics serialized with prometheus::TextEncoder | ||
/// | ||
/// Unfortunately, the prometheus crate does not provide a way to parse metrics | ||
pub fn parse_prometheus_metrics(input: &str) -> Vec<PrometheusMetric> { | ||
let mut metrics = Vec::new(); | ||
let re = Regex::new(r"(?P<name>[^{]+)(?:\{(?P<labels>[^\}]*)\})? (?P<value>.+)").unwrap(); | ||
|
||
for line in input.lines() { | ||
if line.starts_with('#') { | ||
continue; | ||
} | ||
|
||
if let Some(caps) = re.captures(line) { | ||
let name = caps.name("name").unwrap().as_str().to_string(); | ||
let metric_value: f64 = caps | ||
.name("value") | ||
.unwrap() | ||
.as_str() | ||
.parse() | ||
.expect("Failed to parse value"); | ||
|
||
let labels = caps.name("labels").map_or(HashMap::new(), |m| { | ||
m.as_str() | ||
.split(',') | ||
.map(|label| { | ||
let mut parts = label.splitn(2, '='); | ||
let key = parts.next().unwrap().to_string(); | ||
let value = parts.next().unwrap().trim_matches('"').to_string(); | ||
(key, value) | ||
}) | ||
.collect() | ||
}); | ||
|
||
metrics.push(PrometheusMetric { | ||
name, | ||
labels, | ||
metric_value, | ||
}); | ||
} | ||
} | ||
|
||
metrics | ||
} | ||
|
||
/// Filter metrics by name and a subset of the available labels | ||
/// | ||
/// Specify an empty Vec of labels to return all metrics with the specified name | ||
pub fn filter_metrics( | ||
metrics: &[PrometheusMetric], | ||
name: &str, | ||
labels: Vec<(&'static str, &'static str)>, | ||
) -> Vec<PrometheusMetric> { | ||
metrics | ||
.iter() | ||
.filter(|metric| metric.name == name) | ||
.filter(|metric| { | ||
labels | ||
.iter() | ||
.all(|(key, value)| metric.labels.get(*key).map(String::as_str) == Some(*value)) | ||
}) | ||
.cloned() | ||
.collect() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
const TEST_INPUT: &str = r#" | ||
quickwit_search_leaf_search_single_split_warmup_num_bytes_sum 0 | ||
# HELP quickwit_storage_object_storage_request_duration_seconds Duration of object storage requests in seconds. | ||
# TYPE quickwit_storage_object_storage_request_duration_seconds histogram | ||
quickwit_storage_object_storage_request_duration_seconds_bucket{action="delete_objects",le="30"} 0 | ||
quickwit_storage_object_storage_request_duration_seconds_bucket{action="delete_objects",le="+Inf"} 0 | ||
quickwit_storage_object_storage_request_duration_seconds_sum{action="delete_objects"} 0 | ||
quickwit_search_root_search_request_duration_seconds_sum{kind="server",status="success"} 0.004093958 | ||
quickwit_storage_object_storage_requests_total{action="delete_object"} 0 | ||
quickwit_storage_object_storage_requests_total{action="delete_objects"} 0 | ||
"#; | ||
|
||
#[test] | ||
fn test_parse_prometheus_metrics() { | ||
let metrics = parse_prometheus_metrics(TEST_INPUT); | ||
assert_eq!(metrics.len(), 7); | ||
assert_eq!( | ||
metrics[0], | ||
PrometheusMetric { | ||
name: "quickwit_search_leaf_search_single_split_warmup_num_bytes_sum".to_string(), | ||
labels: HashMap::new(), | ||
metric_value: 0.0, | ||
} | ||
); | ||
assert_eq!( | ||
metrics[1], | ||
PrometheusMetric { | ||
name: "quickwit_storage_object_storage_request_duration_seconds_bucket".to_string(), | ||
labels: [ | ||
("action".to_string(), "delete_objects".to_string()), | ||
("le".to_string(), "30".to_string()) | ||
] | ||
.iter() | ||
.cloned() | ||
.collect(), | ||
metric_value: 0.0, | ||
} | ||
); | ||
assert_eq!( | ||
metrics[2], | ||
PrometheusMetric { | ||
name: "quickwit_storage_object_storage_request_duration_seconds_bucket".to_string(), | ||
labels: [ | ||
("action".to_string(), "delete_objects".to_string()), | ||
("le".to_string(), "+Inf".to_string()) | ||
] | ||
.iter() | ||
.cloned() | ||
.collect(), | ||
metric_value: 0.0, | ||
} | ||
); | ||
assert_eq!( | ||
metrics[3], | ||
PrometheusMetric { | ||
name: "quickwit_storage_object_storage_request_duration_seconds_sum".to_string(), | ||
labels: [("action".to_string(), "delete_objects".to_string())] | ||
.iter() | ||
.cloned() | ||
.collect(), | ||
metric_value: 0.0, | ||
} | ||
); | ||
assert_eq!( | ||
metrics[4], | ||
PrometheusMetric { | ||
name: "quickwit_search_root_search_request_duration_seconds_sum".to_string(), | ||
labels: [ | ||
("kind".to_string(), "server".to_string()), | ||
("status".to_string(), "success".to_string()) | ||
] | ||
.iter() | ||
.cloned() | ||
.collect(), | ||
metric_value: 0.004093958, | ||
} | ||
); | ||
assert_eq!( | ||
metrics[5], | ||
PrometheusMetric { | ||
name: "quickwit_storage_object_storage_requests_total".to_string(), | ||
labels: [("action".to_string(), "delete_object".to_string())] | ||
.iter() | ||
.cloned() | ||
.collect(), | ||
metric_value: 0.0, | ||
} | ||
); | ||
assert_eq!( | ||
metrics[6], | ||
PrometheusMetric { | ||
name: "quickwit_storage_object_storage_requests_total".to_string(), | ||
labels: [("action".to_string(), "delete_objects".to_string())] | ||
.iter() | ||
.cloned() | ||
.collect(), | ||
metric_value: 0.0, | ||
} | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_filter_prometheus_metrics() { | ||
let metrics = parse_prometheus_metrics(TEST_INPUT); | ||
{ | ||
let filtered_metric = filter_metrics( | ||
&metrics, | ||
"quickwit_storage_object_storage_request_duration_seconds_bucket", | ||
vec![], | ||
); | ||
assert_eq!(filtered_metric.len(), 2); | ||
} | ||
{ | ||
let filtered_metric = filter_metrics( | ||
&metrics, | ||
"quickwit_search_root_search_request_duration_seconds_sum", | ||
vec![("status", "success")], | ||
); | ||
assert_eq!(filtered_metric.len(), 1); | ||
} | ||
{ | ||
let filtered_metric = | ||
filter_metrics(&metrics, "quickwit_doest_not_exist_metric", vec![]); | ||
assert_eq!(filtered_metric.len(), 0); | ||
} | ||
{ | ||
let filtered_metric = filter_metrics( | ||
&metrics, | ||
"quickwit_storage_object_storage_requests_total", | ||
vec![("does_not_exist_label", "value")], | ||
); | ||
assert_eq!(filtered_metric.len(), 0); | ||
} | ||
{ | ||
let filtered_metric = filter_metrics( | ||
&metrics, | ||
"quickwit_storage_object_storage_requests_total", | ||
vec![("action", "does_not_exist_value")], | ||
); | ||
assert_eq!(filtered_metric.len(), 0); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
quickwit/quickwit-integration-tests/src/tests/prometheus_tests.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright 2021-Present Datadog, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use quickwit_config::service::QuickwitService; | ||
use quickwit_serve::SearchRequestQueryString; | ||
|
||
use crate::test_utils::{filter_metrics, parse_prometheus_metrics, ClusterSandboxBuilder}; | ||
|
||
#[tokio::test] | ||
async fn test_metrics_standalone_server() { | ||
quickwit_common::setup_logging_for_tests(); | ||
let sandbox = ClusterSandboxBuilder::build_and_start_standalone().await; | ||
let client = sandbox.rest_client(QuickwitService::Indexer); | ||
|
||
client | ||
.indexes() | ||
.create( | ||
r#" | ||
version: 0.8 | ||
index_id: my-new-index | ||
doc_mapping: | ||
field_mappings: | ||
- name: body | ||
type: text | ||
"#, | ||
quickwit_config::ConfigFormat::Yaml, | ||
false, | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
client | ||
.search( | ||
"my-new-index", | ||
SearchRequestQueryString { | ||
query: "body:test".to_string(), | ||
max_hits: 10, | ||
..Default::default() | ||
}, | ||
) | ||
.await | ||
.unwrap() | ||
.num_hits, | ||
0 | ||
); | ||
|
||
let prometheus_url = format!("{}metrics", client.base_url()); | ||
let response = reqwest::Client::new() | ||
.get(&prometheus_url) | ||
.send() | ||
.await | ||
.expect("Failed to send request"); | ||
|
||
assert!( | ||
response.status().is_success(), | ||
"Request failed with status {}", | ||
response.status(), | ||
); | ||
|
||
let body = response.text().await.expect("Failed to read response body"); | ||
// println!("Prometheus metrics:\n{}", body); | ||
let metrics = parse_prometheus_metrics(&body); | ||
// The assertions validate some very specific metrics. Feel free to add more as needed. | ||
{ | ||
let filtered_metrics = filter_metrics( | ||
&metrics, | ||
"quickwit_http_requests_total", | ||
vec![("method", "GET")], | ||
); | ||
assert_eq!(filtered_metrics.len(), 1); | ||
// we don't know exactly how many GET requests to expect as they are used to | ||
// poll the node state | ||
assert!(filtered_metrics[0].metric_value > 0.0); | ||
} | ||
{ | ||
let filtered_metrics = filter_metrics( | ||
&metrics, | ||
"quickwit_http_requests_total", | ||
vec![("method", "POST")], | ||
); | ||
assert_eq!(filtered_metrics.len(), 1); | ||
// 2 POST requests: create index + search | ||
assert_eq!(filtered_metrics[0].metric_value, 2.0); | ||
} | ||
{ | ||
let filtered_metrics = filter_metrics( | ||
&metrics, | ||
"quickwit_search_root_search_requests_total", | ||
vec![], | ||
); | ||
assert_eq!(filtered_metrics.len(), 1); | ||
assert_eq!(filtered_metrics[0].metric_value, 1.0); | ||
assert_eq!(filtered_metrics[0].labels.get("status").unwrap(), "success"); | ||
} | ||
sandbox.shutdown().await.unwrap(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah ok. Hmmm I don't think the ClusterSandboxBuilder is spawning separate processes, which means the prom registry is shared with all unit test. How is this working?