Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Invalid type NoneType for attribute X error | AWS-Lambda instrumentation #1785

Merged
merged 12 commits into from
Jun 16, 2023
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#1738](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1738))
- Fix `None does not implement middleware` error when there are no middlewares registered
([#1766](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1766))
- Fix `Invalid type NoneType for attribute X (opentelemetry-instrumentation-aws-lambda)` error when some attributes do not exist
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved
([#1780](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1780))

## Version 1.17.0/0.38b0 (2023-03-22)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,24 +204,30 @@ def _set_api_gateway_v1_proxy_attributes(
span.set_attribute(SpanAttributes.HTTP_ROUTE, lambda_event.get("resource"))

if lambda_event.get("headers"):
span.set_attribute(
SpanAttributes.HTTP_USER_AGENT,
lambda_event["headers"].get("User-Agent"),
)
span.set_attribute(
SpanAttributes.HTTP_SCHEME,
lambda_event["headers"].get("X-Forwarded-Proto"),
)
span.set_attribute(
SpanAttributes.NET_HOST_NAME, lambda_event["headers"].get("Host")
)
if lambda_event["headers"].get("User-Agent"):
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved
span.set_attribute(
SpanAttributes.HTTP_USER_AGENT,
lambda_event["headers"].get("User-Agent"),
)
if lambda_event["headers"].get("X-Forwarded-Proto"):
span.set_attribute(
SpanAttributes.HTTP_SCHEME,
lambda_event["headers"].get("X-Forwarded-Proto"),
)
if lambda_event["headers"].get("Host"):
span.set_attribute(
SpanAttributes.NET_HOST_NAME,
lambda_event["headers"].get("Host"),
)

if lambda_event.get("queryStringParameters"):
if lambda_event.get("queryStringParameters") and lambda_event.get(
"resource"
):
span.set_attribute(
SpanAttributes.HTTP_TARGET,
f"{lambda_event.get('resource')}?{urlencode(lambda_event.get('queryStringParameters'))}",
)
else:
elif lambda_event.get("resource"):
span.set_attribute(
SpanAttributes.HTTP_TARGET, lambda_event.get("resource")
)
Expand All @@ -237,31 +243,37 @@ def _set_api_gateway_v2_proxy_attributes(
More info:
https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
"""
span.set_attribute(
SpanAttributes.NET_HOST_NAME,
lambda_event["requestContext"].get("domainName"),
)

if lambda_event["requestContext"].get("http"):
span.set_attribute(
SpanAttributes.HTTP_METHOD,
lambda_event["requestContext"]["http"].get("method"),
)
if lambda_event["requestContext"].get("domainName"):
span.set_attribute(
SpanAttributes.HTTP_USER_AGENT,
lambda_event["requestContext"]["http"].get("userAgent"),
)
span.set_attribute(
SpanAttributes.HTTP_ROUTE,
lambda_event["requestContext"]["http"].get("path"),
SpanAttributes.NET_HOST_NAME,
lambda_event["requestContext"].get("domainName"),
)

if lambda_event.get("rawQueryString"):
if lambda_event["requestContext"].get("http"):
if lambda_event["requestContext"]["http"].get("method"):
span.set_attribute(
SpanAttributes.HTTP_METHOD,
lambda_event["requestContext"]["http"].get("method"),
)
if lambda_event["requestContext"]["http"].get("userAgent"):
span.set_attribute(
SpanAttributes.HTTP_USER_AGENT,
lambda_event["requestContext"]["http"].get("userAgent"),
)
if lambda_event["requestContext"]["http"].get("path"):
span.set_attribute(
SpanAttributes.HTTP_ROUTE,
lambda_event["requestContext"]["http"].get("path"),
)

if lambda_event.get("rawQueryString") and lambda_event[
"requestContext"
]["http"].get("path"):
span.set_attribute(
SpanAttributes.HTTP_TARGET,
f"{lambda_event['requestContext']['http'].get('path')}?{lambda_event.get('rawQueryString')}",
)
else:
elif lambda_event["requestContext"]["http"].get("path"):
span.set_attribute(
SpanAttributes.HTTP_TARGET,
lambda_event["requestContext"]["http"].get("path"),
Expand Down