Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 14 additions & 0 deletions tests/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,20 @@ def test_report_metrics__samples_pool_and_queue_counts(self, monkeypatch):
]
assert shutdown_requested.timeouts == [7.0]

def test_report_metrics__zero_interval_disables_sampling(self, monkeypatch):
"""Skip sampling when the interval is zero."""
executor = TaskExecutor(
backend=StubQueueStatsBackend(BackendTelemetry(queues={})),
queues=("default",),
sample_interval=datetime.timedelta(0),
)
sentry_metrics = StubSentryMetrics()
monkeypatch.setattr(executor_module, "sentry_metrics", sentry_metrics)

executor.report_metrics()

assert sentry_metrics.gauges == []


class TestWorkerProcess:
"""Tests for the WorkerProcess class."""
Expand Down
49 changes: 25 additions & 24 deletions threadmill/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,30 +246,31 @@ def maintain_worker_pool(self) -> None:

def report_metrics(self) -> None:
"""Sample queue depth and worker counts into Sentry until shutdown."""
while not self.shutdown_requested.is_set():
sentry_metrics.gauge(
"threadmill.worker.count",
self.process_count,
attributes={"pool": "process"},
)
sentry_metrics.gauge(
"threadmill.worker.count",
self.process_count * self.thread_count,
attributes={"pool": "thread"},
)
telemetry = asyncio.run(self.backend.queue_stats())
for queue_name, stats in telemetry.queues.items():
for status, depth in (
("ready", stats.counts.ready),
("running", stats.counts.running),
("deferred", stats.counts.deferred),
):
sentry_metrics.gauge(
"threadmill.queue.depth",
depth,
attributes={"queue": queue_name, "status": status},
)
self.shutdown_requested.wait(self.sample_interval.total_seconds())
if self.sample_interval > datetime.timedelta(0):
while not self.shutdown_requested.is_set():
sentry_metrics.gauge(
"threadmill.worker.count",
self.process_count,
attributes={"pool": "process"},
)
sentry_metrics.gauge(
"threadmill.worker.count",
self.process_count * self.thread_count,
attributes={"pool": "thread"},
)
telemetry = asyncio.run(self.backend.queue_stats())
for queue_name, stats in telemetry.queues.items():
for status, depth in (
("ready", stats.counts.ready),
("running", stats.counts.running),
("deferred", stats.counts.deferred),
):
sentry_metrics.gauge(
"threadmill.queue.depth",
depth,
attributes={"queue": queue_name, "status": status},
)
self.shutdown_requested.wait(self.sample_interval.total_seconds())


class WorkerProcess(multiprocessing.Process):
Expand Down
2 changes: 1 addition & 1 deletion threadmill/management/commands/threadmill.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def add_arguments(self, parser):
default=datetime.timedelta(seconds=30),
help=(
"Interval between queue depth and worker count samples in seconds."
" Defaults to 30."
" Defaults to 30. Set to 0 to disable sampling."
),
)
parser.add_argument(
Expand Down
Loading