{"record":{"id":"4f84a23f593c452e","repo":"apache/beam","slug":"more-than-one-metric-result-matches-name-name-in-namespace","errorCode":null,"errorMessage":"More than one metric result matches name: {name} in namespace {self._namespace}. Metric results count: {num_results}","messagePattern":"More than one metric result matches name: (.+?) in namespace (.+?)\\. Metric results count: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/testing/load_tests/load_test_metrics_utils.py","lineNumber":242,"sourceCode":"      # publish to InfluxDB\n      self.publishers.append(InfluxDBMetricsPublisher(influxdb_options))\n    else:\n      _LOGGER.info(\n          'Missing InfluxDB options. Metrics will not be published to '\n          'InfluxDB')\n    self.filters = filters\n\n  def get_counter_metric(self, result: PipelineResult, name: str) -> int:\n    \"\"\"\n    Return the current value for a long counter, or -1 if can't be retrieved.\n    Note this uses only attempted metrics because some runners don't support\n    committed metrics.\n    \"\"\"\n    filters = MetricsFilter().with_namespace(self._namespace).with_name(name)\n    counters = result.metrics().query(filters)[MetricResults.COUNTERS]\n    num_results = len(counters)\n    if num_results > 1:\n      raise ValueError(\n          f\"More than one metric result matches name: {name} in namespace \"\\\n          f\"{self._namespace}. Metric results count: {num_results}\")\n    elif num_results == 0:\n      return -1\n    else:\n      return counters[0].attempted\n\n  def publish_metrics(\n      self, result: PipelineResult, extra_metrics: Optional[dict] = None):\n    \"\"\"Publish metrics from pipeline result to registered publishers.\"\"\"\n    metric_id = uuid.uuid4().hex\n    metrics = result.metrics().query(self.filters)\n\n    # Metrics from pipeline result are stored in map with keys: 'gauges',\n    # 'distributions' and 'counters'.\n    # Under each key there is list of objects of each metric type. It is\n    # required to prepare metrics for publishing purposes. Expected is to have\n    # a list of dictionaries matching the schema.","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/testing/load_tests/load_test_metrics_utils.py#L224-L260","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Give each counter a unique name (and/or namespace) via Metrics.counter(namespace, unique_name)","If multiple results are legitimate, query result.metrics() directly and aggregate the counters yourself instead of using get_counter_metric","Search the pipeline code for duplicate Metrics.counter registrations with the same namespace+name"],"exampleFix":"# before\n# DoFn A and DoFn B both do:\nelements_counter = Metrics.counter('load_job', 'num_elements')\n# after\n# DoFn A:\nelements_counter = Metrics.counter('load_job', 'num_elements_a')\n# DoFn B:\nelements_counter = Metrics.counter('load_job', 'num_elements_b')","handlingStrategy":"validation","validationCode":"counters = result.metrics().query(MetricsFilter().with_namespace(ns).with_name(name))[MetricResults.COUNTERS]\nif len(counters) > 1:\n    raise ValueError(f'duplicate metric name {ns}/{name}; rename counters before reading')","typeGuard":null,"tryCatchPattern":"try:\n    value = reader.get_counter_metric(name)\nexcept ValueError as e:\n    if 'More than one metric result matches name' in str(e):\n        logging.error('Counter name is ambiguous; use unique names or query metrics directly.')\n        value = None\n    else:\n        raise","preventionTips":["Keep counter (namespace, name) pairs unique across the whole pipeline","Grep pipeline code for duplicate Metrics.counter registrations before running load tests","Aggregate duplicate counters yourself via result.metrics().query() if duplication is intentional"],"tags":["python","apache-beam","metrics","load-testing","ambiguity"],"backgroundTag":"unexpected-response-shape","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}