Skip to content

Commit

Permalink
get a functional test
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewgrossman committed Mar 14, 2023
1 parent 45198d7 commit 19f4c3c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ def _before_request():

activation = trace.use_span(span, end_on_exit=True)
activation.__enter__() # pylint: disable=E1101
breakpoint()
flask_request_environ[_ENVIRON_ACTIVATION_KEY] = activation
flask_request_environ[_ENVIRON_REQCTX_ID_KEY] = id(flask._request_ctx_stack.top)
flask_request_environ[_ENVIRON_SPAN_KEY] = span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from werkzeug.wrappers import Response

from opentelemetry import context
from opentelemetry import trace


class InstrumentationTest:
Expand All @@ -37,6 +38,22 @@ def _sqlcommenter_endpoint():
)
return sqlcommenter_flask_values

@staticmethod
def _copy_context_endpoint():

@flask.copy_current_request_context
def _extract_header():
return flask.request.headers['x-req']

# Despite `_extract_header` copying the request context,
# calling it shouldn't detach the parent Flask span's contextvar
request_header = _extract_header()

return {
'span_name': trace.get_current_span().name,
'request_header': request_header
}

@staticmethod
def _multithreaded_endpoint(count):
def do_random_stuff():
Expand Down Expand Up @@ -84,6 +101,7 @@ def excluded2_endpoint():
self.app.route("/hello/<int:helloid>")(self._hello_endpoint)
self.app.route("/sqlcommenter")(self._sqlcommenter_endpoint)
self.app.route("/multithreaded")(self._multithreaded_endpoint)
self.app.route("/copy_context")(self._copy_context_endpoint)
self.app.route("/excluded/<int:helloid>")(self._hello_endpoint)
self.app.route("/excluded")(excluded_endpoint)
self.app.route("/excluded2")(excluded2_endpoint)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 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 TestCopyContext(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_copycontext(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("/copy_context")(
self._copy_context_endpoint
)
client = Client(self.app, Response)
resp = client.get("/copy_context", headers={'x-req': 'a-header'})

self.assertEqual(200, resp.status_code)
self.assertEqual('/copy_context', resp.json['span_name'])
self.assertEqual('a-header', resp.json['request_header'])

0 comments on commit 19f4c3c

Please sign in to comment.