How to send logs from NextJS to Azure Application Insights using OpenTelemetry and Winston?

I am using winston to log throughout my NextJS application. I want these logs to be collected in Azure Application Insights. Microsoft advises to do so using OpenTelemetry. My setup is as follows:

instrumentation.ts:

import { env } from "~/env";
import { logs } from "@opentelemetry/api-logs";
import { LoggerProvider, SimpleLogRecordProcessor, ConsoleLogRecordExporter } from "@opentelemetry/sdk-logs";
import { BatchSpanProcessor, NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
import { Resource } from "@opentelemetry/resources";
import { registerOTel } from "@vercel/otel";
import { WinstonInstrumentation } from "@opentelemetry/instrumentation-winston";
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
import { registerInstrumentations } from "@opentelemetry/instrumentation";

export async function register() {
    if (
        process.env.NEXT_RUNTIME == "nodejs" &&
        env.APPLICATIONINSIGHTS_CONNECTION_STRING != null
    ) {
        console.log("Registering Application Insights");
        const { AzureMonitorTraceExporter, AzureMonitorLogExporter } =
            await import("@azure/monitor-opentelemetry-exporter");

        const tracerProvider = new NodeTracerProvider({
            resource: new Resource({
                [ATTR_SERVICE_NAME]: env.APPLICATIONINSIGHTS_SERVICE_NAME,
            }),
        });
        tracerProvider.register();
        const traceExporter = new AzureMonitorTraceExporter({
            connectionString: env.APPLICATIONINSIGHTS_CONNECTION_STRING,
        });
        tracerProvider.addSpanProcessor(
            new BatchSpanProcessor(traceExporter, {
                maxQueueSize: 10,
                scheduledDelayMillis: 5000,
            }),
        );
        const logExporter = new AzureMonitorLogExporter({
            connectionString: env.APPLICATIONINSIGHTS_CONNECTION_STRING,
        });
        // Set up OpenTelemetry logging provider
        const loggerProvider = new LoggerProvider();

        // Add a log processor that will export the logs
        loggerProvider.addLogRecordProcessor(
            new SimpleLogRecordProcessor(logExporter),
        );
        // Add a processor to export log record
        loggerProvider.addLogRecordProcessor(
            new SimpleLogRecordProcessor(new ConsoleLogRecordExporter()),
        );
        logs.setGlobalLoggerProvider(loggerProvider);

        const instrumentation = new WinstonInstrumentation({
            disableLogSending: false,
            enabled: true,
            disableLogCorrelation: false,
        });
        registerOTel({
            serviceName: env.APPLICATIONINSIGHTS_SERVICE_NAME,
            traceExporter,
            instrumentations: [instrumentation],
        });
        registerInstrumentations({
            instrumentations: [instrumentation],
        });
    } else {

    }
}

My winston setup is as follows:

import { env } from "~/env";
import winston, { type Logger } from "winston";
import { OpenTelemetryTransportV3 } from "@opentelemetry/winston-transport";

// Create a logger with custom format
const logger: Logger = winston.createLogger({
    transports: [
        new winston.transports.Console({
            level: env.NODE_ENV == "test" ? "error" : "debug",
            format: winston.format.simple(),
        }),
        new OpenTelemetryTransportV3(),
    ],
});
export default logger;

The application does show that the instrumentation code is being executed, and I do get request time information in Application Insights, but the traces table in Application Insights remains empty. What am I doing wrong?

To resolve your issue with Winston logs not appearing in Azure Application Insights, follow these steps:

  1. Check Connection String: Ensure APPLICATIONINSIGHTS_CONNECTION_STRING is correctly set in your environment variables.
  2. Configure Log Exporter: Make sure AzureMonitorLogExporter is properly set up and added to the LoggerProvider.
  3. Persist User State: Verify that userType and token are stored and retrieved from local storage.
const logExporter = new AzureMonitorLogExporter({
    connectionString: env.APPLICATIONINSIGHTS_CONNECTION_STRING,
});
const loggerProvider = new LoggerProvider();
loggerProvider.addLogRecordProcessor(new SimpleLogRecordProcessor(logExporter));
logs.setGlobalLoggerProvider(loggerProvider);
New contributor

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

1

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