denoland/deno · error · TypeError

ContextManager can not be constructed

Error message

ContextManager can not be constructed

What it means

ContextManager in Deno's OpenTelemetry shim (ext/telemetry/telemetry.ts:629) is a static-only namespace class, like Math or JSON. It exists solely to expose static methods active(), with(), and bind(); its constructor deliberately throws a TypeError because context state lives in an AsyncLocalStorage-backed global (CURRENT), not in instances. Constructing or subclassing it is unsupported.

Source

Thrown at ext/telemetry/telemetry.ts:651

    const c = new Context(this.#data);
    c.#data[key] = value;
    return c;
  }

  deleteValue(key: symbol): Context {
    const c = new Context(this.#data);
    delete c.#data[key];
    return c;
  }
}

// TODO(lucacasonato): @opentelemetry/api defines it's own ROOT_CONTEXT
const ROOT_CONTEXT = new Context();

// Context manager for opentelemetry js library
class ContextManager {
  constructor() {
    throw new TypeError("ContextManager can not be constructed");
  }

  static active(): Context {
    return CURRENT.get() ?? ROOT_CONTEXT;
  }

  static with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(
    context: Context,
    fn: F,
    thisArg?: ThisParameterType<F>,
    ...args: A
  ): ReturnType<F> {
    const ctx = CURRENT.enter(context);
    try {
      return ReflectApply(fn, thisArg, args);
    } finally {
      setAsyncContext(ctx);
    }

View on GitHub (pinned to a961cdec3b)

Solutions

  1. Use the static API: ContextManager.active() and ContextManager.with(context, fn, thisArg, ...args)
  2. Remove the new expression entirely; there is no per-instance state to construct
  3. If you genuinely need a custom context manager, depend on the real @opentelemetry/api + SDK npm packages instead of Deno's built-in shim

Example fix

// before
const cm = new ContextManager();
const ctx = cm.active();

// after
const ctx = ContextManager.active();
Defensive patterns

Strategy: validation

Try / catch

try {
  // code that may construct ContextManager
} catch (e) {
  if (e instanceof TypeError && e.message.includes("can not be constructed")) {
    // switch to the static API: ContextManager.active() / .with()
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: new ContextManager(); Reflect.construct(ContextManager, []); subclassing via class MyCM extends ContextManager { constructor() { super(); } } while porting an OTel SDK tutorial that installs a custom context manager.

Common situations: Following OpenTelemetry JS docs for the SDK, where ContextManager is an abstract class you extend and register with the SDK's node async-hooks propagation; copy-pasted instrumentation setup code assuming the SDK API surface; code migrating from @opentelemetry/api's experimental context-propagation packages.

Related errors


AI-assisted analysis of denoland/deno@a961cdec3b (2026-08-20). Data as JSON: /api/errors/360b11ec46460b44. Report an issue: GitHub.