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

Fix Urllib instrumentation - Add status code to span if not None #1430

Merged
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
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