Skip to content

Commit

Permalink
req
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen committed Oct 12, 2023
1 parent 7a1ca3e commit 125028a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
from opentelemetry.instrumentation.requests.package import _instruments
from opentelemetry.instrumentation.requests.version import __version__
from opentelemetry.instrumentation.utils import (
_OpenTelemetrySemanticConventionStability,
_OpenTelemetryStabilityMode,
_OpenTelemetryStabilitySignalType,
_SUPPRESS_INSTRUMENTATION_KEY,
http_status_to_status_code,
)
Expand Down Expand Up @@ -98,6 +101,7 @@ def _instrument(
request_hook: _RequestHookT = None,
response_hook: _ResponseHookT = None,
excluded_urls: ExcludeList = None,
sem_conv_opt_in_mode: _OpenTelemetryStabilityMode = _OpenTelemetryStabilityMode.DEFAULT,
):
"""Enables tracing of all requests calls that go through
:code:`requests.session.Session.request` (this includes
Expand Down Expand Up @@ -138,10 +142,14 @@ def get_or_create_headers():

url = remove_url_credentials(request.url)

span_attributes = {
SpanAttributes.HTTP_METHOD: method,
SpanAttributes.HTTP_URL: url,
}
span_attributes = _OpenTelemetrySemanticConventionStability._get_attributes_based_on_stability_mode(
{
SpanAttributes.HTTP_METHOD: method,
SpanAttributes.HTTP_URL: url,
},
sem_conv_opt_in_mode,
"client",
)

metric_labels = {
SpanAttributes.HTTP_METHOD: method,
Expand All @@ -160,6 +168,12 @@ def get_or_create_headers():
except ValueError:
pass

metric_labels = _OpenTelemetrySemanticConventionStability._get_attributes_based_on_stability_mode(
metric_labels,
sem_conv_opt_in_mode,
"client",
)

with tracer.start_as_current_span(
span_name, kind=SpanKind.CLIENT, attributes=span_attributes
) as span, set_ip_on_next_http_connection(span):
Expand Down Expand Up @@ -203,9 +217,14 @@ def get_or_create_headers():
if result.raw is not None:
version = getattr(result.raw, "version", None)
if version:
metric_labels[SpanAttributes.HTTP_FLAVOR] = (
"1.1" if version == 11 else "1.0"
additional_labels = _OpenTelemetrySemanticConventionStability._get_attributes_based_on_stability_mode(
{
SpanAttributes.HTTP_FLAVOR: "1.1" if version == 11 else "1.0",
},
sem_conv_opt_in_mode,
"client",
)
metric_labels.update(additional_labels)

if callable(response_hook):
response_hook(span, request, result)
Expand Down Expand Up @@ -290,6 +309,9 @@ def _instrument(self, **kwargs):
unit="ms",
description="measures the duration of the outbound HTTP request",
)
sem_conv_opt_in_mode = _OpenTelemetrySemanticConventionStability._get_opentelemetry_stability_opt_in_mode(
_OpenTelemetryStabilitySignalType.HTTP
)
_instrument(
tracer,
duration_histogram,
Expand All @@ -298,6 +320,7 @@ def _instrument(self, **kwargs):
excluded_urls=_excluded_urls_from_env
if excluded_urls is None
else parse_excluded_urls(excluded_urls),
sem_conv_opt_in_mode=sem_conv_opt_in_mode,
)

def _uninstrument(self, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import threading
import urllib.parse
from re import escape, sub
from typing import Dict, Sequence
from typing import Dict, Optional, Sequence

from wrapt import ObjectProxy

Expand Down Expand Up @@ -177,7 +177,7 @@ def _python_path_without_directory(python_path, directory, path_separator):
SpanAttributes.NET_TRANSPORT: SpanAttributes.NETWORK_TRANSPORT,
SpanAttributes.NET_PROTOCOL_NAME: SpanAttributes.NETWORK_PROTOCOL_NAME,
SpanAttributes.NET_PROTOCOL_VERSION: SpanAttributes.NETWORK_PROTOCOL_VERSION,
SpanAttributes.NET_SOCK_FAMILY: (SpanAttributes.NETWORK_TRANSPORT, SpanAttributes.NETWORK_TYPE),
SpanAttributes.NET_SOCK_FAMILY: SpanAttributes.NETWORK_TYPE,
SpanAttributes.NET_PEER_IP: SpanAttributes.CLIENT_SOCKET_ADDRESS,
SpanAttributes.NET_HOST_IP: SpanAttributes.SERVER_SOCKET_ADDRESS,
SpanAttributes.HTTP_SERVER_NAME: SpanAttributes.SERVER_ADDRESS,
Expand All @@ -189,7 +189,7 @@ def _python_path_without_directory(python_path, directory, path_separator):
SpanAttributes.NET_APP_PROTOCOL_NAME: SpanAttributes.NETWORK_PROTOCOL_NAME,
SpanAttributes.NET_APP_PROTOCOL_VERSION: SpanAttributes.NETWORK_PROTOCOL_VERSION,
SpanAttributes.HTTP_CLIENT_IP: SpanAttributes.CLIENT_ADDRESS,
SpanAttributes.HTTP_FLAVOR: (SpanAttributes.NETWORK_PROTOCOL_NAME, SpanAttributes.NETWORK_PROTOCOL_VERSION),
SpanAttributes.HTTP_FLAVOR: SpanAttributes.NETWORK_PROTOCOL_VERSION,
SpanAttributes.NET_HOST_CONNECTION_TYPE: SpanAttributes.NETWORK_CONNECTION_TYPE,
SpanAttributes.NET_HOST_CONNECTION_SUBTYPE: SpanAttributes.NETWORK_CONNECTION_SUBTYPE,
SpanAttributes.NET_HOST_CARRIER_NAME: SpanAttributes.NETWORK_CARRIER_NAME,
Expand Down Expand Up @@ -276,24 +276,57 @@ def _initialize(cls):
] = http_opt_in
_OpenTelemetrySemanticConventionStability._initialized = True


@classmethod
def _get_opentelemetry_stability_opt_in(
type: _OpenTelemetryStabilitySignalType,
# Get OpenTelemetry opt-in mode based off of signal type (http, messaging, etc.)
def _get_opentelemetry_stability_opt_in_mode(
cls,
signal_type: _OpenTelemetryStabilitySignalType,
) -> _OpenTelemetryStabilityMode:
with _OpenTelemetrySemanticConventionStability._lock:
return _OpenTelemetrySemanticConventionStability._OTEL_SEMCONV_STABILITY_SIGNAL_MAPPING.get(
type, _OpenTelemetryStabilityMode.DEFAULT
)
return _OpenTelemetrySemanticConventionStability._OTEL_SEMCONV_STABILITY_SIGNAL_MAPPING.get(
signal_type, _OpenTelemetryStabilityMode.DEFAULT
)


@classmethod
def _get_new_convention(old: str, mapping: Dict[str, str], type=None) -> str:
# Get new semantic convention attribute based off old attribute
# If no new attribute exists for old attribute, returns old attribute
def _get_new_convention(cls, old: str, client_or_server: Optional[str]) -> str:
new_convention = ""
if type is None:
new_convention = mapping.get(old, old)
elif type == "client":
new_convention = _OTEL_HTTP_SEMCONV_MIGRATION_MAPPING_CLIENT.get(old, old)
elif type == "server":
new_convention = _OTEL_HTTP_SEMCONV_MIGRATION_MAPPING_SERVER.get(old, old)
if client_or_server is None:
new_convention = _OTEL_HTTP_SEMCONV_MIGRATION_MAPPING.get(old, old)
elif client_or_server == "client":
if old in _OTEL_HTTP_SEMCONV_MIGRATION_MAPPING:
new_convention = _OTEL_HTTP_SEMCONV_MIGRATION_MAPPING[old]
else:
new_convention = _OTEL_HTTP_SEMCONV_MIGRATION_MAPPING_CLIENT.get(old, old)
elif client_or_server == "server":
if old in _OTEL_HTTP_SEMCONV_MIGRATION_MAPPING:
new_convention = _OTEL_HTTP_SEMCONV_MIGRATION_MAPPING[old]
else:
new_convention = _OTEL_HTTP_SEMCONV_MIGRATION_MAPPING_SERVER.get(old, old)
if isinstance(new_convention, tuple):
new_convention = "".join(new_convention)
return new_convention

@classmethod
# Get updated attributes based off of opt-in mode and client/server
def _get_attributes_based_on_stability_mode(
cls,
old_attributes: Dict[str, str],
mode: _OpenTelemetryStabilityMode,
client_or_server: Optional[str] = None,
) -> Dict[str, str]:
if mode is _OpenTelemetryStabilityMode.DEFAULT:
return old_attributes
attributes = {}
for old_key, value in old_attributes.items():
new_key = _OpenTelemetrySemanticConventionStability._get_new_convention(
old_key,
client_or_server,
)
attributes[new_key] = value
if mode is _OpenTelemetryStabilityMode.HTTP_DUP:
attributes[old_key] = value

return attributes

0 comments on commit 125028a

Please sign in to comment.