Skip to content

Commit 129d8a7

Browse files
revert(tracer): reusing connections (#2999, #3440) (#3468) (#3473)
Issues with reusing connections were detected when testing in the environment. With the timing of 1.0 we're pushing this feature off until version 1.1. (cherry picked from commit 4eaca4a) Co-authored-by: Kyle Verhoog <[email protected]>
1 parent 019294c commit 129d8a7

File tree

3 files changed

+6
-69
lines changed

3 files changed

+6
-69
lines changed

ddtrace/internal/writer.py

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import logging
66
import os
77
import sys
8-
from typing import Dict
98
from typing import List
109
from typing import Optional
1110
from typing import TYPE_CHECKING
@@ -41,8 +40,6 @@
4140
if TYPE_CHECKING:
4241
from ddtrace import Span
4342

44-
from .agent import ConnectionType
45-
4643

4744
log = get_logger(__name__)
4845

@@ -57,7 +54,6 @@
5754
DEFAULT_BUFFER_SIZE = 8 << 20 # 8 MB
5855
DEFAULT_MAX_PAYLOAD_SIZE = 8 << 20 # 8 MB
5956
DEFAULT_PROCESSING_INTERVAL = 1.0
60-
DEFAULT_REUSE_CONNECTIONS = False
6157

6258

6359
def get_writer_buffer_size():
@@ -79,11 +75,6 @@ def get_writer_interval_seconds():
7975
)
8076

8177

82-
def get_writer_reuse_connections():
83-
# type: () -> bool
84-
return asbool(os.getenv("DD_TRACE_WRITER_REUSE_CONNECTIONS", DEFAULT_REUSE_CONNECTIONS))
85-
86-
8778
def _human_size(nbytes):
8879
"""Return a human-readable size."""
8980
i = 0
@@ -255,7 +246,6 @@ def __init__(
255246
report_metrics=False, # type: bool
256247
sync_mode=False, # type: bool
257248
api_version=None, # type: Optional[str]
258-
reuse_connections=None, # type: Optional[bool]
259249
):
260250
# type: (...) -> None
261251
# Pre-conditions:
@@ -311,7 +301,6 @@ def __init__(
311301
self._metrics_reset()
312302
self._drop_sma = SimpleMovingAverage(DEFAULT_SMA_WINDOW)
313303
self._sync_mode = sync_mode
314-
self._conn = None # type: Optional[ConnectionType]
315304
self._retry_upload = tenacity.Retrying(
316305
# Retry RETRY_ATTEMPTS times within the first half of the processing
317306
# interval, using a Fibonacci policy with jitter
@@ -322,7 +311,6 @@ def __init__(
322311
retry=tenacity.retry_if_exception_type((compat.httplib.HTTPException, OSError, IOError)),
323312
)
324313
self._log_error_payloads = asbool(os.environ.get("_DD_TRACE_WRITER_LOG_ERROR_PAYLOADS", False))
325-
self._reuse_connections = get_writer_reuse_connections() if reuse_connections is None else reuse_connections
326314

327315
@property
328316
def _agent_endpoint(self):
@@ -375,34 +363,22 @@ def recreate(self):
375363
api_version=self._api_version,
376364
)
377365

378-
def _reset_connection(self):
379-
# type: () -> None
380-
if self._conn:
381-
self._conn.close()
382-
self._conn = None
383-
384366
def _put(self, data, headers):
385-
# type: (bytes, Dict[str, str]) -> Response
367+
conn = get_connection(self.agent_url, self._timeout)
368+
386369
with StopWatch() as sw:
387-
if self._conn is None:
388-
log.debug("creating new agent connection to %s with timeout %d", self.agent_url, self._timeout)
389-
self._conn = get_connection(self.agent_url, self._timeout)
390370
try:
391-
self._conn.request("PUT", self._endpoint, data, headers)
392-
resp = compat.get_connection_response(self._conn)
371+
conn.request("PUT", self._endpoint, data, headers)
372+
resp = compat.get_connection_response(conn)
393373
t = sw.elapsed()
394374
if t >= self.interval:
395375
log_level = logging.WARNING
396376
else:
397377
log_level = logging.DEBUG
398378
log.log(log_level, "sent %s in %.5fs to %s", _human_size(len(data)), t, self._agent_endpoint)
399379
return Response.from_http_response(resp)
400-
except Exception:
401-
self._reset_connection()
402-
raise
403380
finally:
404-
if self._conn and not self._reuse_connections:
405-
self._reset_connection()
381+
conn.close()
406382

407383
def _downgrade(self, payload, response):
408384
if self._endpoint == "v0.5/traces":
@@ -581,8 +557,4 @@ def _stop_service( # type: ignore[override]
581557
super(AgentWriter, self)._stop_service()
582558
self.join(timeout=timeout)
583559

584-
def on_shutdown(self):
585-
try:
586-
self.periodic()
587-
finally:
588-
self._reset_connection()
560+
on_shutdown = periodic

releasenotes/notes/keep-alive-b5ec5febb435daad.yaml

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/tracer/test_writer.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -575,34 +575,3 @@ def test_writer_recreate_api_version(init_api_version, api_version, endpoint, en
575575
assert writer._api_version == api_version
576576
assert writer._endpoint == endpoint
577577
assert isinstance(writer._encoder, encoder_cls)
578-
579-
580-
def test_writer_reuse_connections_envvar(monkeypatch):
581-
monkeypatch.setenv("DD_TRACE_WRITER_REUSE_CONNECTIONS", "false")
582-
writer = AgentWriter(agent_url="http://localhost:9126")
583-
assert not writer._reuse_connections
584-
585-
monkeypatch.setenv("DD_TRACE_WRITER_REUSE_CONNECTIONS", "true")
586-
writer = AgentWriter(agent_url="http://localhost:9126")
587-
assert writer._reuse_connections
588-
589-
590-
def test_writer_reuse_connections():
591-
# Ensure connection is not reused
592-
writer = AgentWriter(agent_url="http://localhost:9126", reuse_connections=True)
593-
# Do an initial flush to get a connection
594-
writer.flush_queue()
595-
assert writer._conn is None
596-
writer.flush_queue()
597-
assert writer._conn is None
598-
599-
600-
def test_writer_reuse_connections_false():
601-
# Ensure connection is reused
602-
writer = AgentWriter(agent_url="http://localhost:9126", reuse_connections=False)
603-
# Do an initial flush to get a connection
604-
writer.flush_queue()
605-
conn = writer._conn
606-
# And another to potentially have it reset
607-
writer.flush_queue()
608-
assert writer._conn is conn

0 commit comments

Comments
 (0)