{"record":{"id":"360b11ec46460b44","repo":"denoland/deno","slug":"contextmanager-can-not-be-constructed","errorCode":null,"errorMessage":"ContextManager can not be constructed","messagePattern":"ContextManager can not be constructed","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/telemetry/telemetry.ts","lineNumber":651,"sourceCode":"    const c = new Context(this.#data);\n    c.#data[key] = value;\n    return c;\n  }\n\n  deleteValue(key: symbol): Context {\n    const c = new Context(this.#data);\n    delete c.#data[key];\n    return c;\n  }\n}\n\n// TODO(lucacasonato): @opentelemetry/api defines it's own ROOT_CONTEXT\nconst ROOT_CONTEXT = new Context();\n\n// Context manager for opentelemetry js library\nclass ContextManager {\n  constructor() {\n    throw new TypeError(\"ContextManager can not be constructed\");\n  }\n\n  static active(): Context {\n    return CURRENT.get() ?? ROOT_CONTEXT;\n  }\n\n  static with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(\n    context: Context,\n    fn: F,\n    thisArg?: ThisParameterType<F>,\n    ...args: A\n  ): ReturnType<F> {\n    const ctx = CURRENT.enter(context);\n    try {\n      return ReflectApply(fn, thisArg, args);\n    } finally {\n      setAsyncContext(ctx);\n    }","sourceCodeStart":633,"sourceCodeEnd":669,"githubUrl":"https://github.com/denoland/deno/blob/a961cdec3b1948844414ebeecc697dcad76df2d1/ext/telemetry/telemetry.ts#L633-L669","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use the static API: ContextManager.active() and ContextManager.with(context, fn, thisArg, ...args)","Remove the new expression entirely; there is no per-instance state to construct","If you genuinely need a custom context manager, depend on the real @opentelemetry/api + SDK npm packages instead of Deno's built-in shim"],"exampleFix":"// before\nconst cm = new ContextManager();\nconst ctx = cm.active();\n\n// after\nconst ctx = ContextManager.active();","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  // code that may construct ContextManager\n} catch (e) {\n  if (e instanceof TypeError && e.message.includes(\"can not be constructed\")) {\n    // switch to the static API: ContextManager.active() / .with()\n  } else {\n    throw e;\n  }\n}","preventionTips":["Treat ContextManager as a namespace object (like Math); never use new on it","Keep custom context-manager code against the real @opentelemetry/api npm packages, not Deno's shim","Grep migrations for 'new ContextManager' before switching to Deno's built-in otel support"],"tags":["opentelemetry","telemetry","context","static-class","api-misuse"],"backgroundTag":"static-class-instantiation","analyzedSha":"a961cdec3b1948844414ebeecc697dcad76df2d1","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}