Repository navigation
fix: ADDON-91243 change telemetry flush to async - #467
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
| return | ||
| try: | ||
| self._provider.force_flush(timeout_millis=int(timeout_millis)) | ||
| self._flush_controller.request_flush(timeout_millis) |
There was a problem hiding this comment.
flush() no longer delivers metrics from multiprocessing workers on Python 3.9
flush() now only queues the export on a daemon thread and relies on the atexit handler to drain it. On Python 3.9 (the Splunk 9.x runtime), a multiprocessing child started with fork (the Linux default) exits via os._exit() without running atexit. The worker is killed before it exports. The same applies to any explicit os._exit() after flush().
This is a regression: on develop, flush() / StanzaObservabilityRecorder.__exit__ exported synchronously, so these metrics were delivered. Now they are silently dropped, with nothing in the logs.
Repro (Python 3.9): prints EXPORTED on develop, nothing on this branch
import logging, multiprocessing as mp, time
from opentelemetry.sdk.metrics.export import MetricExporter, MetricExportResult
from solnlib.observability import ObservabilityService
ObservabilityService._create_otlp_exporter = lambda self: None
class SlowExporter(MetricExporter): # ~200 ms, like a real gRPC round-trip
def export(self, metrics_data, timeout_millis=10_000, **kwargs):
time.sleep(0.2)
print("EXPORTED", flush=True)
return MetricExportResult.SUCCESS
def shutdown(self, timeout_millis=30_000, **kwargs): pass
def force_flush(self, timeout_millis=10_000): return True
def worker():
svc = ObservabilityService("t", logging.getLogger("t"), ta_name="ta",
ta_version="1", extra_exporters=[SlowExporter()])
svc.event_count_counter.add(5)
svc.flush() # same call StanzaObservabilityRecorder.__exit__ makes
if __name__ == "__main__":
mp.set_start_method("fork")
p = mp.Process(target=worker)
p.start()
p.join()Suggestion: keep a synchronous option for callers that need delivery, e.g. flush(wait=True) or flush_sync(timeout_millis), which blocks until the worker finishes that flush. Also document that the default async flush() gives no delivery guarantee under os._exit() or in forked multiprocessing workers.
There was a problem hiding this comment.
Confirmed — I reproduced the loss of pending metrics when a forked multiprocessing worker exits before the background export completes.
We assess the operational risk as low for the current change: the demonstrated impact is limited to pending telemetry at worker exit. Short-lived workers can lose their entire batch, so this limitation should be documented.
We propose addressing multiprocessing shutdown support in a separate Story: ADDON-91546. It covers bounded shutdown waiting and regression tests on Python 3.9 and 3.13.
| controller.join_until(deadline) | ||
|
|
||
| def _after_fork_child(self) -> None: | ||
| self._lock = threading.Lock() |
There was a problem hiding this comment.
_registered survives fork, so services created in a child never get the exit handler
_after_fork_child resets the lock and the workers, but not _registered. On newer Pythons (verified on 3.13), multiprocessing calls atexit._clear() in a forked child. The inherited _shutdown_all registration is gone, but _registered is still True. Any ObservabilityService created in the child then skips atexit.register(...), is never shut down, and its pending metrics are lost.
This happens whenever the parent created a service before forking, e.g. when the parent records its own metrics.
Repro (Python 3.13): develop prints EXPORTED twice, this branch prints nothing
import logging, multiprocessing as mp, time
from opentelemetry.sdk.metrics.export import MetricExporter, MetricExportResult
from solnlib.observability import ObservabilityService
ObservabilityService._create_otlp_exporter = lambda self: None
class SlowExporter(MetricExporter):
def export(self, metrics_data, timeout_millis=10_000, **kwargs):
time.sleep(0.2)
print("EXPORTED", flush=True)
return MetricExportResult.SUCCESS
def shutdown(self, timeout_millis=30_000, **kwargs): pass
def force_flush(self, timeout_millis=10_000): return True
def worker():
svc = ObservabilityService("child", logging.getLogger("t"), ta_name="ta",
ta_version="1", extra_exporters=[SlowExporter()])
svc.event_count_counter.add(5)
svc.flush()
if __name__ == "__main__":
mp.set_start_method("fork")
ObservabilityService("parent", logging.getLogger("t"), ta_name="ta", ta_version="1")
p = mp.Process(target=worker)
p.start()
p.join()Fix:
| self._lock = threading.Lock() | |
| self._lock = threading.Lock() | |
| self._registered = False |
Re-registering is also safe after a plain os.fork(), where atexit is inherited: the second shutdown_all() call returns early on _closed.
There was a problem hiding this comment.
Confirmed — _registered can remain True after multiprocessing clears the inherited atexit handlers. Resetting it fixes registration when a new service is created in the child, but children using only an inherited service also need handling.
We assess the operational risk as low, with the observed impact limited to pending telemetry during process shutdown. We propose handling this together with the Python 3.9 shutdown issue in ADDON-91546, so the follow-up covers both inherited and newly created services.
Why
StanzaObservabilityRecorderpreviously called OpenTelemetryforce_flush()on the modular input’s thread. A slow or unavailable OTLP collector could therefore delay TA processing whenever the recorder exited itswithblock.What changed
Compatibility and trade-offs
The public
flush()signatures are unchanged, but returning now means the export was scheduled, not completed.timeout_millislimits the background attempt; it is not a caller-side wait. The last metrics may be lost if the exit grace period expires.Fork handling remains limited: the child gets a new worker, but an OTel provider inherited during an in-flight export may retain a locked reader. Recreating the provider after
fork()is outside this PR.Testing