Skip to content

Commit

Permalink
Merge pull request #72 from frigus02/resource-attrs
Browse files Browse the repository at this point in the history
Set cloud role and instance from k8s attributes
  • Loading branch information
frigus02 authored May 9, 2024
2 parents 542e80b + d8b2e5f commit 8fe79de
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,23 @@ 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`) |
//! | `service.version` | Context: Application version (`ai.application.ver`) |
//! | `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:
Expand Down
23 changes: 18 additions & 5 deletions src/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 8fe79de

Please sign in to comment.