Skip to content

Commit ed8ab4c

Browse files
committed
first draft on urllib3 transition
1 parent 5a7935f commit ed8ab4c

File tree

1 file changed

+42
-4
lines changed
  • instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3

1 file changed

+42
-4
lines changed

instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py

+42-4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,25 @@ def response_hook(span, request, response):
8787
import urllib3.connectionpool
8888
import wrapt
8989

90+
from opentelemetry.instrumentation._semconv import (
91+
_client_duration_attrs_new,
92+
_client_duration_attrs_old,
93+
_filter_semconv_duration_attrs,
94+
_get_schema_url,
95+
_HTTPStabilityMode,
96+
_OpenTelemetrySemanticConventionStability,
97+
_OpenTelemetryStabilitySignalType,
98+
_report_new,
99+
_report_old,
100+
_set_http_host,
101+
_set_http_method,
102+
_set_http_net_peer_name_client,
103+
_set_http_network_protocol_version,
104+
_set_http_peer_port_client,
105+
_set_http_scheme,
106+
_set_http_status_code,
107+
_set_http_url,
108+
)
90109
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
91110
from opentelemetry.instrumentation.urllib3.package import _instruments
92111
from opentelemetry.instrumentation.urllib3.version import __version__
@@ -98,14 +117,23 @@ def response_hook(span, request, response):
98117
)
99118
from opentelemetry.metrics import Histogram, get_meter
100119
from opentelemetry.propagate import inject
120+
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
121+
from opentelemetry.semconv.attributes.network_attributes import (
122+
NETWORK_PEER_ADDRESS,
123+
NETWORK_PEER_PORT,
124+
)
101125
from opentelemetry.semconv.metrics import MetricInstruments
126+
from opentelemetry.semconv.metrics.http_metrics import (
127+
HTTP_CLIENT_REQUEST_DURATION,
128+
)
102129
from opentelemetry.semconv.trace import SpanAttributes
103130
from opentelemetry.trace import Span, SpanKind, Tracer, get_tracer
104131
from opentelemetry.trace.status import Status
105132
from opentelemetry.util.http import (
106133
ExcludeList,
107134
get_excluded_urls,
108135
parse_excluded_urls,
136+
sanitize_method,
109137
)
110138
from opentelemetry.util.http.httplib import set_ip_on_next_http_connection
111139

@@ -158,12 +186,18 @@ def _instrument(self, **kwargs):
158186
``excluded_urls``: A string containing a comma-delimited
159187
list of regexes used to exclude URLs from tracking
160188
"""
189+
# initialize semantic conventions opt-in if needed
190+
_OpenTelemetrySemanticConventionStability._initialize()
191+
sem_conv_opt_in_mode = _OpenTelemetrySemanticConventionStability._get_opentelemetry_stability_opt_in_mode(
192+
_OpenTelemetryStabilitySignalType.HTTP,
193+
)
194+
schema_url = _get_schema_url(sem_conv_opt_in_mode)
161195
tracer_provider = kwargs.get("tracer_provider")
162196
tracer = get_tracer(
163197
__name__,
164198
__version__,
165199
tracer_provider,
166-
schema_url="https://opentelemetry.io/schemas/1.11.0",
200+
schema_url=schema_url,
167201
)
168202

169203
excluded_urls = kwargs.get("excluded_urls")
@@ -173,7 +207,7 @@ def _instrument(self, **kwargs):
173207
__name__,
174208
__version__,
175209
meter_provider,
176-
schema_url="https://opentelemetry.io/schemas/1.11.0",
210+
schema_url=schema_url,
177211
)
178212

179213
duration_histogram = meter.create_histogram(
@@ -205,6 +239,7 @@ def _instrument(self, **kwargs):
205239
if excluded_urls is None
206240
else parse_excluded_urls(excluded_urls)
207241
),
242+
sem_conv_opt_in_mode=sem_conv_opt_in_mode,
208243
)
209244

210245
def _uninstrument(self, **kwargs):
@@ -220,6 +255,7 @@ def _instrument(
220255
response_hook: _ResponseHookT = None,
221256
url_filter: _UrlFilterT = None,
222257
excluded_urls: ExcludeList = None,
258+
sem_conv_opt_in_mode: _HTTPStabilityMode = _HTTPStabilityMode.DEFAULT,
223259
):
224260
def instrumented_urlopen(wrapped, instance, args, kwargs):
225261
if not is_http_instrumentation_enabled():
@@ -233,7 +269,7 @@ def instrumented_urlopen(wrapped, instance, args, kwargs):
233269
headers = _prepare_headers(kwargs)
234270
body = _get_url_open_arg("body", args, kwargs)
235271

236-
span_name = method.strip()
272+
span_name = sanitize_method(method.strip())
237273
span_attributes = {
238274
SpanAttributes.HTTP_METHOD: method,
239275
SpanAttributes.HTTP_URL: url,
@@ -282,7 +318,9 @@ def instrumented_urlopen(wrapped, instance, args, kwargs):
282318
)
283319

284320

285-
def _get_url_open_arg(name: str, args: typing.List, kwargs: typing.Mapping):
321+
def _get_url_open_arg(
322+
name: str, args: typing.List, kwargs: typing.Mapping
323+
) -> str:
286324
arg_idx = _URL_OPEN_ARG_TO_INDEX_MAPPING.get(name)
287325
if arg_idx is not None:
288326
try:

0 commit comments

Comments
 (0)