Repository navigation
Conversation
|
@stuart23 I know you've been looking at similar issues |
|
Also @dstandish, worth you looking at this. |
dstandish
left a comment
There was a problem hiding this comment.
i'm not sure about adding all the complexity to forward all metrics through the api server. adding all the extra calls would also increase load on api server.
we already log an event whenever we increment these counters so, we could just move the stats emission to the api server when those events are handled and this would be much simpler and lighter weight.
the cost is users would not have custom metrics -- though, perhaps that's just -- if they do it they will have to figure out how wo make it work properly in backend processing (which is possible)
another approach is use a daemon process to handle the metrics emission, either launched at worker startup or run in a sidecar container.
e5a30bd to
e9014cf
Compare
|
Thanks for digging into this. The 1, 1, 1, 1, 1 counter problem is real and worth fixing. I'm hesitant about the approach, though, because it adds a lot of moving parts:
Before we commit to maintaining all of that, I'd like to check whether a simpler fix covers most of the need. Most task metrics could be emitted by the API server instead These are the first-party metrics emitted from
Each of them corresponds to a state transition the API server already handles: It would also make the metrics more accurate. A task that gets OOM-killed or SIGKILLed never emits The remaining case is narrow With lifecycle metrics on the server side, the only thing left broken is OTel combined with custom metrics emitted from inside a task:
For OTel users who do this, delta temporality ( Suggestion
The trade-off is that the emitting component changes, so dashboards that filter on worker host or service tags would need updating. That would need a newsfragment. Still, it seems much cheaper than a permanent new Execution API surface. What do you think? Are there use cases I'm missing that need worker-side emission? Drafted-by: Claude Code (Opus 5.5); reviewed by @dstandish before posting |
|
The current approach adds too much complexity and so I converted this to a draft while I'm exploring some alternatives. |
50299de to
179b8cb
Compare
334f2b7 to
1667a99
Compare
0b5a0db to
7536fe3
Compare
| # either raised above or was a duplicate request from a running task. | ||
| if previous_state == TaskInstanceState.QUEUED: | ||
| dr._team_name = dr.team_name | ||
| stats.incr("ti.start", tags={**dr.stats_tags, "task_id": ti.task_id}) |
There was a problem hiding this comment.
personally i am of the belief that we should remove dag_id and task_id as attrs. too high cardinality if api server configured with delta to cumulative -- you're likely to get non-increasing series anyway. you should also see what else is in stats_tags.
There was a problem hiding this comment.
I agree but I thought we discussed leaving them as they are for now because users might prefer to filter them in their backend.
So dr.stats_tags are
- always:
dag_id,run_type - sometimes:
team_nameand one tag per Dag tag if enabled in the config
If [metrics] dag_tags_in_metrics is enabled, then tags such as tags=["team:data", "env:prod", "critical"] become in the metrics team=data, env=prod, and critical with an empty value.
ti.stats_tags are the same as dr.stats_tags with some additions
- always:
dag_id,task_id,run_type - sometimes:
team_name,state(on ti.finish),operator_name(on operator counters) and Dag tags if enabled
| @@ -787,6 +803,26 @@ def _build_rejected_gate_payload( | |||
| return TITerminalStatePayload(state=TerminalStateNonSuccess.FAILED, retry_reason=reason, **carried) | |||
|
|
|||
|
|
|||
| def _emit_task_finish_metrics( | |||
There was a problem hiding this comment.
i'm not sure we need all of these attributes. and they can cause problems.
even on api server, you are relying on the same worker getting hit with the same event type for the same team / operator / task / dag before the series gets expired (if delta to cumulative) or before the worker gets cycled (if cumulative), aren't you? otherwise you'll get non-incrementing series.
There was a problem hiding this comment.
These aren't new. These are the old ones that have been moved.
Statsd has an option to disable tags
statsd_disabled_tags:
description: |
If you want to avoid sending all the available metrics tags to StatsD,
you can configure a block list of prefixes (comma separated) to filter out metric tags
that start with the elements of the list (e.g: ``job_id,run_id``)
version_added: 2.6.0
type: string
example: job_id,run_id,dag_id,task_id
default: job_id,run_id
what if we do the same for OTel? That way it's up to the users whether they want the cardinality or not.
| @@ -481,7 +485,11 @@ def get_otel_logger( | |||
| otel_env_config = load_metrics_env_config() | |||
|
|
|||
| effective_service_name: str = otel_env_config.service_name or service_name or "airflow" | |||
| resource = Resource.create(attributes={SERVICE_NAME: effective_service_name}) | |||
| resource_attributes = {SERVICE_NAME: effective_service_name} | |||
| if service_instance_id: | |||
There was a problem hiding this comment.
why not leave it to optional env var configuration to preserve the flexibility ordinarily provided to users?
There was a problem hiding this comment.
It is, but it's decided at the caller. The caller checks the environment and if the user has provided a value, then this is None. If there isn't a value, this gets a fixed one which is then set here.
| def get_otel_logger(short_lived: bool = False) -> SafeOtelLogger: | ||
| service_instance_id = None | ||
| temporality_preference_env = "OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE" | ||
|
|
||
| if short_lived: | ||
| # The temporality env variable defaults to `CUMULATIVE` if unset. | ||
| # If the process is `short_lived`, then set the default fallback to `DELTA`. | ||
| os.environ.setdefault(temporality_preference_env, "DELTA") |
There was a problem hiding this comment.
i feel we should still be leaving this to the user. the user has env vars they can use for this.
There was a problem hiding this comment.
This is just setting the default. If a user has provided a value, then the user value wins
dstandish
left a comment
There was a problem hiding this comment.
reviewed and left comments.
a32764d to
abf209b
Compare
| # LOWMEMORY also exports counters and histograms as deltas. | ||
| if ( | ||
| os.environ[temporality_preference_env].strip().upper() in ("DELTA", "LOWMEMORY") | ||
| and SERVICE_INSTANCE_ID not in load_metrics_env_config().resource_attributes |
There was a problem hiding this comment.
@dstandish See here, if the service instance id isn't provided by the user, then we set a fixed value.
0fc78fd to
e4783f3
Compare
|
@dstandish Thank you for the review! I've addressed your comments and made the changes. Please take a look and let me know how it looks. I've rewritten the documentation changes. |
e4783f3 to
145a60a
Compare
145a60a to
01301e8
Compare
01301e8 to
fd451c1
Compare
fd451c1 to
212f672
Compare
212f672 to
8cd17d3
Compare
Issue
When using OpenTelemetry, every metric exported from the task-sdk while a task is running (including custom user-defined metrics) is broken. Note here that this issue doesn't exist for StatsD.
For every task, a new task runner is created along with a new
MeterProvider. As a result, each series of metrics is treated as new and unique and values don't accumulate.For example, if we had 5 successful tasks, we would expect to see a value 5 for the counter
airflow_ti_successes_total. Instead we end up with a series of 1, 1, 1, 1, 1.Solution
These metrics
ti.startti.finishti_successesandti_failuresoperator_successesandoperator_failurestask.durationare the most important and they have been moved under the API server.
There have been some other small changes for allowing the rest of the task-sdk metrics to continue working along with any user-defined custom metrics.
The temporality environment variable defaults to
cumulativeif unset. For thetask-sdk, now it defaults todeltaif unset.Also, the OTel SDK assigns a random and unique
service.instance.idper process. Just for thetask-sdk, this is set to a fixed value so that all task runners are treated as the same process.For backends that support delta values natively, no config changes are needed. But for backends that don't, such as prometheus, the user will need to extend the
otel-collector.ymlfile so that it converts the delta values back to cumulative. The required config changes have been added to the documentation. They are simple and they could be just copy-pasted.Testing
I've added an integration test that reads the metrics directly from the otel-collector and checks the value of task metrics to make sure that a counter is increased.
I've also added unit tests for the in-memory accumulating logic as well as the forwarding.
Was generative AI tooling used to co-author this PR?
Claude-code Fable 5.1 xhigh
{pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.