Skip to content
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

feat: Support prometheus remote write protocol for metrics #563

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: bare bones push metrics for prometheus
  • Loading branch information
sighphyre committed Oct 14, 2024
commit 255480ae2549696ab17d19c3499aa35cf56c6696
59 changes: 55 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ opentelemetry_sdk = { version = "0.24.0", features = [
"serde_json",
"logs",
] }
prometheus = { version = "0.13.4", features = ["process"] }
prometheus = { version = "0.13.4", features = ["process", "push"] }
prometheus-static-metric = "0.5.1"
rand = "0.8.5"
redis = { version = "0.27.0", features = [
Expand Down
2 changes: 2 additions & 0 deletions server/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ mod tests {
},
upstream_certificate_file: Default::default(),
token_revalidation_interval_seconds: Default::default(),
prometheus_push_interval: 60,
prometheus_push_gateway: None,
};

let result = build_edge(&args, "test-app").await;
Expand Down
8 changes: 8 additions & 0 deletions server/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ pub struct EdgeArgs {
/// If set to true, Edge starts with dynamic behavior. Dynamic behavior means that Edge will accept tokens outside the scope of the startup tokens
#[clap(long, env, default_value_t = false, conflicts_with = "strict")]
pub dynamic: bool,

/// Sets a push gateway url for prometheus metrics, if this is set, prometheus metrics will be automatically pushed
#[clap(long, env)]
pub prometheus_push_gateway: Option<String>,

/// Sets the interval for prometheus push metrics, only relevant if `prometheus_push_gateway` is set. Defaults to 60 seconds
#[clap(long, env, default_value_t = 60)]
pub prometheus_push_interval: u64,
}

pub fn string_to_header_tuple(s: &str) -> Result<(String, String), String> {
Expand Down
35 changes: 35 additions & 0 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ async fn main() -> Result<(), anyhow::Error> {
_ = validator.schedule_revalidation_of_startup_tokens(edge.tokens, lazy_feature_refresher) => {
tracing::info!("Token validator validation of startup tokens was unexpectedly shut down");
}
_ = push_prom(edge.prometheus_push_gateway, edge.prometheus_push_interval) => {
tracing::info!("Prometheus push unexpectedly shut down");
}
}
}
cli::EdgeMode::Offline(offline_args) if offline_args.reload_interval > 0 => {
Expand All @@ -190,6 +193,38 @@ async fn main() -> Result<(), anyhow::Error> {
Ok(())
}

async fn push_prom(url: Option<String>, interval: u64) {
let sleep_duration = tokio::time::Duration::from_secs(interval);
if let Some(address) = url {
loop {
tokio::select! {
_ = tokio::time::sleep(sleep_duration) => {
let encoder = prometheus::TextEncoder::new();
let metric_families = prometheus::gather();
let mut buf = String::new();
encoder.encode_utf8(&metric_families[..], &mut buf).expect("Could not serialize metrics");
let client = reqwest::Client::new();
match client.post(address.clone()).body(buf).send().await {
Ok(_) => {
tracing::info!("Successfully posted data")
}
Err(e) => {
tracing::error!("Err, arg {e:?}")
}
}
}
}
}
} else {
loop {
tokio::select! {
_ = tokio::time::sleep(sleep_duration) => {
}
}
}
}
}

#[cfg(not(tarpaulin_include))]
async fn clean_shutdown(
persistence: Option<Arc<dyn EdgePersistence>>,
Expand Down