apache/beam · error

namespace and name are required to be non-empty, got %q and

Error message

namespace and name are required to be non-empty, got %q and %q

What it means

newName constructs the namespace/name key for a Beam user metric (counter, distribution, gauge). Both parts must be non-empty; otherwise the metric key would be malformed or collide, so newName panics. It is invoked by NewCounter, NewDistribution, and NewGauge.

Source

Thrown at sdks/go/pkg/beam/core/metrics/metrics.go:245

	case kindDoFnMsec:
		return "DoFnMsec"
	default:
		panic(fmt.Sprintf("Unknown metric type value: %v", uint8(t)))
	}
}

// name is a pair of strings identifying a specific metric.
type name struct {
	namespace, name string
}

func (n name) String() string {
	return fmt.Sprintf("%s.%s", n.namespace, n.name)
}

func newName(ns, n string) name {
	if len(n) == 0 || len(ns) == 0 {
		panic(fmt.Sprintf("namespace and name are required to be non-empty, got %q and %q", ns, n))
	}
	return name{namespace: ns, name: n}
}

// We hash the name to a uint64 so we avoid using go's native string hashing for
// every use of a metrics. uint64s have faster lookup than strings as a result.
// Collisions are possible, but statistically unlikely as namespaces and names
// are usually short enough to avoid this. A sync.Pool is used  because it can provide
// goroutine-local values that reduce contention and profiling shows hashName from NewCounter
// can be a contention hotspot. See parallel benches metrics_test.go:BenchmarkMetrics/*
var (
	hashPool = sync.Pool{
		New: func() interface{} {
			return fnv.New64a()
		},
	}
)

View on GitHub (pinned to 12126d8942)

Solutions

  1. Pass a non-empty namespace and a non-empty metric name to the metric constructor.
  2. Validate/trim the inputs before constructing the metric and fail early with a clear message.
  3. Fix the source of the empty string (missing config key, unset env var, wrong constant).

Example fix

// before
c := metrics.NewCounter(appNs, metricName) // panics if either is ""
// after
if appNs == "" || metricName == "" {
	log.Fatalf("metric namespace and name must be non-empty: ns=%q name=%q", appNs, metricName)
}
c := metrics.NewCounter(appNs, metricName)
Defensive patterns

Strategy: validation

Validate before calling

if ns == "" || name == "" {
	return fmt.Errorf("metric ns=%q name=%q must be non-empty", ns, name)
}
c := metrics.NewCounter(ns, name)

Prevention

When it happens

Trigger: Calling metrics.NewCounter(ns, ""), NewCounter("", name), or any of the metric constructors with an empty namespace or empty name string.

Common situations: Metric names pulled from constants/config/env where a variable is empty (missing env var, empty config field, typo'd constant), or building names dynamically with fmt.Sprintf that produced an empty component.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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