Skip to content

Commit 489c7a7

Browse files
authored
fix(telemetry): flush telemetry writer when a process terminates (backport #3598) (#3620)
This is an automatic backport of pull request #3598 done by [Mergify](https://mergify.com). --- <details> <summary>Mergify commands and options</summary> <br /> More conditions and actions can be found in the [documentation](https://docs.mergify.com/). You can also trigger Mergify actions by commenting on this pull request: - `@Mergifyio refresh` will re-evaluate the rules - `@Mergifyio rebase` will rebase this PR on its base branch - `@Mergifyio update` will merge the base branch into this PR - `@Mergifyio backport <destination>` will backport this PR on `<destination>` branch Additionally, on Mergify [dashboard](https://dashboard.mergify.com/) you can: - look at your merge queues - generate the Mergify configuration with the config editor. Finally, you can contact us on https://mergify.com </details>
1 parent 323ee63 commit 489c7a7

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

ddtrace/internal/telemetry/writer.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import List
55
from typing import Optional
66

7+
from ...internal import atexit
78
from ...internal import forksafe
89
from ...settings import _config as config
910
from ..agent import get_connection
@@ -116,11 +117,15 @@ def periodic(self):
116117
exc_info=True,
117118
)
118119

119-
def shutdown(self):
120-
# type: () -> None
120+
def on_shutdown(self):
121121
self._app_closing_event()
122122
self.periodic()
123123

124+
def _stop_service(self, *args, **kwargs):
125+
# type: (...) -> None
126+
super(TelemetryWriter, self)._stop_service()
127+
self.join()
128+
124129
def add_event(self, payload, payload_type):
125130
# type: (Dict, str) -> None
126131
"""
@@ -230,10 +235,9 @@ def disable(self):
230235
return
231236

232237
forksafe.unregister(self._restart)
238+
atexit.unregister(self.stop)
233239

234-
self.stop()
235-
236-
self.join()
240+
self.stop()
237241

238242
def enable(self):
239243
# type: () -> None
@@ -249,6 +253,7 @@ def enable(self):
249253
self._enabled = True
250254

251255
forksafe.register(self._restart)
256+
atexit.register(self.stop)
252257

253258
# add_event _locks around adding to the events queue
254259
self.app_started_event()

tests/tracer/telemetry/test_writer.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ def test_add_app_started_event(mock_time, mock_send_request, telemetry_writer):
121121

122122

123123
def test_add_app_closing_event(mock_time, mock_send_request, telemetry_writer):
124-
"""asserts that shutdown() queues and sends an app-closing telemetry request"""
124+
"""asserts that on_shutdown() queues and sends an app-closing telemetry request"""
125125
# send app closed event
126-
telemetry_writer.shutdown()
126+
telemetry_writer.on_shutdown()
127127
# assert that one request was sent
128128
assert len(httpretty.latest_requests()) == 1
129129
# ensure an app closing event was sent
@@ -212,7 +212,7 @@ def test_send_failing_request(mock_status, mock_send_request, telemetry_writer):
212212
"""asserts that a warning is logged when an unsuccessful response is returned by the http client"""
213213
with mock.patch("ddtrace.internal.telemetry.writer.log") as log:
214214
# sends failing app-closing event
215-
telemetry_writer.shutdown()
215+
telemetry_writer.on_shutdown()
216216
# asserts unsuccessful status code was logged
217217
log.warning.assert_called_with(
218218
"failed to send telemetry to the Datadog Agent at %s/%s. response: %s",
@@ -224,7 +224,7 @@ def test_send_failing_request(mock_status, mock_send_request, telemetry_writer):
224224
assert len(httpretty.latest_requests()) == 1
225225

226226

227-
def test_send_request_exception(telemetry_writer):
227+
def test_send_request_exception():
228228
"""asserts that an error is logged when an exception is raised by the http client"""
229229
# create a telemetry writer with an invalid agent url.
230230
# this will raise an Exception on _send_request
@@ -233,7 +233,7 @@ def test_send_request_exception(telemetry_writer):
233233

234234
with mock.patch("ddtrace.internal.telemetry.writer.log") as log:
235235
# sends failing app-closing event
236-
telemetry_writer.shutdown()
236+
telemetry_writer.on_shutdown()
237237
# assert an exception was logged
238238
log.warning.assert_called_with(
239239
"failed to send telemetry to the Datadog Agent at %s/%s.",
@@ -243,6 +243,12 @@ def test_send_request_exception(telemetry_writer):
243243
)
244244

245245

246+
def test_telemetry_graceful_shutdown(mock_time, mock_send_request, telemetry_writer):
247+
telemetry_writer.start()
248+
telemetry_writer.stop()
249+
assert httpretty.last_request().parsed_body == _get_request_body({}, "app-closing")
250+
251+
246252
def _get_request_body(payload, payload_type, seq_id=1):
247253
"""used to test the body of requests intercepted by httpretty"""
248254
return {

0 commit comments

Comments
 (0)