Skip to content

Allow filtering logs being sent to AppInsights by log level #36456

@cosminstefanxp

Description

@cosminstefanxp

Is your feature request related to a problem? Please describe.
I am using pino for logging, and would like trace and debug level logs to be printed to console, but I don't want them in Application Insights. Currently, there's no way, using useAzureMonitor and PinoInstrumentation to filter a log record.

The issue is that useAzureMonitor automatically registers a AzureLogRecordProcessor, before the log processors. So there's no way to stop a log from being sent.

Describe the solution you'd like
A way to filter logs by min level. E.g. could be via a configuration option. Or allow a way to wrap the AzureLogRecordProcessor, so we can do something like:

export class LevelGateProcessor implements LogRecordProcessor {
  constructor(
    private readonly min: SeverityNumber,
    private readonly next: LogRecordProcessor
  ) {}

  onEmit(record: LogRecord) {
    const sev = record.severityNumber ?? SeverityNumber.INFO;
    if (sev >= this.min) this.next.onEmit(record);
  }
  forceFlush = () => this.next.forceFlush();
  shutdown   = () => this.next.shutdown();
}

// and then...
useAzureMonitor({
  // This still controls traces/metrics; fine to keep it
  azureMonitorExporterOptions: { connectionString },

  // Provide your own logs pipeline; the distro will use these instead of the default
  logRecordProcessors: [
    // Only send INFO+ to App Insights; TRACE/DEBUG stay on console (e.g., via pino)
    new LevelGateProcessor(SeverityNumber.INFO, /* Get a reference to the AzureLogRecordProcessor somehow */),
  ],
});

Maybe making the logRecordProcessors a function.

Describe alternatives you've considered
I've tried to use LevelGateProcessor with existing system - no success
I've tried to tweak pino-instrumentation, but it doesn't support this use case either - and probably shouldn't, since it's the purpose of a LogRecordProcessor.
I've tried to not use pino-instrumentation and use a transport directly, but it still wouldn't work with useAzureMonitor.

Metadata

Metadata

Assignees

Labels

ClientThis issue points to a problem in the data-plane of the library.Monitor - ExporterMonitor OpenTelemetry Exportercustomer-reportedIssues that are reported by GitHub users external to the Azure organization.questionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions