{"record":{"id":"57a363d05b4bfb23","repo":"apache/seatunnel","slug":"ignoring-attempted-add-of-a-metric-due-to-being-nu","errorCode":null,"errorMessage":"Ignoring attempted add of a metric due to being null for name {}.","messagePattern":"Ignoring attempted add of a metric due to being null for name (.+?)\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"seatunnel-api/src/main/java/org/apache/seatunnel/api/common/metrics/AbstractMetricsContext.java","lineNumber":63,"sourceCode":"    }\n\n    @Override\n    public Meter meter(String name) {\n        if (metrics.containsKey(name)) {\n            return (Meter) metrics.get(name);\n        }\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    }","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/apache/seatunnel/blob/cf67b549a7a6c35fa0beb12d83c62892427ea919/seatunnel-api/src/main/java/org/apache/seatunnel/api/common/metrics/AbstractMetricsContext.java#L45-L81","documentation":"AbstractMetricsContext.addMetric guards against registering a null Metric instance. A null metric is rejected with a warning and not stored, so the named metric silently disappears from reports. This is a defensive check in the metrics registry: counter(name, ...) or meter(name, ...) was handed a null Metric object.","triggerScenarios":"Calling addMetric(name, null) directly, or counter()/meter() factory methods that return null (e.g. a null-returning metric registry lookup) and then pass the result to addMetric.","commonSituations":"Custom metric registries or third-party metric backends (Prometheus/Dropwizard adapters) returning null for unregistered metrics; plugin code constructing metrics lazily and passing an uninitialized reference.","solutions":["Find where the null Metric is produced and initialize it before registering.","Check that the underlying metric registry/backend is initialized before creating counters/meters.","Guard the factory call: only call addMetric when the metric instance is non-null.","Log at DEBUG which code path returns null and fix the provider."],"exampleFix":"// before\nCounter c = registry.counter(name); // may return null\nmetricsContext.addMetric(name, c);\n// after\nCounter c = registry.counter(name);\nif (c != null) {\n    metricsContext.addMetric(name, c);\n}","handlingStrategy":"type-guard","validationCode":"if (name == null || name.isEmpty()) throw new IllegalArgumentException(\"metric name required\");","typeGuard":"if (metric == null) { LOG.warn(\"skipping null metric {}\", name); return; } metricsContext.addMetric(name, metric);","tryCatchPattern":"// not exception-based; guard inputs before addMetric","preventionTips":["Never let metric factory methods return null; initialize registries first","Null-check metrics before registration","Add unit tests asserting metricsContext.counter(name) returns non-null"],"tags":["metrics","null-check","observability"],"backgroundTag":"null-argument","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"}