Skip to content

Commit 7198d79

Browse files
Fix urllib3 patching not activating tracing (#2349) (#2352)
* Fix urllib3 patching not activating tracing * remove unused import * update circleci job * Always use docker-compose httpbin for urllib3 tests * forgot to checkout the code (cherry picked from commit 8b33989) Co-authored-by: Brett Langdon <[email protected]>
1 parent 0e123f2 commit 7198d79

File tree

7 files changed

+84
-14
lines changed

7 files changed

+84
-14
lines changed

.circleci/config.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -929,14 +929,14 @@ jobs:
929929
pattern: '^rediscluster_contrib-'
930930

931931
urllib3:
932-
<<: *contrib_job
933-
docker:
934-
- image: *ddtrace_dev_image
935-
- *httpbin_local
936-
resource_class: *resource_class
932+
<<: *machine_executor
937933
steps:
934+
- checkout
935+
# `snapshot: true` will run `docker-compose logs -f` for us
936+
- run: docker-compose up -d httpbin_local
938937
- run_test:
939938
pattern: 'urllib3'
939+
snapshot: true
940940

941941
vertica:
942942
<<: *contrib_job

ddtrace/contrib/urllib3/patch.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def patch():
3737
setattr(urllib3, "__datadog_patch", True)
3838

3939
_w("urllib3", "connectionpool.HTTPConnectionPool.urlopen", _wrap_urlopen)
40+
Pin().onto(urllib3.connectionpool.HTTPConnectionPool)
4041

4142

4243
def unpatch():

docker-compose.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,11 @@ services:
139139
- "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
140140
- "/var/run/docker.sock:/var/run/docker.sock"
141141

142+
httpbin_local:
143+
image: kennethreitz/httpbin@sha256:2c7abc4803080c22928265744410173b6fea3b898872c01c5fd0f0f9df4a59fb
144+
ports:
145+
- "127.0.0.1:8001:80"
146+
147+
142148
volumes:
143149
ddagent:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
Fix ``urllib3`` patching not properly activating the integration.

tests/contrib/urllib3/test_urllib3.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
from ddtrace.pin import Pin
1212
from tests.opentracer.utils import init_tracer
1313
from tests.utils import TracerTestCase
14+
from tests.utils import snapshot
1415

1516

16-
# socket name comes from https://english.stackexchange.com/a/44048
17-
SOCKET = "httpbin.org"
17+
# host:port of httpbin_local container
18+
HOST = "localhost"
19+
PORT = 8001
20+
SOCKET = "{}:{}".format(HOST, PORT)
1821
URL_200 = "http://{}/status/200".format(SOCKET)
1922
URL_500 = "http://{}/status/500".format(SOCKET)
2023

@@ -37,7 +40,7 @@ def tearDown(self):
3740
class TestUrllib3(BaseUrllib3TestCase):
3841
def test_HTTPConnectionPool_traced(self):
3942
"""Tests that requests made from the HTTPConnectionPool are traced"""
40-
pool = urllib3.connectionpool.HTTPConnectionPool(SOCKET)
43+
pool = urllib3.connectionpool.HTTPConnectionPool(HOST, PORT)
4144
# Test a relative URL
4245
r = pool.request("GET", "/status/200")
4346
assert r.status == 200
@@ -98,9 +101,9 @@ def test_args_kwargs(self):
98101

99102
for args, kwargs in inputs:
100103

101-
with self.override_config("urllib3", {}):
104+
with self.override_http_config("urllib3", {"_whitelist_headers": set()}):
102105
config.urllib3.http.trace_headers(["accept"])
103-
pool = urllib3.connectionpool.HTTPConnectionPool(SOCKET)
106+
pool = urllib3.connectionpool.HTTPConnectionPool(HOST, PORT)
104107
out = pool.urlopen(*args, **kwargs)
105108
assert out.status == 200
106109
spans = self.pop_spans()
@@ -124,7 +127,7 @@ def test_untraced_request(self):
124127
def test_double_patch(self):
125128
"""Ensure that double patch doesn't duplicate instrumentation"""
126129
patch()
127-
connpool = urllib3.connectionpool.HTTPConnectionPool(SOCKET)
130+
connpool = urllib3.connectionpool.HTTPConnectionPool(HOST, PORT)
128131
setattr(connpool, "datadog_tracer", self.tracer)
129132

130133
out = connpool.urlopen("GET", URL_200)
@@ -209,8 +212,7 @@ def test_default_service_name(self):
209212

210213
def test_user_set_service_name(self):
211214
"""Test the user-set service name is set on the span"""
212-
with self.override_config("urllib3", dict(split_by_domain=False)):
213-
config.urllib3["service_name"] = "clients"
215+
with self.override_config("urllib3", dict(split_by_domain=False, service_name="clients")):
214216
out = self.http.request("GET", URL_200)
215217
assert out.status == 200
216218
spans = self.pop_spans()
@@ -312,7 +314,7 @@ def test_request_and_response_headers(self):
312314
assert s.get_tag("http.response.headers.access-control-allow-origin") is None
313315

314316
# Enabled when explicitly configured
315-
with self.override_config("urllib3", {}):
317+
with self.override_http_config("urllib3", {"_whitelist_headers": set()}):
316318
config.urllib3.http.trace_headers(["my-header", "access-control-allow-origin"])
317319
self.http.request("GET", URL_200, headers={"my-header": "my_value"})
318320
spans = self.pop_spans()
@@ -384,3 +386,24 @@ def test_distributed_tracing_disabled(self):
384386
m_make_request.assert_called_with(
385387
mock.ANY, "GET", "/status/200", body=None, chunked=mock.ANY, headers={}, timeout=mock.ANY
386388
)
389+
390+
391+
@pytest.fixture()
392+
def patch_urllib3():
393+
patch()
394+
try:
395+
yield
396+
finally:
397+
unpatch()
398+
399+
400+
@snapshot()
401+
def test_urllib3_poolmanager_snapshot(patch_urllib3):
402+
pool = urllib3.PoolManager()
403+
pool.request("GET", URL_200)
404+
405+
406+
@snapshot()
407+
def test_urllib3_connectionpool_snapshot(patch_urllib3):
408+
pool = urllib3.connectionpool.HTTPConnectionPool(HOST, PORT)
409+
pool.request("GET", "/status/200")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[[{"name" "urllib3.request"
2+
"service" "urllib3"
3+
"resource" "urllib3.request"
4+
"type" "http"
5+
"error" 0
6+
"span_id" 0
7+
"trace_id" 0
8+
"parent_id" nil
9+
"start" 1619121392243862000
10+
"duration" 9724000
11+
"meta" {"runtime-id" "17c4dec94e02426e936e6a7c445f2a38"
12+
"http.method" "GET"
13+
"http.url" "http://localhost:8001/status/200"
14+
"http.status_code" "200"}
15+
"metrics" {"_dd.agent_psr" 1.0
16+
"_sampling_priority_v1" 1
17+
"system.pid" 39986
18+
"_dd.tracer_kr" 1.0}}]]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[[{"name" "urllib3.request"
2+
"service" "urllib3"
3+
"resource" "urllib3.request"
4+
"type" "http"
5+
"error" 0
6+
"span_id" 0
7+
"trace_id" 0
8+
"parent_id" nil
9+
"start" 1619121391909007000
10+
"duration" 10709000
11+
"meta" {"runtime-id" "17c4dec94e02426e936e6a7c445f2a38"
12+
"http.method" "GET"
13+
"http.url" "http://localhost:8001/status/200"
14+
"http.status_code" "200"}
15+
"metrics" {"_dd.agent_psr" 1.0
16+
"_sampling_priority_v1" 1
17+
"system.pid" 39986
18+
"_dd.tracer_kr" 1.0}}]]

0 commit comments

Comments
 (0)