Skip to content

[API Proposal]: rate limited logging #4915

@verdie-g

Description

@verdie-g

Background and motivation

When developing a web service, logs are usually added for unhappy path, for example, a database query failed because the remote server was unreachable. The code would look like this:

try
{
    QueryDatabase();
}
catch (Exception e)
{
    Logger.LogError(e, "Error querying the database");
}

One subtle issue with that code that could have very nasty consequences is that if the database is down, all queries will induce an error log. That would create a huge spikes of logs that could overload the logs collector and make it drop logs that were crucial to understand the root cause of the issue.

Because this issue is easy to miss, I think would be great to integrate a token bucket rate limiter in the LoggerMessageAttribute.

dotnet/runtime#82465 seems to be related.

API Proposal

namespace Microsoft.Extensions.Logging;

public class LoggerMessageAttribute
{
    /// <summary>
    /// Maximum number of logs to restore each replenishment.
    /// </summary>
    public int LogsPerPeriod { get; set; }

    /// <summary>
    /// Period between replenishments in milliseconds.
    /// </summary>
    public int ReplenishmentPeriod { get; set; }
}

If LogsPerPeriod or ReplenishmentPeriod are equal or less than 0 they should be both ignored.

API Usage

try
{
    QueryDatabase();
}
catch (DatabaseException e)
{
    // A maximum of 5 logs will be sent by second.
    LogDatabaseError(e, e.HostName);
}

[LoggerMessage(
    EventId = 0,
    Level = LogLevel.Error,
    Message = "Error querying the database {hostName}"),
    LogsPerPeriod = 5,
    ReplenishmentPeriod = 1000]
public static partial void LogDatabaseError(ILogger logger, Exception e, string hostName);

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-telemetry

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions