diff --git a/doc/config.md b/doc/config.md index 876d12e..375bc22 100644 --- a/doc/config.md +++ b/doc/config.md @@ -148,3 +148,29 @@ Configures environment names and optional attributes (used once alerting the sta ## health_metrics Configures health metrics for components. + +Every entry names the Status Dashboard component the service is reported +against: `component_name` plus the attributes of the environment +(`environments[].attributes`, for example `region: EU-DE`). A service that is +published as one global component can override single attributes for itself +with `component_attributes`; an attribute with the same name replaces the +environment value, a new name is added. + +```yaml +health_metrics: + console: + service: console + component_name: "T Cloud Public Console" + category: global + component_attributes: + region: Global + metrics: + - console.api_down + - console.api_slow + - console.api_success_rate_low + expressions: + - expression: "console.api_slow || console.api_success_rate_low" + weight: 1 + - expression: "console.api_down" + weight: 2 +``` diff --git a/doc/schemas/config-schema.json b/doc/schemas/config-schema.json index 60d764f..9c77e2c 100644 --- a/doc/schemas/config-schema.json +++ b/doc/schemas/config-schema.json @@ -279,6 +279,17 @@ "category": { "type": "string" }, + "component_attributes": { + "description": "Overrides the environment attributes for components that are not bound to a region, such as the global sites.", + "default": null, + "type": [ + "object", + "null" + ], + "additionalProperties": { + "type": "string" + } + }, "component_name": { "type": [ "string", diff --git a/src/bin/reporter.rs b/src/bin/reporter.rs index 91c0f4d..d0a13cf 100644 --- a/src/bin/reporter.rs +++ b/src/bin/reporter.rs @@ -10,7 +10,7 @@ use anyhow::Context; use cloudmon_metrics::config::OidcIdentity; use cloudmon_metrics::sd::{ build_auth_headers, build_component_id_cache, build_incident_data, create_incident, - fetch_components, find_component_id, Component, ComponentAttribute, + fetch_components, find_component_id, merge_component_attributes, Component, ComponentAttribute, }; use cloudmon_metrics::{api::v1::ServiceHealthResponse, config::Config}; @@ -115,7 +115,10 @@ async fn metric_watcher( component.0.clone(), Component { name: name.clone(), - attributes: env_attrs.clone(), + attributes: merge_component_attributes( + &env_attrs, + component.1.component_attributes.as_ref(), + ), }, ); } diff --git a/src/common.rs b/src/common.rs index 9adeae7..94d547f 100644 --- a/src/common.rs +++ b/src/common.rs @@ -591,6 +591,7 @@ mod tests { service: service.to_string(), component_name: None, category: "test".to_string(), + component_attributes: None, metrics: metric_names, expressions: expression_defs, }, diff --git a/src/sd.rs b/src/sd.rs index 29f9319..86ce128 100644 --- a/src/sd.rs +++ b/src/sd.rs @@ -109,6 +109,31 @@ pub fn find_component_id(cache: &ComponentCache, target: &Component) -> Option>, +) -> Vec { + let mut merged = environment_attributes.to_vec(); + let overrides = match overrides { + Some(overrides) => overrides, + None => return merged, + }; + + for (name, value) in overrides { + let attribute = ComponentAttribute { + name: name.clone(), + value: value.clone(), + }; + match merged.iter().position(|attr| attr.name == attribute.name) { + Some(index) => merged[index] = attribute, + None => merged.push(attribute), + } + } + + merged.sort(); + merged +} + /// Build incident data structure for V2 API /// timestamp: metric timestamp in seconds since epoch pub fn build_incident_data(component_id: u32, impact: u8, timestamp: i64) -> IncidentData { @@ -155,3 +180,44 @@ pub async fn create_incident( Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + + fn attribute(name: &str, value: &str) -> ComponentAttribute { + ComponentAttribute { + name: name.to_string(), + value: value.to_string(), + } + } + + #[test] + fn merge_keeps_the_environment_attributes_without_overrides() { + let environment = vec![attribute("region", "EU-DE")]; + let merged = merge_component_attributes(&environment, None); + assert_eq!(merged, vec![attribute("region", "EU-DE")]); + } + + #[test] + fn merge_replaces_an_existing_attribute() { + let environment = vec![attribute("region", "EU-DE")]; + let overrides = HashMap::from([("region".to_string(), "Global".to_string())]); + let merged = merge_component_attributes(&environment, Some(&overrides)); + assert_eq!(merged, vec![attribute("region", "Global")]); + } + + #[test] + fn merge_adds_an_attribute_missing_from_the_environment() { + let environment = vec![attribute("region", "EU-DE")]; + let overrides = HashMap::from([("url".to_string(), "https://example.com".to_string())]); + let merged = merge_component_attributes(&environment, Some(&overrides)); + assert_eq!( + merged, + vec![ + attribute("region", "EU-DE"), + attribute("url", "https://example.com") + ] + ); + } +} diff --git a/src/types.rs b/src/types.rs index a185b2e..6a2e2ab 100644 --- a/src/types.rs +++ b/src/types.rs @@ -100,6 +100,10 @@ pub struct ServiceHealthDef { pub service: String, pub component_name: Option, pub category: String, + /// Overrides the environment attributes for components that are not bound + /// to a region, such as the global sites. + #[serde(default)] + pub component_attributes: Option>, pub metrics: Vec, pub expressions: Vec, } @@ -240,6 +244,7 @@ impl AppState { service: health_def.service.clone(), component_name: health_def.component_name.clone(), category: health_def.category.clone(), + component_attributes: health_def.component_attributes.clone(), metrics: health_def.metrics.clone(), expressions: Vec::new(), };