Skip to content

Commit 19f4c3c

Browse files
get a functional test
1 parent 45198d7 commit 19f4c3c

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

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

-1
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,6 @@ def _before_request():
397397

398398
activation = trace.use_span(span, end_on_exit=True)
399399
activation.__enter__() # pylint: disable=E1101
400-
breakpoint()
401400
flask_request_environ[_ENVIRON_ACTIVATION_KEY] = activation
402401
flask_request_environ[_ENVIRON_REQCTX_ID_KEY] = id(flask._request_ctx_stack.top)
403402
flask_request_environ[_ENVIRON_SPAN_KEY] = span

instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py

+18
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from werkzeug.wrappers import Response
2121

2222
from opentelemetry import context
23+
from opentelemetry import trace
2324

2425

2526
class InstrumentationTest:
@@ -37,6 +38,22 @@ def _sqlcommenter_endpoint():
3738
)
3839
return sqlcommenter_flask_values
3940

41+
@staticmethod
42+
def _copy_context_endpoint():
43+
44+
@flask.copy_current_request_context
45+
def _extract_header():
46+
return flask.request.headers['x-req']
47+
48+
# Despite `_extract_header` copying the request context,
49+
# calling it shouldn't detach the parent Flask span's contextvar
50+
request_header = _extract_header()
51+
52+
return {
53+
'span_name': trace.get_current_span().name,
54+
'request_header': request_header
55+
}
56+
4057
@staticmethod
4158
def _multithreaded_endpoint(count):
4259
def do_random_stuff():
@@ -84,6 +101,7 @@ def excluded2_endpoint():
84101
self.app.route("/hello/<int:helloid>")(self._hello_endpoint)
85102
self.app.route("/sqlcommenter")(self._sqlcommenter_endpoint)
86103
self.app.route("/multithreaded")(self._multithreaded_endpoint)
104+
self.app.route("/copy_context")(self._copy_context_endpoint)
87105
self.app.route("/excluded/<int:helloid>")(self._hello_endpoint)
88106
self.app.route("/excluded")(excluded_endpoint)
89107
self.app.route("/excluded2")(excluded2_endpoint)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright The OpenTelemetry Authors
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 flask
16+
from werkzeug.test import Client
17+
from werkzeug.wrappers import Response
18+
19+
from opentelemetry.instrumentation.flask import FlaskInstrumentor
20+
from opentelemetry.test.wsgitestutil import WsgiTestBase
21+
22+
# pylint: disable=import-error
23+
from .base_test import InstrumentationTest
24+
25+
26+
class TestCopyContext(InstrumentationTest, WsgiTestBase):
27+
def setUp(self):
28+
super().setUp()
29+
FlaskInstrumentor().instrument()
30+
self.app = flask.Flask(__name__)
31+
self._common_initialization()
32+
33+
def tearDown(self):
34+
super().tearDown()
35+
with self.disable_logging():
36+
FlaskInstrumentor().uninstrument()
37+
38+
def test_copycontext(self):
39+
"""Test that instrumentation tear down does not blow up
40+
when the request thread spawn children threads and the request
41+
context is copied to the children threads
42+
"""
43+
self.app = flask.Flask(__name__)
44+
self.app.route("/copy_context")(
45+
self._copy_context_endpoint
46+
)
47+
client = Client(self.app, Response)
48+
resp = client.get("/copy_context", headers={'x-req': 'a-header'})
49+
50+
self.assertEqual(200, resp.status_code)
51+
self.assertEqual('/copy_context', resp.json['span_name'])
52+
self.assertEqual('a-header', resp.json['request_header'])

0 commit comments

Comments
 (0)