Skip to content

Commit

Permalink
map faas.* attributes to generic_task in resource mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Sep 19, 2023
1 parent a0f1b44 commit f235f3b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -59,3 +61,4 @@ class ResourceAttributes:
REGION = "region"
TASK_ID = "task_id"
ZONE = "zone"
UNKNOWN_SERVICE_PREFIX = "unknown_service"
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
9 changes: 9 additions & 0 deletions opentelemetry-resourcedetector-gcp/tests/test_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down

0 comments on commit f235f3b

Please sign in to comment.