diff --git a/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py b/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py index 1da8faa7fd..48bc478b80 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py +++ b/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py @@ -12,6 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. +from concurrent.futures import ThreadPoolExecutor, as_completed +from random import randint + import flask from werkzeug.test import Client from werkzeug.wrappers import Response @@ -34,6 +37,26 @@ def _sqlcommenter_endpoint(): ) return sqlcommenter_flask_values + + + @staticmethod + def _multithreaded_endpoint(count): + def do_random_stuff(): + @flask.copy_current_request_context + def inner(): + return randint(0, 100) + return inner + + executor = ThreadPoolExecutor(count) + futures = [] + for _ in range(count): + futures.append(executor.submit(do_random_stuff())) + numbers = [] + for future in as_completed(futures): + numbers.append(future.result()) + + return ' '.join([str(i) for i in numbers]) + @staticmethod def _custom_response_headers(): resp = flask.Response("test response") @@ -61,6 +84,7 @@ def excluded2_endpoint(): # pylint: disable=no-member self.app.route("/hello/")(self._hello_endpoint) self.app.route("/sqlcommenter")(self._sqlcommenter_endpoint) + self.app.route("/multithreaded")(self._multithreaded_endpoint) self.app.route("/excluded/")(self._hello_endpoint) self.app.route("/excluded")(excluded_endpoint) self.app.route("/excluded2")(excluded2_endpoint) diff --git a/instrumentation/opentelemetry-instrumentation-flask/tests/test_multithreading.py b/instrumentation/opentelemetry-instrumentation-flask/tests/test_multithreading.py new file mode 100644 index 0000000000..4c1f993e57 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-flask/tests/test_multithreading.py @@ -0,0 +1,50 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import flask +from werkzeug.test import Client +from werkzeug.wrappers import Response + +from opentelemetry.instrumentation.flask import FlaskInstrumentor +from opentelemetry.test.wsgitestutil import WsgiTestBase + +# pylint: disable=import-error +from .base_test import InstrumentationTest + + +class TestMultiThreading(InstrumentationTest, WsgiTestBase): + def setUp(self): + super().setUp() + FlaskInstrumentor().instrument() + self.app = flask.Flask(__name__) + self._common_initialization() + + def tearDown(self): + super().tearDown() + with self.disable_logging(): + FlaskInstrumentor().uninstrument() + + def test_multithreaded(self): + """Test that instrumentation tear down does not blow up + when the request thread spawn children threads and the request + context is copied to the children threads + """ + self.app = flask.Flask(__name__) + self.app.route("/multithreaded/")(self._multithreaded_endpoint) + client = Client(self.app, Response) + count = 5 + resp = client.get(f"/multithreaded/{count}") + self.assertEqual(200, resp.status_code) + # Should return the specified number of random integers + self.assertEqual(count, len(resp.text.split(' '))) \ No newline at end of file diff --git a/scripts/otel_packaging.py b/scripts/otel_packaging.py index c7765c10b7..2f42e44189 100644 --- a/scripts/otel_packaging.py +++ b/scripts/otel_packaging.py @@ -14,6 +14,7 @@ import os import subprocess +from subprocess import CalledProcessError import tomli @@ -28,12 +29,18 @@ def get_instrumentation_packages(): if not os.path.isdir(pkg_path): continue - version = subprocess.check_output( - "hatch version", - shell=True, - cwd=pkg_path, - universal_newlines=True, - ) + try: + version = subprocess.check_output( + "hatch version", + shell=True, + cwd=pkg_path, + universal_newlines=True, + ) + except CalledProcessError as exc: + print(f"Could not get hatch version from path {pkg_path}") + print(exc.output) + raise exc + pyproject_toml_path = os.path.join(pkg_path, "pyproject.toml") with open(pyproject_toml_path, "rb") as file: