microsoft / ApplicationInsights-node.js

Microsoft Application Insights SDK for Node.js
MIT License
325 stars 140 forks source link

Problems with fetching logs from winston inside Nextjs 14 #1348

Open not-aesthetic-code opened 5 months ago

not-aesthetic-code commented 5 months ago

Hey, I'm struggling with receiving logs from winston. Unfortunately any of logs is gathered. I know that appInsights is connected because I'm able to hit with the other telemetric requests.

Environment: Nextjs 14.0.4 Node: 20.10.0

Execution Whole app is runned inside docker container, where the final command to run the app is: node --require ./scripts/appInsights.js && next dev,

Config: appInsights.js ` appInsights .setup(') .setAutoCollectConsole(true, true) // tried (true, false) (true) also .setAutoCollectDependencies(true) .setAutoCollectExceptions(true) .setAutoCollectHeartbeat(true) .setAutoCollectPerformance(true, true) .setAutoCollectRequests(true) .setAutoDependencyCorrelation(true) .setDistributedTracingMode(appInsights.DistributedTracingModes.AI_AND_W3C) .setSendLiveMetrics(true) .setUseDiskRetryCaching(true)

appInsights.start() ` Winston:

`import { Logger, createLogger, format, transports } from "winston" const { json } = format

export const logger = (moduleName: string): Logger => { const nameOfModule = moduleName.toUpperCase(); const loggingFormat = json();

const loggerObject = createLogger({ format: loggingFormat, defaultMeta: { context: nameOfModule }, transports: [new transports.Console()], })

return loggerObject }`

winston usage

for example: `import {logger} from '@services/logger'

export const apiLogger = logger("API") apiLogger.error('Error fetching favicon:', e);`

I was able to artifically generate telemetric data right after appInsights.start() with these functions: `function trackEvent() { const client = appInsights.defaultClient; const telemetry = { name: 'Custom Event V2', properties: { key: 'value' } }; client.trackEvent(telemetry); client.flush();

console.log('Telemetry data sent - Event:', telemetry); }

function trackTrace() { const client = appInsights.defaultClient; const message = 'This is a sample trace message.'; client.trackTrace({ message }); client.flush();

console.log('Telemetry data sent - Trace:', message); }

function trackMetric() { const client = appInsights.defaultClient; const metricName = 'Custom Metric'; const value = Math.floor(Math.random() * 100); client.trackMetric({ name: metricName, value }); client.flush();

console.log('Telemetry data sent - Metric:', metricName, value); }

trackEvent(); trackTrace(); trackMetric(); `

But besides of that, no trace is visible on my app insights resource.

Nextjs info. I've tried to implement telemetry according this logs too by importing a script into register function mentioned in this doc https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation also with no result.

Looking for feedback from you!

JacksonWeber commented 5 months ago

@not-aesthetic-code are you seeing the winston logs from above printed in the console? Or is it not showing up in App Insights or there?

not-aesthetic-code commented 5 months ago

@JacksonWeber Yes, every log from winston was clearly visible from the console, but non of these logs was send to the app Insights.

not-aesthetic-code commented 5 months ago

so @JacksonWeber Do we have any idea here?

JacksonWeber commented 4 months ago

@not-aesthetic-code It looks like in your above example you're able to manually collect the other telemetry types. Are you able to auto-collect any telemetry outside of logs? (dependencies, requests, etc.)

not-aesthetic-code commented 4 months ago

@JacksonWeber Yes, I was able to collect mostly Dependencies. But also, I've seen the collected traces & requests, even 1 exception. But somehow, no winson logs were visible in the traces.

JacksonWeber commented 3 months ago

@not-aesthetic-code I'm not exactly sure in what order your app will load these components. But based on the fact that you're seeing other telemetry with the exception of Winston, it seems that the issue lies specifically with instrumenting Winston. It's important that the Application Insights SDK is initialized before Winston so that it can be properly instrumented.