apache/beam · error · ValueError

More than one metric result matches name: {name} in namespac

Error message

More than one metric result matches name: {name} in namespace {self._namespace}. Metric results count: {num_results}

What it means

MetricsReader.get_counter_metric queries the pipeline's MetricsFilter for a counter by name and namespace and expects at most one matching counter. If the query returns more than one counter result, it raises ValueError because it cannot disambiguate which metric is intended.

Source

Thrown at sdks/python/apache_beam/testing/load_tests/load_test_metrics_utils.py:242

      # publish to InfluxDB
      self.publishers.append(InfluxDBMetricsPublisher(influxdb_options))
    else:
      _LOGGER.info(
          'Missing InfluxDB options. Metrics will not be published to '
          'InfluxDB')
    self.filters = filters

  def get_counter_metric(self, result: PipelineResult, name: str) -> int:
    """
    Return the current value for a long counter, or -1 if can't be retrieved.
    Note this uses only attempted metrics because some runners don't support
    committed metrics.
    """
    filters = MetricsFilter().with_namespace(self._namespace).with_name(name)
    counters = result.metrics().query(filters)[MetricResults.COUNTERS]
    num_results = len(counters)
    if num_results > 1:
      raise ValueError(
          f"More than one metric result matches name: {name} in namespace "\
          f"{self._namespace}. Metric results count: {num_results}")
    elif num_results == 0:
      return -1
    else:
      return counters[0].attempted

  def publish_metrics(
      self, result: PipelineResult, extra_metrics: Optional[dict] = None):
    """Publish metrics from pipeline result to registered publishers."""
    metric_id = uuid.uuid4().hex
    metrics = result.metrics().query(self.filters)

    # Metrics from pipeline result are stored in map with keys: 'gauges',
    # 'distributions' and 'counters'.
    # Under each key there is list of objects of each metric type. It is
    # required to prepare metrics for publishing purposes. Expected is to have
    # a list of dictionaries matching the schema.

View on GitHub (pinned to 12126d8942)

Solutions

  1. Give each counter a unique name (and/or namespace) via Metrics.counter(namespace, unique_name)
  2. If multiple results are legitimate, query result.metrics() directly and aggregate the counters yourself instead of using get_counter_metric
  3. Search the pipeline code for duplicate Metrics.counter registrations with the same namespace+name

Example fix

# before
# DoFn A and DoFn B both do:
elements_counter = Metrics.counter('load_job', 'num_elements')
# after
# DoFn A:
elements_counter = Metrics.counter('load_job', 'num_elements_a')
# DoFn B:
elements_counter = Metrics.counter('load_job', 'num_elements_b')
Defensive patterns

Strategy: validation

Validate before calling

counters = result.metrics().query(MetricsFilter().with_namespace(ns).with_name(name))[MetricResults.COUNTERS]
if len(counters) > 1:
    raise ValueError(f'duplicate metric name {ns}/{name}; rename counters before reading')

Try / catch

try:
    value = reader.get_counter_metric(name)
except ValueError as e:
    if 'More than one metric result matches name' in str(e):
        logging.error('Counter name is ambiguous; use unique names or query metrics directly.')
        value = None
    else:
        raise

Prevention

When it happens

Trigger: Registering multiple Metrics.counter metrics with the same name and namespace in one pipeline (e.g., same counter name used in different DoFns under the same namespace), then calling get_counter_metric on the pipeline result.

Common situations: Reusing a metric name across transforms/DoFns; creating counters in a loop without unique names; copy-pasted metric name in multiple steps of the same pipeline while relying on namespace uniqueness.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/4f84a23f593c452e. Report an issue: GitHub.