Best practices for logging and monitoring PySpark applications
Instruction: Outline the best practices for logging and monitoring in PySpark applications to ensure operational visibility and reliability.
Updated
Instrument the pipeline at both the application and Spark execution layers. Include a run/query ID, source version or offset range, code/configuration version and output version so an incident can be traced without logging sensitive payloads.
Python logging covers Python processes where it is configured; configuring it on the driver does not automatically capture every executor's Python logs or JVM/Log4j messages. Centralize the relevant driver, executor and cluster-manager logs with access control, retention and redaction. Do not log credentials, unrestricted raw rows or one message per record in a high-volume job.
Enable and protect appropriate Spark event logs so the history server can show completed application execution. Use the UI and supported metrics endpoints to inspect stage/task duration distributions, executor loss, retries, shuffle read/write, spill, garbage collection and memory. The metrics integration and permissions must actually be configured; naming Prometheus or a dashboard does not enable it.
Log action completion and committed output, not just transformation construction. Spark is lazy: creating a filtered DataFrame can succeed while the later write fails. Track accepted/rejected counts and freshness against source expectations, and make incomplete publication visible.
For streaming, monitor input/processed rates, batch duration, source lag, state-store growth, watermark progress, late/dropped data and failed queries. Choose bounded metric labels to avoid per-user/event cardinality explosions. Business usage metrics may complement these signals but do not replace pipeline-health measures.
Define actionable alerts with owners and runbooks: sustained backlog, missed completion/freshness target, repeated failures, excessive quarantine or unusual output volume. Test alerts and recovery, and tune verbosity from measured overhead. Aggregate diagnostic counters with documented retry semantics; task retries can make naive per-attempt counts differ from logical event counts.
Spark monitoring, Structured Streaming monitoring, Event-log security