Skip to content

Commit

Permalink
Update semconv to 1.20.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lmolkova committed Apr 11, 2023
1 parent e4d8f10 commit 776b9c4
Show file tree
Hide file tree
Showing 81 changed files with 642 additions and 542 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from aio_pika.abc import AbstractIncomingMessage

from opentelemetry import trace
from opentelemetry.semconv.trace import SpanAttributes

from opentelemetry.instrumentation.aio_pika.callback_decorator import (
CallbackDecorator,
)
Expand Down Expand Up @@ -64,7 +66,7 @@ async def wrapper(wrapped, instance, args, kwargs):
def _instrument(self, **kwargs):
tracer_provider = kwargs.get("tracer_provider", None)
tracer = trace.get_tracer(
_INSTRUMENTATION_MODULE_NAME, __version__, tracer_provider
_INSTRUMENTATION_MODULE_NAME, __version__, tracer_provider, schema_url=SpanAttributes.SCHEMA_URL
)
self._instrument_queue(tracer)
self._instrument_exchange(tracer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ def set_operation(self, operation: MessagingOperationValues):

def set_destination(self, destination: str):
self._destination = destination
self._attributes[SpanAttributes.MESSAGING_DESTINATION] = destination
if self._kind == SpanKind.PRODUCER:
self._attributes[SpanAttributes.MESSAGING_DESTINATION_NAME] = destination
else:
self._attributes[SpanAttributes.MESSAGING_SOURCE_NAME] = destination

def set_channel(self, channel: AbstractChannel):
connection = channel.connection
Expand All @@ -71,7 +74,7 @@ def set_message(self, message: AbstractMessage):
] = properties.message_id
if properties.correlation_id:
self._attributes[
SpanAttributes.MESSAGING_CONVERSATION_ID
SpanAttributes.MESSAGING_MESSAGE_CONVERSATION_ID
] = properties.correlation_id

def build(self) -> Optional[Span]:
Expand All @@ -80,7 +83,10 @@ def build(self) -> Optional[Span]:
if self._operation:
self._attributes[SpanAttributes.MESSAGING_OPERATION] = self._operation.value
else:
self._attributes[SpanAttributes.MESSAGING_TEMP_DESTINATION] = True
if self._kind == SpanKind.PRODUCER:
self._attributes[SpanAttributes.MESSAGING_DESTINATION_TEMPORARY] = True
else:
self._attributes[SpanAttributes.MESSAGING_SOURCE_TEMPORARY] = True
span = self._tracer.start_span(
self._generate_span_name(), kind=self._kind, attributes=self._attributes
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
class TestInstrumentedQueueAioRmq7(TestCase):
EXPECTED_ATTRIBUTES = {
SpanAttributes.MESSAGING_SYSTEM: MESSAGING_SYSTEM,
SpanAttributes.MESSAGING_DESTINATION: EXCHANGE_NAME,
SpanAttributes.MESSAGING_SOURCE_NAME: EXCHANGE_NAME,
SpanAttributes.NET_PEER_NAME: SERVER_HOST,
SpanAttributes.NET_PEER_PORT: SERVER_PORT,
SpanAttributes.MESSAGING_MESSAGE_ID: MESSAGE_ID,
SpanAttributes.MESSAGING_CONVERSATION_ID: CORRELATION_ID,
SpanAttributes.MESSAGING_MESSAGE_CONVERSATION_ID: CORRELATION_ID,
SpanAttributes.MESSAGING_OPERATION: "receive",
}

Expand Down Expand Up @@ -81,11 +81,11 @@ def test_decorate_callback(self):
class TestInstrumentedQueueAioRmq8(TestCase):
EXPECTED_ATTRIBUTES = {
SpanAttributes.MESSAGING_SYSTEM: MESSAGING_SYSTEM,
SpanAttributes.MESSAGING_DESTINATION: EXCHANGE_NAME,
SpanAttributes.MESSAGING_SOURCE_NAME: EXCHANGE_NAME,
SpanAttributes.NET_PEER_NAME: SERVER_HOST,
SpanAttributes.NET_PEER_PORT: SERVER_PORT,
SpanAttributes.MESSAGING_MESSAGE_ID: MESSAGE_ID,
SpanAttributes.MESSAGING_CONVERSATION_ID: CORRELATION_ID,
SpanAttributes.MESSAGING_MESSAGE_CONVERSATION_ID: CORRELATION_ID,
SpanAttributes.MESSAGING_OPERATION: "receive",
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@
class TestInstrumentedExchangeAioRmq7(TestCase):
EXPECTED_ATTRIBUTES = {
SpanAttributes.MESSAGING_SYSTEM: MESSAGING_SYSTEM,
SpanAttributes.MESSAGING_DESTINATION: f"{EXCHANGE_NAME},{ROUTING_KEY}",
SpanAttributes.MESSAGING_DESTINATION_NAME: f"{EXCHANGE_NAME},{ROUTING_KEY}",
SpanAttributes.NET_PEER_NAME: SERVER_HOST,
SpanAttributes.NET_PEER_PORT: SERVER_PORT,
SpanAttributes.MESSAGING_MESSAGE_ID: MESSAGE_ID,
SpanAttributes.MESSAGING_CONVERSATION_ID: CORRELATION_ID,
SpanAttributes.MESSAGING_TEMP_DESTINATION: True,
SpanAttributes.MESSAGING_MESSAGE_CONVERSATION_ID: CORRELATION_ID,
SpanAttributes.MESSAGING_DESTINATION_TEMPORARY: True,
}

def setUp(self):
Expand Down Expand Up @@ -97,12 +97,12 @@ def test_robust_publish(self):
class TestInstrumentedExchangeAioRmq8(TestCase):
EXPECTED_ATTRIBUTES = {
SpanAttributes.MESSAGING_SYSTEM: MESSAGING_SYSTEM,
SpanAttributes.MESSAGING_DESTINATION: f"{EXCHANGE_NAME},{ROUTING_KEY}",
SpanAttributes.MESSAGING_DESTINATION_NAME: f"{EXCHANGE_NAME},{ROUTING_KEY}",
SpanAttributes.NET_PEER_NAME: SERVER_HOST,
SpanAttributes.NET_PEER_PORT: SERVER_PORT,
SpanAttributes.MESSAGING_MESSAGE_ID: MESSAGE_ID,
SpanAttributes.MESSAGING_CONVERSATION_ID: CORRELATION_ID,
SpanAttributes.MESSAGING_TEMP_DESTINATION: True,
SpanAttributes.MESSAGING_MESSAGE_CONVERSATION_ID: CORRELATION_ID,
SpanAttributes.MESSAGING_DESTINATION_TEMPORARY: True,
}

def setUp(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def create_trace_config(
# Explicitly specify the type for the `request_hook` and `response_hook` param and rtype to work
# around this issue.

tracer = get_tracer(__name__, __version__, tracer_provider)
tracer = get_tracer(__name__, __version__, tracer_provider, schema_url=SpanAttributes.SCHEMA_URL)

def _end_trace(trace_config_ctx: types.SimpleNamespace):
context_api.detach(trace_config_ctx.token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def client_response_hook(span: Span, message: dict):
get_custom_headers,
normalise_request_header_name,
normalise_response_header_name,
remove_url_credentials,
parse_http_host,
)

_ServerRequestHookT = typing.Optional[typing.Callable[[Span, dict], None]]
Expand Down Expand Up @@ -294,37 +294,48 @@ def set(
def collect_request_attributes(scope):
"""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)

schema = scope.get("scheme")
result = {
SpanAttributes.HTTP_SCHEME: schema,
SpanAttributes.NET_PROTOCOL_VERSION: scope.get("http_version"),
}

host = "0.0.0.0"
port = 80 if schema == "http" else 443
http_host_value_list = asgi_getter.get(scope, "host")
if http_host_value_list and len(http_host_value_list) > 0:
host, port_str = parse_http_host(http_host_value_list[0])
if port_str:
port = int(port_str)
else:
server = scope.get("server")
if server:
host, _ = parse_http_host(server[0])
port = server[1]
result[SpanAttributes.NET_HOST_NAME] = host
result[SpanAttributes.NET_HOST_PORT] = port

target = scope.get("path")
query_string = scope.get("query_string")
if query_string and http_url:
if query_string:
if isinstance(query_string, bytes):
query_string = query_string.decode("utf8")
http_url += "?" + urllib.parse.unquote(query_string)
query = urllib.parse.unquote(query_string)
target += "?" + query
result[SpanAttributes.HTTP_TARGET] = target

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

http_host_value_list = asgi_getter.get(scope, "host")
if http_host_value_list:
result[SpanAttributes.HTTP_SERVER_NAME] = ",".join(
http_host_value_list
)
http_user_agent = asgi_getter.get(scope, "user-agent")
if http_user_agent:
result[SpanAttributes.HTTP_USER_AGENT] = http_user_agent[0]
result[SpanAttributes.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]
result[SpanAttributes.NET_SOCK_PEER_ADDR] = scope.get("client")[0]
result[SpanAttributes.NET_SOCK_PEER_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 @@ -429,7 +440,7 @@ def get_default_span_details(scope: dict) -> Tuple[str, dict]:
return span_name, {}


def _collect_target_attribute(
def _collect_route_attribute(
scope: typing.Dict[str, typing.Any]
) -> typing.Optional[str]:
"""
Expand Down Expand Up @@ -490,9 +501,9 @@ def __init__(
meter=None,
):
self.app = guarantee_single_callable(app)
self.tracer = trace.get_tracer(__name__, __version__, tracer_provider)
self.tracer = trace.get_tracer(__name__, __version__, tracer_provider, schema_url=SpanAttributes.SCHEMA_URL)
self.meter = (
get_meter(__name__, __version__, meter_provider)
get_meter(__name__, __version__, meter_provider, schema_url=SpanAttributes.SCHEMA_URL)
if meter is None
else meter
)
Expand Down Expand Up @@ -579,9 +590,9 @@ async def __call__(self, scope, receive, send):
await self.app(scope, otel_receive, otel_send)
finally:
if scope["type"] == "http":
target = _collect_target_attribute(scope)
if target:
duration_attrs[SpanAttributes.HTTP_TARGET] = target
route = _collect_route_attribute(scope)
if route:
duration_attrs[SpanAttributes.HTTP_ROUTE] = route
duration = max(round((default_timer() - start) * 1000), 0)
self.duration_histogram.record(duration, duration_attrs)
self.active_requests_counter.add(
Expand Down
Loading

0 comments on commit 776b9c4

Please sign in to comment.