beam icon indicating copy to clipboard operation
beam copied to clipboard

[Bug]: [Python/Dataflow] DataflowMetrics._populate_metrics raises KeyError when only tentative or committed metric is present

Open jk-shah opened this issue 3 weeks ago • 1 comments

What happened?

Calling DataflowMetrics.query() (for example while polling a running Dataflow job where only tentative='true' metrics have been reported so far, or querying a completed job / metric entry where only the non-tentative committed entry is present in get_job_metrics) raises KeyError: 'committed' or KeyError: 'tentative'.

Minimal reproducer:

from types import SimpleNamespace
from apache_beam.runners.dataflow.dataflow_metrics import DataflowMetrics

response = SimpleNamespace(metrics=[
    SimpleNamespace(
        name=SimpleNamespace(
            origin='user',
            name='rows_read',
            context={'namespace': 'my_ns', 'tentative': 'true'},
        ),
        scalar=42,
        distribution=None,
    )
])

dm = DataflowMetrics()
out = []
dm._populate_metrics(response, out, user_metrics=True)
# KeyError: 'committed'



##Cause

In sdks/python/apache_beam/runners/dataflow/dataflow_metrics.py (lines 187–196), _get_metric_value explicitly handles None, but _populate_metrics indexes metric['tentative'] and metric['committed'] directly instead of using .get(...):

```python
    for metric_key, metric in metrics_by_name.items():
      attempted = self._get_metric_value(metric['tentative'])
      committed = self._get_metric_value(metric['committed'])
      result.append(
          MetricResult(metric_key, attempted=attempted, committed=committed))

Replacing metric['tentative'] / metric['committed'] with metric.get('tentative') / metric.get('committed') resolves the crash cleanly.



### Issue Priority

Priority: 2 (default / most bugs should be filed as P2)

### Issue Components

- [x] Component: Python SDK
- [ ] Component: Java SDK
- [ ] Component: Go SDK
- [ ] Component: Typescript SDK
- [ ] Component: IO connector
- [ ] Component: Beam YAML
- [ ] Component: Beam examples
- [ ] Component: Beam playground
- [ ] Component: Beam katas
- [ ] Component: Website
- [ ] Component: Infrastructure
- [ ] Component: Spark Runner
- [ ] Component: Flink Runner
- [ ] Component: Prism Runner
- [ ] Component: Hazelcast Jet Runner
- [x] Component: Google Cloud Dataflow Runner

jk-shah avatar Nov 08 '26 06:11 jk-shah

.take-issue

09Catho avatar Nov 08 '26 12:11 09Catho