{"record":{"id":"c6b93463fc5c8f25","repo":"apache/seatunnel","slug":"name-collision-metricscontext-already-contains-a","errorCode":null,"errorMessage":"Name collision: MetricsContext already contains a Metric with the name '{}'. Metric will not be reported.","messagePattern":"Name collision: MetricsContext already contains a Metric with the name '(.+?)'\\. Metric will not be reported\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"seatunnel-api/src/main/java/org/apache/seatunnel/api/common/metrics/AbstractMetricsContext.java","lineNumber":69,"sourceCode":"        }\n        return this.meter(name, new ThreadSafeQPSMeter(name));\n    }\n\n    @Override\n    public <M extends Meter> M meter(String name, M meter) {\n        this.addMetric(name, meter);\n        return meter;\n    }\n\n    protected void addMetric(String name, Metric metric) {\n        if (metric == null) {\n            log.warn(\"Ignoring attempted add of a metric due to being null for name {}.\", name);\n        } else {\n            synchronized (this) {\n                Metric prior = this.metrics.put(name, metric);\n                if (prior != null) {\n                    this.metrics.put(name, prior);\n                    log.warn(\n                            \"Name collision: MetricsContext already contains a Metric with the name '\"\n                                    + name\n                                    + \"'. Metric will not be reported.\");\n                }\n            }\n        }\n    }\n\n    @Override\n    public String toString() {\n        return \"AbstractMetricsContext{\" + \"metrics=\" + metrics + '}';\n    }\n}\n","sourceCodeStart":51,"sourceCodeEnd":83,"githubUrl":"https://github.com/apache/seatunnel/blob/cf67b549a7a6c35fa0beb12d83c62892427ea919/seatunnel-api/src/main/java/org/apache/seatunnel/api/common/metrics/AbstractMetricsContext.java#L51-L83","documentation":"AbstractMetricsContext.addMetric detects a name collision when putting a Metric into the metrics map: if a Metric already exists under that name, the original metric is restored and the new one is discarded with a warning. Duplicate metric names mean one of the two metrics will never be reported.","triggerScenarios":"Calling counter(name)/meter(name)/addMetric(name, metric) twice with the same metric name within the same MetricsContext, e.g. re-registering metrics on task retry or across overlapping component lifecycles.","commonSituations":"Source/sink or transform plugins registering metrics with hard-coded names that collide across subtasks; checkpoint restart re-invoking initialization code; copy-pasted metric names within one pipeline.","solutions":["Uniquify metric names by including subtask/parallelism/table identifiers, e.g. name + \"#\" + subtaskIndex.","Guard registration with a check that the name is not already present before calling counter()/meter().","Move metric registration to one-time initialization (open/init) rather than per-record or per-retry paths.","If re-registration is intended, remove the old metric first."],"exampleFix":"// before\ncontext.counter(\"records_received\"); // collides on retry\ncontext.counter(\"records_received\");\n// after\nString name = \"records_received#\" + context.getIndexOfSubtask();\nif (!metricsContext.contains(name)) {\n    context.counter(name);\n}","handlingStrategy":"validation","validationCode":"Set<String> seen = new HashSet<>();\nif (!seen.add(metricName)) throw new IllegalStateException(\"duplicate metric name: \" + metricName);","typeGuard":"if (existingMetrics.containsKey(metricName)) { metricName = metricName + \"#\" + subtaskIndex; }","tryCatchPattern":"// warning-only path; prevent duplicates rather than catching","preventionTips":["Namespace metric names with subtask index / table / plugin id","Register metrics once in open()/init()","Grep plugin code for hard-coded metric name strings before reuse","Review metric names during checkpoint/retry code paths"],"tags":["metrics","naming-collision","observability"],"backgroundTag":"conflicting-config-options","analyzedSha":"cf67b549a7a6c35fa0beb12d83c62892427ea919","analyzedAt":"2026-09-10T21:44:55.265Z","contentChangedAt":"2026-09-10T21:44:55.265Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}