open-telemetry/opentelemetry-dotnet

[feature request] Being able to filter logs like activities with a processor

Open

#5924 aperta il 25 ott 2024

Vedi su GitHub
 (2 commenti) (0 reazioni) (0 assegnatari)C# (889 fork)auto 404
contribfestdocumentationenhancementgood first issuekeep-openpkg:OpenTelemetry

Metriche repository

Star
 (3725 star)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

Package

OpenTelemetry

Is your feature request related to a problem?

Is it possible to give the possibility to custom processors to be able to filter "logs" as it is already possible with custom processors on "activities"?

What is the expected behavior?

Example:

/// <summary>
///     Represents the processor for filtering logs on <see cref="LogRecord"/> if is necessary.
/// </summary>
public class FilterLogsProcessor : BaseProcessor<LogRecord>
{
    /// <inheritdoc/>
    public override void OnEnd(LogRecord data)
    {
        var pathAttributeValue = data.Attributes?.FirstOrDefault(attribute => attribute.Key == "Path").Value?.ToString();
        if (!string.IsNullOrEmpty(pathAttributeValue)
            && (pathAttributeValue.Contains("liveness") || pathAttributeValue.Contains("health") || pathAttributeValue.Contains("metrics")))
        {
            // TODO: Configure the log record here, so that it is not exported
        }

        var endpointNameAttributeValue = data.Attributes?.FirstOrDefault(attribute => attribute.Key == "EndpointName").Value?.ToString();
        if (!string.IsNullOrEmpty(endpointNameAttributeValue)
            && (endpointNameAttributeValue.Contains("Health")))
        {
            // TODO: Configure the log record here, so that it is not exported
        }

        base.OnEnd(data);
    }
}

Which alternative solutions or features have you considered?

At the moment, we have not found any workaround to avoid polluting our Azure Application Insights with the logs of our healthchecks

Additional context

We use the Azure Monitor exporter as follows:

builder.Logging.AddOpenTelemetry(builder =>
{
    builder.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(TelemetryHelper.ServiceName, serviceInstanceId: TelemetryHelper.ServiceInstanceId));

    builder.IncludeFormattedMessage = true;
    builder.IncludeScopes = true;
    builder.ParseStateValues = true;

    builder.AddProcessor(new RemoveExceptionProcessor());
    builder.AddProcessor(new FilterLogsProcessor());

    builder.AddAzureMonitorLogExporter(options => options.ConnectionString = configuration["AzureMonitor:ApplicationInsights:ConnectionString"]);
});

Guida contributor