Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that metrics are flushed before shutting down #1139

Merged
merged 3 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions elasticapm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@ def __init__(self, config=None, **inline):
set_client(self)

def start_threads(self):
with self._thread_starter_lock:
current_pid = os.getpid()
if self._pid != current_pid:
current_pid = os.getpid()
if self._pid != current_pid:
with self._thread_starter_lock:
self.logger.debug("Detected PID change from %r to %r, starting threads", self._pid, current_pid)
for manager_type, manager in self._thread_managers.items():
for manager_type, manager in sorted(self._thread_managers.items(), key=lambda item: item[1].priority):
self.logger.debug("Starting %s thread", manager_type)
manager.start_thread(pid=current_pid)
self._pid = current_pid
Expand Down Expand Up @@ -303,7 +303,7 @@ def end_transaction(self, name=None, result="", duration=None):
def close(self):
if self.config.enabled:
with self._thread_starter_lock:
for _, manager in self._thread_managers.items():
for _, manager in sorted(self._thread_managers.items(), key=lambda item: item[1].priority):
manager.stop_thread()
global CLIENT_SINGLETON
CLIENT_SINGLETON = None
Expand Down
2 changes: 2 additions & 0 deletions elasticapm/metrics/base_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def stop_thread(self):
logger.debug("Cancelling collect timer")
self._collect_timer.cancel()
self._collect_timer = None
# collect one last time
self.collect()

@property
def collect_interval(self):
Expand Down
2 changes: 2 additions & 0 deletions elasticapm/transport/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import gzip
import os
import random
import sys
import threading
import time
import timeit
Expand Down Expand Up @@ -86,6 +87,7 @@ def __init__(
self._closed = False
self._processors = processors if processors is not None else []
super(Transport, self).__init__()
self.priority = sys.maxsize # ensure that the transport thread is always started/stopped last
felixbarny marked this conversation as resolved.
Show resolved Hide resolved

@property
def _max_flush_time(self):
Expand Down
1 change: 1 addition & 0 deletions elasticapm/utils/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def cancel(self):
class ThreadManager(object):
def __init__(self):
self.pid = None
self.priority = 100

def start_thread(self, pid=None):
if not pid:
Expand Down
18 changes: 18 additions & 0 deletions tests/metrics/base_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@ def target():
assert metricset.counter("x").val == expected


@pytest.mark.parametrize("sending_elasticapm_client", [{"metrics_interval": "30s"}], indirect=True)
def test_metrics_flushed_on_shutdown(sending_elasticapm_client):
# this is ugly, we need an API for this at some point...
metricset = MetricsSet(sending_elasticapm_client._metrics)
sending_elasticapm_client._metrics._metricsets["foo"] = metricset
metricset.counter("x").inc()
sending_elasticapm_client.close()
assert sending_elasticapm_client.httpserver.payloads
for item in sending_elasticapm_client.httpserver.payloads[0]:
try:
assert item["metricset"]["samples"]["x"]["value"] == 1
break
except KeyError:
pass
else:
assert False, "no item found with matching dict path metricset.samples.x.value"


@mock.patch("elasticapm.metrics.base_metrics.DISTINCT_LABEL_LIMIT", 3)
def test_metric_limit(caplog, elasticapm_client):
m = MetricsSet(MetricsRegistry(elasticapm_client))
Expand Down