I am using OpenTelemetry and have it linked up with Azure Monitor (App Insights). I see all structured logging and traces appear in AI, which is nice.
I have read about the difference in tracing vs logging and that they are different pipelines. However, from an observability point of view, I need both in my (distributed application).
Ideally, I want to add a lot of logging, using ILogger, defining the LogLevel correctly. But then I want to make sure that at 'configuration time', so without code changes, the output level towards Application Insights can be adapted, in case issues occur, for example.
That's why I created a BaseProcessor<LogRecord> that I have wired up in the Application startup, with the following code.
// AIN : Using span processors to filter out telemetry
// This is used for filtering traces (dependencies, etc), which are called activities
builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) =>
builder.AddProcessor(new AzureMonitorActivityFilterProcessor()));
// Here we are filtering the logs with a Processor
builder.Services.ConfigureOpenTelemetryLoggerProvider((sp, builder) =>
builder.AddProcessor(new AzureMonitorLogFilterProcessor()));
builder.Services.AddOpenTelemetry()
.WithLogging()
.WithTracing()
.WithMetrics()
.UseAzureMonitor();
And in the next part, I have the code (just empty, ready to implement) for the BaseProcessor. The OnStart and OnEnd methods are being called, and I was assuming I would be able to indicate on a certain LogRecord (based on external configuration) that is should not be forwarded/sent in the pipeline towards the exporter.
But I don't see how I can do that.
using System.Diagnostics;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Logs;
public class AzureMonitorLogFilterProcessor : BaseProcessor<LogRecord>
{
// The OnStart method is called when an activity is started. This is the ideal place to filter activities.
public override void OnStart(LogRecord log)
{
if(log.LogLevel <= LogLevel.Information)
{
// What to set here, so that the log is not sent to the Azure Monitor?
}
}
public override void OnEnd(LogRecord log)
{
if(log.LogLevel <= LogLevel.Information)
{
// What to set here, so that the log is not sent to the Azure Monitor?
}
base.OnEnd(log);
}
}
Maybe I am totally wrong and using Processors for things they are not intended for, but I'd be happy to be pointed to the right way of doing this.
Thanks
