From 39f44b188f3a77eb46ef9e9ae727fee5f0ce0796 Mon Sep 17 00:00:00 2001 From: Jeremy Vriens Date: Tue, 13 Sep 2022 09:54:49 +0200 Subject: [PATCH 1/3] Fixed result and responseCode when using http.HTTPStatuscode for AzureExporter. --- .../ext/azure/trace_exporter/__init__.py | 4 +- .../tests/test_azure_trace_exporter.py | 60 +++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/contrib/opencensus-ext-azure/opencensus/ext/azure/trace_exporter/__init__.py b/contrib/opencensus-ext-azure/opencensus/ext/azure/trace_exporter/__init__.py index bb29c1b30..2c17c0d6b 100644 --- a/contrib/opencensus-ext-azure/opencensus/ext/azure/trace_exporter/__init__.py +++ b/contrib/opencensus-ext-azure/opencensus/ext/azure/trace_exporter/__init__.py @@ -136,7 +136,7 @@ def span_data_to_envelope(self, sd): data.url = sd.attributes[HTTP_URL] data.properties['request.url'] = sd.attributes[HTTP_URL] if HTTP_STATUS_CODE in sd.attributes: - status_code = sd.attributes[HTTP_STATUS_CODE] + status_code = int(sd.attributes[HTTP_STATUS_CODE]) data.responseCode = str(status_code) data.success = ( status_code >= 200 and status_code <= 399 @@ -175,7 +175,7 @@ def span_data_to_envelope(self, sd): data.name = sd.attributes[HTTP_METHOD] \ + ' ' + parse_url.path if HTTP_STATUS_CODE in sd.attributes: - status_code = sd.attributes[HTTP_STATUS_CODE] + status_code = int(sd.attributes[HTTP_STATUS_CODE]) data.resultCode = str(status_code) data.success = 200 <= status_code < 400 elif sd.status.code == 0: diff --git a/contrib/opencensus-ext-azure/tests/test_azure_trace_exporter.py b/contrib/opencensus-ext-azure/tests/test_azure_trace_exporter.py index 1613b4dae..8e6fe1bda 100644 --- a/contrib/opencensus-ext-azure/tests/test_azure_trace_exporter.py +++ b/contrib/opencensus-ext-azure/tests/test_azure_trace_exporter.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import http import json import os import shutil @@ -21,6 +22,7 @@ from opencensus.ext.azure import trace_exporter from opencensus.ext.azure.common.transport import TransportStatusCode +from opencensus.trace.attributes_helper import COMMON_ATTRIBUTES from opencensus.trace.link import Link TEST_FOLDER = os.path.abspath('.test.trace.exporter') @@ -875,4 +877,62 @@ def test_span_data_to_envelope(self): ))) self.assertFalse(envelope.data.baseData.success) + # IntEnum HTTP status code attribute + # span_kind=SpanKind.CLIENT + envelope = next(exporter.span_data_to_envelope(SpanData( + name='test', + context=SpanContext( + trace_id='6e0c63257de34c90bf9efcd03927272e', + span_id='6e0c63257de34c91', + trace_options=TraceOptions('1'), + tracestate=Tracestate(), + from_header=False, + ), + span_id='6e0c63257de34c92', + parent_span_id='6e0c63257de34c93', + attributes={ + COMMON_ATTRIBUTES['HTTP_STATUS_CODE']: http.HTTPStatus.OK, + }, + start_time='2010-10-24T07:28:38.123456Z', + end_time='2010-10-24T07:28:38.234567Z', + stack_trace=None, + links=[], + status=Status(0), + annotations=None, + message_events=None, + same_process_as_parent_span=None, + child_span_count=None, + span_kind=SpanKind.CLIENT, + ))) + self.assertEqual('200', envelope.data.baseData.resultCode) + self.assertTrue(envelope.data.baseData.success) + # span_kind=SpanKind.SERVER + envelope = next(exporter.span_data_to_envelope(SpanData( + name='test', + context=SpanContext( + trace_id='6e0c63257de34c90bf9efcd03927272e', + span_id='6e0c63257de34c91', + trace_options=TraceOptions('1'), + tracestate=Tracestate(), + from_header=False, + ), + span_id='6e0c63257de34c92', + parent_span_id='6e0c63257de34c93', + attributes={ + COMMON_ATTRIBUTES['HTTP_STATUS_CODE']: http.HTTPStatus.OK + }, + start_time='2010-10-24T07:28:38.123456Z', + end_time='2010-10-24T07:28:38.234567Z', + stack_trace=None, + links=None, + status=Status(0), + annotations=None, + message_events=None, + same_process_as_parent_span=None, + child_span_count=None, + span_kind=SpanKind.SERVER, + ))) + self.assertEqual('200', envelope.data.baseData.responseCode) + self.assertTrue(envelope.data.baseData.success) + exporter._stop() From 331613ab4eb4713ec7073b5f04b0be8788209aba Mon Sep 17 00:00:00 2001 From: Jeremy Vriens Date: Tue, 13 Sep 2022 09:58:23 +0200 Subject: [PATCH 2/3] Added CHANGELOG entry. --- contrib/opencensus-ext-azure/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/contrib/opencensus-ext-azure/CHANGELOG.md b/contrib/opencensus-ext-azure/CHANGELOG.md index 848c5da0d..2bb0060f2 100644 --- a/contrib/opencensus-ext-azure/CHANGELOG.md +++ b/contrib/opencensus-ext-azure/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +- Fixed resultCode and responseCode for traces when using http.HTTPStatus +- ([#1163](https://github.com/census-instrumentation/opencensus-python/issues/1163)) + ## 1.1.7 Released 2022-08-18 From 951756718bcf2c11a2fd1161a13a009738a71356 Mon Sep 17 00:00:00 2001 From: Jeremy Vriens Date: Tue, 13 Sep 2022 11:17:31 +0200 Subject: [PATCH 3/3] Tests are Python2 compatible. --- .../tests/test_azure_trace_exporter.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/contrib/opencensus-ext-azure/tests/test_azure_trace_exporter.py b/contrib/opencensus-ext-azure/tests/test_azure_trace_exporter.py index 8e6fe1bda..a8a81516b 100644 --- a/contrib/opencensus-ext-azure/tests/test_azure_trace_exporter.py +++ b/contrib/opencensus-ext-azure/tests/test_azure_trace_exporter.py @@ -12,7 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import http +from six.moves import http_client + import json import os import shutil @@ -891,7 +892,7 @@ def test_span_data_to_envelope(self): span_id='6e0c63257de34c92', parent_span_id='6e0c63257de34c93', attributes={ - COMMON_ATTRIBUTES['HTTP_STATUS_CODE']: http.HTTPStatus.OK, + COMMON_ATTRIBUTES['HTTP_STATUS_CODE']: http_client.OK, }, start_time='2010-10-24T07:28:38.123456Z', end_time='2010-10-24T07:28:38.234567Z', @@ -919,7 +920,7 @@ def test_span_data_to_envelope(self): span_id='6e0c63257de34c92', parent_span_id='6e0c63257de34c93', attributes={ - COMMON_ATTRIBUTES['HTTP_STATUS_CODE']: http.HTTPStatus.OK + COMMON_ATTRIBUTES['HTTP_STATUS_CODE']: http_client.OK, }, start_time='2010-10-24T07:28:38.123456Z', end_time='2010-10-24T07:28:38.234567Z',