Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diagnostics: add cache key #50

Open
vtomskih opened this issue Nov 12, 2024 · 0 comments
Open

Diagnostics: add cache key #50

vtomskih opened this issue Nov 12, 2024 · 0 comments

Comments

@vtomskih
Copy link

Description

I will be able to implement a metric observer in which I will write metrics for specific queries by key and see their counter and duration. In addition to metrics, this will allow you to expand the context of data in logs and traces.

My custom implementation:

internal class CustomMetricsMemcachedDiagnosticObserver
{
    private readonly ICustomMemcachedMetrics _metrics;

    public MetricsMemcachedDiagnosticListener(ICustomMemcachedMetrics metrics)
    {
        _metrics = metrics;
    }

    [DiagnosticName(MemcachedDiagnosticSource.CommandDurationDiagnosticName)]
    public void ObserveCommandDuration(string commandName, double duration, string cacheKey)
    {
        _metrics.ObserveCommandDurationSeconds(commandName, cacheKey, duration);
    }
    
    [DiagnosticName(MemcachedDiagnosticSource.CommandsTotalDiagnosticName)]
    public void ObserveCommandsTotal(string commandName, string isSuccessful, string cacheKey)
    {
        _metrics.IncrementExecutedCommand(commandName, cacheKey, isSuccessful);
    }
}
internal class NuGetMemcachedMetrics : ICustomMemcachedMetrics
{
    // ..fields
    // ..ctor

    public void ObserveCommandDurationSeconds(string commandName, string cacheKey, double durationSeconds)
    {
        var normalizedKey = GetNormalizedKey(cacheKey);

        _commandDurationSeconds.Record(
            durationSeconds,
            new KeyValuePair<string, object>(CommandNameLabel, commandName),
            new KeyValuePair<string, object>(CacheKeyLabel, normalizedKey)
        );
    }

    public void ObserveExecutedCommand(string commandName, string cacheKey, string isSuccessful)
    {
        var normalizedKey = GetNormalizedKey(cacheKey);

        _commandsTotal.Add(1,
            new KeyValuePair<string, object>[]
            {
                new(CommandNameLabel, commandName),
                new(CacheKeyLabel, cacheKey),
                new(IsSuccessfulLabel, isSuccessful)
            });
    }

    private static string GetNormalizedKey(string cacheKey)
    {
        if (string.IsNullOrEmpty(cacheKey))
            return "<no-cache-key>";

        return packageId.StartsWith("packages-", StringComparison.OrdinalIgnoreCase) ? "packages"
            : packageId.StartsWith("package-versions-", StringComparison.OrdinalIgnoreCase) ? "package-versions"
            : packageId.StartsWith("package-", StringComparison.OrdinalIgnoreCase) ? "package"
            : "mirror";
    }
}

What needs to be done

Add cacheKey to ObserveExecutedCommand and ObserveCommandDurationSeconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant