Skip to content

[Bug]: OpenTelemetry Span Not Being Captured in Jest Tests with Instrumentation (But it works with other test runners) #15033

Closed
@python3js

Description

@python3js

Version

27.5.1

Steps to reproduce

  1. Set up a Node.js project with Jest, OpenTelemetry SDK, and BullMQInstrumentation.
  2. Configure the NodeTracerProvider with an InMemorySpanExporter and a SimpleSpanProcessor.
  3. Write a Jest test to add a job to a BullMQ queue.
  4. Run the Jest test and check the captured spans.

Expected behavior

The InMemorySpanExporter should have captured exactly one span corresponding to the job addition operation.

Actual behavior

No spans are captured in the Jest environment, despite the operation completing successfully.

When running tests using Jest and the OpenTelemetry Node.js SDK with the BullMQInstrumentation plugin, spans expected from the operation of adding a job to a BullMQ queue are not being captured. The same setup appears to function correctly when run outside of Jest (e.g., directly in Node.js scripts or with other test runners like Mocha).

Additional context

The setup and test configurations are as follows:

import { createTestNode } from "@test-utils";
import { context } from "@opentelemetry/api";
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
import { AsyncHooksContextManager } from "@opentelemetry/context-async-hooks";
import { InMemorySpanExporter, SimpleSpanProcessor } from "@opentelemetry/sdk-trace-base";
import { BullMQInstrumentation } from "@jenniferplusplus/opentelemetry-instrumentation-bullmq"
import { v4 } from "uuid";

import type * as bullmq from 'bullmq';

let Queue: typeof bullmq.Queue;

describe("tracing with BullMQInstrumentation", () => {
  const provider = new NodeTracerProvider();
  const memoryExporter = new InMemorySpanExporter();
  provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));

  const instrumentation = new BullMQInstrumentation();
  const contextManager = new AsyncHooksContextManager();

  beforeAll(async () => {
    await node.up();
    contextManager.enable();
    context.setGlobalContextManager(contextManager);
    instrumentation.setTracerProvider(provider);
    instrumentation.enable();
  });

  afterAll(async () => {
    await node.down();
    contextManager.disable();
    instrumentation.disable();
  });

  it("should create a span for add", async () => {
    const q = new Queue('queue', {connection});
    await q.addBulk([{name: 'testJob', data: {test: 'yes'}}])
    
    const spans = memoryExporter.getFinishedSpans();

    expect(spans).toHaveLength(1); 
  });
});

Environment

System:
    OS: Windows 11 10.0.22631
    CPU: (12) x64 13th Gen Intel(R) Core(TM) i7-1355U
  Binaries:
    Node: 14.21.3 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.21 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 6.14.18 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.15.5 - ~\AppData\Local\pnpm\pnpm.EXE

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions