CK.AppIdentity.Monitoring.Metrics 0.3.3

dotnet add package CK.AppIdentity.Monitoring.Metrics --version 0.3.3
                    
NuGet\Install-Package CK.AppIdentity.Monitoring.Metrics -Version 0.3.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CK.AppIdentity.Monitoring.Metrics" Version="0.3.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CK.AppIdentity.Monitoring.Metrics" Version="0.3.3" />
                    
Directory.Packages.props
<PackageReference Include="CK.AppIdentity.Monitoring.Metrics" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CK.AppIdentity.Monitoring.Metrics --version 0.3.3
                    
#r "nuget: CK.AppIdentity.Monitoring.Metrics, 0.3.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CK.AppIdentity.Monitoring.Metrics@0.3.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CK.AppIdentity.Monitoring.Metrics&version=0.3.3
                    
Install as a Cake Addin
#tool nuget:?package=CK.AppIdentity.Monitoring.Metrics&version=0.3.3
                    
Install as a Cake Tool

CK.AppIdentity.Monitoring.Metrics

Integrates CK.Monitoring.Metrics with CK.AppIdentity for automatic FasterLog management and consumer registration.

Overview

This package provides:

  • MetricsFeatureDriver: An ApplicationIdentityFeatureDriver that owns the shared FasterLog instance
  • IMetricsConsumer / MetricsConsumerBase: Interfaces and base class for implementing metrics consumers
  • Automatic path resolution: FasterLog storage uses the application's LocalFileStore

Architecture

MetricsFeatureDriver (owns FasterLog)
    │
    ├── Creates FasterLog at LocalFileStore path
    ├── Injects FasterLog into MetricsLogHandler via action
    ├── Manages consumer registration
    ├── Handles truncation and orphan cleanup
    │
    └── Consumers (registered via RegisterConsumer)
        ├── CsvMetricsConsumer
        ├── (other consumers...)
        └── Each uses named FasterLog iterators

Configuration

{
  "CK-AppIdentity": {
    "FullName": "MyDomain/$MyApp/#Dev",
    "Local": {
      "Metrics": {
        "Path": "FasterLog/Metrics",
        "MemoryPageCount": 2,
        "TruncationIntervalMs": 60000,
        "HandlerWaitTimeoutMs": 30000
      }
    }
  }
}

Configuration Options

Property Type Default Description
Path string FasterLog/Metrics Relative path within LocalFileStore for FasterLog storage
MemoryPageCount int 2 Number of memory pages for FasterLog
TruncationIntervalMs int 60000 Interval between log truncation checks (0 to disable)
HandlerWaitTimeoutMs int 30000 Timeout waiting for MetricsLogHandler to be configured

Implementing a Consumer

Create a consumer by extending MetricsConsumerBase:

public class MyMetricsConsumer : MetricsConsumerBase
{
    public MyMetricsConsumer(FasterLog log, string name)
        : base(log, name,
               retryDelayMs: 2000,
               batchThresholdBytes: 2 << 21,
               maxBatchAgeMs: 60000,              // Send after 1 minute even if batch not full
               gracefulShutdownTimeoutMs: 5000)  // Wait up to 5s for final flush on shutdown
    {
    }

    protected override Task<TimeSpan> ProcessEntriesAsync(
        IEnumerable<ReadOnlyMemory<byte>> entries)
    {
        foreach (var entry in entries)
        {
            // Entry format: DateTime (8 bytes) + ASCII text
            var buffer = entry.ToArray();
            var dateTime = DateTime.FromBinary(BitConverter.ToInt64(buffer, 0));
            var text = Encoding.ASCII.GetString(buffer, 8, buffer.Length - 8);

            // Process the metric...
        }

        return Task.FromResult(TimeSpan.Zero); // No throttling
    }
}

Create a feature driver to register the consumer:

public class MyConsumerFeatureDriver : ApplicationIdentityFeatureDriver
{
    readonly MetricsFeatureDriver _metricsDriver;
    MyMetricsConsumer? _consumer;

    public MyConsumerFeatureDriver(
        ApplicationIdentityService s,
        MetricsFeatureDriver metricsDriver)
        : base(s, isAllowedByDefault: true)
    {
        _metricsDriver = metricsDriver; // DI ensures correct order
    }

    protected override async Task<bool> SetupAsync(FeatureLifetimeContext context)
    {
        if (_metricsDriver.FasterLog == null) return true;

        _consumer = new MyMetricsConsumer(_metricsDriver.FasterLog, "my-consumer");
        _metricsDriver.RegisterConsumer(_consumer);
        await _consumer.StartAsync(context.Monitor, CancellationToken.None);
        return true;
    }

    protected override async Task TeardownAsync(FeatureLifetimeContext context)
    {
        if (_consumer != null)
        {
            await _metricsDriver.RemoveConsumerAsync(_consumer.Name);
        }
    }
}

Consumer Features

  • Named iterators: Each consumer has a unique name (max 20 chars) used for FasterLog's persisted iterator
  • Recovery: On restart, consumers resume from their last committed position
  • Retry-on-failure: Exceptions in ProcessEntriesAsync trigger automatic retry after delay
  • Batching: Entries are batched up to a configurable size threshold
  • Time-based batching: Entries are sent after maxBatchAgeMs even if the batch threshold isn't reached
  • Graceful shutdown: Pending entries are flushed before the consumer shuts down (configurable timeout)
  • Auto-truncation: The driver periodically truncates the log up to the slowest consumer's position

Constructor Parameters

Parameter Default Description
retryDelayMs 2000 Delay in ms before retrying after a processing failure
batchThresholdBytes 2 MiB Size threshold for batching entries
maxBatchAgeMs 60000 Max time (ms) entries can accumulate before sending. Set to 0 for immediate sending.
gracefulShutdownTimeoutMs 5000 Max time (ms) to wait for final flush on shutdown. Set to 0 to skip graceful flush.

Orphan Cleanup

When a consumer is removed from configuration:

  1. On next startup, its iterator address is recovered from FasterLog
  2. During shutdown, orphaned iterators (recovered but never registered) are cleaned up
  3. This prevents unbounded log growth from abandoned consumers
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on CK.AppIdentity.Monitoring.Metrics:

Package Downloads
CK.AppIdentity.Monitoring.Metrics.Csv

Package Description

CK.AppIdentity.Monitoring.Metrics.InfluxDb

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.3.3 130 1/26/2026
0.3.2 123 1/26/2026
0.3.1 125 1/25/2026
0.3.0 126 1/24/2026