Skip to content

Commit

Permalink
Add method to urllib3 trace (#1130)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
lrafeei and mergify[bot] committed May 3, 2024
1 parent a447772 commit 8b9e394
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
26 changes: 12 additions & 14 deletions newrelic/hooks/external_urllib3.py
Expand Up @@ -14,34 +14,32 @@

from newrelic.api.external_trace import ExternalTraceWrapper
from newrelic.common.object_wrapper import wrap_function_wrapper
from newrelic.hooks.external_httplib2 import (
_nr_wrapper_httplib2_endheaders_wrapper)
from newrelic.hooks.external_httplib2 import _nr_wrapper_httplib2_endheaders_wrapper


def _nr_wrapper_make_request_(wrapped, instance, args, kwargs):

def _bind_params(conn, method, url, *args, **kwargs):
return "%s://%s:%s" % (instance.scheme, conn.host, conn.port)
return method, "%s://%s:%s" % (instance.scheme, conn.host, conn.port)

url_for_apm_ui = _bind_params(*args, **kwargs)
method, url_for_apm_ui = _bind_params(*args, **kwargs)

return ExternalTraceWrapper(wrapped, 'urllib3', url_for_apm_ui)(*args, **kwargs)
return ExternalTraceWrapper(wrapped, "urllib3", url_for_apm_ui, method=method)(*args, **kwargs)


def instrument_urllib3_connectionpool(module):
wrap_function_wrapper(module, 'HTTPSConnectionPool._make_request',
_nr_wrapper_make_request_)
wrap_function_wrapper(module, 'HTTPConnectionPool._make_request',
_nr_wrapper_make_request_)
wrap_function_wrapper(module, "HTTPSConnectionPool._make_request", _nr_wrapper_make_request_)
wrap_function_wrapper(module, "HTTPConnectionPool._make_request", _nr_wrapper_make_request_)


def instrument_urllib3_connection(module):
# Don't combine the instrument functions into a single function. Keep
# the 'connect' monkey patch separate, because it is also used to patch
# urllib3 within the requests package.

wrap_function_wrapper(module, 'HTTPSConnection.endheaders',
_nr_wrapper_httplib2_endheaders_wrapper('urllib3', 'https'))
wrap_function_wrapper(
module, "HTTPSConnection.endheaders", _nr_wrapper_httplib2_endheaders_wrapper("urllib3", "https")
)

wrap_function_wrapper(module, 'HTTPConnection.endheaders',
_nr_wrapper_httplib2_endheaders_wrapper('urllib3', 'http'))
wrap_function_wrapper(
module, "HTTPConnection.endheaders", _nr_wrapper_httplib2_endheaders_wrapper("urllib3", "http")
)
8 changes: 4 additions & 4 deletions tests/external_urllib3/test_urllib3.py
Expand Up @@ -45,13 +45,13 @@

@pytest.fixture(scope="session")
def metrics(server):
scoped = [("External/localhost:%d/urllib3/" % server.port, 1)]
scoped = [("External/localhost:%d/urllib3/GET" % server.port, 1)]

rollup = [
("External/all", 1),
("External/allOther", 1),
("External/localhost:%d/all" % server.port, 1),
("External/localhost:%d/urllib3/" % server.port, 1),
("External/localhost:%d/urllib3/GET" % server.port, 1),
]

return scoped, rollup
Expand Down Expand Up @@ -162,13 +162,13 @@ def _test():


def test_port_included(server):
scoped = [("External/localhost:%d/urllib3/" % server.port, 1)]
scoped = [("External/localhost:%d/urllib3/GET" % server.port, 1)]

rollup = [
("External/all", 1),
("External/allOther", 1),
("External/localhost:%d/all" % server.port, 1),
("External/localhost:%d/urllib3/" % server.port, 1),
("External/localhost:%d/urllib3/GET" % server.port, 1),
]

@validate_transaction_errors(errors=[])
Expand Down

0 comments on commit 8b9e394

Please sign in to comment.