Skip to content

Commit 6277c65

Browse files
authored
fix(telemetry): flush telemetry writer when a process terminates (backport #3598) (#3621)
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 1e869be commit 6277c65

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
"""
@@ -229,10 +234,9 @@ def disable(self):
229234
return
230235

231236
forksafe.unregister(self._restart)
237+
atexit.unregister(self.stop)
232238

233-
self.stop()
234-
235-
self.join()
239+
self.stop()
236240

237241
def enable(self):
238242
# type: () -> None
@@ -248,6 +252,7 @@ def enable(self):
248252
self._enabled = True
249253

250254
forksafe.register(self._restart)
255+
atexit.register(self.stop)
251256

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

tests/tracer/telemetry/test_writer.py

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

101101

102102
def test_add_app_closing_event(mock_time, mock_send_request, telemetry_writer):
103-
"""asserts that shutdown() queues and sends an app-closing telemetry request"""
103+
"""asserts that on_shutdown() queues and sends an app-closing telemetry request"""
104104
# send app closed event
105-
telemetry_writer.shutdown()
105+
telemetry_writer.on_shutdown()
106106
# assert that one request was sent
107107
assert len(httpretty.latest_requests()) == 1
108108
# ensure an app closing event was sent
@@ -191,7 +191,7 @@ def test_send_failing_request(mock_status, mock_send_request, telemetry_writer):
191191
"""asserts that a warning is logged when an unsuccessful response is returned by the http client"""
192192
with mock.patch("ddtrace.internal.telemetry.writer.log") as log:
193193
# sends failing app-closing event
194-
telemetry_writer.shutdown()
194+
telemetry_writer.on_shutdown()
195195
# asserts unsuccessful status code was logged
196196
log.warning.assert_called_with(
197197
"failed to send telemetry to the Datadog Agent at %s/%s. response: %s",
@@ -203,7 +203,7 @@ def test_send_failing_request(mock_status, mock_send_request, telemetry_writer):
203203
assert len(httpretty.latest_requests()) == 1
204204

205205

206-
def test_send_request_exception(telemetry_writer):
206+
def test_send_request_exception():
207207
"""asserts that an error is logged when an exception is raised by the http client"""
208208
# create a telemetry writer with an invalid agent url.
209209
# this will raise an Exception on _send_request
@@ -212,7 +212,7 @@ def test_send_request_exception(telemetry_writer):
212212

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
# assert an exception was logged
217217
log.warning.assert_called_with(
218218
"failed to send telemetry to the Datadog Agent at %s/%s.",
@@ -222,6 +222,12 @@ def test_send_request_exception(telemetry_writer):
222222
)
223223

224224

225+
def test_telemetry_graceful_shutdown(mock_time, mock_send_request, telemetry_writer):
226+
telemetry_writer.start()
227+
telemetry_writer.stop()
228+
assert httpretty.last_request().parsed_body == _get_request_body({}, "app-closing")
229+
230+
225231
def _get_request_body(payload, payload_type, seq_id=1):
226232
"""used to test the body of requests intercepted by httpretty"""
227233
return {

0 commit comments

Comments
 (0)