From d8b2e5fc8182a23eb70a351c3d0b0a587e176a3b Mon Sep 17 00:00:00 2001 From: Jan Kuehle Date: Thu, 9 May 2024 17:41:06 +0200 Subject: [PATCH] Set cloud role and instance from k8s attributes --- CHANGELOG.md | 2 ++ src/lib.rs | 10 ++++++++++ src/tags.rs | 23 ++++++++++++++++++----- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 948ccad..b02c436 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +- Set tags Cloud role and Cloud role instance also from resource attributes `k8s.{deployment,replicaset,statefulset,job,cronjob,daemonset,pod}.name`. This matches [the behavior of the JS exporter](https://github.com/Azure/azure-sdk-for-js/blob/c66cad23c4b803719db65cb48a453b0adc13307b/sdk/monitor/monitor-opentelemetry-exporter/src/utils/common.ts#L75-L138). + ## [0.31.0] - 2024-05-09 - Change how the tags Could role, Cloud role instance, Application version and Internal SDK version are extracted: diff --git a/src/lib.rs b/src/lib.rs index 9b7fd9f..8df41ef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -170,6 +170,13 @@ async fn main() { //! | OpenTelemetry attribute key | Application Insights field | //! | ---------------------------------------------- | -------------------------------------------------------- | //! | `service.namespace` + `service.name` | Context: Cloud role (`ai.cloud.role`) | +//! | `k8s.deployment.name` | Context: Cloud role (`ai.cloud.role`) | +//! | `k8s.replicaset.name` | Context: Cloud role (`ai.cloud.role`) | +//! | `k8s.statefulset.name` | Context: Cloud role (`ai.cloud.role`) | +//! | `k8s.job.name` | Context: Cloud role (`ai.cloud.role`) | +//! | `k8s.cronjob.name` | Context: Cloud role (`ai.cloud.role`) | +//! | `k8s.daemonset.name` | Context: Cloud role (`ai.cloud.role`) | +//! | `k8s.pod.name` | Context: Cloud role instance (`ai.cloud.roleInstance`) | //! | `service.instance.id` | Context: Cloud role instance (`ai.cloud.roleInstance`) | //! | `device.id` | Context: Device id (`ai.device.id`) | //! | `device.model.name` | Context: Device model (`ai.device.model`) | @@ -177,6 +184,9 @@ async fn main() { //! | `telemetry.sdk.name` + `telemetry.sdk.version` | Context: Internal SDK version (`ai.internal.sdkVersion`) | //! | `ai.*` | Context: AppInsights Tag (`ai.*`) | //! +//! If `service.name` is the default (i.e. starts with "unknown_service:"), the Kubernetes based +//! values take precedence. +//! //! ## Spans //! //! The OpenTelemetry SpanKind determines the Application Insights telemetry type: diff --git a/src/tags.rs b/src/tags.rs index f4d139e..87854b7 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -124,14 +124,27 @@ fn build_tags_from_resource_attrs( cloud_role.insert_str(0, &service_namespace.as_str()); } + if service_name.as_str().starts_with("unknown_service:") { + if let Some(k8s_name) = attrs_map + .get(semcov::resource::K8S_DEPLOYMENT_NAME) + .or_else(|| attrs_map.get(semcov::resource::K8S_REPLICASET_NAME)) + .or_else(|| attrs_map.get(semcov::resource::K8S_STATEFULSET_NAME)) + .or_else(|| attrs_map.get(semcov::resource::K8S_JOB_NAME)) + .or_else(|| attrs_map.get(semcov::resource::K8S_CRONJOB_NAME)) + .or_else(|| attrs_map.get(semcov::resource::K8S_DAEMONSET_NAME)) + { + cloud_role = k8s_name.as_str().into_owned(); + } + } + tags.insert(tags::CLOUD_ROLE, cloud_role); } - if let Some(service_instance) = attrs_map.get(semcov::resource::SERVICE_INSTANCE_ID) { - tags.insert( - tags::CLOUD_ROLE_INSTANCE, - service_instance.as_str().into_owned(), - ); + if let Some(instance) = attrs_map + .get(semcov::resource::K8S_POD_NAME) + .or_else(|| attrs_map.get(semcov::resource::SERVICE_INSTANCE_ID)) + { + tags.insert(tags::CLOUD_ROLE_INSTANCE, instance.as_str().into_owned()); } if let Some(device_id) = attrs_map.get(semcov::resource::DEVICE_ID) {