Skip to content

Commit

Permalink
Fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
shalevr committed Aug 8, 2022
1 parent baca2bf commit 9db1dba
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,30 +211,20 @@ def instrumented_urlopen(wrapped, instance, args, kwargs):
if callable(response_hook):
response_hook(span, instance, response)

metric_labels = {
SpanAttributes.HTTP_METHOD: method,
SpanAttributes.HTTP_HOST: instance.host,
SpanAttributes.HTTP_SCHEME: instance.scheme,
SpanAttributes.HTTP_STATUS_CODE: response.status,
SpanAttributes.NET_PEER_NAME: instance.host,
SpanAttributes.NET_PEER_PORT: instance.port,
}

version = getattr(response, "version")
if version:
metric_labels[SpanAttributes.HTTP_FLAVOR] = (
"1.1" if version == 11 else "1.0"
)

request_size = 0 if body is None else len(body)
response_size = int(response.headers.get("Content-Length", 0))
metric_attributes = create_metric_attributes(
instance, response, method
)

duration_histogram.record(elapsed_time, attributes=metric_labels)
duration_histogram.record(
elapsed_time, attributes=metric_attributes
)
request_size_histogram.record(
request_size, attributes=metric_labels
request_size, attributes=metric_attributes
)
response_size_histogram.record(
response_size, attributes=metric_labels
response_size, attributes=metric_attributes
)

return response
Expand Down Expand Up @@ -311,6 +301,29 @@ def _is_instrumentation_suppressed() -> bool:
)


def create_metric_attributes(
instance: urllib3.connectionpool.HTTPConnectionPool,
response: urllib3.response.HTTPResponse,
method: str,
) -> dict:
metric_attributes = {
SpanAttributes.HTTP_METHOD: method,
SpanAttributes.HTTP_HOST: instance.host,
SpanAttributes.HTTP_SCHEME: instance.scheme,
SpanAttributes.HTTP_STATUS_CODE: response.status,
SpanAttributes.NET_PEER_NAME: instance.host,
SpanAttributes.NET_PEER_PORT: instance.port,
}

version = getattr(response, "version")
if version:
metric_attributes[SpanAttributes.HTTP_FLAVOR] = (
"1.1" if version == 11 else "1.0"
)

return metric_attributes


@contextlib.contextmanager
def _suppress_further_instrumentation():
token = context.attach(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def test_basic_metric_check_client_size_post(self):
"net.peer.port": self.assert_port,
}

body, content_type = encode_multipart_formdata(data_fields)
body = encode_multipart_formdata(data_fields)[0]

expected_data = {
"http.client.request.size": len(body),
Expand Down

0 comments on commit 9db1dba

Please sign in to comment.