Skip to content

Commit 7316bcf

Browse files
committed
Update to include excluded paths
1 parent cda10f4 commit 7316bcf

File tree

5 files changed

+73
-23
lines changed

5 files changed

+73
-23
lines changed

docs/examples/opentelemetry-example-app/src/opentelemetry_example_app/flask_example.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
# It must be done before instrumenting any library
3434
trace.set_tracer_provider(TracerProvider())
3535

36-
opentelemetry.ext.http_requests.RequestsInstrumentor().instrument()
36+
opentelemetry.ext.requests.RequestsInstrumentor().instrument()
3737

3838
trace.get_tracer_provider().add_span_processor(
3939
SimpleExportSpanProcessor(ConsoleSpanExporter())

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

+19-12
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,21 @@ def _wrapped_app(environ, start_response):
7878
environ[_ENVIRON_STARTTIME_KEY] = time_ns()
7979

8080
def _start_response(status, response_headers, *args, **kwargs):
81-
span = flask.request.environ.get(_ENVIRON_SPAN_KEY)
82-
83-
if span:
84-
otel_wsgi.add_response_attributes(
85-
span, status, response_headers
86-
)
87-
else:
88-
_logger.warning(
89-
"Flask environ's OpenTelemetry span "
90-
"missing at _start_response(%s)",
91-
status,
92-
)
81+
82+
if not _disable_trace(flask.request.url):
83+
84+
span = flask.request.environ.get(_ENVIRON_SPAN_KEY)
85+
86+
if span:
87+
otel_wsgi.add_response_attributes(
88+
span, status, response_headers
89+
)
90+
else:
91+
_logger.warning(
92+
"Flask environ's OpenTelemetry span "
93+
"missing at _start_response(%s)",
94+
status,
95+
)
9396

9497
return start_response(status, response_headers, *args, **kwargs)
9598

@@ -99,6 +102,9 @@ def _start_response(status, response_headers, *args, **kwargs):
99102

100103

101104
def _before_request():
105+
if _disable_trace(flask.request.url):
106+
return
107+
102108
environ = flask.request.environ
103109
span_name = flask.request.endpoint or otel_wsgi.get_default_span_name(
104110
environ
@@ -160,6 +166,7 @@ def __init__(self, *args, **kwargs):
160166
def _disable_trace(url):
161167
excluded_hosts = configuration.Configuration().FLASK_EXCLUDED_HOSTS
162168
excluded_paths = configuration.Configuration().FLASK_EXCLUDED_PATHS
169+
163170
if excluded_hosts:
164171
excluded_hosts = str.split(excluded_hosts, ",")
165172
if disable_tracing_hostname(url, excluded_hosts):

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

+24-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from unittest.mock import patch
16+
1517
from flask import request
1618

17-
from opentelemetry import trace as trace_api
19+
from opentelemetry import trace
1820

1921

2022
def expected_attributes(override_attributes):
@@ -36,6 +38,7 @@ def expected_attributes(override_attributes):
3638

3739

3840
class InstrumentationTest:
41+
3942
# pylint: disable=no-member
4043
def test_only_strings_in_environ(self):
4144
"""
@@ -65,7 +68,7 @@ def test_simple_uninstrument(self):
6568
span_list = self.memory_exporter.get_finished_spans()
6669
self.assertEqual(len(span_list), 1)
6770
self.assertEqual(span_list[0].name, "hello_endpoint")
68-
self.assertEqual(span_list[0].kind, trace_api.SpanKind.SERVER)
71+
self.assertEqual(span_list[0].kind, trace.SpanKind.SERVER)
6972
self.assertEqual(span_list[0].attributes, expected_attrs)
7073

7174
def test_404(self):
@@ -84,7 +87,7 @@ def test_404(self):
8487
span_list = self.memory_exporter.get_finished_spans()
8588
self.assertEqual(len(span_list), 1)
8689
self.assertEqual(span_list[0].name, "/bye")
87-
self.assertEqual(span_list[0].kind, trace_api.SpanKind.SERVER)
90+
self.assertEqual(span_list[0].kind, trace.SpanKind.SERVER)
8891
self.assertEqual(span_list[0].attributes, expected_attrs)
8992

9093
def test_internal_error(self):
@@ -102,5 +105,22 @@ def test_internal_error(self):
102105
span_list = self.memory_exporter.get_finished_spans()
103106
self.assertEqual(len(span_list), 1)
104107
self.assertEqual(span_list[0].name, "hello_endpoint")
105-
self.assertEqual(span_list[0].kind, trace_api.SpanKind.SERVER)
108+
self.assertEqual(span_list[0].kind, trace.SpanKind.SERVER)
106109
self.assertEqual(span_list[0].attributes, expected_attrs)
110+
111+
@patch.dict(
112+
"os.environ", # type: ignore
113+
{
114+
"OPENTELEMETRY_PYTHON_FLASK_EXCLUDED_HOSTS": (
115+
"http://localhost/excluded"
116+
),
117+
"OPENTELEMETRY_PYTHON_FLASK_EXCLUDED_PATHS": "excluded2",
118+
},
119+
)
120+
def test_excluded_path(self):
121+
self.client.get("/hello/123")
122+
self.client.get("/excluded")
123+
self.client.get("/excluded2")
124+
span_list = self.memory_exporter.get_finished_spans()
125+
self.assertEqual(len(span_list), 1)
126+
self.assertEqual(span_list[0].name, "hello_endpoint")

ext/opentelemetry-ext-flask/tests/test_automatic.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
from werkzeug.test import Client
1919
from werkzeug.wrappers import BaseResponse
2020

21-
from opentelemetry import trace as trace_api
21+
from opentelemetry import trace
22+
from opentelemetry.configuration import Configuration
2223
from opentelemetry.ext.flask import FlaskInstrumentor
2324
from opentelemetry.test.wsgitestutil import WsgiTestBase
2425

@@ -30,6 +31,8 @@ class TestAutomatic(WsgiTestBase, InstrumentationTest):
3031
def setUp(self):
3132
super().setUp()
3233

34+
Configuration._instance = None # pylint: disable=protected-access
35+
Configuration.__slots__ = [] # pylint: disable=protected-access
3336
FlaskInstrumentor().instrument()
3437

3538
self.app = flask.Flask(__name__)
@@ -39,7 +42,16 @@ def hello_endpoint(helloid):
3942
raise ValueError(":-(")
4043
return "Hello: " + str(helloid)
4144

45+
def excluded_endpoint():
46+
return "excluded"
47+
48+
def excluded2_endpoint():
49+
return "excluded2"
50+
4251
self.app.route("/hello/<int:helloid>")(hello_endpoint)
52+
self.app.route("/excluded/<int:helloid>")(hello_endpoint)
53+
self.app.route("/excluded")(excluded_endpoint)
54+
self.app.route("/excluded2")(excluded2_endpoint)
4355

4456
self.client = Client(self.app, BaseResponse)
4557

@@ -58,7 +70,7 @@ def test_uninstrument(self):
5870
span_list = self.memory_exporter.get_finished_spans()
5971
self.assertEqual(len(span_list), 1)
6072
self.assertEqual(span_list[0].name, "hello_endpoint")
61-
self.assertEqual(span_list[0].kind, trace_api.SpanKind.SERVER)
73+
self.assertEqual(span_list[0].kind, trace.SpanKind.SERVER)
6274
self.assertEqual(span_list[0].attributes, expected_attrs)
6375

6476
FlaskInstrumentor().uninstrument()
@@ -82,5 +94,5 @@ def hello_endpoint(helloid):
8294
span_list = self.memory_exporter.get_finished_spans()
8395
self.assertEqual(len(span_list), 1)
8496
self.assertEqual(span_list[0].name, "hello_endpoint")
85-
self.assertEqual(span_list[0].kind, trace_api.SpanKind.SERVER)
97+
self.assertEqual(span_list[0].kind, trace.SpanKind.SERVER)
8698
self.assertEqual(span_list[0].attributes, expected_attrs)

ext/opentelemetry-ext-flask/tests/test_programmatic.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
from werkzeug.test import Client
1919
from werkzeug.wrappers import BaseResponse
2020

21-
from opentelemetry import trace as trace_api
21+
from opentelemetry import trace
22+
from opentelemetry.configuration import Configuration
2223
from opentelemetry.ext.flask import FlaskInstrumentor
2324
from opentelemetry.test.wsgitestutil import WsgiTestBase
2425

@@ -30,6 +31,8 @@ class TestProgrammatic(WsgiTestBase, InstrumentationTest):
3031
def setUp(self):
3132
super().setUp()
3233

34+
Configuration._instance = None # pylint: disable=protected-access
35+
Configuration.__slots__ = [] # pylint: disable=protected-access
3336
self.app = Flask(__name__)
3437

3538
FlaskInstrumentor().instrument_app(self.app)
@@ -39,7 +42,15 @@ def hello_endpoint(helloid):
3942
raise ValueError(":-(")
4043
return "Hello: " + str(helloid)
4144

45+
def excluded_endpoint():
46+
return "excluded"
47+
48+
def excluded2_endpoint():
49+
return "excluded2"
50+
4251
self.app.route("/hello/<int:helloid>")(hello_endpoint)
52+
self.app.route("/excluded")(excluded_endpoint)
53+
self.app.route("/excluded2")(excluded2_endpoint)
4354

4455
self.client = Client(self.app, BaseResponse)
4556

@@ -58,7 +69,7 @@ def test_uninstrument(self):
5869
span_list = self.memory_exporter.get_finished_spans()
5970
self.assertEqual(len(span_list), 1)
6071
self.assertEqual(span_list[0].name, "hello_endpoint")
61-
self.assertEqual(span_list[0].kind, trace_api.SpanKind.SERVER)
72+
self.assertEqual(span_list[0].kind, trace.SpanKind.SERVER)
6273
self.assertEqual(span_list[0].attributes, expected_attrs)
6374

6475
FlaskInstrumentor().uninstrument_app(self.app)
@@ -72,5 +83,5 @@ def test_uninstrument(self):
7283
span_list = self.memory_exporter.get_finished_spans()
7384
self.assertEqual(len(span_list), 1)
7485
self.assertEqual(span_list[0].name, "hello_endpoint")
75-
self.assertEqual(span_list[0].kind, trace_api.SpanKind.SERVER)
86+
self.assertEqual(span_list[0].kind, trace.SpanKind.SERVER)
7687
self.assertEqual(span_list[0].attributes, expected_attrs)

0 commit comments

Comments
 (0)