Skip to content

Commit fa76a1b

Browse files
authored
Merge branch 'main' into rads-1996/redact-sensitive-params
2 parents 576466b + 6c89a56 commit fa76a1b

File tree

2 files changed

+59
-42
lines changed

2 files changed

+59
-42
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,16 @@ def response_hook(span: Span, environ: WSGIEnvironment, status: str, response_he
252252
from opentelemetry.instrumentation.wsgi.version import __version__
253253
from opentelemetry.metrics import MeterProvider, get_meter
254254
from opentelemetry.propagators.textmap import Getter
255+
from opentelemetry.semconv._incubating.attributes.http_attributes import (
256+
HTTP_HOST,
257+
HTTP_SERVER_NAME,
258+
HTTP_URL,
259+
)
255260
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
256261
from opentelemetry.semconv.metrics import MetricInstruments
257262
from opentelemetry.semconv.metrics.http_metrics import (
258263
HTTP_SERVER_REQUEST_DURATION,
259264
)
260-
from opentelemetry.semconv.trace import SpanAttributes
261265
from opentelemetry.trace import TracerProvider
262266
from opentelemetry.trace.status import Status, StatusCode
263267
from opentelemetry.util.http import (
@@ -335,7 +339,7 @@ def collect_request_attributes(
335339
# old semconv v1.12.0
336340
server_name = environ.get("SERVER_NAME")
337341
if _report_old(sem_conv_opt_in_mode):
338-
result[SpanAttributes.HTTP_SERVER_NAME] = server_name
342+
result[HTTP_SERVER_NAME] = server_name
339343

340344
_set_http_scheme(
341345
result,
@@ -349,7 +353,7 @@ def collect_request_attributes(
349353
_set_http_net_host(result, host, sem_conv_opt_in_mode)
350354
# old semconv v1.12.0
351355
if _report_old(sem_conv_opt_in_mode):
352-
result[SpanAttributes.HTTP_HOST] = host
356+
result[HTTP_HOST] = host
353357
if host_port:
354358
_set_http_net_host_port(
355359
result,

instrumentation/opentelemetry-instrumentation-wsgi/tests/test_wsgi_middleware.py

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@
3737
NumberDataPoint,
3838
)
3939
from opentelemetry.sdk.resources import Resource
40+
from opentelemetry.semconv._incubating.attributes.http_attributes import (
41+
HTTP_FLAVOR,
42+
HTTP_HOST,
43+
HTTP_METHOD,
44+
HTTP_SCHEME,
45+
HTTP_SERVER_NAME,
46+
HTTP_STATUS_CODE,
47+
HTTP_TARGET,
48+
HTTP_URL,
49+
HTTP_USER_AGENT,
50+
)
51+
from opentelemetry.semconv._incubating.attributes.net_attributes import (
52+
NET_HOST_NAME,
53+
NET_HOST_PORT,
54+
)
4055
from opentelemetry.semconv.attributes.http_attributes import (
4156
HTTP_REQUEST_METHOD,
4257
HTTP_RESPONSE_STATUS_CODE,
@@ -54,7 +69,9 @@
5469
URL_QUERY,
5570
URL_SCHEME,
5671
)
57-
from opentelemetry.semconv.trace import SpanAttributes
72+
from opentelemetry.semconv.attributes.user_agent_attributes import (
73+
USER_AGENT_ORIGINAL,
74+
)
5875
from opentelemetry.test.test_base import TestBase
5976
from opentelemetry.test.wsgitestutil import WsgiTestBase
6077
from opentelemetry.trace import StatusCode
@@ -245,14 +262,14 @@ def validate_response(
245262
self.assertEqual(span_list[0].kind, trace_api.SpanKind.SERVER)
246263
expected_attributes = {}
247264
expected_attributes_old = {
248-
SpanAttributes.HTTP_SERVER_NAME: "127.0.0.1",
249-
SpanAttributes.HTTP_SCHEME: "http",
250-
SpanAttributes.NET_HOST_PORT: 80,
251-
SpanAttributes.HTTP_HOST: "127.0.0.1",
252-
SpanAttributes.HTTP_FLAVOR: "1.0",
253-
SpanAttributes.HTTP_URL: "http://127.0.0.1/",
254-
SpanAttributes.HTTP_STATUS_CODE: 200,
255-
SpanAttributes.NET_HOST_NAME: "127.0.0.1",
265+
HTTP_SERVER_NAME: "127.0.0.1",
266+
HTTP_SCHEME: "http",
267+
NET_HOST_PORT: 80,
268+
HTTP_HOST: "127.0.0.1",
269+
HTTP_FLAVOR: "1.0",
270+
HTTP_URL: "http://127.0.0.1/",
271+
HTTP_STATUS_CODE: 200,
272+
NET_HOST_NAME: "127.0.0.1",
256273
}
257274
expected_attributes_new = {
258275
SERVER_PORT: 80,
@@ -269,7 +286,7 @@ def validate_response(
269286
expected_attributes.update(span_attributes or {})
270287
if http_method is not None:
271288
if old_sem_conv:
272-
expected_attributes[SpanAttributes.HTTP_METHOD] = http_method
289+
expected_attributes[HTTP_METHOD] = http_method
273290
if new_sem_conv:
274291
expected_attributes[HTTP_REQUEST_METHOD] = http_method
275292
self.assertEqual(span_list[0].attributes, expected_attributes)
@@ -523,14 +540,14 @@ def test_request_attributes(self):
523540
self.assertDictEqual(
524541
attrs,
525542
{
526-
SpanAttributes.HTTP_METHOD: "GET",
527-
SpanAttributes.HTTP_HOST: "127.0.0.1",
528-
SpanAttributes.HTTP_URL: "http://127.0.0.1/?foo=bar",
529-
SpanAttributes.NET_HOST_PORT: 80,
530-
SpanAttributes.HTTP_SCHEME: "http",
531-
SpanAttributes.HTTP_SERVER_NAME: "127.0.0.1",
532-
SpanAttributes.HTTP_FLAVOR: "1.0",
533-
SpanAttributes.NET_HOST_NAME: "127.0.0.1",
543+
HTTP_METHOD: "GET",
544+
HTTP_HOST: "127.0.0.1",
545+
HTTP_URL: "http://127.0.0.1/?foo=bar",
546+
NET_HOST_PORT: 80,
547+
HTTP_SCHEME: "http",
548+
HTTP_SERVER_NAME: "127.0.0.1",
549+
HTTP_FLAVOR: "1.0",
550+
NET_HOST_NAME: "127.0.0.1",
534551
},
535552
)
536553

@@ -565,10 +582,10 @@ def validate_url(
565582
):
566583
parts = urlsplit(expected_url)
567584
expected_old = {
568-
SpanAttributes.HTTP_SCHEME: parts.scheme,
569-
SpanAttributes.NET_HOST_PORT: parts.port
585+
HTTP_SCHEME: parts.scheme,
586+
NET_HOST_PORT: parts.port
570587
or (80 if parts.scheme == "http" else 443),
571-
SpanAttributes.HTTP_SERVER_NAME: parts.hostname, # Not true in the general case, but for all tests.
588+
HTTP_SERVER_NAME: parts.hostname, # Not true in the general case, but for all tests.
572589
}
573590
expected_new = {
574591
SERVER_PORT: parts.port or (80 if parts.scheme == "http" else 443),
@@ -578,13 +595,13 @@ def validate_url(
578595
}
579596
if old_semconv:
580597
if raw:
581-
expected_old[SpanAttributes.HTTP_TARGET] = expected_url.split(
598+
expected_old[HTTP_TARGET] = expected_url.split(
582599
parts.netloc, 1
583600
)[1]
584601
else:
585-
expected_old[SpanAttributes.HTTP_URL] = expected_url
602+
expected_old[HTTP_URL] = expected_url
586603
if has_host:
587-
expected_old[SpanAttributes.HTTP_HOST] = parts.hostname
604+
expected_old[HTTP_HOST] = parts.hostname
588605
if new_semconv:
589606
if raw:
590607
expected_new[URL_PATH] = expected_url.split(parts.path, 1)[1]
@@ -713,9 +730,9 @@ def test_request_attributes_with_conflicting_nonstandard_port(self):
713730
":8080" # Note that we do not correct SERVER_PORT
714731
)
715732
expected = {
716-
SpanAttributes.HTTP_HOST: "127.0.0.1:8080",
717-
SpanAttributes.HTTP_URL: "http://127.0.0.1:8080/",
718-
SpanAttributes.NET_HOST_PORT: 80,
733+
HTTP_HOST: "127.0.0.1:8080",
734+
HTTP_URL: "http://127.0.0.1:8080/",
735+
NET_HOST_PORT: 80,
719736
}
720737
self.assertGreaterEqual(
721738
otel_wsgi.collect_request_attributes(self.environ).items(),
@@ -729,9 +746,7 @@ def test_request_attributes_with_faux_scheme_relative_raw_uri(self):
729746
def test_request_attributes_pathless(self):
730747
self.environ["RAW_URI"] = ""
731748
self.assertIsNone(
732-
otel_wsgi.collect_request_attributes(self.environ).get(
733-
SpanAttributes.HTTP_TARGET
734-
)
749+
otel_wsgi.collect_request_attributes(self.environ).get(HTTP_TARGET)
735750
)
736751

737752
def test_request_attributes_with_full_request_uri(self):
@@ -741,8 +756,8 @@ def test_request_attributes_with_full_request_uri(self):
741756
"http://docs.python.org:80/3/library/urllib.parse.html?highlight=params#url-parsing" # Might happen in a CONNECT request
742757
)
743758
expected_old = {
744-
SpanAttributes.HTTP_HOST: "127.0.0.1:8080",
745-
SpanAttributes.HTTP_TARGET: "http://docs.python.org:80/3/library/urllib.parse.html?highlight=params#url-parsing",
759+
HTTP_HOST: "127.0.0.1:8080",
760+
HTTP_TARGET: "http://docs.python.org:80/3/library/urllib.parse.html?highlight=params#url-parsing",
746761
}
747762
expected_new = {
748763
URL_PATH: "/3/library/urllib.parse.html",
@@ -762,8 +777,8 @@ def test_request_attributes_with_full_request_uri(self):
762777

763778
def test_http_user_agent_attribute(self):
764779
self.environ["HTTP_USER_AGENT"] = "test-useragent"
765-
expected = {SpanAttributes.HTTP_USER_AGENT: "test-useragent"}
766-
expected_new = {SpanAttributes.USER_AGENT_ORIGINAL: "test-useragent"}
780+
expected = {HTTP_USER_AGENT: "test-useragent"}
781+
expected_new = {USER_AGENT_ORIGINAL: "test-useragent"}
767782
self.assertGreaterEqual(
768783
otel_wsgi.collect_request_attributes(self.environ).items(),
769784
expected.items(),
@@ -784,10 +799,8 @@ def test_response_attributes(self):
784799
{},
785800
sem_conv_opt_in_mode=_StabilityMode.HTTP,
786801
)
787-
expected = (mock.call(SpanAttributes.HTTP_STATUS_CODE, 404),)
788-
expected_new = (
789-
mock.call(SpanAttributes.HTTP_RESPONSE_STATUS_CODE, 404),
790-
)
802+
expected = (mock.call(HTTP_STATUS_CODE, 404),)
803+
expected_new = (mock.call(HTTP_RESPONSE_STATUS_CODE, 404),)
791804
self.assertEqual(self.span.set_attribute.call_count, 2)
792805
self.span.set_attribute.assert_has_calls(expected, any_order=True)
793806
self.span.set_attribute.assert_has_calls(expected_new, any_order=True)
@@ -803,7 +816,7 @@ def test_response_attributes_noop(self):
803816

804817
self.assertEqual(mock_span.set_attribute.call_count, 0)
805818
self.assertEqual(mock_span.is_recording.call_count, 2)
806-
self.assertEqual(attrs[SpanAttributes.HTTP_STATUS_CODE], 404)
819+
self.assertEqual(attrs[HTTP_STATUS_CODE], 404)
807820

808821
def test_remove_sensitive_params(self):
809822
self.environ["HTTP_HOST"] = "username:password@mock"

0 commit comments

Comments
 (0)