Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions sentry_sdk/integrations/_asgi_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from enum import Enum
from typing import TYPE_CHECKING

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.data_collection import _apply_data_collection_filtering_to_query_string
from sentry_sdk.integrations._wsgi_common import _filter_headers
Expand Down Expand Up @@ -161,7 +163,9 @@ def _get_request_attributes(
client_options = sentry_sdk.get_client().options
if ty in ("http", "websocket"):
if asgi_scope.get("method"):
attributes["http.request.method"] = asgi_scope["method"].upper()
attributes[ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] = asgi_scope[
"method"
].upper()

headers = _get_headers(asgi_scope)

Expand All @@ -178,10 +182,10 @@ def _get_request_attributes(
behaviour=client_options["data_collection"]["url_query_params"],
)
if filtered_query_string:
attributes["http.query"] = filtered_query_string
attributes[ATTRIBUTE_NAMES.HTTP_QUERY] = filtered_query_string

path = _get_path(asgi_scope=asgi_scope, root_path_in_path=root_path_in_path)
attributes["url.path"] = path
attributes[ATTRIBUTE_NAMES.URL_PATH] = path

url_without_query_string = _get_url(
asgi_scope,
Expand All @@ -190,7 +194,7 @@ def _get_request_attributes(
path=path,
)

attributes["url.full"] = (
attributes[ATTRIBUTE_NAMES.URL_FULL] = (
f"{url_without_query_string}?{filtered_query_string}"
if filtered_query_string is not None
else url_without_query_string
Expand All @@ -200,6 +204,6 @@ def _get_request_attributes(
if asgi_scope_client:
if client_options["data_collection"]["user_info"]:
ip = _get_ip(asgi_scope)
attributes["client.address"] = ip
attributes[ATTRIBUTE_NAMES.CLIENT_ADDRESS] = ip

return attributes
57 changes: 34 additions & 23 deletions sentry_sdk/integrations/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import weakref
from functools import wraps

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.consts import _SENTRY_HEADER_NAMES, OP, SPANDATA
from sentry_sdk.data_collection import (
Expand Down Expand Up @@ -118,7 +120,7 @@ async def sentry_app_handle(
scope.add_event_processor(_make_request_processor(weak_request))

scope.set_attribute(
SPANDATA.SENTRY_IS_LOCALHOST,
ATTRIBUTE_NAMES.SENTRY_IS_LOCALHOST,
_is_localhost(
client_ip=_get_client_ip(request),
host_header=request.headers.get("Host"),
Expand All @@ -130,7 +132,9 @@ async def sentry_app_handle(

user_agent = request.headers.get("User-Agent")
if user_agent:
scope.set_attribute(SPANDATA.USER_AGENT_ORIGINAL, user_agent)
scope.set_attribute(
ATTRIBUTE_NAMES.USER_AGENT_ORIGINAL, user_agent
)

headers = dict(request.headers)

Expand All @@ -146,12 +150,12 @@ async def sentry_app_handle(
url_attributes = {}
client_address_attributes = {}

url_attributes["url.full"] = "%s://%s%s" % (
url_attributes[ATTRIBUTE_NAMES.URL_FULL] = "%s://%s%s" % (
request.scheme,
request.host,
request.path,
)
url_attributes["url.path"] = request.path
url_attributes[ATTRIBUTE_NAMES.URL_PATH] = request.path

if request.query_string:
filtered_query_string = (
Expand All @@ -163,25 +167,31 @@ async def sentry_app_handle(
)
)
if filtered_query_string:
url_attributes["url.query"] = filtered_query_string
url_attributes["url.full"] += "?" + filtered_query_string
url_attributes[ATTRIBUTE_NAMES.URL_QUERY] = (
filtered_query_string
)
url_attributes[ATTRIBUTE_NAMES.URL_FULL] += (
"?" + filtered_query_string
)

if request.remote:
if client.options["data_collection"]["user_info"]:
client_address_attributes["client.address"] = request.remote
client_address_attributes[
ATTRIBUTE_NAMES.CLIENT_ADDRESS
] = request.remote
scope.set_attribute(
SPANDATA.USER_IP_ADDRESS, request.remote
ATTRIBUTE_NAMES.USER_IP_ADDRESS, request.remote
)

span = sentry_sdk.start_span(
# If this name makes it to the UI, AIOHTTP's URL
# resolver did not find a route or died trying.
name="generic AIOHTTP request",
attributes={
"sentry.op": OP.HTTP_SERVER,
"sentry.origin": AioHttpIntegration.origin,
"sentry.segment.name.source": SegmentNameSource.ROUTE.value,
"http.request.method": request.method,
ATTRIBUTE_NAMES.SENTRY_OP: OP.HTTP_SERVER,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: AioHttpIntegration.origin,
ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: SegmentNameSource.ROUTE.value,
ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD: request.method,
**url_attributes,
**client_address_attributes,
**header_attributes,
Expand All @@ -195,7 +205,7 @@ async def sentry_app_handle(
response = await old_handle(self, request)
except HTTPException as e:
span.set_attribute(
"http.response.status_code", e.status_code
ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE, e.status_code
)

if e.status_code >= 400:
Expand Down Expand Up @@ -227,7 +237,8 @@ async def sentry_app_handle(
pass
else:
span.set_attribute(
"http.response.status_code", response_status
ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE,
response_status,
)
span.status = (
SpanStatus.ERROR.value
Expand Down Expand Up @@ -256,7 +267,7 @@ async def sentry_urldispatcher_resolve(

server_span = sentry_sdk.get_current_scope()._server_segment_span
if server_span is not None and pattern is not None:
server_span.set_attribute(SPANDATA.HTTP_ROUTE, pattern)
server_span.set_attribute(ATTRIBUTE_NAMES.HTTP_ROUTE, pattern)

current_scope = sentry_sdk.get_current_scope()

Expand Down Expand Up @@ -309,30 +320,30 @@ async def on_request_start(

span: "Optional[Span]" = None
attributes: "Attributes" = {
"sentry.op": OP.HTTP_CLIENT,
"sentry.origin": AioHttpIntegration.origin,
"http.request.method": method,
ATTRIBUTE_NAMES.SENTRY_OP: OP.HTTP_CLIENT,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: AioHttpIntegration.origin,
ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD: method,
}
if parsed_url is not None:
url_full = parsed_url.url
attributes["url.path"] = params.url.path
attributes[ATTRIBUTE_NAMES.URL_PATH] = params.url.path

if parsed_url.query:
filtered_query = _apply_data_collection_filtering_to_query_string(
query_string=parsed_url.query,
behaviour=client.options["data_collection"]["url_query_params"],
)
if filtered_query:
attributes["url.query"] = filtered_query
attributes[ATTRIBUTE_NAMES.URL_QUERY] = filtered_query
url_full += "?" + filtered_query
breadcrumb[SPANDATA.HTTP_QUERY] = filtered_query

if parsed_url.fragment:
attributes["url.fragment"] = parsed_url.fragment
attributes[ATTRIBUTE_NAMES.URL_FRAGMENT] = parsed_url.fragment
url_full += "#" + parsed_url.fragment
breadcrumb[SPANDATA.HTTP_FRAGMENT] = parsed_url.fragment

attributes["url.full"] = url_full
attributes[ATTRIBUTE_NAMES.URL_FULL] = url_full
breadcrumb["url"] = url_full

if sentry_sdk.get_current_span() is not None:
Expand Down Expand Up @@ -438,7 +449,7 @@ async def on_request_end(
if span is None:
return

span.set_attribute("http.response.status_code", status)
span.set_attribute(ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE, status)
span.status = SpanStatus.ERROR.value if status >= 400 else SpanStatus.OK.value

with capture_internal_exceptions():
Expand Down
29 changes: 17 additions & 12 deletions sentry_sdk/integrations/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
from functools import partial
from typing import TYPE_CHECKING

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.consts import OP
from sentry_sdk.integrations._asgi_common import (
_get_headers,
_get_ip,
Expand Down Expand Up @@ -188,7 +190,7 @@ async def _run_app(

headers = _get_headers(scope)
sentry_scope.set_attribute(
SPANDATA.SENTRY_IS_LOCALHOST,
ATTRIBUTE_NAMES.SENTRY_IS_LOCALHOST,
_is_localhost(
client_ip=_get_ip(scope),
url_host=scope.get("server")[0]
Expand All @@ -201,7 +203,7 @@ async def _run_app(

if headers.get("user-agent"):
sentry_scope.set_attribute(
SPANDATA.USER_AGENT_ORIGINAL, headers["user-agent"]
ATTRIBUTE_NAMES.USER_AGENT_ORIGINAL, headers["user-agent"]
)

ty = scope["type"]
Expand All @@ -214,26 +216,28 @@ async def _run_app(

span: "Optional[ContextManager[Optional[Span]]]" = None
attributes: "Attributes" = {
"sentry.segment.name.source": getattr(
ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: getattr(
transaction_source, "value", transaction_source
),
"sentry.origin": self.span_origin,
"network.protocol.name": ty,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: self.span_origin,
ATTRIBUTE_NAMES.NETWORK_PROTOCOL_NAME: ty,
}

ip = _get_ip(scope)
if ip:
client_options = sentry_sdk.get_client().options
if client_options["data_collection"]["user_info"]:
sentry_scope.set_attribute(SPANDATA.USER_IP_ADDRESS, ip)
sentry_scope.set_attribute(
ATTRIBUTE_NAMES.USER_IP_ADDRESS, ip
)

if ty in ("http", "websocket"):
if ty == "websocket" or method in self.http_methods_to_capture:
sentry_sdk.continue_trace(_get_headers(scope))

Scope.set_custom_sampling_context({"asgi_scope": scope})

attributes["sentry.op"] = f"{ty}.server"
attributes[ATTRIBUTE_NAMES.SENTRY_OP] = f"{ty}.server"
span = sentry_sdk.start_span(
name=transaction_name,
attributes=attributes,
Expand All @@ -245,7 +249,7 @@ async def _run_app(

Scope.set_custom_sampling_context({"asgi_scope": scope})

attributes["sentry.op"] = OP.HTTP_SERVER
attributes[ATTRIBUTE_NAMES.SENTRY_OP] = OP.HTTP_SERVER
span = sentry_sdk.start_span(
name=transaction_name,
attributes=attributes,
Expand Down Expand Up @@ -276,7 +280,7 @@ async def _sentry_wrapped_send(
"error" if event["status"] >= 400 else "ok"
)
span.set_attribute(
"http.response.status_code",
ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE,
event["status"],
)

Expand All @@ -303,7 +307,7 @@ async def _sentry_wrapped_send(
span is not None
and span.name != _DEFAULT_TRANSACTION_NAME
and span.get_attributes().get(
"sentry.segment.name.source"
ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE
)
in [
SegmentNameSource.COMPONENT.value,
Expand All @@ -318,7 +322,8 @@ async def _sentry_wrapped_send(
)
span.name = name
span.set_attribute(
"sentry.segment.name.source", source
ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE,
source,
)
finally:
_asgi_middleware_applied.set(False)
Expand Down
5 changes: 3 additions & 2 deletions sentry_sdk/integrations/bottle.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import functools
from typing import TYPE_CHECKING

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.consts import SPANDATA
from sentry_sdk.integrations import (
_DEFAULT_FAILED_REQUEST_STATUS_CODES,
DidNotEnable,
Expand Down Expand Up @@ -112,7 +113,7 @@ def _patched_handle(self: "Bottle", environ: "Dict[str, Any]") -> "Any":
pass

if route_path is not None:
server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path)
server_span.set_attribute(ATTRIBUTE_NAMES.HTTP_ROUTE, route_path)

name = _get_transaction_name(bottle_request)
if name:
Expand Down
Loading
Loading