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 Aug 31, 2023
1 parent b0ca7de commit 4f2b35b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 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 @@ -186,7 +195,12 @@ def _create_monitored_resource(
for mr_key, map_config in mapping.items():
mr_value = None
for otel_key in map_config.otel_keys:
if otel_key in resource_attrs:
if otel_key in resource_attrs and not (
isinstance(resource_attrs[otel_key], str)
and str(resource_attrs[otel_key]).startswith(
_constants.UNKNOWN_SERVICE_PREFIX
)
):
mr_value = resource_attrs[otel_key]
break

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 4f2b35b

Please sign in to comment.