|
| 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 | +from logging import NOTSET, WARNING, disable |
| 16 | +from unittest import main |
| 17 | + |
| 18 | +# This is used instead of from flask import Flask, request because if not then |
| 19 | +# FlaskInstrumentor().instrument() would need to be called before importing |
| 20 | +# Flask. This is just an intrinsic limitation due the fact that we are testing |
| 21 | +# the instrumentor in a way that mimics how it would be called with the |
| 22 | +# opentelemetry-auto-instrumentation command. This does not mean that the |
| 23 | +# instrumentor should be used in this way in end user applications. For those |
| 24 | +# cases, FlaskInstrumentor.instrument(app=app) should be used. |
| 25 | +import flask |
| 26 | +from werkzeug.test import Client |
| 27 | +from werkzeug.wrappers import BaseResponse |
| 28 | + |
| 29 | +from opentelemetry import trace as trace_api |
| 30 | +from opentelemetry.ext.flask import FlaskInstrumentor |
| 31 | +from opentelemetry.test.wsgitestutil import WsgiTestBase |
| 32 | + |
| 33 | +from .base_test import InstrumentationTest, expected_attributes |
| 34 | + |
| 35 | + |
| 36 | +class TestAutomatic(WsgiTestBase, InstrumentationTest): |
| 37 | + def setUp(self): |
| 38 | + super().setUp() |
| 39 | + |
| 40 | + FlaskInstrumentor().instrument() |
| 41 | + |
| 42 | + self.app = flask.Flask(__name__) |
| 43 | + |
| 44 | + def hello_endpoint(helloid): |
| 45 | + if helloid == 500: |
| 46 | + raise ValueError(":-(") |
| 47 | + return "Hello: " + str(helloid) |
| 48 | + |
| 49 | + self.app.route("/hello/<int:helloid>")(hello_endpoint) |
| 50 | + |
| 51 | + self.client = Client(self.app, BaseResponse) |
| 52 | + |
| 53 | + def tearDown(self): |
| 54 | + disable(WARNING) |
| 55 | + FlaskInstrumentor().uninstrument() |
| 56 | + disable(NOTSET) |
| 57 | + |
| 58 | + def test_uninstrument(self): |
| 59 | + expected_attrs = expected_attributes( |
| 60 | + {"http.target": "/hello/123", "http.route": "/hello/<int:helloid>"} |
| 61 | + ) |
| 62 | + resp = self.client.get("/hello/123") |
| 63 | + self.assertEqual(200, resp.status_code) |
| 64 | + self.assertEqual([b"Hello: 123"], list(resp.response)) |
| 65 | + span_list = self.memory_exporter.get_finished_spans() |
| 66 | + self.assertEqual(len(span_list), 1) |
| 67 | + self.assertEqual(span_list[0].name, "hello_endpoint") |
| 68 | + self.assertEqual(span_list[0].kind, trace_api.SpanKind.SERVER) |
| 69 | + self.assertEqual(span_list[0].attributes, expected_attrs) |
| 70 | + |
| 71 | + FlaskInstrumentor().uninstrument() |
| 72 | + |
| 73 | + expected_attrs = expected_attributes( |
| 74 | + {"http.target": "/hello/123", "http.route": "/hello/<int:helloid>"} |
| 75 | + ) |
| 76 | + resp = self.client.get("/hello/123") |
| 77 | + self.assertEqual(200, resp.status_code) |
| 78 | + self.assertEqual([b"Hello: 123"], list(resp.response)) |
| 79 | + span_list = self.memory_exporter.get_finished_spans() |
| 80 | + self.assertEqual(len(span_list), 1) |
| 81 | + self.assertEqual(span_list[0].name, "hello_endpoint") |
| 82 | + self.assertEqual(span_list[0].kind, trace_api.SpanKind.SERVER) |
| 83 | + self.assertEqual(span_list[0].attributes, expected_attrs) |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + main() |
0 commit comments