redis/node-redis · error · OpenTelemetryError

OTelMetrics already initialized

Error message

OTelMetrics already initialized

What it means

OTelMetrics is itself a singleton guarded by a static #initialized flag; OTelMetrics.init() throws OpenTelemetryError if called twice. In practice this is invoked from OpenTelemetry.init(), so the user-facing cause is the same as the OpenTelemetry singleton error — repeated initialization of the metrics subsystem.

Source

Thrown at packages/client/lib/opentelemetry/metrics.ts:544

      this.commandMetrics = { destroy() {} };
    }

    this.#channelSubscribers = new OTelChannelSubscribers(
      this.#options,
      this.#instruments,
      this.#options.enabledMetricGroups,
    );
  }

  public static init({
    api,
    config,
  }: {
    api: OpenTelemetryApiModule;
    config?: ObservabilityConfig;
  }) {
    if (OTelMetrics.#initialized) {
      throw new OpenTelemetryError("OTelMetrics already initialized");
    }
    const instance = new OTelMetrics(api, config);
    OTelMetrics.#instance = instance;
    OTelMetrics.#initialized = true;
  }

  /**
   * Reset the instance to noop. Used for testing.
   *
   * @internal
   */
  public static reset() {
    if (!OTelMetrics.#initialized) return;
    OTelMetrics.#instance.commandMetrics.destroy();
    OTelMetrics.#instance.#channelSubscribers.destroy();
    OTelMetrics.#initialized = false;
  }

View on GitHub (pinned to bb5beb5657)

Solutions

  1. Initialize metrics once via OpenTelemetry.init(); do not call OTelMetrics.init() directly.
  2. In tests, call OTelMetrics.reset() before re-initializing.
  3. Guard the init call site with OTelMetrics.isInitialized().
  4. Consolidate bootstrap so only one caller initializes OTel.

Example fix

// before
OTelMetrics.init({ api, config }); // called per test

// after
if (!OTelMetrics.isInitialized()) OTelMetrics.init({ api, config });
// in afterEach: OTelMetrics.reset();
Defensive patterns

Strategy: validation

Validate before calling

if (!OTelMetrics.isInitialized()) OTelMetrics.init({ api, config });

Try / catch

try { OTelMetrics.init({ api, config }); } catch (e) { if (!/already initialized/.test(e.message)) throw e; }

Prevention

When it happens

Trigger: Directly calling OTelMetrics.init() more than once, or calling OpenTelemetry.init() twice (which delegates here). Also reachable in test setups that call OTelMetrics.init without OTelMetrics.reset() between cases.

Common situations: Test suites repeatedly initializing metrics; HMR/watch reload re-running bootstrap; a library and an app both trying to initialize OTel metrics in the same process.

Related errors


AI-assisted analysis of redis/node-redis@bb5beb5657 (2026-08-03). Data as JSON: /data/errors/f412b113a994de38.json. Report an issue: GitHub.