0Pricing
C# Academy · Lesson

Collecting Metrics

Record counters, gauges, and histograms.

Collecting Metrics is a free C# Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Metrics?

Metrics are numeric measurements aggregated over time. They power dashboards and alerts: request rate, error count, latency, queue depth. Unlike logs, they stay cheap at high volume because they aggregate.

The Meter API

.NET emits metrics through System.Diagnostics.Metrics. You create a Meter, then use it to create instruments like counters and histograms.

using System.Diagnostics.Metrics;

private static readonly Meter Meter = new("Checkout.Orders");

Counters

A Counter is a value that only goes up, perfect for counting events such as orders placed or errors. You call Add to increment it.

private static readonly Counter<long> OrdersPlaced =
    Meter.CreateCounter<long>("orders.placed");

OrdersPlaced.Add(1);

Counter Dimensions

Attach tags to slice a counter by dimension, like status or region, so you can aggregate and filter in your backend.

OrdersPlaced.Add(1,
    new KeyValuePair<string, object>("status", "completed"),
    new KeyValuePair<string, object>("region", "eu"));

Histograms

A Histogram records a distribution of values, ideal for latency or payload sizes. Backends compute percentiles (p50, p95, p99) from the recorded samples.

private static readonly Histogram<double> OrderDuration =
    Meter.CreateHistogram<double>("order.duration", "ms");

OrderDuration.Record(elapsedMs);

UpDownCounters

An UpDownCounter can increase or decrease, suited to values like active connections or items currently in a queue.

private static readonly UpDownCounter<long> ActiveOrders =
    Meter.CreateUpDownCounter<long>("orders.active");

ActiveOrders.Add(1);   // started
ActiveOrders.Add(-1);  // finished

Observable Instruments

Observable instruments are read on demand via a callback, good for gauges like current memory or cache size that you sample rather than increment.

Meter.CreateObservableGauge("cache.size",
    () => _cache.Count);

Registering The Meter

As with tracing, OTel collects only meters you register. Use AddMeter with your meter name.

builder.Services.AddOpenTelemetry()
    .WithMetrics(metrics => metrics
        .AddMeter("Checkout.Orders"));

Built-In Metrics

Instrumentation packages expose ready-made metrics for ASP.NET Core (request duration, active requests) and the runtime (GC, thread pool), giving instant operational insight.

metrics
    .AddAspNetCoreInstrumentation()
    .AddRuntimeInstrumentation();

Choosing The Right Instrument

Pick by behavior: monotonic count (Counter), value that rises and falls (UpDownCounter), distribution (Histogram), sampled current value (Observable Gauge). The right choice makes dashboards and alerts straightforward.

Keep Cardinality In Check

Each unique tag combination creates a separate time series. High-cardinality tags (like user id) can explode storage and cost. Use bounded, low-cardinality dimensions such as status or region.

Quick Check

Test metric collection.

Recap

Metrics aggregate numeric measurements cheaply. The Meter API creates instruments: Counter (monotonic), UpDownCounter (rises and falls), Histogram (distributions for percentiles), and Observable gauges (sampled). Tags add dimensions, but watch cardinality. Register meters with AddMeter and add built-in ASP.NET Core and runtime instrumentation for instant insight.

Frequently asked questions

Is the “Collecting Metrics” lesson free?

Yes — the full text of “Collecting Metrics” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Collecting Metrics”?

Record counters, gauges, and histograms. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Collecting Metrics” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Three Pillars of Observability
  2. Distributed Tracing
  3. Collecting Metrics
  4. Exporting to Backends
← Back to C# Academy