Skip to content

Commit d516792

Browse files
authored
Merge branch 'main' into add-genshi-testing
2 parents 5cbcd3a + be4fb3d commit d516792

File tree

6 files changed

+250
-13
lines changed

6 files changed

+250
-13
lines changed

newrelic/hooks/adapter_waitress.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,16 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import newrelic.api.wsgi_application
16-
import newrelic.api.in_function
15+
from newrelic.api.in_function import wrap_in_function
16+
from newrelic.api.wsgi_application import WSGIApplicationWrapper
17+
from newrelic.common.package_version_utils import get_package_version
1718

18-
def instrument_waitress_server(module):
1919

20-
def wrap_wsgi_application_entry_point(server, application,
21-
*args, **kwargs):
22-
application = newrelic.api.wsgi_application.WSGIApplicationWrapper(
23-
application)
20+
def instrument_waitress_server(module):
21+
def wrap_wsgi_application_entry_point(server, application, *args, **kwargs):
22+
dispatcher_details = ("Waitress", get_package_version("waitress"))
23+
application = WSGIApplicationWrapper(application, dispatcher=dispatcher_details)
2424
args = [server, application] + list(args)
2525
return (args, kwargs)
2626

27-
newrelic.api.in_function.wrap_in_function(module,
28-
'WSGIServer.__init__', wrap_wsgi_application_entry_point)
27+
wrap_in_function(module, "WSGIServer.__init__", wrap_wsgi_application_entry_point)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2010 New Relic, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from threading import Thread
16+
from time import sleep
17+
18+
from testing_support.sample_applications import (
19+
raise_exception_application,
20+
raise_exception_finalize,
21+
raise_exception_response,
22+
simple_app_raw,
23+
)
24+
from testing_support.util import get_open_port
25+
26+
27+
def sample_application(environ, start_response):
28+
path_info = environ.get("PATH_INFO")
29+
30+
if path_info.startswith("/raise-exception-application"):
31+
return raise_exception_application(environ, start_response)
32+
elif path_info.startswith("/raise-exception-response"):
33+
return raise_exception_response(environ, start_response)
34+
elif path_info.startswith("/raise-exception-finalize"):
35+
return raise_exception_finalize(environ, start_response)
36+
37+
return simple_app_raw(environ, start_response)
38+
39+
40+
def setup_application():
41+
port = get_open_port()
42+
43+
def run_wsgi():
44+
from waitress import serve
45+
46+
serve(sample_application, host="127.0.0.1", port=port)
47+
48+
wsgi_thread = Thread(target=run_wsgi)
49+
wsgi_thread.daemon = True
50+
wsgi_thread.start()
51+
52+
sleep(1)
53+
54+
return port

tests/adapter_waitress/conftest.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2010 New Relic, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pytest
16+
import webtest
17+
from testing_support.fixtures import ( # noqa: F401; pylint: disable=W0611
18+
collector_agent_registration_fixture,
19+
collector_available_fixture,
20+
)
21+
22+
_default_settings = {
23+
"transaction_tracer.explain_threshold": 0.0,
24+
"transaction_tracer.transaction_threshold": 0.0,
25+
"transaction_tracer.stack_trace_threshold": 0.0,
26+
"debug.log_data_collector_payloads": True,
27+
"debug.record_transaction_failure": True,
28+
}
29+
30+
collector_agent_registration = collector_agent_registration_fixture(
31+
app_name="Python Agent Test (Waitress)", default_settings=_default_settings
32+
)
33+
34+
35+
@pytest.fixture(autouse=True, scope="session")
36+
def target_application():
37+
import _application
38+
39+
port = _application.setup_application()
40+
return webtest.TestApp("http://localhost:%d" % port)

tests/adapter_waitress/test_wsgi.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Copyright 2010 New Relic, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
from testing_support.fixtures import (
17+
override_application_settings,
18+
raise_background_exceptions,
19+
wait_for_background_threads,
20+
)
21+
from testing_support.validators.validate_transaction_errors import (
22+
validate_transaction_errors,
23+
)
24+
from testing_support.validators.validate_transaction_metrics import (
25+
validate_transaction_metrics,
26+
)
27+
28+
from newrelic.common.package_version_utils import get_package_version
29+
30+
WAITRESS_VERSION = get_package_version("waitress")
31+
32+
33+
@override_application_settings({"transaction_name.naming_scheme": "framework"})
34+
def test_wsgi_application_index(target_application):
35+
@validate_transaction_metrics(
36+
"_application:sample_application",
37+
custom_metrics=[
38+
("Python/Dispatcher/Waitress/%s" % WAITRESS_VERSION, 1),
39+
],
40+
)
41+
@raise_background_exceptions()
42+
@wait_for_background_threads()
43+
def _test():
44+
response = target_application.get("/")
45+
assert response.status == "200 OK"
46+
47+
_test()
48+
49+
50+
@override_application_settings({"transaction_name.naming_scheme": "framework"})
51+
def test_raise_exception_application(target_application):
52+
@validate_transaction_errors(["builtins:RuntimeError"])
53+
@validate_transaction_metrics(
54+
"_application:sample_application",
55+
custom_metrics=[
56+
("Python/Dispatcher/Waitress/%s" % WAITRESS_VERSION, 1),
57+
],
58+
)
59+
@raise_background_exceptions()
60+
@wait_for_background_threads()
61+
def _test():
62+
response = target_application.get("/raise-exception-application/", status=500)
63+
assert response.status == "500 Internal Server Error"
64+
65+
_test()
66+
67+
68+
@override_application_settings({"transaction_name.naming_scheme": "framework"})
69+
def test_raise_exception_response(target_application):
70+
@validate_transaction_errors(["builtins:RuntimeError"])
71+
@validate_transaction_metrics(
72+
"_application:sample_application",
73+
custom_metrics=[
74+
("Python/Dispatcher/Waitress/%s" % WAITRESS_VERSION, 1),
75+
],
76+
)
77+
@raise_background_exceptions()
78+
@wait_for_background_threads()
79+
def _test():
80+
response = target_application.get("/raise-exception-response/", status=500)
81+
assert response.status == "500 Internal Server Error"
82+
83+
_test()
84+
85+
86+
@override_application_settings({"transaction_name.naming_scheme": "framework"})
87+
def test_raise_exception_finalize(target_application):
88+
@validate_transaction_errors(["builtins:RuntimeError"])
89+
@validate_transaction_metrics(
90+
"_application:sample_application",
91+
custom_metrics=[
92+
("Python/Dispatcher/Waitress/%s" % WAITRESS_VERSION, 1),
93+
],
94+
)
95+
@raise_background_exceptions()
96+
@wait_for_background_threads()
97+
def _test():
98+
response = target_application.get("/raise-exception-finalize/", status=500)
99+
assert response.status == "500 Internal Server Error"
100+
101+
_test()

tests/testing_support/sample_applications.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ def fully_featured_app(environ, start_response):
7979
environ["wsgi.input"].readlines()
8080

8181
if use_user_attrs:
82-
8382
for attr, val in _custom_parameters.items():
8483
add_custom_attribute(attr, val)
8584

@@ -97,7 +96,6 @@ def fully_featured_app(environ, start_response):
9796
n_errors = int(environ.get("n_errors", 1))
9897
for i in range(n_errors):
9998
try:
100-
10199
# append number to stats engine to get unique errors, so they
102100
# don't immediately get filtered out.
103101

@@ -122,7 +120,6 @@ def fully_featured_app(environ, start_response):
122120

123121
@wsgi_application()
124122
def simple_exceptional_app(environ, start_response):
125-
126123
start_response("500 :(", [])
127124

128125
raise ValueError("Transaction had bad value")
@@ -140,9 +137,47 @@ def simple_app_raw(environ, start_response):
140137
simple_app = wsgi_application()(simple_app_raw)
141138

142139

140+
def raise_exception_application(environ, start_response):
141+
raise RuntimeError("raise_exception_application")
142+
143+
status = "200 OK"
144+
output = b"WSGI RESPONSE"
145+
146+
response_headers = [("Content-type", "text/plain"), ("Content-Length", str(len(output)))]
147+
start_response(status, response_headers)
148+
149+
return [output]
150+
151+
152+
def raise_exception_response(environ, start_response):
153+
status = "200 OK"
154+
155+
response_headers = [("Content-type", "text/plain")]
156+
start_response(status, response_headers)
157+
158+
yield b"WSGI"
159+
160+
raise RuntimeError("raise_exception_response")
161+
162+
yield b" "
163+
yield b"RESPONSE"
164+
165+
166+
def raise_exception_finalize(environ, start_response):
167+
status = "200 OK"
168+
169+
response_headers = [("Content-type", "text/plain")]
170+
start_response(status, response_headers)
171+
172+
try:
173+
yield b"WSGI RESPONSE"
174+
175+
finally:
176+
raise RuntimeError("raise_exception_finalize")
177+
178+
143179
@wsgi_application()
144180
def simple_custom_event_app(environ, start_response):
145-
146181
params = {"snowman": "\u2603", "foo": "bar"}
147182
record_custom_event("SimpleAppEvent", params)
148183

tox.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ envlist =
5151
python-adapter_hypercorn-py38-hypercorn{0010,0011,0012,0013},
5252
python-adapter_uvicorn-py37-uvicorn03,
5353
python-adapter_uvicorn-{py37,py38,py39,py310,py311}-uvicornlatest,
54+
python-adapter_waitress-{py37,py38,py39}-waitress010404,
55+
python-adapter_waitress-{py37,py38,py39,py310}-waitress02,
56+
python-adapter_waitress-{py37,py38,py39,py310,py311}-waitresslatest,
5457
python-agent_features-{py27,py37,py38,py39,py310,py311}-{with,without}_extensions,
5558
python-agent_features-{pypy,pypy37}-without_extensions,
5659
python-agent_streaming-py27-grpc0125-{with,without}_extensions,
@@ -193,6 +196,10 @@ deps =
193196
adapter_uvicorn-uvicorn03: uvicorn<0.4
194197
adapter_uvicorn-uvicorn014: uvicorn<0.15
195198
adapter_uvicorn-uvicornlatest: uvicorn
199+
adapter_waitress: WSGIProxy2
200+
adapter_waitress-waitress010404: waitress<1.4.5
201+
adapter_waitress-waitress02: waitress<2.1
202+
adapter_waitress-waitresslatest: waitress
196203
agent_features: beautifulsoup4
197204
application_celery: celery<6.0
198205
application_celery-py{py37,37}: importlib-metadata<5.0
@@ -420,6 +427,7 @@ changedir =
420427
adapter_gunicorn: tests/adapter_gunicorn
421428
adapter_hypercorn: tests/adapter_hypercorn
422429
adapter_uvicorn: tests/adapter_uvicorn
430+
adapter_waitress: tests/adapter_waitress
423431
agent_features: tests/agent_features
424432
agent_streaming: tests/agent_streaming
425433
agent_unittests: tests/agent_unittests

0 commit comments

Comments
 (0)