Skip to content

Commit 75c73d1

Browse files
authored
refactor aws lambda span (#3541)
* refactor: migrate http_attributes from SpanAttributes to new semantic conventions and refactor tests * fix precommit linting * refactor aws lambda to use new semantics * refactor tests * update resourceattributes to cloud attrs * update main for attrs
1 parent a164d37 commit 75c73d1

File tree

2 files changed

+77
-49
lines changed

2 files changed

+77
-49
lines changed

instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,25 @@ def custom_event_context_extractor(lambda_event):
8686
from opentelemetry.instrumentation.utils import unwrap
8787
from opentelemetry.metrics import MeterProvider, get_meter_provider
8888
from opentelemetry.propagate import get_global_textmap
89-
from opentelemetry.semconv.resource import ResourceAttributes
90-
from opentelemetry.semconv.trace import SpanAttributes
89+
from opentelemetry.semconv._incubating.attributes.cloud_attributes import (
90+
CLOUD_ACCOUNT_ID,
91+
CLOUD_RESOURCE_ID,
92+
)
93+
from opentelemetry.semconv._incubating.attributes.faas_attributes import (
94+
FAAS_INVOCATION_ID,
95+
FAAS_TRIGGER,
96+
)
97+
from opentelemetry.semconv._incubating.attributes.http_attributes import (
98+
HTTP_METHOD,
99+
HTTP_ROUTE,
100+
HTTP_SCHEME,
101+
HTTP_STATUS_CODE,
102+
HTTP_TARGET,
103+
HTTP_USER_AGENT,
104+
)
105+
from opentelemetry.semconv._incubating.attributes.net_attributes import (
106+
NET_HOST_NAME,
107+
)
91108
from opentelemetry.trace import (
92109
Span,
93110
SpanKind,
@@ -171,38 +188,34 @@ def _set_api_gateway_v1_proxy_attributes(
171188
More info:
172189
https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
173190
"""
174-
span.set_attribute(
175-
SpanAttributes.HTTP_METHOD, lambda_event.get("httpMethod")
176-
)
191+
span.set_attribute(HTTP_METHOD, lambda_event.get("httpMethod"))
177192

178193
if lambda_event.get("headers"):
179194
if "User-Agent" in lambda_event["headers"]:
180195
span.set_attribute(
181-
SpanAttributes.HTTP_USER_AGENT,
196+
HTTP_USER_AGENT,
182197
lambda_event["headers"]["User-Agent"],
183198
)
184199
if "X-Forwarded-Proto" in lambda_event["headers"]:
185200
span.set_attribute(
186-
SpanAttributes.HTTP_SCHEME,
201+
HTTP_SCHEME,
187202
lambda_event["headers"]["X-Forwarded-Proto"],
188203
)
189204
if "Host" in lambda_event["headers"]:
190205
span.set_attribute(
191-
SpanAttributes.NET_HOST_NAME,
206+
NET_HOST_NAME,
192207
lambda_event["headers"]["Host"],
193208
)
194209
if "resource" in lambda_event:
195-
span.set_attribute(SpanAttributes.HTTP_ROUTE, lambda_event["resource"])
210+
span.set_attribute(HTTP_ROUTE, lambda_event["resource"])
196211

197212
if lambda_event.get("queryStringParameters"):
198213
span.set_attribute(
199-
SpanAttributes.HTTP_TARGET,
214+
HTTP_TARGET,
200215
f"{lambda_event['resource']}?{urlencode(lambda_event['queryStringParameters'])}",
201216
)
202217
else:
203-
span.set_attribute(
204-
SpanAttributes.HTTP_TARGET, lambda_event["resource"]
205-
)
218+
span.set_attribute(HTTP_TARGET, lambda_event["resource"])
206219

207220
return span
208221

@@ -217,34 +230,34 @@ def _set_api_gateway_v2_proxy_attributes(
217230
"""
218231
if "domainName" in lambda_event["requestContext"]:
219232
span.set_attribute(
220-
SpanAttributes.NET_HOST_NAME,
233+
NET_HOST_NAME,
221234
lambda_event["requestContext"]["domainName"],
222235
)
223236

224237
if lambda_event["requestContext"].get("http"):
225238
if "method" in lambda_event["requestContext"]["http"]:
226239
span.set_attribute(
227-
SpanAttributes.HTTP_METHOD,
240+
HTTP_METHOD,
228241
lambda_event["requestContext"]["http"]["method"],
229242
)
230243
if "userAgent" in lambda_event["requestContext"]["http"]:
231244
span.set_attribute(
232-
SpanAttributes.HTTP_USER_AGENT,
245+
HTTP_USER_AGENT,
233246
lambda_event["requestContext"]["http"]["userAgent"],
234247
)
235248
if "path" in lambda_event["requestContext"]["http"]:
236249
span.set_attribute(
237-
SpanAttributes.HTTP_ROUTE,
250+
HTTP_ROUTE,
238251
lambda_event["requestContext"]["http"]["path"],
239252
)
240253
if lambda_event.get("rawQueryString"):
241254
span.set_attribute(
242-
SpanAttributes.HTTP_TARGET,
255+
HTTP_TARGET,
243256
f"{lambda_event['requestContext']['http']['path']}?{lambda_event['rawQueryString']}",
244257
)
245258
else:
246259
span.set_attribute(
247-
SpanAttributes.HTTP_TARGET,
260+
HTTP_TARGET,
248261
lambda_event["requestContext"]["http"]["path"],
249262
)
250263

@@ -319,11 +332,11 @@ def _instrumented_lambda_handler_call( # noqa pylint: disable=too-many-branches
319332
# See more:
320333
# https://github.com/open-telemetry/semantic-conventions/blob/main/docs/faas/aws-lambda.md#resource-detector
321334
span.set_attribute(
322-
SpanAttributes.CLOUD_RESOURCE_ID,
335+
CLOUD_RESOURCE_ID,
323336
lambda_context.invoked_function_arn,
324337
)
325338
span.set_attribute(
326-
SpanAttributes.FAAS_INVOCATION_ID,
339+
FAAS_INVOCATION_ID,
327340
lambda_context.aws_request_id,
328341
)
329342

@@ -335,7 +348,7 @@ def _instrumented_lambda_handler_call( # noqa pylint: disable=too-many-branches
335348
":"
336349
)[4]
337350
span.set_attribute(
338-
ResourceAttributes.CLOUD_ACCOUNT_ID,
351+
CLOUD_ACCOUNT_ID,
339352
account_id,
340353
)
341354

@@ -354,7 +367,7 @@ def _instrumented_lambda_handler_call( # noqa pylint: disable=too-many-branches
354367
if isinstance(lambda_event, dict) and lambda_event.get(
355368
"requestContext"
356369
):
357-
span.set_attribute(SpanAttributes.FAAS_TRIGGER, "http")
370+
span.set_attribute(FAAS_TRIGGER, "http")
358371

359372
if lambda_event.get("version") == "2.0":
360373
_set_api_gateway_v2_proxy_attributes(
@@ -367,7 +380,7 @@ def _instrumented_lambda_handler_call( # noqa pylint: disable=too-many-branches
367380

368381
if isinstance(result, dict) and result.get("statusCode"):
369382
span.set_attribute(
370-
SpanAttributes.HTTP_STATUS_CODE,
383+
HTTP_STATUS_CODE,
371384
result.get("statusCode"),
372385
)
373386
finally:

instrumentation/opentelemetry-instrumentation-aws-lambda/tests/test_aws_lambda_instrumentation_manual.py

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,25 @@
3333
TRACE_ID_FIRST_PART_LENGTH,
3434
TRACE_ID_VERSION,
3535
)
36-
from opentelemetry.semconv.resource import ResourceAttributes
37-
from opentelemetry.semconv.trace import SpanAttributes
36+
from opentelemetry.semconv._incubating.attributes.cloud_attributes import (
37+
CLOUD_ACCOUNT_ID,
38+
CLOUD_RESOURCE_ID,
39+
)
40+
from opentelemetry.semconv._incubating.attributes.faas_attributes import (
41+
FAAS_INVOCATION_ID,
42+
FAAS_TRIGGER,
43+
)
44+
from opentelemetry.semconv._incubating.attributes.http_attributes import (
45+
HTTP_METHOD,
46+
HTTP_ROUTE,
47+
HTTP_SCHEME,
48+
HTTP_STATUS_CODE,
49+
HTTP_TARGET,
50+
HTTP_USER_AGENT,
51+
)
52+
from opentelemetry.semconv._incubating.attributes.net_attributes import (
53+
NET_HOST_NAME,
54+
)
3855
from opentelemetry.test.test_base import TestBase
3956
from opentelemetry.trace import NoOpTracerProvider, SpanKind, StatusCode
4057
from opentelemetry.trace.propagation.tracecontext import (
@@ -68,11 +85,9 @@ def __init__(self, aws_request_id, invoked_function_arn):
6885
)
6986

7087
MOCK_LAMBDA_CONTEXT_ATTRIBUTES = {
71-
SpanAttributes.CLOUD_RESOURCE_ID: MOCK_LAMBDA_CONTEXT.invoked_function_arn,
72-
SpanAttributes.FAAS_INVOCATION_ID: MOCK_LAMBDA_CONTEXT.aws_request_id,
73-
ResourceAttributes.CLOUD_ACCOUNT_ID: MOCK_LAMBDA_CONTEXT.invoked_function_arn.split(
74-
":"
75-
)[4],
88+
CLOUD_RESOURCE_ID: MOCK_LAMBDA_CONTEXT.invoked_function_arn,
89+
FAAS_INVOCATION_ID: MOCK_LAMBDA_CONTEXT.aws_request_id,
90+
CLOUD_ACCOUNT_ID: MOCK_LAMBDA_CONTEXT.invoked_function_arn.split(":")[4],
7691
}
7792

7893
MOCK_XRAY_TRACE_ID = 0x5FB7331105E8BB83207FA31D4D9CDB4C
@@ -571,14 +586,14 @@ def test_api_gateway_proxy_event_sets_attributes(self):
571586
self.assertSpanHasAttributes(
572587
span,
573588
{
574-
SpanAttributes.FAAS_TRIGGER: "http",
575-
SpanAttributes.HTTP_METHOD: "POST",
576-
SpanAttributes.HTTP_ROUTE: "/{proxy+}",
577-
SpanAttributes.HTTP_TARGET: "/{proxy+}?foo=bar",
578-
SpanAttributes.NET_HOST_NAME: "1234567890.execute-api.us-east-1.amazonaws.com",
579-
SpanAttributes.HTTP_USER_AGENT: "Custom User Agent String",
580-
SpanAttributes.HTTP_SCHEME: "https",
581-
SpanAttributes.HTTP_STATUS_CODE: 200,
589+
FAAS_TRIGGER: "http",
590+
HTTP_METHOD: "POST",
591+
HTTP_ROUTE: "/{proxy+}",
592+
HTTP_TARGET: "/{proxy+}?foo=bar",
593+
NET_HOST_NAME: "1234567890.execute-api.us-east-1.amazonaws.com",
594+
HTTP_USER_AGENT: "Custom User Agent String",
595+
HTTP_SCHEME: "https",
596+
HTTP_STATUS_CODE: 200,
582597
},
583598
)
584599

@@ -599,12 +614,12 @@ def test_api_gateway_http_api_proxy_event_sets_attributes(self):
599614
self.assertSpanHasAttributes(
600615
span,
601616
{
602-
SpanAttributes.FAAS_TRIGGER: "http",
603-
SpanAttributes.HTTP_METHOD: "POST",
604-
SpanAttributes.HTTP_ROUTE: "/path/to/resource",
605-
SpanAttributes.HTTP_TARGET: "/path/to/resource?parameter1=value1&parameter1=value2&parameter2=value",
606-
SpanAttributes.NET_HOST_NAME: "id.execute-api.us-east-1.amazonaws.com",
607-
SpanAttributes.HTTP_USER_AGENT: "agent",
617+
FAAS_TRIGGER: "http",
618+
HTTP_METHOD: "POST",
619+
HTTP_ROUTE: "/path/to/resource",
620+
HTTP_TARGET: "/path/to/resource?parameter1=value1&parameter1=value2&parameter2=value",
621+
NET_HOST_NAME: "id.execute-api.us-east-1.amazonaws.com",
622+
HTTP_USER_AGENT: "agent",
608623
},
609624
)
610625

@@ -625,8 +640,8 @@ def test_alb_conventional_event_sets_attributes(self):
625640
self.assertSpanHasAttributes(
626641
span,
627642
{
628-
SpanAttributes.FAAS_TRIGGER: "http",
629-
SpanAttributes.HTTP_METHOD: "GET",
643+
FAAS_TRIGGER: "http",
644+
HTTP_METHOD: "GET",
630645
},
631646
)
632647

@@ -647,8 +662,8 @@ def test_alb_multi_value_header_event_sets_attributes(self):
647662
self.assertSpanHasAttributes(
648663
span,
649664
{
650-
SpanAttributes.FAAS_TRIGGER: "http",
651-
SpanAttributes.HTTP_METHOD: "GET",
665+
FAAS_TRIGGER: "http",
666+
HTTP_METHOD: "GET",
652667
},
653668
)
654669

0 commit comments

Comments
 (0)