Skip to content

Commit

Permalink
feat: add victoria_metrics module (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
level-one-learner authored Dec 29, 2023
1 parent 662aae9 commit 3d09ac4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ postgres = []
rabbitmq = []
redis = []
trufflesuite_ganachecli = []
victoria_metrics = []
zookeeper = []

[dependencies]
Expand All @@ -52,7 +53,7 @@ postgres = "0.19.7"
pretty_env_logger = "0.5.0"
rdkafka = "0.36.0"
redis = "0.24.0"
reqwest = { version = "0.11.20", features = ["blocking"] }
reqwest = { version = "0.11.20", features = ["blocking", "json"] }
serde = { version = "1.0.188", features = [ "derive" ] }
serde_json = "1.0.107"
tokio = { version = "1", features = ["macros"] }
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub mod redis;
#[cfg(feature = "trufflesuite_ganachecli")]
#[cfg_attr(docsrs, doc(cfg(feature = "trufflesuite_ganachecli")))]
pub mod trufflesuite_ganachecli;
#[cfg(feature = "victoria_metrics")]
#[cfg_attr(docsrs, doc(cfg(feature = "victoria_metrics")))]
pub mod victoria_metrics;
#[cfg(feature = "zookeeper")]
#[cfg_attr(docsrs, doc(cfg(feature = "zookeeper")))]
pub mod zookeeper;
Expand Down
73 changes: 73 additions & 0 deletions src/victoria_metrics/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use testcontainers::{core::WaitFor, Image};

const NAME: &str = "victoriametrics/victoria-metrics";
const TAG: &str = "v1.96.0";

/// Module to work with [`VictoriaMetrics`] inside of tests.
///
/// Starts an instance of single-node VictoriaMetrics.
///
/// This module is based on the official [`VictoriaMetrics docker image`].
///
/// # Example
/// ```
/// use testcontainers::clients;
/// use testcontainers_modules::victoria_metrics;
///
/// let docker = clients::Cli::default();
/// let victoria_metrics_instance = docker.run(victoria_metrics::VictoriaMetrics);
///
/// let import_url = format!("http://127.0.0.1:{}/api/v1/import", victoria_metrics_instance.get_host_port_ipv4(8428));
/// let export_url = format!("http://127.0.0.1:{}/api/v1/export", victoria_metrics_instance.get_host_port_ipv4(8428));
///
/// // operate on the import and export URLs..
/// ```
///
/// [`VictoriaMetrics`]: https://docs.victoriametrics.com/
/// [`VictoriaMetrics API examples`]: https://docs.victoriametrics.com/url-examples.html#victoriametrics-api-examples
/// [`VictoriaMetrics Docker image`]: https://hub.docker.com/r/victoriametrics/victoria-metrics
#[derive(Debug, Default)]
pub struct VictoriaMetrics;

impl Image for VictoriaMetrics {
type Args = ();

fn name(&self) -> String {
NAME.to_owned()
}

fn tag(&self) -> String {
TAG.to_owned()
}

fn ready_conditions(&self) -> Vec<WaitFor> {
vec![
WaitFor::message_on_stderr("started VictoriaMetrics"),
WaitFor::message_on_stderr(
"pprof handlers are exposed at http://127.0.0.1:8428/debug/pprof/",
),
]
}
}

#[cfg(test)]
mod tests {
use crate::victoria_metrics::VictoriaMetrics as VictoriaMetricsImage;
use testcontainers::clients;

#[test]
fn query_buildinfo() {
let docker = clients::Cli::default();
let node = docker.run(VictoriaMetricsImage);
let host_port = node.get_host_port_ipv4(8428);
let url = format!("http://127.0.0.1:{}/api/v1/status/buildinfo", host_port);

let response = reqwest::blocking::get(url)
.unwrap()
.json::<serde_json::Value>()
.unwrap();
let version = response["data"]["version"].as_str().unwrap();

assert_eq!(version, "2.24.0");
}
}

0 comments on commit 3d09ac4

Please sign in to comment.