Skip to content

Commit

Permalink
Map faas.* attributes to generic_task in resource mapping (#273)
Browse files Browse the repository at this point in the history
* map faas.* attributes to generic_task in resource mapping
  • Loading branch information
dashpole authored Sep 28, 2023
1 parent a0f1b44 commit 1abfb2a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 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 @@ -168,7 +174,10 @@ def get_monitored_resource(
# fallback to generic_task
if (
ResourceAttributes.SERVICE_NAME in attrs
and ResourceAttributes.SERVICE_INSTANCE_ID in attrs
or ResourceAttributes.FAAS_NAME in attrs
) and (
ResourceAttributes.SERVICE_INSTANCE_ID in attrs
or ResourceAttributes.FAAS_INSTANCE in attrs
):
mr = _create_monitored_resource(_constants.GENERIC_TASK, attrs)
else:
Expand All @@ -186,10 +195,19 @@ 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 str(
resource_attrs[otel_key]
).startswith(_constants.UNKNOWN_SERVICE_PREFIX):
mr_value = resource_attrs[otel_key]
break

if (
mr_value is None
and ResourceAttributes.SERVICE_NAME in map_config.otel_keys
):
# The service name started with unknown_service, and was ignored above.
mr_value = resource_attrs.get(ResourceAttributes.SERVICE_NAME)

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,28 @@
'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 faas fallback]
dict({
'labels': dict({
'job': 'unknown_service',
'location': 'myregion',
'namespace': 'servicens',
'task_id': 'faasinstance',
}),
'type': 'generic_task',
})
# ---
# name: test_get_monitored_resource[generic task fallback region]
dict({
'labels': dict({
Expand Down
21 changes: 20 additions & 1 deletion opentelemetry-resourcedetector-gcp/tests/test_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,25 @@
},
id="generic task fallback global",
),
pytest.param(
{
"service.name": "unknown_service",
"cloud.region": "myregion",
"service.namespace": "servicens",
"faas.name": "faasname",
"faas.instance": "faasinstance",
},
id="generic task faas",
),
pytest.param(
{
"service.name": "unknown_service",
"cloud.region": "myregion",
"service.namespace": "servicens",
"faas.instance": "faasinstance",
},
id="generic task faas fallback",
),
# generic node
pytest.param(
{
Expand Down Expand Up @@ -213,7 +232,7 @@
def test_get_monitored_resource(
otel_attributes: Attributes, snapshot: SnapshotAssertion
) -> None:
resource = Resource.create(otel_attributes)
resource = Resource(otel_attributes)
monitored_resource_data = get_monitored_resource(resource)
as_dict = dataclasses.asdict(monitored_resource_data)
assert as_dict == snapshot
Expand Down

0 comments on commit 1abfb2a

Please sign in to comment.