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
58 changes: 34 additions & 24 deletions src/spanner/src/observability/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,16 +325,11 @@ fn key_values_to_metric_labels<'a>(
}

pub(crate) fn resource_to_monitored_resource(resource: &Resource) -> MonitoredResource {
let mut labels = HashMap::new();
for (key, val) in resource.iter() {
let key_str = key.as_str();
match key_str {
"instance_id" | "location" | "instance_config" | "client_hash" => {
labels.insert(key_str.to_string(), value_to_string(val));
}
_ => {}
}
}
let labels: HashMap<String, String> = resource
.iter()
.filter(|(key, _)| is_monitored_resource_label(key.as_str()))
.map(|(key, value)| (key.as_str().to_string(), value_to_string(value)))
.collect();

MonitoredResource::new()
.set_type(SPANNER_RESOURCE_TYPE.to_string())
Expand Down Expand Up @@ -524,33 +519,48 @@ mod tests {

let monitored_res = super::resource_to_monitored_resource(&resource);

assert_eq!(monitored_res.r#type, "spanner_instance_client");
assert_eq!(
monitored_res.labels.get("instance_id").map(|s| s.as_str()),
Some("my-instance")
monitored_res.r#type, "spanner_instance_client",
"Resource type must be spanner_instance_client"
);
assert_eq!(
monitored_res.labels.get("project_id").map(String::as_str),
Some("my-project"),
"MonitoredResource must contain project_id"
);
assert_eq!(
monitored_res.labels.get("instance_id").map(String::as_str),
Some("my-instance"),
"MonitoredResource must contain instance_id"
);
assert_eq!(
monitored_res.labels.get("location").map(|s| s.as_str()),
Some("us-central1")
monitored_res.labels.get("location").map(String::as_str),
Some("us-central1"),
"MonitoredResource must contain location"
);
assert_eq!(
monitored_res
.labels
.get("instance_config")
.map(|s| s.as_str()),
Some("regional-us-central1")
.map(String::as_str),
Some("regional-us-central1"),
"MonitoredResource must contain instance_config"
);
assert_eq!(
monitored_res.labels.get("client_hash").map(|s| s.as_str()),
Some("abc1234")
monitored_res.labels.get("client_hash").map(String::as_str),
Some("abc1234"),
"MonitoredResource must contain client_hash"
);

// project_id must be excluded
assert!(!monitored_res.labels.contains_key("project_id"));

// Unrelated OpenTelemetry resource attributes must be filtered out
assert!(!monitored_res.labels.contains_key("service.name"));
assert!(!monitored_res.labels.contains_key("telemetry.sdk.version"));
assert!(
!monitored_res.labels.contains_key("service.name"),
"service.name must be filtered out"
);
assert!(
!monitored_res.labels.contains_key("telemetry.sdk.version"),
"telemetry.sdk.version must be filtered out"
);
}

#[test]
Expand Down
11 changes: 10 additions & 1 deletion src/spanner/src/observability/mock_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use tokio::sync::mpsc::{Receiver, channel};
use tokio::task::JoinHandle;

const TEST_PROJECT: &str = "test-project";
const TEST_CLIENT_UID: &str = "test-uid-12345";
const TEST_CLIENT_HASH: &str = "abc1234";
const TEST_DATABASE: &str = "test-database";
Expand Down Expand Up @@ -226,6 +227,14 @@ async fn streaming_sql_happy_path_records_all_metrics_and_time_series() -> anyho
monitored_resource.r#type, "spanner_instance_client",
"Resource type must be spanner_instance_client"
);
assert_eq!(
monitored_resource
.labels
.get("project_id")
.map(String::as_str),
Some(TEST_PROJECT),
"MonitoredResource must contain project_id"
);
assert_eq!(
monitored_resource
.labels
Expand Down Expand Up @@ -2183,7 +2192,7 @@ async fn setup_mock_client_with_metrics(

let resource = Resource::builder()
.with_attributes([
KeyValue::new("project_id", "test-project"),
KeyValue::new("project_id", TEST_PROJECT),
KeyValue::new("instance_id", TEST_INSTANCE),
KeyValue::new("location", TEST_LOCATION),
KeyValue::new("instance_config", TEST_INSTANCE_CONFIG),
Expand Down
Loading