@@ -90,7 +90,6 @@ def response_hook(span, req, resp):
90
90
---
91
91
"""
92
92
93
- from functools import partial
94
93
from logging import getLogger
95
94
from sys import exc_info
96
95
from typing import Collection
@@ -135,48 +134,32 @@ def response_hook(span, req, resp):
135
134
_instrument_app = "API"
136
135
137
136
138
- class FalconInstrumentor (BaseInstrumentor ):
139
- # pylint: disable=protected-access,attribute-defined-outside-init
140
- """An instrumentor for falcon.API
141
-
142
- See `BaseInstrumentor`
143
- """
144
-
145
- def instrumentation_dependencies (self ) -> Collection [str ]:
146
- return _instruments
147
-
148
- def _instrument (self , ** kwargs ):
149
- self ._original_falcon_api = getattr (falcon , _instrument_app )
150
- setattr (
151
- falcon , _instrument_app , partial (_InstrumentedFalconAPI , ** kwargs )
152
- )
153
-
154
- def _uninstrument (self , ** kwargs ):
155
- setattr (falcon , _instrument_app , self ._original_falcon_api )
156
-
157
-
158
137
class _InstrumentedFalconAPI (getattr (falcon , _instrument_app )):
159
138
def __init__ (self , * args , ** kwargs ):
139
+ otel_opts = kwargs .pop ("_otel_opts" , {})
140
+
160
141
# inject trace middleware
161
142
middlewares = kwargs .pop ("middleware" , [])
162
- tracer_provider = kwargs .pop ("tracer_provider" , None )
143
+ tracer_provider = otel_opts .pop ("tracer_provider" , None )
163
144
if not isinstance (middlewares , (list , tuple )):
164
145
middlewares = [middlewares ]
165
146
166
- self ._tracer = trace .get_tracer (__name__ , __version__ , tracer_provider )
147
+ self ._otel_tracer = trace .get_tracer (
148
+ __name__ , __version__ , tracer_provider
149
+ )
167
150
168
151
trace_middleware = _TraceMiddleware (
169
- self ._tracer ,
170
- kwargs .pop (
152
+ self ._otel_tracer ,
153
+ otel_opts .pop (
171
154
"traced_request_attributes" , get_traced_request_attrs ("FALCON" )
172
155
),
173
- kwargs .pop ("request_hook" , None ),
174
- kwargs .pop ("response_hook" , None ),
156
+ otel_opts .pop ("request_hook" , None ),
157
+ otel_opts .pop ("response_hook" , None ),
175
158
)
176
159
middlewares .insert (0 , trace_middleware )
177
160
kwargs ["middleware" ] = middlewares
178
161
179
- self ._excluded_urls = get_excluded_urls ("FALCON" )
162
+ self ._otel_excluded_urls = get_excluded_urls ("FALCON" )
180
163
super ().__init__ (* args , ** kwargs )
181
164
182
165
def _handle_exception (
@@ -190,13 +173,13 @@ def _handle_exception(
190
173
191
174
def __call__ (self , env , start_response ):
192
175
# pylint: disable=E1101
193
- if self ._excluded_urls .url_disabled (env .get ("PATH_INFO" , "/" )):
176
+ if self ._otel_excluded_urls .url_disabled (env .get ("PATH_INFO" , "/" )):
194
177
return super ().__call__ (env , start_response )
195
178
196
179
start_time = _time_ns ()
197
180
198
181
span , token = _start_internal_or_server_span (
199
- tracer = self ._tracer ,
182
+ tracer = self ._otel_tracer ,
200
183
span_name = otel_wsgi .get_default_span_name (env ),
201
184
start_time = start_time ,
202
185
context_carrier = env ,
@@ -321,3 +304,27 @@ def process_response(
321
304
322
305
if self ._response_hook :
323
306
self ._response_hook (span , req , resp )
307
+
308
+
309
+ class FalconInstrumentor (BaseInstrumentor ):
310
+ # pylint: disable=protected-access,attribute-defined-outside-init
311
+ """An instrumentor for falcon.API
312
+
313
+ See `BaseInstrumentor`
314
+ """
315
+
316
+ def instrumentation_dependencies (self ) -> Collection [str ]:
317
+ return _instruments
318
+
319
+ def _instrument (self , ** opts ):
320
+ self ._original_falcon_api = getattr (falcon , _instrument_app )
321
+
322
+ class FalconAPI (_InstrumentedFalconAPI ):
323
+ def __init__ (self , * args , ** kwargs ):
324
+ kwargs ["_otel_opts" ] = opts
325
+ super ().__init__ (* args , ** kwargs )
326
+
327
+ setattr (falcon , _instrument_app , FalconAPI )
328
+
329
+ def _uninstrument (self , ** kwargs ):
330
+ setattr (falcon , _instrument_app , self ._original_falcon_api )
0 commit comments