From 0787052ab0f10e627796a51fadc2cfc7fbc95100 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Tue, 1 Oct 2019 15:41:23 -0700 Subject: [PATCH] Minor Release for Flask (#795) --- CHANGELOG.md | 6 +++ contrib/opencensus-ext-flask/CHANGELOG.md | 6 +++ .../opencensus/ext/flask/flask_middleware.py | 8 ++-- .../tests/test_flask_middleware.py | 43 +++++++++++++++++++ contrib/opencensus-ext-flask/version.py | 2 +- opencensus/common/version/__init__.py | 2 +- 6 files changed, 62 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4e02c005..bb78b55d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## Unreleased +## 0.7.5 +Released 2019-10-01 + +- Updated `flask` module + ([#781](https://github.com/census-instrumentation/opencensus-python/pull/781)) + ## 0.7.4 Released 2019-09-30 diff --git a/contrib/opencensus-ext-flask/CHANGELOG.md b/contrib/opencensus-ext-flask/CHANGELOG.md index 413294096..befd70cef 100644 --- a/contrib/opencensus-ext-flask/CHANGELOG.md +++ b/contrib/opencensus-ext-flask/CHANGELOG.md @@ -2,6 +2,12 @@ ## Unreleased +## 0.7.3 +Released 2019-10-01 + +- Check that `url_rule` is not `None` before dereferencing property. + ([#781](https://github.com/census-instrumentation/opencensus-python/pull/781)) + ## 0.7.2 Released 2019-08-26 diff --git a/contrib/opencensus-ext-flask/opencensus/ext/flask/flask_middleware.py b/contrib/opencensus-ext-flask/opencensus/ext/flask/flask_middleware.py index ebd8d24ff..fcbdc41e7 100644 --- a/contrib/opencensus-ext-flask/opencensus/ext/flask/flask_middleware.py +++ b/contrib/opencensus-ext-flask/opencensus/ext/flask/flask_middleware.py @@ -176,9 +176,11 @@ def _after_request(self, response): try: tracer = execution_context.get_opencensus_tracer() - tracer.add_attribute_to_current_span( - HTTP_ROUTE, flask.request.url_rule.rule - ) + url_rule = flask.request.url_rule + if url_rule is not None: + tracer.add_attribute_to_current_span( + HTTP_ROUTE, url_rule.rule + ) tracer.add_attribute_to_current_span( HTTP_STATUS_CODE, response.status_code diff --git a/contrib/opencensus-ext-flask/tests/test_flask_middleware.py b/contrib/opencensus-ext-flask/tests/test_flask_middleware.py index 6cb59f177..bcd11a506 100644 --- a/contrib/opencensus-ext-flask/tests/test_flask_middleware.py +++ b/contrib/opencensus-ext-flask/tests/test_flask_middleware.py @@ -19,6 +19,7 @@ from google.rpc import code_pb2 import flask +from werkzeug.exceptions import NotFound import mock from opencensus.ext.flask import flask_middleware @@ -311,6 +312,48 @@ def test__after_request_sampled(self): self.assertEqual(span.attributes, expected_attributes) assert isinstance(span.parent_span, base.NullContextManager) + def test__after_request_invalid_url(self): + flask_trace_header = 'traceparent' + trace_id = '2dd43a1d6b2549c6bc2a1a54c2fc0b05' + span_id = '6e0c63257de34c92' + flask_trace_id = '00-{}-{}-00'.format(trace_id, span_id) + + app = self.create_app() + flask_middleware.FlaskMiddleware( + app=app, + sampler=samplers.AlwaysOnSampler() + ) + + context = app.test_request_context( + path='/this-url-does-not-exist', + headers={flask_trace_header: flask_trace_id} + ) + + with context: + app.preprocess_request() + tracer = execution_context.get_opencensus_tracer() + self.assertIsNotNone(tracer) + + span = tracer.current_span() + + try: + rv = app.dispatch_request() + except NotFound as e: + rv = app.handle_user_exception(e) + app.finalize_request(rv) + + # http.route should not be set + expected_attributes = { + 'http.host': u'localhost', + 'http.method': u'GET', + 'http.path': u'/this-url-does-not-exist', + 'http.url': u'http://localhost/this-url-does-not-exist', + 'http.status_code': 404 + } + + self.assertEqual(span.attributes, expected_attributes) + assert isinstance(span.parent_span, base.NullContextManager) + def test__after_request_blacklist(self): flask_trace_header = 'traceparent' trace_id = '2dd43a1d6b2549c6bc2a1a54c2fc0b05' diff --git a/contrib/opencensus-ext-flask/version.py b/contrib/opencensus-ext-flask/version.py index c652125f7..b7a1f8944 100644 --- a/contrib/opencensus-ext-flask/version.py +++ b/contrib/opencensus-ext-flask/version.py @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -__version__ = '0.7.2' +__version__ = '0.7.3' diff --git a/opencensus/common/version/__init__.py b/opencensus/common/version/__init__.py index d5d5f1a28..65727e838 100644 --- a/opencensus/common/version/__init__.py +++ b/opencensus/common/version/__init__.py @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -__version__ = '0.7.4' +__version__ = '0.7.5'