redis/go-redis · error

failed to create connection count metric: %w

Error message

failed to create connection count metric: %w

What it means

Returned by createRecorder when meter.Int64UpDownCounter fails to create the db.client.connection.count UpDownCounter (dbconv.ClientConnectionCount). Init aborts with 'failed to create metrics recorder', leaving observability uninitialized. This only happens when the underlying MeterProvider errors on instrument registration.

Source

Thrown at extra/redisotel-native/redisotel.go:211

		if err != nil {
			return nil, fmt.Errorf("failed to create operation duration histogram: %w", err)
		}
		operationDuration = operationDurationConv.Inst()
	}

	var connectionCount metric.Int64UpDownCounter // OTel semconv: UpDownCounter
	var connectionCreateTime metric.Float64Histogram
	var connectionRelaxedTimeout metric.Int64UpDownCounter
	var connectionHandoff metric.Int64Counter

	if cfg.isMetricGroupEnabled(MetricGroupConnectionBasic) {
		connectionCount, err = meter.Int64UpDownCounter(
			dbconv.ClientConnectionCount{}.Name(),
			metric.WithDescription(dbconv.ClientConnectionCount{}.Description()),
			metric.WithUnit(dbconv.ClientConnectionCount{}.Unit()),
		)
		if err != nil {
			return nil, fmt.Errorf("failed to create connection count metric: %w", err)
		}

		var connectionCreateTimeOpts []metric.Float64HistogramOption
		if cfg.histAggregation == HistogramAggregationExplicitBucket {
			connectionCreateTimeOpts = append(connectionCreateTimeOpts,
				metric.WithExplicitBucketBoundaries(cfg.bucketsConnectionCreateTime...),
			)
		}
		var connectionCreateTimeConv dbconv.ClientConnectionCreateTime
		connectionCreateTimeConv, err = dbconv.NewClientConnectionCreateTime(meter, connectionCreateTimeOpts...)
		if err != nil {
			return nil, fmt.Errorf("failed to create connection create time histogram: %w", err)
		}
		connectionCreateTime = connectionCreateTimeConv.Inst()

		connectionRelaxedTimeout, err = meter.Int64UpDownCounter(
			MetricConnectionRelaxedTimeout,
			metric.WithDescription("How many times the connection timeout has been increased/decreased (after a server maintenance notification)"),

View on GitHub (pinned to c5cad058c7)

Solutions

  1. Provide a working MeterProvider in cfg.MeterProvider or set a global one via otel.SetMeterProvider before Init
  2. Verify the provider was not Shutdown earlier in the process
  3. Inspect the wrapped error via errors.As/Unwrap and the otel global error handler for the root cause
  4. Align go.opentelemetry.io/otel versions across modules (go mod tidy / go mod vendor) so dbconv and the SDK agree on semconv
  5. Temporarily disable MetricGroupFlagConnectionBasic to isolate which instrument fails

Example fix

// before
otel.SetMeterProvider(noopProvider)
_ = obs.Init(&redisotel.Config{Enabled: true})
// after
sdk := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader))
_ = obs.Init(&redisotel.Config{Enabled: true, MeterProvider: sdk})
Defensive patterns

Strategy: validation

Validate before calling

if cfg.MetricGroups&redisotel.MetricGroupFlagConnectionBasic != 0 && cfg.MeterProvider == nil {
    cfg.MeterProvider = otel.GetMeterProvider() // ensure a real SDK is set globally
}

Type guard

if mp, ok := cfg.MeterProvider.(*sdkmetric.MeterProvider); !ok || mp == nil { /* configure SDK */ }

Try / catch

err := obs.Init(cfg)
if err != nil && strings.Contains(err.Error(), "connection count metric") {
    log.Printf("connection-count instrument failed: %v", err)
}

Prevention

When it happens

Trigger: Calling Init with MetricGroupFlagConnectionBasic enabled while the MeterProvider is shut down, is the global no-op provider with a failing error handler path, or a custom provider that rejects the instrument name/unit/description from dbconv semconv v1.38.0.

Common situations: Provider shutdown ordering bugs in test suites; using an unsupported or stub MeterProvider; version mismatch between go-redis redisotel-native's dbconv package and the installed otel/metric SDK.

Related errors


AI-assisted analysis of redis/go-redis@c5cad058c7 (2026-09-01). Data as JSON: /api/errors/29363e0ff3fe7a28. Report an issue: GitHub.