@@ -68,6 +68,27 @@ or `clean_dead_pids=True` to remove only files from PIDs that no longer exist.
6868Both default to ` False ` . Use a dedicated metrics directory per worker process
6969group.
7070
71+ Cleanup is owned by the parent process and performed by
72+ ` MetricsSettings.clean_metrics_directory() ` , which is idempotent per process
73+ (the destructive step runs at most once per directory). Spawned workers only
74+ ensure the directory exists (via ` create_metrics_collector ` ) and never delete
75+ ` .db ` files, so a newly started or restarted worker can never wipe metrics
76+ belonging to live sibling processes.
77+
78+ ` TaskHandler.__init__ ` calls ` clean_metrics_directory() ` for you before any
79+ worker is spawned, which is sufficient for worker-only apps. If the parent
80+ process * also* collects metrics before constructing the ` TaskHandler ` (for
81+ example, registering task/workflow definitions or starting workflows through an
82+ instrumented client), build the parent's collector with
83+ ` create_metrics_collector_for_parent(metrics_settings) ` instead of
84+ ` create_metrics_collector ` . The parent factory cleans the directory up front --
85+ before the collector's first write -- and then creates the collector. Because
86+ ` clean_metrics_directory() ` is idempotent, ` TaskHandler ` 's later call is then a
87+ no-op and cannot orphan the parent's own ` .db ` files. For a long-lived parent
88+ that shares the directory, prefer ` clean_dead_pids=True ` over
89+ ` clean_directory=True ` -- it never deletes a live process's file regardless of
90+ ordering.
91+
7192## Selecting Canonical Metrics
7293
7394Set ` WORKER_CANONICAL_METRICS ` before the worker starts:
@@ -290,7 +311,13 @@ sum(rate(task_execute_time_seconds_count[5m])) by (taskType)
290311 implementations never mixes stale metric names.
291312- Pass ` clean_dead_pids=True ` to ` MetricsSettings ` to remove ` .db ` files from
292313 PIDs that no longer exist. Use ` clean_directory=True ` only when you are sure
293- no other live process shares the same directory.
314+ no other live process shares the same directory. Cleanup is idempotent per
315+ process and runs in the parent (via ` clean_metrics_directory() ` , which
316+ ` TaskHandler.__init__ ` calls) before workers spawn; workers never wipe the
317+ directory, so restarts and newly spawned workers preserve sibling metrics. If
318+ the parent also collects, build its collector with
319+ ` create_metrics_collector_for_parent() ` (cleans up front, then creates) so it
320+ doesn't orphan the parent's own files.
294321- Restart workers after changing ` WORKER_CANONICAL_METRICS ` .
295322
296323### High Cardinality
@@ -342,8 +369,15 @@ unreleased metrics harmonization work. For a summary, see the project
342369 across all processes (main, workers, MetricsProvider).
343370 - ` MetricsSettings ` gains ` clean_directory ` (default ` False ` ) to wipe all
344371 ` .db ` files and ` clean_dead_pids ` (default ` False ` ) to remove only ` .db `
345- files from PIDs that no longer exist. Both are executed by the factory
346- against the resolved metrics directory.
372+ files from PIDs that no longer exist. Cleanup is applied by
373+ ` MetricsSettings.clean_metrics_directory() ` , which is idempotent per process
374+ and invoked by the parent (` TaskHandler.__init__ ` calls it, or a
375+ parent that also collects builds its collector via
376+ ` create_metrics_collector_for_parent() ` , which cleans up front then creates)
377+ before workers spawn.
378+ ` create_metrics_collector ` (the per-worker path) is non-destructive and
379+ only ensures the directory exists, so spawned/restarted workers never wipe
380+ live sibling metrics.
347381 - ` CONDUCTOR_MP_START_METHOD ` env var (` spawn ` / ` fork ` / ` forkserver ` ;
348382 default ` fork ` on POSIX, ` spawn ` on Windows) to control the worker pool's
349383 multiprocessing start method (motivated by a ` prometheus_client ` lock-fork
0 commit comments