Skip to content

Commit

Permalink
request_attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen committed Jun 20, 2024
1 parent 283555e commit 6df07da
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,21 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
from opentelemetry.semconv._incubating.metrics.http_metrics import (
create_http_server_active_requests,
create_http_server_request_body_size,
create_http_server_request_duration,
create_http_server_response_body_size,
)
from opentelemetry.semconv.metrics import MetricInstruments
from opentelemetry.semconv.attributes.client_attributes import CLIENT_PORT
from opentelemetry.semconv.metrics.http_metrics import (
HTTP_SERVER_REQUEST_DURATION,
)
from opentelemetry.semconv.attributes.server_attributes import (
SERVER_ADDRESS,
SERVER_PORT,
)
from opentelemetry.semconv.attributes.url_attributes import (
URL_FULL,
URL_SCHEME,
)
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace import set_span_in_context
from opentelemetry.trace.status import Status, StatusCode
Expand Down Expand Up @@ -308,7 +319,7 @@ def set(
asgi_setter = ASGISetter()


def collect_request_attributes(scope):
def collect_request_attributes(scope, sem_conv_opt_in_mode):
"""Collects HTTP request attributes from the ASGI scope and returns a
dictionary to be used as span creation attributes."""
server_host, port, http_url = get_host_port_url_tuple(scope)
Expand All @@ -317,18 +328,29 @@ def collect_request_attributes(scope):
if isinstance(query_string, bytes):
query_string = query_string.decode("utf8")
http_url += "?" + urllib.parse.unquote(query_string)
result = {}

if _report_old(sem_conv_opt_in_mode):
result[SpanAttributes.HTTP_SCHEME] = scope.get("scheme")
result[SpanAttributes.HTTP_HOST] = server_host
result[SpanAttributes.NET_HOST_PORT] = port
result[SpanAttributes.HTTP_FLAVOR] = scope.get("http_version")
result[SpanAttributes.HTTP_TARGET] = scope.get("path")
result[SpanAttributes.HTTP_URL] = remove_url_credentials(http_url)

if _report_new(sem_conv_opt_in_mode):
result[URL_SCHEME] = scope.get("scheme")
result[SERVER_ADDRESS] = server_host
result[SERVER_PORT] = port
result[NETWORK_PROTOCOL_VERSION] = scope.get("http_version")
result[URL_FULL] = scope.get("path")

result = {
SpanAttributes.HTTP_SCHEME: scope.get("scheme"),
SpanAttributes.HTTP_HOST: server_host,
SpanAttributes.NET_HOST_PORT: port,
SpanAttributes.HTTP_FLAVOR: scope.get("http_version"),
SpanAttributes.HTTP_TARGET: scope.get("path"),
SpanAttributes.HTTP_URL: remove_url_credentials(http_url),
}
http_method = scope.get("method")
if http_method:
result[SpanAttributes.HTTP_METHOD] = http_method
if _report_old(sem_conv_opt_in_mode):
result[SpanAttributes.HTTP_METHOD] = http_method
if _report_new(sem_conv_opt_in_mode):
result[HTTP_REQUEST_METHOD] = http_method

http_host_value_list = asgi_getter.get(scope, "host")
if http_host_value_list:
Expand All @@ -337,11 +359,20 @@ def collect_request_attributes(scope):
)
http_user_agent = asgi_getter.get(scope, "user-agent")
if http_user_agent:
result[SpanAttributes.HTTP_USER_AGENT] = http_user_agent[0]
if _report_old(sem_conv_opt_in_mode):
result[SpanAttributes.HTTP_USER_AGENT] = http_user_agent[0]
if _report_new(sem_conv_opt_in_mode):
result[USER_AGENT_ORIGINAL] = http_user_agent[0]

if "client" in scope and scope["client"] is not None:
result[SpanAttributes.NET_PEER_IP] = scope.get("client")[0]
result[SpanAttributes.NET_PEER_PORT] = scope.get("client")[1]
if _report_old(sem_conv_opt_in_mode):
result[SpanAttributes.NET_PEER_IP] = scope.get("client")[0]
result[SpanAttributes.NET_PEER_PORT] = scope.get("client")[1]
if _report_new(sem_conv_opt_in_mode):
result[CLIENT_SOCKET_ADDRESS] = scope.get("client")[0]
result[CLIENT_PORT] = scope.get("client")[1]

# remove None values
result = {k: v for k, v in result.items() if v is not None}
Expand Down Expand Up @@ -533,7 +564,11 @@ def __init__(
)
self.duration_histogram_new = None
if _report_new(sem_conv_opt_in_mode):
self.duration_histogram_new = create_http_server_request_duration(self.meter)
self.duration_histogram_new = self.meter.create_histogram(
name=HTTP_SERVER_REQUEST_DURATION,
description="Duration of HTTP server requests.",
unit="s",
)
self.server_response_size_histogram = None
if _report_old(sem_conv_opt_in_mode):
self.server_response_size_histogram = self.meter.create_histogram(
Expand Down Expand Up @@ -563,6 +598,7 @@ def __init__(
self.client_request_hook = client_request_hook
self.client_response_hook = client_response_hook
self.content_length_header = None
self._sem_conv_opt_in_mode = sem_conv_opt_in_mode

# Environment variables as constructor parameters
self.http_capture_headers_server_request = (
Expand Down Expand Up @@ -616,7 +652,7 @@ async def __call__(

span_name, additional_attributes = self.default_span_details(scope)

attributes = collect_request_attributes(scope)
attributes = collect_request_attributes(scope, self._sem_conv_opt_in_mode)
attributes.update(additional_attributes)
span, token = _start_internal_or_server_span(
tracer=self.tracer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
from enum import Enum

from opentelemetry.instrumentation.utils import http_status_to_status_code
from opentelemetry.semconv.attributes.client_attributes import (
CLIENT_ADDRESS,
CLIENT_PORT,
)
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
from opentelemetry.semconv.attributes.http_attributes import (
HTTP_REQUEST_METHOD,
Expand All @@ -33,8 +37,13 @@
)
from opentelemetry.semconv.attributes.url_attributes import (
URL_FULL,
URL_PATH,
URL_QUERY,
URL_SCHEME,
)
from opentelemetry.semconv.attributes.user_agent_attributes import (
USER_AGENT_ORIGINAL,
)
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace.status import Status, StatusCode

Expand Down Expand Up @@ -280,38 +289,38 @@ def _set_http_net_host(result, host, sem_conv_opt_in_mode):
if _report_old(sem_conv_opt_in_mode):
set_string_attribute(result, SpanAttributes.NET_HOST_NAME, host)
if _report_new(sem_conv_opt_in_mode):
set_string_attribute(result, SpanAttributes.SERVER_ADDRESS, host)
set_string_attribute(result, SERVER_ADDRESS, host)


def _set_http_net_host_port(result, port, sem_conv_opt_in_mode):
if _report_old(sem_conv_opt_in_mode):
set_int_attribute(result, SpanAttributes.NET_HOST_PORT, port)
if _report_new(sem_conv_opt_in_mode):
set_int_attribute(result, SpanAttributes.SERVER_PORT, port)
set_int_attribute(result, SERVER_PORT, port)


def _set_http_target(result, target, path, query, sem_conv_opt_in_mode):
if _report_old(sem_conv_opt_in_mode):
set_string_attribute(result, SpanAttributes.HTTP_TARGET, target)
if _report_new(sem_conv_opt_in_mode):
if path:
set_string_attribute(result, SpanAttributes.URL_PATH, path)
set_string_attribute(result, URL_PATH, path)
if query:
set_string_attribute(result, SpanAttributes.URL_QUERY, query)
set_string_attribute(result, URL_QUERY, query)


def _set_http_peer_ip(result, ip, sem_conv_opt_in_mode):
if _report_old(sem_conv_opt_in_mode):
set_string_attribute(result, SpanAttributes.NET_PEER_IP, ip)
if _report_new(sem_conv_opt_in_mode):
set_string_attribute(result, SpanAttributes.CLIENT_ADDRESS, ip)
set_string_attribute(result, CLIENT_ADDRESS, ip)


def _set_http_peer_port_server(result, port, sem_conv_opt_in_mode):
if _report_old(sem_conv_opt_in_mode):
set_int_attribute(result, SpanAttributes.NET_PEER_PORT, port)
if _report_new(sem_conv_opt_in_mode):
set_int_attribute(result, SpanAttributes.CLIENT_PORT, port)
set_int_attribute(result, CLIENT_PORT, port)


def _set_http_user_agent(result, user_agent, sem_conv_opt_in_mode):
Expand All @@ -321,23 +330,23 @@ def _set_http_user_agent(result, user_agent, sem_conv_opt_in_mode):
)
if _report_new(sem_conv_opt_in_mode):
set_string_attribute(
result, SpanAttributes.USER_AGENT_ORIGINAL, user_agent
result, USER_AGENT_ORIGINAL, user_agent
)


def _set_http_net_peer_name_server(result, name, sem_conv_opt_in_mode):
if _report_old(sem_conv_opt_in_mode):
set_string_attribute(result, SpanAttributes.NET_PEER_NAME, name)
if _report_new(sem_conv_opt_in_mode):
set_string_attribute(result, SpanAttributes.CLIENT_ADDRESS, name)
set_string_attribute(result, CLIENT_ADDRESS, name)


def _set_http_flavor_version(result, version, sem_conv_opt_in_mode):
if _report_old(sem_conv_opt_in_mode):
set_string_attribute(result, SpanAttributes.HTTP_FLAVOR, version)
if _report_new(sem_conv_opt_in_mode):
set_string_attribute(
result, SpanAttributes.NETWORK_PROTOCOL_VERSION, version
result, NETWORK_PROTOCOL_VERSION, version
)


Expand Down

0 comments on commit 6df07da

Please sign in to comment.