Skip to content

Commit

Permalink
Fix Urllib instrumentation - Add status code to span if not None (ope…
Browse files Browse the repository at this point in the history
  • Loading branch information
shalevr authored and CircleCI committed Nov 13, 2022
1 parent 23e0ef5 commit ec21276
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixed

- Fix bug in Urllib instrumentation - add status code to span attributes only if the status code is not None.
([#1430](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1430))

## Version 1.14.0/0.35b0 (2022-11-03)

### Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def _instrumented_open_call(
code_ = result.getcode()
labels[SpanAttributes.HTTP_STATUS_CODE] = str(code_)

if span.is_recording():
if span.is_recording() and code_ is not None:
span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, code_)
span.set_status(Status(http_status_to_status_code(code_)))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import socket
import urllib
from unittest import mock
from unittest.mock import patch
from urllib import request
from urllib.error import HTTPError
from urllib.request import OpenerDirector
Expand Down Expand Up @@ -150,6 +151,35 @@ def test_not_foundbasic(self):
trace.StatusCode.ERROR,
)

@staticmethod
def mock_get_code(*args, **kwargs):
return None

@patch("http.client.HTTPResponse.getcode", new=mock_get_code)
def test_response_code_none(self):

result = self.perform_request(self.URL)

self.assertEqual(result.read(), b"Hello!")
span = self.assert_span()

self.assertIs(span.kind, trace.SpanKind.CLIENT)
self.assertEqual(span.name, "HTTP GET")

self.assertEqual(
span.attributes,
{
SpanAttributes.HTTP_METHOD: "GET",
SpanAttributes.HTTP_URL: self.URL,
},
)

self.assertIs(span.status.status_code, trace.StatusCode.UNSET)

self.assertEqualSpanInstrumentationInfo(
span, opentelemetry.instrumentation.urllib
)

def test_uninstrument(self):
URLLibInstrumentor().uninstrument()
result = self.perform_request(self.URL)
Expand Down

0 comments on commit ec21276

Please sign in to comment.