Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed result and responseCode when using http.HTTPStatuscode for Azur… #1164

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions contrib/opencensus-ext-azure/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
61 changes: 61 additions & 0 deletions contrib/opencensus-ext-azure/tests/test_azure_trace_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from six.moves import http_client

import json
import os
import shutil
Expand All @@ -21,6 +23,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')
Expand Down Expand Up @@ -875,4 +878,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_client.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_client.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()