feat: add service monitors support to prom #66

Merged
johnride merged 9 commits from monitoring_servicemonitor into master 2025-07-02 15:29:23 +00:00
3 changed files with 57 additions and 38 deletions
Showing only changes of commit ca05c67a8e - Show all commits

View File

@ -20,8 +20,6 @@ use harmony::{
topology::{K8sAnywhereTopology, Url},
};
use harmony_macros::http_scheme;
#[tokio::main]
async fn main() {
let discord_receiver = DiscordWebhook {
@ -47,33 +45,23 @@ async fn main() {
let service_monitor_endpoint = ServiceMonitorEndpoint {
port: Some("80".to_string()),
target_port: None,
bearer_token_file: None,
interval: None,
path: "/metrics".to_string(),
scheme: http_scheme!("http"),
tls_config: None,
metric_relabelings: vec![],
relabelings: vec![],
scheme: HTTPScheme::HTTP,
..Default::default()
};
let service_monitor = ServiceMonitor {
name: "test-service-monitor".to_string(),
additional_labels: None,
job_label: None,
target_labels: vec![],
pod_target_labels: vec![],
selector: Selector {
match_labels: HashMap::new(),
match_expressions: vec![MatchExpression {
key: "test".to_string(),
operator: "In".to_string(),
operator: Operator::In,
values: vec!["test-service".to_string()],
}],
},
namespace_selector: None,
endpoints: vec![service_monitor_endpoint],
fallback_scrape_protocol: None,
..Default::default()
};
let alerting_score = HelmPrometheusAlertingScore {
receivers: vec![Box::new(discord_receiver)],

View File

@ -54,6 +54,22 @@ pub struct AlertGroup {
pub groups: Vec<AlertManagerRuleGroup>,
}
#[derive(Debug, Clone, Serialize)]
pub enum HTTPScheme {
#[serde(rename = "http")]
HTTP,
#[serde(rename = "https")]
HTTPS,
johnride marked this conversation as resolved Outdated

Not a String, should be a path type of some sort. I think we already handle this somewhere else correctly.

You might have to implement Serialize for it or wrap it into another type that Serializes as a String but that's ok.

Not a String, should be a path type of some sort. I think we already handle this somewhere else correctly. You might have to implement Serialize for it or wrap it into another type that Serializes as a String but that's ok.
}
#[derive(Debug, Clone, Serialize)]
pub enum Operator {
In,
NotIn,
Exists,
DoesNotExist,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PrometheusConfigValues {
@ -116,7 +132,7 @@ pub struct ServiceMonitorEndpoint {
// ## HTTP scheme to use for scraping
// ##
pub scheme: String,
pub scheme: HTTPScheme,
taha marked this conversation as resolved Outdated

I guess operator is not any string? Probably should be an enum too.

I guess operator is not any string? Probably should be an enum too.
// ## TLS configuration to use when scraping the endpoint
// ##
@ -146,7 +162,7 @@ pub struct ServiceMonitorEndpoint {
#[serde(rename_all = "camelCase")]
pub struct MatchExpression {
pub key: String,
pub operator: String,
pub operator: Operator,
pub values: Vec<String>,
}
@ -201,3 +217,38 @@ pub struct ServiceMonitor {
// # ref: https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api-reference/api.md#monitoring.coreos.com/v1.ScrapeProtocol
pub fallback_scrape_protocol: Option<String>,
}
impl Default for ServiceMonitor {
fn default() -> Self {
Self {
name: Default::default(),
additional_labels: Default::default(),
job_label: Default::default(),
target_labels: Default::default(),
pod_target_labels: Default::default(),
selector: Selector {
match_labels: HashMap::new(),
match_expressions: vec![],
},
namespace_selector: Default::default(),
endpoints: Default::default(),
fallback_scrape_protocol: Default::default(),
}
}
}
impl Default for ServiceMonitorEndpoint {
fn default() -> Self {
Self {
port: Some("80".to_string()),
target_port: Default::default(),
bearer_token_file: Default::default(),
interval: Default::default(),
path: "/metrics".to_string(),
scheme: HTTPScheme::HTTP,
tls_config: Default::default(),
metric_relabelings: Default::default(),
relabelings: Default::default(),
}
}
}

View File

@ -133,26 +133,6 @@ pub fn ingress_path(input: TokenStream) -> TokenStream {
}
}
/// Verify that a string is a valid http scheme
/// Panics if not http or https
#[proc_macro]
pub fn http_scheme(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as LitStr);
let scheme_str = input.value();
if scheme_str.to_lowercase() == "http" {
let expanded = quote! {(#scheme_str.to_lowercase().to_string()) };
return TokenStream::from(expanded);
}
if scheme_str.to_lowercase() == "https" {
let expanded = quote! {(#scheme_str.to_lowercase().to_string()) };
return TokenStream::from(expanded);
}
panic!("Invalid HTTP scheme")
}
#[proc_macro]
pub fn cidrv4(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as LitStr);