open telemetry and jest exporter-collector-grpc produces a onInit is not a function

I’m encountering an error related to OpenTelemetry while running a simple Jest test. The error suggests that a file is being imported after the Jest environment has been torn down. Below are the details of my code and the error.

I’m trying to set up OpenTelemetry in my Node.js application to monitor and collect telemetry data. The setup seems to work fine during normal execution, but when I run my Jest tests, I encounter a problem. It appears that the OpenTelemetry exporter is trying to execute a function after the Jest environment has been torn down, causing a ReferenceError.

/* eslint-disable no-console */
import { NodeTracerProvider } from '@opentelemetry/node';
import { SimpleSpanProcessor } from '@opentelemetry/tracing';
import { CollectorTraceExporter } from '@opentelemetry/exporter-collector-grpc';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { PgInstrumentation } from '@opentelemetry/instrumentation-pg';
import { NestInstrumentation } from '@opentelemetry/instrumentation-nestjs-core';
import { RedisInstrumentation } from '@opentelemetry/instrumentation-redis';
import { MeterProvider, PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics-base';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-grpc';
import * as grpc from '@grpc/grpc-js';
import { Meter } from '@opentelemetry/api';
import { Counter } from '@opentelemetry/api-metrics';

const provider: NodeTracerProvider = new NodeTracerProvider();
const exporter: CollectorTraceExporter = new CollectorTraceExporter({
    url: 'grpc://localhost:4317',
    credentials: grpc.credentials.createInsecure(),
});

provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
provider.register();

const metricExporter: OTLPMetricExporter = new OTLPMetricExporter({
    url: 'grpc://localhost:4317',
    credentials: grpc.credentials.createInsecure(),
});
const meterProvider: MeterProvider = new MeterProvider();

meterProvider.addMetricReader(
    new PeriodicExportingMetricReader({
        exporter: metricExporter,
        exportIntervalMillis: 6000,
    }),
);

const meter: Meter = meterProvider.getMeter('meter-meter');
const requestCounter: Counter = meter.createCounter('requests', {
    description: 'Count all incoming requests',
});
requestCounter.add(1);

export function setupTelemetry(): () => void {
    const unregister: () => void = registerInstrumentations({
        tracerProvider: provider,
        meterProvider: meterProvider,
        instrumentations: [
            new HttpInstrumentation(),
            new PgInstrumentation(),
            new RedisInstrumentation(),
            new NestInstrumentation(),
        ],
    });

    return unregister;
}

export function shutdownTelemetry(): void {
    provider
        .shutdown()
        .then(() => {
            console.log('Tracer provider shutdown successfully.');
        })
        .catch((err: string) => console.error('Tracer provider shutdown failed:', err));

    meterProvider
        .shutdown()
        .then(() => {
            console.log('Meter provider shutdown successfully.');
        })
        .catch((err: string) => console.error('Meter provider shutdown failed:', err));
}

export function flushTelemetry(): void {
    provider
        .forceFlush()
        .then(() => {
            console.log('Telemetry data flushed successfully.');
        })
        .catch((err: string) => console.error('Telemetry data flush failed:', err));
}

this is the test from jest

import { setupTelemetry, shutdownTelemetry, flushTelemetry } from './telemetry.js';

describe('setupTelemetry', () => {
    beforeAll(() => {
        setupTelemetry();
    });

    afterAll(() => {
        shutdownTelemetry();
        flushTelemetry();
    });

    it('should execute setupTelemetry without throwing an error', () => {
        expect(() => setupTelemetry()).not.toThrow();
    });
});

and this is the console error I get

ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From core/telemtry/telemtry.module.spec.ts.

      at Immediate._onImmediate (../node_modules/@opentelemetry/exporter-collector-grpc/src/CollectorExporterNodeBase.ts:83:26)
/Users/naelalshowaikh/Desktop/Fucking work/dbildungs-iam-server/node_modules/@opentelemetry/exporter-collector-grpc/build/src/CollectorExporterNodeBase.js:59
            onInit(this, config);
            ^

TypeError: onInit is not a function
    at Immediate.<anonymous> (/Users/naelalshowaikh/Desktop/Fucking work/dbildungs-iam-server/node_modules/@opentelemetry/exporter-collector-grpc/src/CollectorExporterNodeBase.ts:84:7)
    at processImmediate (node:internal/timers:478:21)

Node.js v21.7.3

I added the shutdown and flush methods because I thought that might solve the problem, but the error persists. Any advice on how to resolve this issue would be greatly appreciated.

New contributor

user25276531 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật