From f235f3b5064bdc6cdf2228887927a827c0dd08b2 Mon Sep 17 00:00:00 2001 From: David Ashpole Date: Tue, 22 Aug 2023 17:29:33 +0000 Subject: [PATCH] map faas.* attributes to generic_task in resource mapping --- .../gcp_resource_detector/_constants.py | 3 ++ .../gcp_resource_detector/_mapping.py | 31 ++++++++++++++++--- .../tests/__snapshots__/test_mapping.ambr | 11 +++++++ .../tests/test_mapping.py | 9 ++++++ 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/opentelemetry-resourcedetector-gcp/src/opentelemetry/resourcedetector/gcp_resource_detector/_constants.py b/opentelemetry-resourcedetector-gcp/src/opentelemetry/resourcedetector/gcp_resource_detector/_constants.py index 73ed4fd6..27f74707 100644 --- a/opentelemetry-resourcedetector-gcp/src/opentelemetry/resourcedetector/gcp_resource_detector/_constants.py +++ b/opentelemetry-resourcedetector-gcp/src/opentelemetry/resourcedetector/gcp_resource_detector/_constants.py @@ -35,6 +35,8 @@ class ResourceAttributes: SERVICE_INSTANCE_ID = "service.instance.id" SERVICE_NAME = "service.name" SERVICE_NAMESPACE = "service.namespace" + FAAS_INSTANCE = "faas.instance" + FAAS_NAME = "faas.name" AWS_ACCOUNT = "aws_account" @@ -59,3 +61,4 @@ class ResourceAttributes: REGION = "region" TASK_ID = "task_id" ZONE = "zone" +UNKNOWN_SERVICE_PREFIX = "unknown_service" diff --git a/opentelemetry-resourcedetector-gcp/src/opentelemetry/resourcedetector/gcp_resource_detector/_mapping.py b/opentelemetry-resourcedetector-gcp/src/opentelemetry/resourcedetector/gcp_resource_detector/_mapping.py index 1c22e66c..c7e107a1 100644 --- a/opentelemetry-resourcedetector-gcp/src/opentelemetry/resourcedetector/gcp_resource_detector/_mapping.py +++ b/opentelemetry-resourcedetector-gcp/src/opentelemetry/resourcedetector/gcp_resource_detector/_mapping.py @@ -110,8 +110,14 @@ def __init__(self, *otel_keys: str, fallback: str = ""): fallback="global", ), _constants.NAMESPACE: MapConfig(ResourceAttributes.SERVICE_NAMESPACE), - _constants.JOB: MapConfig(ResourceAttributes.SERVICE_NAME), - _constants.TASK_ID: MapConfig(ResourceAttributes.SERVICE_INSTANCE_ID), + _constants.JOB: MapConfig( + ResourceAttributes.SERVICE_NAME, + ResourceAttributes.FAAS_NAME, + ), + _constants.TASK_ID: MapConfig( + ResourceAttributes.SERVICE_INSTANCE_ID, + ResourceAttributes.FAAS_INSTANCE, + ), }, _constants.GENERIC_NODE: { _constants.LOCATION: MapConfig( @@ -169,6 +175,9 @@ def get_monitored_resource( if ( ResourceAttributes.SERVICE_NAME in attrs and ResourceAttributes.SERVICE_INSTANCE_ID in attrs + ) or ( + ResourceAttributes.FAAS_NAME in attrs + and ResourceAttributes.FAAS_INSTANCE in attrs ): mr = _create_monitored_resource(_constants.GENERIC_TASK, attrs) else: @@ -185,10 +194,24 @@ def _create_monitored_resource( for mr_key, map_config in mapping.items(): mr_value = None + fallback_value = None for otel_key in map_config.otel_keys: if otel_key in resource_attrs: - mr_value = resource_attrs[otel_key] - break + if ( + otel_key == ResourceAttributes.SERVICE_NAME + and isinstance(resource_attrs[otel_key], str) + and str(resource_attrs[otel_key]).startswith( + _constants.UNKNOWN_SERVICE_PREFIX + ) + ): + # Only use values with an unknown_service prefix as the last resort + fallback_value = resource_attrs[otel_key] + else: + mr_value = resource_attrs[otel_key] + break + + if mr_value is None: + mr_value = fallback_value if mr_value is None: mr_value = map_config.fallback diff --git a/opentelemetry-resourcedetector-gcp/tests/__snapshots__/test_mapping.ambr b/opentelemetry-resourcedetector-gcp/tests/__snapshots__/test_mapping.ambr index 4a3f2777..ea297d8c 100644 --- a/opentelemetry-resourcedetector-gcp/tests/__snapshots__/test_mapping.ambr +++ b/opentelemetry-resourcedetector-gcp/tests/__snapshots__/test_mapping.ambr @@ -98,6 +98,17 @@ 'type': 'generic_task', }) # --- +# name: test_get_monitored_resource[generic task faas] + dict({ + 'labels': dict({ + 'job': 'faasname', + 'location': 'myregion', + 'namespace': 'servicens', + 'task_id': 'faasinstance', + }), + 'type': 'generic_task', + }) +# --- # name: test_get_monitored_resource[generic task fallback region] dict({ 'labels': dict({ diff --git a/opentelemetry-resourcedetector-gcp/tests/test_mapping.py b/opentelemetry-resourcedetector-gcp/tests/test_mapping.py index f8ed7131..fc9262e8 100644 --- a/opentelemetry-resourcedetector-gcp/tests/test_mapping.py +++ b/opentelemetry-resourcedetector-gcp/tests/test_mapping.py @@ -164,6 +164,15 @@ }, id="generic task fallback global", ), + pytest.param( + { + "cloud.region": "myregion", + "service.namespace": "servicens", + "faas.name": "faasname", + "faas.instance": "faasinstance", + }, + id="generic task faas", + ), # generic node pytest.param( {