Skip to content

Commit 65fb21c

Browse files
authored
Falcon: handle env vars locally vs globally (open-telemetry#650)
All instrumentations read most env vars today at module level. This has a few disadvantages. - harder to test. - code executed on import even if instrumentation is not used. - forces to use global vars. This commit fixes the Falcon instrumentation to handle env vars locally during instrumentor initialization and considerably simplifies testing. Other instrumentations should receive similar treatment.
1 parent c4639ee commit 65fb21c

File tree

2 files changed

+11
-26
lines changed

2 files changed

+11
-26
lines changed

instrumentation/opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon/__init__.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ def response_hook(span, req, resp):
124124
_ENVIRON_EXC = "opentelemetry-falcon.exc"
125125

126126

127-
_excluded_urls = get_excluded_urls("FALCON")
128-
_traced_request_attrs = get_traced_request_attrs("FALCON")
129127
_response_propagation_setter = FuncSetter(falcon.Response.append_header)
130128

131129

@@ -159,17 +157,21 @@ def __init__(self, *args, **kwargs):
159157

160158
trace_middleware = _TraceMiddleware(
161159
self._tracer,
162-
kwargs.pop("traced_request_attributes", _traced_request_attrs),
160+
kwargs.pop(
161+
"traced_request_attributes", get_traced_request_attrs("FALCON")
162+
),
163163
kwargs.pop("request_hook", None),
164164
kwargs.pop("response_hook", None),
165165
)
166166
middlewares.insert(0, trace_middleware)
167167
kwargs["middleware"] = middlewares
168+
169+
self._excluded_urls = get_excluded_urls("FALCON")
168170
super().__init__(*args, **kwargs)
169171

170172
def __call__(self, env, start_response):
171173
# pylint: disable=E1101
172-
if _excluded_urls.url_disabled(env.get("PATH_INFO", "/")):
174+
if self._excluded_urls.url_disabled(env.get("PATH_INFO", "/")):
173175
return super().__call__(env, start_response)
174176

175177
start_time = _time_ns()

instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py

+5-22
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,13 @@
2626
from opentelemetry.semconv.trace import SpanAttributes
2727
from opentelemetry.test.test_base import TestBase
2828
from opentelemetry.trace import StatusCode, format_span_id, format_trace_id
29-
from opentelemetry.util.http import get_excluded_urls, get_traced_request_attrs
3029

3130
from .app import make_app
3231

3332

3433
class TestFalconBase(TestBase):
3534
def setUp(self):
3635
super().setUp()
37-
FalconInstrumentor().instrument(
38-
request_hook=getattr(self, "request_hook", None),
39-
response_hook=getattr(self, "response_hook", None),
40-
)
41-
self.app = make_app()
42-
# pylint: disable=protected-access
4336
self.env_patch = patch.dict(
4437
"os.environ",
4538
{
@@ -48,20 +41,12 @@ def setUp(self):
4841
},
4942
)
5043
self.env_patch.start()
51-
self.exclude_patch = patch(
52-
"opentelemetry.instrumentation.falcon._excluded_urls",
53-
get_excluded_urls("FALCON"),
54-
)
55-
middleware = self.app._middleware[0][ # pylint:disable=W0212
56-
0
57-
].__self__
58-
self.traced_patch = patch.object(
59-
middleware,
60-
"_traced_request_attrs",
61-
get_traced_request_attrs("FALCON"),
44+
45+
FalconInstrumentor().instrument(
46+
request_hook=getattr(self, "request_hook", None),
47+
response_hook=getattr(self, "response_hook", None),
6248
)
63-
self.exclude_patch.start()
64-
self.traced_patch.start()
49+
self.app = make_app()
6550

6651
def client(self):
6752
return testing.TestClient(self.app)
@@ -71,8 +56,6 @@ def tearDown(self):
7156
with self.disable_logging():
7257
FalconInstrumentor().uninstrument()
7358
self.env_patch.stop()
74-
self.exclude_patch.stop()
75-
self.traced_patch.stop()
7659

7760

7861
class TestFalconInstrumentation(TestFalconBase):

0 commit comments

Comments
 (0)