open-telemetry/opentelemetry-dotnet

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

Open

#5.924 aberto em 25 de out. de 2024

Ver no GitHub
 (2 comments) (0 reactions) (0 assignees)C# (889 forks)auto 404
contribfestdocumentationenhancementgood first issuekeep-openpkg:OpenTelemetry

Métricas do repositório

Stars
 (3.725 stars)
Métricas de merge de PR
 (Métricas PR pendentes)

Description

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"]);
});

Guia do colaborador