Skip to content

observability: if an async context manager has not been enabled, make sure to register one #2146

@odeke-em

Description

@odeke-em

Reported offline by @alkatrivedi, whereby their code didn't involve creating a new traces at all but was using this instrumented package and their code used async/await yet spans that are supposed to be nested were disjoint.

OpenTelemetry-JS acknowledges that for code to work correctly and for context to be correctly propagated (as a replacement for threadlocal storage) a context manager must be enabled beforehand https://opentelemetry.io/docs/languages/js/context/

Customers should never have to think about these reasons and do it themselves, instead our package should automatically handle this scenario so that customers can transparently keep their code without any needs to edit

Task

  • Check for if a prior context manager was enabled and if not, create oen like this ideally in src/instrument.ts
const {AsyncHooksContextManager} = require('@opentelemetry/context-async-hooks');
const {context} = require('@opentelemetry/api');
const contextManager = new AsyncHooksContextManager();
contextManager.enable();
context.setGlobalContextManager(contextManager);

along with regression tests for it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api: spannerIssues related to the googleapis/nodejs-spanner API.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions