How to define custom metrics #69096
-
|
Is there any recommended way to define and emit custom metrics in Airflow? For example using custom stats like: from airflow.sdk.observability import stats
stats.gauge("my_service.queue_depth", 42)
# ...and similarly stats.incr(...) / stats.decr(...) / stats.timing(...) / stats.timer(...)If this is the case, a dedicated "Custom Metrics" section in |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Yeah, that's basically the supported way. In Airflow 3 the metrics client moved to the task SDK, and the import you wrote is the right one: from airflow.sdk.observability import stats
stats.gauge("my_service.queue_depth", 42)
stats.incr("my_service.processed")
stats.decr("my_service.in_flight")
stats.timing("my_service.batch_ms", 1234) # ms or a timedelta
with stats.timer("my_service.batch"): # times the block
...
The one gotcha that trips people up: these calls are no-ops unless you actually have a metrics backend turned on. Out of the box nothing is emitting, so your gauge goes nowhere and it looks broken. Turn one on in config: [metrics]
statsd_on = True
statsd_host = localhost
statsd_port = 8125
statsd_prefix = airflowor the OpenTelemetry equivalent with Also watch Agree a dedicated "Custom Metrics" section in metrics.rst would help, this comes up a lot. |
Beta Was this translation helpful? Give feedback.
Yeah, that's basically the supported way. In Airflow 3 the metrics client moved to the task SDK, and the import you wrote is the right one:
from airflow.stats import Statsstill works but it's deprecated now (it just warns and forwards to the same place), so theairflow.sdk.observabilitypath is the one to use going forward.The one gotcha that trips people up: these calls are no-ops unless you ac…