Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static IDataProtectionBuilder AddDataProtection(this IServiceCollection s
ArgumentNullThrowHelper.ThrowIfNull(services);

services.TryAddSingleton<IActivator, TypeForwardingActivator>();
services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, MetricsLoggerProvider>());
Comment thread
amcasey marked this conversation as resolved.
services.AddOptions();
AddDataProtectionServices(services);

Expand Down
97 changes: 97 additions & 0 deletions src/DataProtection/DataProtection/src/MetricsLoggerProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Metrics;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Xml.Linq;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Win32;

namespace Microsoft.AspNetCore.DataProtection;

internal sealed class MetricsLoggerProvider : ILoggerProvider
{
public const string MeterName = "Microsoft.AspNetCore.DataProtection";
Comment thread
amcasey marked this conversation as resolved.
private const string CategoryNamePrefix = "Microsoft.AspNetCore.DataProtection";
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I include a dot at the end? It's a namespace we "own", so I'm not that worried about picking up, e.g., "DataProtection2".


private readonly Meter? _meter;

private readonly ILogger _logger;

public MetricsLoggerProvider()
{
_logger = NullLogger.Instance;
}

public MetricsLoggerProvider(IMeterFactory meterFactory)
{
_meter = meterFactory.Create(MeterName);

var counter = _meter.CreateCounter<long>(
"aspnetcore.data_protection.log_messages",
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We went with "rate_limiting", so I went with "data_protection", rather than "dataprotection".

unit: "{message}",
description: "Number of log records that have been logged.");

_logger = new MetricsLogger(counter);
}

ILogger ILoggerProvider.CreateLogger(string categoryName) =>
categoryName.StartsWith(CategoryNamePrefix, StringComparison.Ordinal)
? _logger
: NullLogger.Instance;

void IDisposable.Dispose() => _meter?.Dispose();

private sealed class MetricsLogger : ILogger
{
private readonly Counter<long> _counter;

public MetricsLogger(Counter<long> counter)
{
_counter = counter;
}

IDisposable? ILogger.BeginScope<TState>(TState state) => null;

bool ILogger.IsEnabled(LogLevel logLevel)
{
switch (logLevel)
{
case LogLevel.Critical:
case LogLevel.Error:
case LogLevel.Warning:
return _counter.Enabled;
default:
return false;
}
}

void ILogger.Log<TState>(LogLevel logLevel, EventId eventId, TState _state, Exception? _exception, Func<TState, Exception?, string> _formatter)
Comment thread
amcasey marked this conversation as resolved.
{
var levelName = logLevel switch
{
LogLevel.Critical => "critical",
LogLevel.Error => "error",
LogLevel.Warning => "warning",
_ => null,
};

if (levelName is null || !_counter.Enabled)
{
return;
}

var tags = new TagList(
[
new("aspnetcore.data_protection.log_message_id", eventId.Name ?? eventId.Id.ToString(CultureInfo.InvariantCulture)),
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably, these should be dotnet- or Microsoft.Extensions- scoped, but that seems premature.

Copy link
Copy Markdown

@reyang reyang Apr 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just personal thoughts, maybe use "aspnetcore.data_protection.logs.event_name" and "aspnetcore.data_protection.logs.log_level"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine by me

new("aspnetcore.data_protection.log_level", levelName),
]);
_counter.Add(1, tags);
}
}
}