22
22
from opentelemetry .sdk .resources import Resource
23
23
from opentelemetry .semconv .trace import SpanAttributes
24
24
from opentelemetry .test .test_base import TestBase
25
- from opentelemetry .util .http import get_excluded_urls
25
+ from opentelemetry .util .http import get_excluded_urls , ExcludeList
26
26
27
27
28
28
class TestFastAPIManualInstrumentation (TestBase ):
@@ -31,6 +31,17 @@ def _create_app(self):
31
31
self ._instrumentor .instrument_app (app )
32
32
return app
33
33
34
+ def _create_app_explicit_excluded_urls (self ):
35
+ app = self ._create_fastapi_app ()
36
+ to_exclude = "/user/123,/foobar"
37
+ excluded_urls = [
38
+ excluded_url .strip () for excluded_url in to_exclude .split ("," )
39
+ ]
40
+ self ._instrumentor .instrument_app (
41
+ app , excluded_urls = ExcludeList (excluded_urls )
42
+ )
43
+ return app
44
+
34
45
def setUp (self ):
35
46
super ().setUp ()
36
47
self .env_patch = patch .dict (
@@ -84,6 +95,17 @@ def test_fastapi_excluded_urls(self):
84
95
spans = self .memory_exporter .get_finished_spans ()
85
96
self .assertEqual (len (spans ), 0 )
86
97
98
+ def test_fastapi_excluded_urls_not_env (self ):
99
+ """Ensure that given fastapi routes are excluded when passed explicitly (not in the environment)"""
100
+ app = self ._create_app_explicit_excluded_urls ()
101
+ client = TestClient (app )
102
+ client .get ("/user/123" )
103
+ spans = self .memory_exporter .get_finished_spans ()
104
+ self .assertEqual (len (spans ), 0 )
105
+ client .get ("/foobar" )
106
+ spans = self .memory_exporter .get_finished_spans ()
107
+ self .assertEqual (len (spans ), 0 )
108
+
87
109
@staticmethod
88
110
def _create_fastapi_app ():
89
111
app = fastapi .FastAPI ()
@@ -124,6 +146,24 @@ def _create_app(self):
124
146
self ._instrumentor .instrument (tracer_provider = tracer_provider )
125
147
return self ._create_fastapi_app ()
126
148
149
+ def _create_app_explicit_excluded_urls (self ):
150
+ resource = Resource .create ({"key1" : "value1" , "key2" : "value2" })
151
+ tracer_provider , exporter = self .create_tracer_provider (
152
+ resource = resource
153
+ )
154
+ self .memory_exporter = exporter
155
+
156
+ to_exclude = "/user/123,/foobar"
157
+ excluded_urls = [
158
+ excluded_url .strip () for excluded_url in to_exclude .split ("," )
159
+ ]
160
+ self ._instrumentor .uninstrument () # Disable previous instrumentation (setUp)
161
+ self ._instrumentor .instrument (
162
+ tracer_provider = tracer_provider ,
163
+ excluded_urls = ExcludeList (excluded_urls ),
164
+ )
165
+ return self ._create_fastapi_app ()
166
+
127
167
def test_request (self ):
128
168
self ._client .get ("/foobar" )
129
169
spans = self .memory_exporter .get_finished_spans ()
0 commit comments