Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 49b27ef

Browse files
olancereyang
authored andcommitted
Fixes value for http.route in Flask middleware (#759)
1 parent 577fb61 commit 49b27ef

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

contrib/opencensus-ext-flask/opencensus/ext/flask/flask_middleware.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,6 @@ def _before_request(self):
155155
tracer.add_attribute_to_current_span(
156156
HTTP_PATH, flask.request.path
157157
)
158-
tracer.add_attribute_to_current_span(
159-
HTTP_ROUTE, flask.request.path
160-
)
161158
tracer.add_attribute_to_current_span(
162159
HTTP_URL, str(flask.request.url)
163160
)
@@ -179,6 +176,9 @@ def _after_request(self, response):
179176

180177
try:
181178
tracer = execution_context.get_opencensus_tracer()
179+
tracer.add_attribute_to_current_span(
180+
HTTP_ROUTE, flask.request.url_rule.rule
181+
)
182182
tracer.add_attribute_to_current_span(
183183
HTTP_STATUS_CODE,
184184
str(response.status_code)

contrib/opencensus-ext-flask/tests/test_flask_middleware.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ def create_app():
5151
def index():
5252
return 'test flask trace' # pragma: NO COVER
5353

54+
@app.route('/wiki/<entry>')
55+
def wiki(entry):
56+
return 'test flask trace' # pragma: NO COVER
57+
5458
@app.route('/_ah/health')
5559
def health_check():
5660
return 'test health check' # pragma: NO COVER
@@ -155,7 +159,6 @@ def test__before_request(self):
155159
'http.host': u'localhost',
156160
'http.method': u'GET',
157161
'http.path': u'/wiki/Rabbit',
158-
'http.route': u'/wiki/Rabbit',
159162
'http.url': u'http://localhost/wiki/Rabbit',
160163
}
161164

@@ -220,7 +223,6 @@ def test_header_encoding(self):
220223
'http.host': u'localhost',
221224
'http.method': u'GET',
222225
'http.path': u'/wiki/Rabbit',
223-
'http.route': u'/wiki/Rabbit',
224226
'http.url': u'http://localhost/wiki/Rabbit',
225227
}
226228

@@ -248,7 +250,6 @@ def test_header_is_none(self):
248250
'http.host': u'localhost',
249251
'http.method': u'GET',
250252
'http.path': u'/wiki/Rabbit',
251-
'http.route': u'/wiki/Rabbit',
252253
'http.url': u'http://localhost/wiki/Rabbit',
253254
}
254255

@@ -278,13 +279,37 @@ def test__after_request_sampled(self):
278279
flask_trace_id = '00-{}-{}-00'.format(trace_id, span_id)
279280

280281
app = self.create_app()
281-
flask_middleware.FlaskMiddleware(app=app)
282+
flask_middleware.FlaskMiddleware(
283+
app=app,
284+
sampler=samplers.AlwaysOnSampler()
285+
)
282286

283-
response = app.test_client().get(
284-
'/',
285-
headers={flask_trace_header: flask_trace_id})
287+
context = app.test_request_context(
288+
path='/wiki/Rabbit',
289+
headers={flask_trace_header: flask_trace_id}
290+
)
286291

287-
self.assertEqual(response.status_code, 200)
292+
with context:
293+
app.preprocess_request()
294+
tracer = execution_context.get_opencensus_tracer()
295+
self.assertIsNotNone(tracer)
296+
297+
span = tracer.current_span()
298+
299+
rv = app.dispatch_request()
300+
app.finalize_request(rv)
301+
302+
expected_attributes = {
303+
'http.host': u'localhost',
304+
'http.method': u'GET',
305+
'http.path': u'/wiki/Rabbit',
306+
'http.url': u'http://localhost/wiki/Rabbit',
307+
'http.route': u'/wiki/<entry>',
308+
'http.status_code': u'200'
309+
}
310+
311+
self.assertEqual(span.attributes, expected_attributes)
312+
assert isinstance(span.parent_span, base.NullContextManager)
288313

289314
def test__after_request_blacklist(self):
290315
flask_trace_header = 'traceparent'

0 commit comments

Comments
 (0)