diff --git a/CHANGELOG.md b/CHANGELOG.md index 38ea9c2fb..583891b1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog ## Unreleased +- Updated `django`, `flask`, `httplib`, `requests` and `pyramid` modules. + ([#755](https://github.com/census-instrumentation/opencensus-python/pull/755)) - Added `http code` to `grpc code` status code mapping on `utils` ([#746](https://github.com/census-instrumentation/opencensus-python/pull/746)) diff --git a/contrib/opencensus-ext-django/CHANGELOG.md b/contrib/opencensus-ext-django/CHANGELOG.md index 507b05f51..284104707 100644 --- a/contrib/opencensus-ext-django/CHANGELOG.md +++ b/contrib/opencensus-ext-django/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog ## Unreleased +- Updated `http.status_code` attribute to be an int. + ([#755](https://github.com/census-instrumentation/opencensus-python/pull/755)) ## 0.7.0 Released 2019-07-31 diff --git a/contrib/opencensus-ext-django/examples/app/views.py b/contrib/opencensus-ext-django/examples/app/views.py index ba01e98c1..d8ea57037 100644 --- a/contrib/opencensus-ext-django/examples/app/views.py +++ b/contrib/opencensus-ext-django/examples/app/views.py @@ -59,7 +59,7 @@ def greetings(request): def trace_requests(request): response = requests.get('http://www.google.com') - return HttpResponse(str(response.status_code)) + return HttpResponse(response.status_code) def mysql_trace(request): diff --git a/contrib/opencensus-ext-django/opencensus/ext/django/middleware.py b/contrib/opencensus-ext-django/opencensus/ext/django/middleware.py index d1da88ca5..68e1e0b39 100644 --- a/contrib/opencensus-ext-django/opencensus/ext/django/middleware.py +++ b/contrib/opencensus-ext-django/opencensus/ext/django/middleware.py @@ -215,7 +215,7 @@ def process_response(self, request, response): span = _get_django_span() span.add_attribute( attribute_key=HTTP_STATUS_CODE, - attribute_value=str(response.status_code)) + attribute_value=response.status_code) _set_django_attributes(span, request) diff --git a/contrib/opencensus-ext-django/tests/test_django_middleware.py b/contrib/opencensus-ext-django/tests/test_django_middleware.py index 15998e88c..cbd992d4a 100644 --- a/contrib/opencensus-ext-django/tests/test_django_middleware.py +++ b/contrib/opencensus-ext-django/tests/test_django_middleware.py @@ -222,7 +222,7 @@ def test_process_response(self): 'http.path': u'/wiki/Rabbit', 'http.route': u'/wiki/Rabbit', 'http.url': u'http://testserver/wiki/Rabbit', - 'http.status_code': '200', + 'http.status_code': 200, 'django.user.id': '123', 'django.user.name': 'test_name' } @@ -277,7 +277,7 @@ def test_process_response_unfinished_child_span(self): 'http.path': u'/wiki/Rabbit', 'http.route': u'/wiki/Rabbit', 'http.url': u'http://testserver/wiki/Rabbit', - 'http.status_code': '500', + 'http.status_code': 500, 'django.user.id': '123', 'django.user.name': 'test_name' } diff --git a/contrib/opencensus-ext-flask/CHANGELOG.md b/contrib/opencensus-ext-flask/CHANGELOG.md index 6a590c4cd..549e8c993 100644 --- a/contrib/opencensus-ext-flask/CHANGELOG.md +++ b/contrib/opencensus-ext-flask/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog ## Unreleased +- Updated `http.status_code` attribute to be an int. + ([#755](https://github.com/census-instrumentation/opencensus-python/pull/755)) ## 0.7.1 Released 2019-08-05 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 fec2a8c23..ebd8d24ff 100644 --- a/contrib/opencensus-ext-flask/opencensus/ext/flask/flask_middleware.py +++ b/contrib/opencensus-ext-flask/opencensus/ext/flask/flask_middleware.py @@ -181,7 +181,7 @@ def _after_request(self, response): ) tracer.add_attribute_to_current_span( HTTP_STATUS_CODE, - str(response.status_code) + response.status_code ) except Exception: # pragma: NO COVER log.error('Failed to trace request', exc_info=True) diff --git a/contrib/opencensus-ext-flask/tests/test_flask_middleware.py b/contrib/opencensus-ext-flask/tests/test_flask_middleware.py index 3e68f7124..6cb59f177 100644 --- a/contrib/opencensus-ext-flask/tests/test_flask_middleware.py +++ b/contrib/opencensus-ext-flask/tests/test_flask_middleware.py @@ -305,7 +305,7 @@ def test__after_request_sampled(self): 'http.path': u'/wiki/Rabbit', 'http.url': u'http://localhost/wiki/Rabbit', 'http.route': u'/wiki/', - 'http.status_code': u'200' + 'http.status_code': 200 } self.assertEqual(span.attributes, expected_attributes) diff --git a/contrib/opencensus-ext-httplib/CHANGELOG.md b/contrib/opencensus-ext-httplib/CHANGELOG.md index 36cba4701..2bd0acffd 100644 --- a/contrib/opencensus-ext-httplib/CHANGELOG.md +++ b/contrib/opencensus-ext-httplib/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog ## Unreleased +- Updated `http.status_code` attribute to be an int. + ([#755](https://github.com/census-instrumentation/opencensus-python/pull/755)) ## 0.7.1 Released 2019-08-06 diff --git a/contrib/opencensus-ext-httplib/opencensus/ext/httplib/trace.py b/contrib/opencensus-ext-httplib/opencensus/ext/httplib/trace.py index 2a053bed1..54ed0653d 100644 --- a/contrib/opencensus-ext-httplib/opencensus/ext/httplib/trace.py +++ b/contrib/opencensus-ext-httplib/opencensus/ext/httplib/trace.py @@ -121,7 +121,7 @@ def call(self, *args, **kwargs): # Add the status code to attributes _tracer.add_attribute_to_current_span( - HTTP_STATUS_CODE, str(result.status)) + HTTP_STATUS_CODE, result.status) _tracer.end_span() return result diff --git a/contrib/opencensus-ext-pyramid/CHANGELOG.md b/contrib/opencensus-ext-pyramid/CHANGELOG.md index 7dc6896b2..d7d68ed91 100644 --- a/contrib/opencensus-ext-pyramid/CHANGELOG.md +++ b/contrib/opencensus-ext-pyramid/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog ## Unreleased +- Updated `http.status_code` attribute to be an int. + ([#755](https://github.com/census-instrumentation/opencensus-python/pull/755)) ## 0.7.0 Released 2019-07-31 diff --git a/contrib/opencensus-ext-pyramid/examples/app/__init__.py b/contrib/opencensus-ext-pyramid/examples/app/__init__.py index 045fc6428..24dcc1b82 100644 --- a/contrib/opencensus-ext-pyramid/examples/app/__init__.py +++ b/contrib/opencensus-ext-pyramid/examples/app/__init__.py @@ -28,7 +28,7 @@ def hello(request): @view_config(route_name='trace_requests') def trace_requests(request): response = requests.get('http://www.google.com') - return Response(str(response.status_code)) + return Response(response.status_code) def main(global_config, **settings): diff --git a/contrib/opencensus-ext-pyramid/opencensus/ext/pyramid/pyramid_middleware.py b/contrib/opencensus-ext-pyramid/opencensus/ext/pyramid/pyramid_middleware.py index 1608ea436..e2ce58c6d 100644 --- a/contrib/opencensus-ext-pyramid/opencensus/ext/pyramid/pyramid_middleware.py +++ b/contrib/opencensus-ext-pyramid/opencensus/ext/pyramid/pyramid_middleware.py @@ -121,7 +121,7 @@ def _after_request(self, request, response): tracer = execution_context.get_opencensus_tracer() tracer.add_attribute_to_current_span( HTTP_STATUS_CODE, - str(response.status_code)) + response.status_code) tracer.end_span() tracer.finish() diff --git a/contrib/opencensus-ext-pyramid/tests/test_pyramid_middleware.py b/contrib/opencensus-ext-pyramid/tests/test_pyramid_middleware.py index c016c8d11..13df75ddd 100644 --- a/contrib/opencensus-ext-pyramid/tests/test_pyramid_middleware.py +++ b/contrib/opencensus-ext-pyramid/tests/test_pyramid_middleware.py @@ -241,7 +241,7 @@ def dummy_handler(request): 'http.path': u'/', 'http.route': u'/', 'http.url': u'http://example.com', - 'http.status_code': '200', + 'http.status_code': 200, } self.assertEqual(span.parent_span.span_id, span_id) diff --git a/contrib/opencensus-ext-stackdriver/opencensus/ext/stackdriver/trace_exporter/__init__.py b/contrib/opencensus-ext-stackdriver/opencensus/ext/stackdriver/trace_exporter/__init__.py index d83d63ba4..b5dc999d7 100644 --- a/contrib/opencensus-ext-stackdriver/opencensus/ext/stackdriver/trace_exporter/__init__.py +++ b/contrib/opencensus-ext-stackdriver/opencensus/ext/stackdriver/trace_exporter/__init__.py @@ -28,6 +28,7 @@ from opencensus.trace import span_data from opencensus.trace.attributes import Attributes + # Agent AGENT = 'opencensus-python [{}]'.format(__version__) @@ -274,6 +275,16 @@ def map_attributes(self, attribute_map): if (attribute_key in ATTRIBUTE_MAPPING): new_key = ATTRIBUTE_MAPPING.get(attribute_key) value[new_key] = value.pop(attribute_key) + if new_key == '/http/status_code': + # workaround: Stackdriver expects status to be str + hack = value[new_key] + hack = hack['int_value'] + if not isinstance(hack, int): + hack = hack['value'] + value[new_key] = {'string_value': { + 'truncated_byte_count': 0, + 'value': str(hack), + }} return attribute_map diff --git a/contrib/opencensus-ext-stackdriver/tests/test_stackdriver_exporter.py b/contrib/opencensus-ext-stackdriver/tests/test_stackdriver_exporter.py index c64806825..c48e21681 100644 --- a/contrib/opencensus-ext-stackdriver/tests/test_stackdriver_exporter.py +++ b/contrib/opencensus-ext-stackdriver/tests/test_stackdriver_exporter.py @@ -448,8 +448,9 @@ def test_translate_common_attributes_to_stackdriver(self): } }, '/http/status_code': { - 'int_value': { - 'value': 200 + 'string_value': { + 'truncated_byte_count': 0, + 'value': '200' } }, '/http/url': { @@ -532,6 +533,37 @@ def test_translate_common_attributes_to_stackdriver(self): exporter.map_attributes(attributes) self.assertEqual(attributes, expected_attributes) + def test_translate_common_attributes_status_code(self): + project_id = 'PROJECT' + client = mock.Mock() + client.project = project_id + exporter = trace_exporter.StackdriverExporter( + client=client, project_id=project_id) + + attributes = { + 'outer key': 'some value', + 'attributeMap': { + 'http.status_code': { + 'int_value': 200 + } + } + } + + expected_attributes = { + 'outer key': 'some value', + 'attributeMap': { + '/http/status_code': { + 'string_value': { + 'truncated_byte_count': 0, + 'value': '200' + } + } + } + } + + exporter.map_attributes(attributes) + self.assertEqual(attributes, expected_attributes) + class Test_set_attributes_gae(unittest.TestCase): @mock.patch('opencensus.ext.stackdriver.trace_exporter.' diff --git a/tests/system/trace/django/django_system_test.py b/tests/system/trace/django/django_system_test.py index 0bb227e2c..4dba05db4 100644 --- a/tests/system/trace/django/django_system_test.py +++ b/tests/system/trace/django/django_system_test.py @@ -134,7 +134,9 @@ def test_with_retry(self): if span.get('name') == '[mysql.query]SELECT 2*3': self.assertEqual( labels.get('mysql.cursor.method.name'), 'execute') - self.assertEqual(labels.get('mysql.query'), 'SELECT 2*3') + self.assertEqual( + labels.get('mysql.query'), 'SELECT 2*3' + ) self.assertTrue(request_succeeded) @@ -168,7 +170,9 @@ def test_with_retry(self): if span.get('name') == '[postgresql.query]SELECT 2*3': self.assertEqual( - labels.get('postgresql.cursor.method.name'), 'execute') + labels.get( + 'postgresql.cursor.method.name'), 'execute' + ) self.assertEqual( labels.get('postgresql.query'), 'SELECT 2*3') diff --git a/tests/system/trace/flask/flask_system_test.py b/tests/system/trace/flask/flask_system_test.py index a733cd245..e89f66aa5 100644 --- a/tests/system/trace/flask/flask_system_test.py +++ b/tests/system/trace/flask/flask_system_test.py @@ -162,7 +162,9 @@ def test_with_retry(self): for span in spans: labels = span.get('labels') if '/http/status_code' in labels.keys(): - self.assertEqual(labels.get('/http/status_code'), '200') + self.assertEqual( + labels.get('/http/status_code'), '200' + ) request_succeeded = True if span.get('name') == '[postgresql.query]SELECT 2*3':