opentelemetry-js icon indicating copy to clipboard operation
opentelemetry-js copied to clipboard

Missing active context in the Prisma event listener

Open Owen1435 opened this issue 1 year ago • 2 comments
trafficstars

I am trying to log Prisma queries, but the active context is missing in the event listener. Maybe someone knows how to fix it?

@Injectable()
export class PrismaService extends PrismaClient<LogEvents, 'info' | 'warn' | 'error' | 'query'> implements OnModuleInit {
    constructor() {
        super({ log: [{ emit: 'event', level: 'query' }] });
    }

    public async onModuleInit(): Promise<void> {
        this.$on('query', () => {
            console.log(context.active()); // no active context
        });
        await this.$connect();
    }
}

Init telemetry

...
const sdk = new NodeSDK({
        autoDetectResources: true,
        contextManager: new AsyncLocalStorageContextManager(),
        spanProcessors,
        metricReader,
        instrumentations: [
            new NestInstrumentation(),
            new HttpInstrumentation(),
            new ExpressInstrumentation(),
            new PrismaInstrumentation({ middleware: true }),
        ],
    });

sdk.start();
  • [x] This only affects the JavaScript OpenTelemetry library
  • [x] This may affect other libraries, but I would like to get opinions here first

Owen1435 avatar Jun 19 '24 14:06 Owen1435