Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 26 additions & 0 deletions doc/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
11 changes: 11 additions & 0 deletions doc/schemas/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 5 additions & 2 deletions src/bin/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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(),
),
},
);
}
Expand Down
1 change: 1 addition & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
66 changes: 66 additions & 0 deletions src/sd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,31 @@ pub fn find_component_id(cache: &ComponentCache, target: &Component) -> Option<u
.map(|((_name, _attrs), id)| *id)
}

pub fn merge_component_attributes(
environment_attributes: &[ComponentAttribute],
overrides: Option<&HashMap<String, String>>,
) -> Vec<ComponentAttribute> {
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 {
Expand Down Expand Up @@ -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")
]
);
}
}
5 changes: 5 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ pub struct ServiceHealthDef {
pub service: String,
pub component_name: Option<String>,
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<HashMap<String, String>>,
pub metrics: Vec<String>,
pub expressions: Vec<MetricExpressionDef>,
}
Expand Down Expand Up @@ -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(),
};
Expand Down
Loading