apache/druid · error · IllegalStateException

Value of NaN is not allowed!

Error message

Value of NaN is not allowed!

What it means

ServiceMetricEvent.Builder.setMetric rejects NaN values with ISE. Metric values must be real finite numbers because NaN cannot be serialized/aggregated meaningfully by metrics consumers (e.g. graphite, statsd, dimensional store). Throwing keeps NaN out of the metrics pipeline.

Solutions

  1. Fix the computation producing NaN (guard against division by zero / empty inputs).
  2. Sanitize before emitting: if (Double.isNaN(v) || Double.isInfinite(v)) skip or substitute 0/null policy value.
  3. Emit an explicit absence (don't call setMetric) and log a warning so missing data is distinguishable from zero.
  4. Check monitor code ordering — a metric computed before its inputs exist often yields NaN.

Example fix

// before
double avg = total / count; // count == 0 -> NaN
builder.setMetric("task/avg/time", avg); // ISE
// after
double avg = count > 0 ? total / count : 0.0;
if (!Double.isNaN(avg) && !Double.isInfinite(avg)) {
  builder.setMetric("task/avg/time", avg);
}
Defensive patterns

Strategy: validation

Validate before calling

double v = metricValue.doubleValue();
if (Double.isNaN(v)) { return; /* or substitute policy value */ }
builder.setMetric(metricName, metricValue);

Try / catch

try {
  builder.setMetric(name, value);
} catch (IllegalStateException e) {
  log.warn("Skipping non-finite metric [%s]", name);
}

Prevention

When it happens

Trigger: Calling setMetric(name, value) where value.doubleValue() is NaN — typically from a 0.0/0.0 division, sqrt of a negative, or parsing a non-numeric string into a double.

Common situations: Custom Druid monitors computing ratios/averages over empty collections; emitting timing metrics when no samples were taken; ingestion stats divided by zero rows.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of apache/druid@9b90983fd2 (2026-09-07). Data as JSON: /api/errors/9b979f70c241daf4. Report an issue: GitHub.

Appendix: source

Thrown at processing/src/main/java/org/apache/druid/java/util/emitter/service/ServiceMetricEvent.java:195

      if (dim == null) {
        throw new IAE("Dimension name cannot be null");
      } else if (value == null) {
        throw new IAE("Value of dimension[%s] cannot be null", dim);
      }

      userDims.put(dim, value);
      return this;
    }

    public Object getDimension(String dim)
    {
      return userDims.get(dim);
    }

    public Builder setMetric(String metric, Number value)
    {
      if (Double.isNaN(value.doubleValue())) {
        throw new ISE("Value of NaN is not allowed!");
      }
      if (Double.isInfinite(value.doubleValue())) {
        throw new ISE("Value of Infinite is not allowed!");
      }

      this.metric = metric;
      this.value = value;
      return this;
    }

    public Builder setCreatedTime(DateTime createdTime)
    {
      this.createdTime = createdTime;
      return this;
    }

    @Override
    public ServiceMetricEvent build(ImmutableMap<String, String> serviceDimensions)

View on GitHub (pinned to 9b90983fd2)