Skip to content

Enable more metrics #159

@albi005

Description

@albi005

Gemini slop:

Step 1: Install Required NuGet Packages

To capture "all" standard metrics, you need the core SDK plus the instrumentation libraries for the different parts of the .NET stack.

Run the following commands in your project folder:

# Core OpenTelemetry and ASP.NET Core integration
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Instrumentation.AspNetCore

# Runtime metrics (GC, Memory, CPU, ThreadPool, etc.)
dotnet add package OpenTelemetry.Instrumentation.Runtime

# Process metrics (Physical/Virtual memory, CPU time for the process)
dotnet add package OpenTelemetry.Instrumentation.Process

# HttpClient metrics (Outgoing HTTP request durations, error rates)
dotnet add package OpenTelemetry.Instrumentation.Http

# Exporter (Choose one: Prometheus is common for metrics)
dotnet add package OpenTelemetry.Exporter.Prometheus.AspNetCore
# OR for Console output (debugging):
# dotnet add package OpenTelemetry.Exporter.Console

Step 2: Configure Global Metrics Export

In your Program.cs, configure OpenTelemetry. The key to getting "all" metrics is chaining the Add...Instrumentation methods.

using OpenTelemetry.Metrics;

var builder = WebApplication.CreateBuilder(args);

// Add OpenTelemetry Metrics
builder.Services.AddOpenTelemetry()
    .WithMetrics(metrics =>
    {
        metrics
            // 1. Enable Built-in Metrics (The "All" part)
            .AddAspNetCoreInstrumentation()   // Incoming HTTP requests (ASP.NET Core)
            .AddHttpClientInstrumentation()   // Outgoing HTTP requests (HttpClient)
            .AddRuntimeInstrumentation()      // Memory, GC, ThreadPool, Assemblies
            .AddProcessInstrumentation()      // CPU usage, RAM usage
            
            // 2. Add Custom Metrics
            // Subscribe to a specific custom meter by name
            .AddMeter("MyCompany.MyApplication.Meter") 
            // OR use a Wildcard to subscribe to ALL custom meters starting with a prefix
            .AddMeter("MyCompany.*")
            
            // 3. Configure the Exporter (Where to send the data)
            // Example: Expose a Prometheus scraping endpoint
            .AddPrometheusExporter();
            
            // Example: Debugging to Console
            // .AddConsoleExporter();
    });

var app = builder.Build();

// 4. Map the Prometheus scraping endpoint (if using Prometheus)
app.MapPrometheusScrapingEndpoint();

app.MapGet("/", () => "Hello World!");

app.Run();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions