|
| 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