Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP transition for wsgi #2425

Merged
merged 53 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
27e4c2c
Update .pylintrc
lzchen Apr 3, 2024
c6b4c05
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Apr 5, 2024
f690fc8
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Apr 9, 2024
a674535
wsgi, requests
lzchen Apr 9, 2024
9bb6f8e
wsgi metrics
lzchen Apr 10, 2024
01d2f5b
wsgi resp
lzchen Apr 10, 2024
c453f07
fix wsgi tests
lzchen Apr 11, 2024
d101296
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Apr 11, 2024
b93458d
util
lzchen Apr 11, 2024
a4f4fc8
fix tests
lzchen Apr 11, 2024
fe47978
fix wsgi
lzchen Apr 11, 2024
b469c5c
pyramid
lzchen Apr 11, 2024
d635840
flacon
lzchen Apr 11, 2024
715f282
fix tests
lzchen Apr 11, 2024
e0e19c3
falcon
lzchen Apr 11, 2024
08806ac
wsgi tests
lzchen Apr 15, 2024
3eef437
Update CHANGELOG.md
lzchen Apr 15, 2024
15cc32c
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Apr 15, 2024
c440866
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Apr 17, 2024
60337bc
name
lzchen Apr 17, 2024
6bebebc
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Apr 17, 2024
69206ca
fixes
lzchen Apr 18, 2024
6396a09
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Apr 18, 2024
1359c68
lint
lzchen Apr 18, 2024
f3348a2
lint
lzchen Apr 18, 2024
4e1afd5
Merge branch 'main' into wsgi
lzchen Apr 18, 2024
e37fa8f
Update test_requests_integration.py
lzchen Apr 18, 2024
f387ad9
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
lzchen Apr 18, 2024
4bd66ea
Merge branch 'wsgi' of https://github.com/lzchen/opentelemetry-python…
lzchen Apr 18, 2024
03252ff
wsgi, requests
lzchen Apr 9, 2024
fe50c2e
wsgi metrics
lzchen Apr 10, 2024
de0e426
wsgi resp
lzchen Apr 10, 2024
0d57a3e
fix wsgi tests
lzchen Apr 11, 2024
d25c7d7
util
lzchen Apr 11, 2024
60ec5e5
fix tests
lzchen Apr 11, 2024
4b83027
fix wsgi
lzchen Apr 11, 2024
1767580
pyramid
lzchen Apr 11, 2024
b76af80
flacon
lzchen Apr 11, 2024
b28742a
fix tests
lzchen Apr 11, 2024
9308600
falcon
lzchen Apr 11, 2024
f65ebb4
wsgi tests
lzchen Apr 15, 2024
fa09345
Update CHANGELOG.md
lzchen Apr 15, 2024
35d24e8
name
lzchen Apr 17, 2024
99be783
fixes
lzchen Apr 18, 2024
6dfa631
lint
lzchen Apr 18, 2024
998b996
lint
lzchen Apr 18, 2024
f0bf49d
Update test_requests_integration.py
lzchen Apr 18, 2024
34c320d
Fix lint
ocelotl Apr 18, 2024
ee05a47
lint
lzchen Apr 19, 2024
debed72
Merge branch 'wsgi' of https://github.com/lzchen/opentelemetry-python…
lzchen Apr 19, 2024
0ab0097
Update __init__.py
lzchen Apr 19, 2024
d683a72
Update test_wsgi_middleware.py
lzchen Apr 19, 2024
d2c12c2
Update __init__.py
lzchen Apr 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
- `opentelemetry-sdk-extension-aws` Register AWS resource detectors under the
`opentelemetry_resource_detector` entry point
([#2382](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2382))

### Breaking changes

Expand All @@ -21,6 +18,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `opentelemetry-sdk-extension-aws` Register AWS resource detectors under the
`opentelemetry_resource_detector` entry point
([#2382](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2382))
- `opentelemetry-instrumentation-wsgi` Implement new semantic convention opt-in with stable http semantic conventions
([#2425](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2425))
- `opentelemetry-instrumentation-threading` Initial release for threading
([#2253](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2253))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@
from unittest.mock import Mock, patch

import pytest
from falcon import __version__ as _falcon_verison
from falcon import __version__ as _falcon_version
from falcon import testing
from packaging import version as package_version

from opentelemetry import trace
from opentelemetry.instrumentation._semconv import (
_server_active_requests_count_attrs_old,
_server_duration_attrs_old,
)
from opentelemetry.instrumentation.falcon import FalconInstrumentor
from opentelemetry.instrumentation.propagators import (
TraceResponsePropagator,
get_global_response_propagator,
set_global_response_propagator,
)
from opentelemetry.instrumentation.wsgi import (
_active_requests_count_attrs,
_duration_attrs,
)
from opentelemetry.sdk.metrics.export import (
HistogramDataPoint,
NumberDataPoint,
Expand All @@ -53,8 +53,8 @@
"http.server.duration",
]
_recommended_attrs = {
"http.server.active_requests": _active_requests_count_attrs,
"http.server.duration": _duration_attrs,
"http.server.active_requests": _server_active_requests_count_attrs_old,
"http.server.duration": _server_duration_attrs_old,
}


Expand Down Expand Up @@ -125,7 +125,7 @@ def _test_method(self, method):
SpanAttributes.NET_HOST_PORT: 80,
SpanAttributes.HTTP_HOST: "falconframework.org",
SpanAttributes.HTTP_TARGET: "/",
SpanAttributes.NET_PEER_PORT: "65133",
SpanAttributes.NET_PEER_PORT: 65133,
SpanAttributes.HTTP_FLAVOR: "1.1",
"falcon.resource": "HelloWorldResource",
SpanAttributes.HTTP_STATUS_CODE: 201,
Expand Down Expand Up @@ -156,7 +156,7 @@ def test_404(self):
SpanAttributes.NET_HOST_PORT: 80,
SpanAttributes.HTTP_HOST: "falconframework.org",
SpanAttributes.HTTP_TARGET: "/",
SpanAttributes.NET_PEER_PORT: "65133",
SpanAttributes.NET_PEER_PORT: 65133,
SpanAttributes.HTTP_FLAVOR: "1.1",
SpanAttributes.HTTP_STATUS_CODE: 404,
},
Expand Down Expand Up @@ -193,7 +193,7 @@ def test_500(self):
SpanAttributes.NET_HOST_PORT: 80,
SpanAttributes.HTTP_HOST: "falconframework.org",
SpanAttributes.HTTP_TARGET: "/",
SpanAttributes.NET_PEER_PORT: "65133",
SpanAttributes.NET_PEER_PORT: 65133,
SpanAttributes.HTTP_FLAVOR: "1.1",
SpanAttributes.HTTP_STATUS_CODE: 500,
},
Expand Down Expand Up @@ -226,7 +226,7 @@ def test_url_template(self):
SpanAttributes.NET_HOST_PORT: 80,
SpanAttributes.HTTP_HOST: "falconframework.org",
SpanAttributes.HTTP_TARGET: "/",
SpanAttributes.NET_PEER_PORT: "65133",
SpanAttributes.NET_PEER_PORT: 65133,
SpanAttributes.HTTP_FLAVOR: "1.1",
"falcon.resource": "UserResource",
SpanAttributes.HTTP_STATUS_CODE: 200,
Expand Down Expand Up @@ -336,6 +336,7 @@ def test_falcon_metric_values(self):
"http.flavor": "1.1",
"http.server_name": "falconframework.org",
"net.host.port": 80,
"net.host.name": "falconframework.org",
"http.status_code": 404,
}
expected_requests_count_attributes = {
Expand All @@ -344,6 +345,8 @@ def test_falcon_metric_values(self):
"http.scheme": "http",
"http.flavor": "1.1",
"http.server_name": "falconframework.org",
"net.host.name": "falconframework.org",
"net.host.port": 80,
}
start = default_timer()
self.client().simulate_get("/hello/756")
Expand Down Expand Up @@ -523,7 +526,7 @@ def test_custom_request_header_not_added_in_internal_span(self):
self.assertNotIn(key, span.attributes)

@pytest.mark.skipif(
condition=package_version.parse(_falcon_verison)
condition=package_version.parse(_falcon_version)
< package_version.parse("2.0.0"),
reason="falcon<2 does not implement custom response headers",
)
Expand Down Expand Up @@ -558,7 +561,7 @@ def test_custom_response_header_added_in_server_span(self):
self.assertNotIn(key, span.attributes)

@pytest.mark.skipif(
condition=package_version.parse(_falcon_verison)
condition=package_version.parse(_falcon_version)
< package_version.parse("2.0.0"),
reason="falcon<2 does not implement custom response headers",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
from flask import Flask, request

from opentelemetry import trace
from opentelemetry.instrumentation._semconv import (
_server_active_requests_count_attrs_old,
_server_duration_attrs_old,
)
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.instrumentation.propagators import (
TraceResponsePropagator,
get_global_response_propagator,
set_global_response_propagator,
)
from opentelemetry.instrumentation.wsgi import (
OpenTelemetryMiddleware,
_active_requests_count_attrs,
_duration_attrs,
)
from opentelemetry.instrumentation.wsgi import OpenTelemetryMiddleware
from opentelemetry.sdk.metrics.export import (
HistogramDataPoint,
NumberDataPoint,
Expand All @@ -54,6 +54,7 @@ def expected_attributes(override_attributes):
SpanAttributes.HTTP_SERVER_NAME: "localhost",
SpanAttributes.HTTP_SCHEME: "http",
SpanAttributes.NET_HOST_PORT: 80,
SpanAttributes.NET_HOST_NAME: "localhost",
SpanAttributes.HTTP_HOST: "localhost",
SpanAttributes.HTTP_TARGET: "/",
SpanAttributes.HTTP_FLAVOR: "1.1",
Expand All @@ -69,8 +70,8 @@ def expected_attributes(override_attributes):
"http.server.duration",
]
_recommended_attrs = {
"http.server.active_requests": _active_requests_count_attrs,
"http.server.duration": _duration_attrs,
"http.server.active_requests": _server_active_requests_count_attrs_old,
"http.server.duration": _server_duration_attrs_old,
}


Expand Down Expand Up @@ -358,13 +359,16 @@ def test_basic_metric_success(self):
"http.server_name": "localhost",
"net.host.port": 80,
"http.status_code": 200,
"net.host.name": "localhost",
}
expected_requests_count_attributes = {
"http.method": "GET",
"http.host": "localhost",
"http.scheme": "http",
"http.flavor": "1.1",
"http.server_name": "localhost",
"net.host.name": "localhost",
"net.host.port": 80,
}
self._assert_basic_metric(
expected_duration_attributes,
Expand All @@ -374,20 +378,23 @@ def test_basic_metric_success(self):
def test_basic_metric_nonstandard_http_method_success(self):
self.client.open("/hello/756", method="NONSTANDARD")
expected_duration_attributes = {
"http.method": "UNKNOWN",
"http.method": "_OTHER",
"http.host": "localhost",
"http.scheme": "http",
"http.flavor": "1.1",
"http.server_name": "localhost",
"net.host.port": 80,
"http.status_code": 405,
"net.host.name": "localhost",
}
expected_requests_count_attributes = {
"http.method": "UNKNOWN",
"http.method": "_OTHER",
"http.host": "localhost",
"http.scheme": "http",
"http.flavor": "1.1",
"http.server_name": "localhost",
"net.host.name": "localhost",
"net.host.port": 80,
}
self._assert_basic_metric(
expected_duration_attributes,
Expand All @@ -410,13 +417,16 @@ def test_basic_metric_nonstandard_http_method_allowed_success(self):
"http.server_name": "localhost",
"net.host.port": 80,
"http.status_code": 405,
"net.host.name": "localhost",
}
expected_requests_count_attributes = {
"http.method": "NONSTANDARD",
"http.host": "localhost",
"http.scheme": "http",
"http.flavor": "1.1",
"http.server_name": "localhost",
"net.host.name": "localhost",
"net.host.port": 80,
}
self._assert_basic_metric(
expected_duration_attributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
from pyramid.config import Configurator

from opentelemetry import trace
from opentelemetry.instrumentation._semconv import (
_server_active_requests_count_attrs_old,
_server_duration_attrs_old,
)
from opentelemetry.instrumentation.pyramid import PyramidInstrumentor
from opentelemetry.sdk.metrics.export import (
HistogramDataPoint,
Expand All @@ -31,8 +35,6 @@
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS,
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST,
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE,
_active_requests_count_attrs,
_duration_attrs,
)

# pylint: disable=import-error
Expand All @@ -43,8 +45,8 @@
"http.server.duration",
]
_recommended_attrs = {
"http.server.active_requests": _active_requests_count_attrs,
"http.server.duration": _duration_attrs,
"http.server.active_requests": _server_active_requests_count_attrs_old,
"http.server.duration": _server_duration_attrs_old,
}


Expand Down Expand Up @@ -213,13 +215,16 @@ def test_basic_metric_success(self):
"http.server_name": "localhost",
"net.host.port": 80,
"http.status_code": 200,
"net.host.name": "localhost",
}
expected_requests_count_attributes = {
"http.method": "GET",
"http.host": "localhost",
"http.scheme": "http",
"http.flavor": "1.1",
"http.server_name": "localhost",
"net.host.name": "localhost",
"net.host.port": 80,
}
metrics_list = self.memory_metrics_reader.get_metrics_data()
for metric in (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def expected_attributes(override_attributes):
SpanAttributes.HTTP_SERVER_NAME: "localhost",
SpanAttributes.HTTP_SCHEME: "http",
SpanAttributes.NET_HOST_PORT: 80,
SpanAttributes.NET_HOST_NAME: "localhost",
SpanAttributes.HTTP_HOST: "localhost",
SpanAttributes.HTTP_TARGET: "/",
SpanAttributes.HTTP_FLAVOR: "1.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,20 @@
_SPAN_ATTRIBUTES_ERROR_TYPE,
_SPAN_ATTRIBUTES_NETWORK_PEER_ADDRESS,
_SPAN_ATTRIBUTES_NETWORK_PEER_PORT,
_filter_duration_attrs,
_client_duration_attrs_new,
_client_duration_attrs_old,
_filter_semconv_duration_attrs,
_get_schema_url,
_HTTPStabilityMode,
_OpenTelemetrySemanticConventionStability,
_OpenTelemetryStabilityMode,
_OpenTelemetryStabilitySignalType,
_report_new,
_report_old,
_set_http_hostname,
_set_http_host,
_set_http_method,
_set_http_net_peer_name,
_set_http_net_peer_name_client,
_set_http_network_protocol_version,
_set_http_port,
_set_http_peer_port_client,
_set_http_scheme,
_set_http_status_code,
_set_http_url,
Expand Down Expand Up @@ -117,7 +119,7 @@ def _instrument(
request_hook: _RequestHookT = None,
response_hook: _ResponseHookT = None,
excluded_urls: ExcludeList = None,
sem_conv_opt_in_mode: _OpenTelemetryStabilityMode = _OpenTelemetryStabilityMode.DEFAULT,
sem_conv_opt_in_mode: _HTTPStabilityMode = _HTTPStabilityMode.DEFAULT,
):
"""Enables tracing of all requests calls that go through
:code:`requests.session.Session.request` (this includes
Expand Down Expand Up @@ -174,14 +176,14 @@ def get_or_create_headers():
metric_labels, parsed_url.scheme, sem_conv_opt_in_mode
)
if parsed_url.hostname:
_set_http_hostname(
_set_http_host(
metric_labels, parsed_url.hostname, sem_conv_opt_in_mode
)
_set_http_net_peer_name(
_set_http_net_peer_name_client(
metric_labels, parsed_url.hostname, sem_conv_opt_in_mode
)
if _report_new(sem_conv_opt_in_mode):
_set_http_hostname(
_set_http_host(
span_attributes,
parsed_url.hostname,
sem_conv_opt_in_mode,
Expand All @@ -191,11 +193,11 @@ def get_or_create_headers():
_SPAN_ATTRIBUTES_NETWORK_PEER_ADDRESS
] = parsed_url.hostname
if parsed_url.port:
_set_http_port(
_set_http_peer_port_client(
metric_labels, parsed_url.port, sem_conv_opt_in_mode
)
if _report_new(sem_conv_opt_in_mode):
_set_http_port(
_set_http_peer_port_client(
span_attributes, parsed_url.port, sem_conv_opt_in_mode
)
# Use semconv library when available
Expand Down Expand Up @@ -284,16 +286,22 @@ def get_or_create_headers():
).__qualname__

if duration_histogram_old is not None:
duration_attrs_old = _filter_duration_attrs(
metric_labels, _OpenTelemetryStabilityMode.DEFAULT
duration_attrs_old = _filter_semconv_duration_attrs(
metric_labels,
_client_duration_attrs_old,
_client_duration_attrs_new,
_HTTPStabilityMode.DEFAULT,
)
duration_histogram_old.record(
max(round(elapsed_time * 1000), 0),
attributes=duration_attrs_old,
)
if duration_histogram_new is not None:
duration_attrs_new = _filter_duration_attrs(
metric_labels, _OpenTelemetryStabilityMode.HTTP
duration_attrs_new = _filter_semconv_duration_attrs(
metric_labels,
_client_duration_attrs_old,
_client_duration_attrs_new,
_HTTPStabilityMode.HTTP,
)
duration_histogram_new.record(
elapsed_time, attributes=duration_attrs_new
Expand Down Expand Up @@ -341,7 +349,10 @@ def get_default_span_name(method):
Returns:
span name
"""
return sanitize_method(method.upper().strip())
method = sanitize_method(method.upper().strip())
if method == "_OTHER":
return "HTTP"
return method


class RequestsInstrumentor(BaseInstrumentor):
Expand Down
Loading
Loading