Skip to content

Commit 1abfb2a

Browse files
authored
Map faas.* attributes to generic_task in resource mapping (#273)
* map faas.* attributes to generic_task in resource mapping
1 parent a0f1b44 commit 1abfb2a

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

opentelemetry-resourcedetector-gcp/src/opentelemetry/resourcedetector/gcp_resource_detector/_constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class ResourceAttributes:
3535
SERVICE_INSTANCE_ID = "service.instance.id"
3636
SERVICE_NAME = "service.name"
3737
SERVICE_NAMESPACE = "service.namespace"
38+
FAAS_INSTANCE = "faas.instance"
39+
FAAS_NAME = "faas.name"
3840

3941

4042
AWS_ACCOUNT = "aws_account"
@@ -59,3 +61,4 @@ class ResourceAttributes:
5961
REGION = "region"
6062
TASK_ID = "task_id"
6163
ZONE = "zone"
64+
UNKNOWN_SERVICE_PREFIX = "unknown_service"

opentelemetry-resourcedetector-gcp/src/opentelemetry/resourcedetector/gcp_resource_detector/_mapping.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,14 @@ def __init__(self, *otel_keys: str, fallback: str = ""):
110110
fallback="global",
111111
),
112112
_constants.NAMESPACE: MapConfig(ResourceAttributes.SERVICE_NAMESPACE),
113-
_constants.JOB: MapConfig(ResourceAttributes.SERVICE_NAME),
114-
_constants.TASK_ID: MapConfig(ResourceAttributes.SERVICE_INSTANCE_ID),
113+
_constants.JOB: MapConfig(
114+
ResourceAttributes.SERVICE_NAME,
115+
ResourceAttributes.FAAS_NAME,
116+
),
117+
_constants.TASK_ID: MapConfig(
118+
ResourceAttributes.SERVICE_INSTANCE_ID,
119+
ResourceAttributes.FAAS_INSTANCE,
120+
),
115121
},
116122
_constants.GENERIC_NODE: {
117123
_constants.LOCATION: MapConfig(
@@ -168,7 +174,10 @@ def get_monitored_resource(
168174
# fallback to generic_task
169175
if (
170176
ResourceAttributes.SERVICE_NAME in attrs
171-
and ResourceAttributes.SERVICE_INSTANCE_ID in attrs
177+
or ResourceAttributes.FAAS_NAME in attrs
178+
) and (
179+
ResourceAttributes.SERVICE_INSTANCE_ID in attrs
180+
or ResourceAttributes.FAAS_INSTANCE in attrs
172181
):
173182
mr = _create_monitored_resource(_constants.GENERIC_TASK, attrs)
174183
else:
@@ -186,10 +195,19 @@ def _create_monitored_resource(
186195
for mr_key, map_config in mapping.items():
187196
mr_value = None
188197
for otel_key in map_config.otel_keys:
189-
if otel_key in resource_attrs:
198+
if otel_key in resource_attrs and not str(
199+
resource_attrs[otel_key]
200+
).startswith(_constants.UNKNOWN_SERVICE_PREFIX):
190201
mr_value = resource_attrs[otel_key]
191202
break
192203

204+
if (
205+
mr_value is None
206+
and ResourceAttributes.SERVICE_NAME in map_config.otel_keys
207+
):
208+
# The service name started with unknown_service, and was ignored above.
209+
mr_value = resource_attrs.get(ResourceAttributes.SERVICE_NAME)
210+
193211
if mr_value is None:
194212
mr_value = map_config.fallback
195213

opentelemetry-resourcedetector-gcp/tests/__snapshots__/test_mapping.ambr

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,28 @@
9898
'type': 'generic_task',
9999
})
100100
# ---
101+
# name: test_get_monitored_resource[generic task faas]
102+
dict({
103+
'labels': dict({
104+
'job': 'faasname',
105+
'location': 'myregion',
106+
'namespace': 'servicens',
107+
'task_id': 'faasinstance',
108+
}),
109+
'type': 'generic_task',
110+
})
111+
# ---
112+
# name: test_get_monitored_resource[generic task faas fallback]
113+
dict({
114+
'labels': dict({
115+
'job': 'unknown_service',
116+
'location': 'myregion',
117+
'namespace': 'servicens',
118+
'task_id': 'faasinstance',
119+
}),
120+
'type': 'generic_task',
121+
})
122+
# ---
101123
# name: test_get_monitored_resource[generic task fallback region]
102124
dict({
103125
'labels': dict({

opentelemetry-resourcedetector-gcp/tests/test_mapping.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,25 @@
164164
},
165165
id="generic task fallback global",
166166
),
167+
pytest.param(
168+
{
169+
"service.name": "unknown_service",
170+
"cloud.region": "myregion",
171+
"service.namespace": "servicens",
172+
"faas.name": "faasname",
173+
"faas.instance": "faasinstance",
174+
},
175+
id="generic task faas",
176+
),
177+
pytest.param(
178+
{
179+
"service.name": "unknown_service",
180+
"cloud.region": "myregion",
181+
"service.namespace": "servicens",
182+
"faas.instance": "faasinstance",
183+
},
184+
id="generic task faas fallback",
185+
),
167186
# generic node
168187
pytest.param(
169188
{
@@ -213,7 +232,7 @@
213232
def test_get_monitored_resource(
214233
otel_attributes: Attributes, snapshot: SnapshotAssertion
215234
) -> None:
216-
resource = Resource.create(otel_attributes)
235+
resource = Resource(otel_attributes)
217236
monitored_resource_data = get_monitored_resource(resource)
218237
as_dict = dataclasses.asdict(monitored_resource_data)
219238
assert as_dict == snapshot

0 commit comments

Comments
 (0)