Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
python3js opened this issue Apr 24, 2024 · 7 comments

Comments

@python3js
Copy link

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
@python3js python3js changed the title [Bug]: OpenTelemetry Span Not Being Captured in Jest Tests with Instrumentation [Bug]: OpenTelemetry Span Not Being Captured in Jest Tests with Instrumentation (But it works with other test runners) Apr 24, 2024
@mrazauskas
Copy link
Contributor

Did you try upgrading Jest? The version you are using is two years old. The code base got significant changes since v27 was released.

@python3js
Copy link
Author

@mrazauskas I just tried it with the latest version of jest ^29.5.12 and still having the same issue.

@python3js
Copy link
Author

python3js commented Apr 24, 2024

What I found is that the patching method, _patchAddJob doesn't even get called in jest.

@mrazauskas
Copy link
Contributor

Could also be a setup issue. Hard to say looking at code snippets only. I would suggest putting together a minimal reproduction repo (without TypeScript).

@python3js
Copy link
Author

python3js commented Apr 24, 2024

@python3js
Copy link
Author

Turns out jest's auto-mocking feature can cause issues since instrumentation relies on it running first before the package is imported in user-code.

For the tests to work you have to manually set the instrumented module manually

like this:

import * as yourModule from "yourModule"
const instrumentation = new BullMQInstrumentation();
instrumentation._modules[0].moduleExports = yourModule 

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants