Skip to content

Commit b1c81f1

Browse files
committed
metrics-cache: make push interval configurable
CMK-36784
1 parent 161155e commit b1c81f1

5 files changed

Lines changed: 50 additions & 2 deletions

File tree

charts/cmk-rustik/templates/metrics-cache/deployment.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ spec:
6363
{{- end }}
6464
{{- if .Values.push.enabled }}
6565
- --push-receiver={{ required "push.url is required when push mode is enabled" .Values.push.url }}
66+
- --push-interval={{ .Values.push.interval }}
6667
{{- if .Values.push.insecureSkipSiteCaVerification }}
6768
- --push-registration-insecure-skip-site-ca-verification
6869
{{- end }}

charts/cmk-rustik/values.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ push:
130130
# been warned. When set to true, will trust whatever push-agent receiver
131131
# answers at the configured URL, without verifying its identity.
132132
insecureSkipSiteCaVerification: false
133+
# interval is how often, in seconds, sections are generated and pushed to
134+
# the Checkmk site. Must be greater than zero.
135+
interval: 60
133136

134137
# pull allows you to configure pull mode, so that Checkmk's special agent can
135138
# query the agent directly.

metrics-cache/src/cli_args.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clap::Parser;
22
use regex::Regex;
33
use std::time::Duration;
4+
use thiserror::Error;
45

56
pub struct TlsConfig {
67
pub secret_name: Option<String>,
@@ -183,6 +184,14 @@ pub struct CliArgs {
183184
#[arg(long, env = "CMK_PULL_SHARED_SECRET", hide_env_values = true)]
184185
pub pull_shared_secret: Option<String>,
185186

187+
/// Push interval in seconds for push mode. Ignored if push mode is not enabled.
188+
#[arg(
189+
long = "push-interval",
190+
value_parser = parse_push_interval,
191+
default_value = "60"
192+
)]
193+
pub push_interval: Duration,
194+
186195
/// Enable push mode and send sections to the specified server (including
187196
/// port)
188197
#[arg(long = "push-receiver")]
@@ -278,6 +287,23 @@ fn parse_duration_days(arg: &str) -> Result<Duration, std::num::ParseIntError> {
278287
Ok(Duration::from_secs(60 * 60 * 24 * days))
279288
}
280289

290+
#[derive(Error, Debug)]
291+
enum PushIntervalError {
292+
#[error("must be a whole number of seconds")]
293+
NotANumber(#[from] std::num::ParseIntError),
294+
#[error("must be greater than zero")]
295+
Zero,
296+
}
297+
298+
fn parse_push_interval(arg: &str) -> Result<Duration, PushIntervalError> {
299+
let interval = parse_duration_secs(arg)?;
300+
// if 0, tokio panics
301+
match interval.is_zero() {
302+
true => Err(PushIntervalError::Zero),
303+
false => Ok(interval),
304+
}
305+
}
306+
281307
#[cfg(test)]
282308
mod tests {
283309
use super::*;
@@ -371,4 +397,21 @@ mod tests {
371397
);
372398
}
373399
}
400+
401+
#[test]
402+
fn push_interval_parse_correctly() {
403+
for interval in ["1", "80"] {
404+
assert!(parse(&["--push-interval", interval]).is_ok());
405+
}
406+
}
407+
408+
#[test]
409+
fn push_interval_cannot_be_zero() {
410+
assert_eq!(
411+
parse(&["--push-interval", "0"])
412+
.expect_err("push interval of zero should fail")
413+
.kind(),
414+
ErrorKind::ValueValidation
415+
);
416+
}
374417
}

metrics-cache/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ async fn main() -> anyhow::Result<()> {
152152
// Push to Checkmk server
153153
res = async {
154154
match push_client {
155-
Some(client) => push_loop(client, state.clone()).await,
155+
Some(client) => push_loop(client, state.clone(), args.push_interval).await,
156156
None => std::future::pending().await,
157157
}
158158
} => {

metrics-cache/src/push/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ async fn push_cycle(
6868
pub async fn push_loop(
6969
client: CheckmkPushClient,
7070
state: AppState<impl TokenValidator>,
71+
push_interval: Duration,
7172
) -> Result<(), WriterDropped> {
7273
state.stores.wait_until_all_ready().await?;
73-
let mut interval = time::interval(Duration::from_secs(60)); // TODO: Unhardcode
74+
let mut interval = time::interval(push_interval);
7475
loop {
7576
interval.tick().await; // note: The very first tick() is no-op
7677
match push_cycle(&client, &state).await {

0 commit comments

Comments
 (0)