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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Fix `Invalid type NoneType for attribute X (opentelemetry-instrumentation-aws-lambda)` error when some attributes do not exist
([#1780](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1780))

## Version 1.18.0/0.39b0 (2023-05-10)

- `opentelemetry-instrumentation-system-metrics` Add `process.` prefix to `runtime.memory`, `runtime.cpu.time`, and `runtime.gc_count`. Change `runtime.memory` from count to UpDownCounter. ([#1735](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1735))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,30 +201,35 @@ def _set_api_gateway_v1_proxy_attributes(
span.set_attribute(
SpanAttributes.HTTP_METHOD, lambda_event.get("httpMethod")
)
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 "User-Agent" in lambda_event["headers"]:
span.set_attribute(
SpanAttributes.HTTP_USER_AGENT,
lambda_event["headers"]["User-Agent"],
)
if "X-Forwarded-Proto" in lambda_event["headers"]:
span.set_attribute(
SpanAttributes.HTTP_SCHEME,
lambda_event["headers"]["X-Forwarded-Proto"],
)
if "Host" in lambda_event["headers"]:
span.set_attribute(
SpanAttributes.NET_HOST_NAME,
lambda_event["headers"]["Host"],
)
if "resource" in lambda_event:
span.set_attribute(SpanAttributes.HTTP_ROUTE, lambda_event["resource"])

if lambda_event.get("queryStringParameters"):
span.set_attribute(
SpanAttributes.HTTP_TARGET,
f"{lambda_event.get('resource')}?{urlencode(lambda_event.get('queryStringParameters'))}",
)
else:
span.set_attribute(
SpanAttributes.HTTP_TARGET, lambda_event.get("resource")
)
if lambda_event.get("queryStringParameters"):
span.set_attribute(
SpanAttributes.HTTP_TARGET,
f"{lambda_event['resource']}?{urlencode(lambda_event['queryStringParameters'])}",
)
else:
span.set_attribute(
SpanAttributes.HTTP_TARGET, lambda_event["resource"]
)

return span

Expand All @@ -237,35 +242,38 @@ 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"):
if "domainName" in lambda_event["requestContext"]:
span.set_attribute(
SpanAttributes.HTTP_METHOD,
lambda_event["requestContext"]["http"].get("method"),
)
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"]["domainName"],
)

if lambda_event.get("rawQueryString"):
if lambda_event["requestContext"].get("http"):
if "method" in lambda_event["requestContext"]["http"]:
span.set_attribute(
SpanAttributes.HTTP_TARGET,
f"{lambda_event['requestContext']['http'].get('path')}?{lambda_event.get('rawQueryString')}",
SpanAttributes.HTTP_METHOD,
lambda_event["requestContext"]["http"]["method"],
)
else:
if "userAgent" in lambda_event["requestContext"]["http"]:
span.set_attribute(
SpanAttributes.HTTP_TARGET,
lambda_event["requestContext"]["http"].get("path"),
SpanAttributes.HTTP_USER_AGENT,
lambda_event["requestContext"]["http"]["userAgent"],
)
if "path" in lambda_event["requestContext"]["http"]:
span.set_attribute(
SpanAttributes.HTTP_ROUTE,
lambda_event["requestContext"]["http"]["path"],
)
if lambda_event.get("rawQueryString"):
span.set_attribute(
SpanAttributes.HTTP_TARGET,
f"{lambda_event['requestContext']['http']['path']}?{lambda_event['rawQueryString']}",
)
else:
span.set_attribute(
SpanAttributes.HTTP_TARGET,
lambda_event["requestContext"]["http"]["path"],
)

return span

Expand Down