Table of Contents

Class MeterCapture

Namespace
MetricsAssertions
Assembly
MetricsAssertions.dll

A meter-wide measurement capture composed from a bundle of per-instrument InstrumentCapture (each backed by a Microsoft.Extensions.Diagnostics.Metrics.Testing.MetricCollector<T>). Built fluently for a known instrument set - strongly typed at construction, uniform to query and assert - for tests that need to assert across several instruments of one meter at once (e.g. an observable-gauge scrape).

Build with For(string) + Add*; query per instrument via the indexer or CounterTotal(string) / Samples(string) / MeasurementCount(string), or across all via Measurements. Call RecordObservable() before reading observable gauges.

public sealed class MeterCapture : IDisposable
Inheritance
MeterCapture
Implements
Inherited Members
Extension Methods

Properties

this[string]

Gets the per-instrument capture for instrumentName.

public InstrumentCapture this[string instrumentName] { get; }

Parameters

instrumentName string

The instrument name added to the bundle.

Property Value

InstrumentCapture

Measurements

Gets every captured measurement across all bundled instruments as a queryable, assertable set.

public MeasurementSet Measurements { get; }

Property Value

MeasurementSet

MeterName

Gets the meter name this capture is scoped to.

public string MeterName { get; }

Property Value

string

Methods

Add<T>(Instrument<T>, TimeProvider?)

Adds the referenceable instrument to the bundle.

public MeterCapture Add<T>(Instrument<T> instrument, TimeProvider? timeProvider = null) where T : struct

Parameters

instrument Instrument<T>

The instrument to capture.

timeProvider TimeProvider

An optional clock for measurement timestamps and waits.

Returns

MeterCapture

Type Parameters

T

The instrument's value type.

Add<T>(string, TimeProvider?)

Adds the named instrument (typically a counter / up-down counter / histogram) to the bundle.

public MeterCapture Add<T>(string instrumentName, TimeProvider? timeProvider = null) where T : struct

Parameters

instrumentName string

The instrument name.

timeProvider TimeProvider

An optional clock for measurement timestamps and waits.

Returns

MeterCapture

Type Parameters

T

The instrument's value type.

Contains(string)

Returns whether an instrument named instrumentName is bundled.

public bool Contains(string instrumentName)

Parameters

instrumentName string

The instrument name to look for.

Returns

bool

CounterTotal(string)

Gets the net total for a bundled counter / up-down counter.

public long CounterTotal(string instrumentName)

Parameters

instrumentName string

The instrument name.

Returns

long

Dispose()

Releases every bundled collector.

public void Dispose()

For(string)

Begins building a capture for the meter named meterName created directly (new Meter(name), no scope). For a meter created by an IMeterFactory, use For(string, object?).

public static MeterCapture For(string meterName)

Parameters

meterName string

The meter name to capture.

Returns

MeterCapture

For(string, object?)

Begins building a capture for the meter named meterName whose Scope equals meterScope. Pass the IMeterFactory instance for a DI-created meter; a null scope only matches a directly-created meter and would capture nothing from a factory-created one.

public static MeterCapture For(string meterName, object? meterScope)

Parameters

meterName string

The meter name to capture.

meterScope object

The meter scope to match (the IMeterFactory instance, or null).

Returns

MeterCapture

HasMeasurementTagged(string, string, object?)

Returns whether a bundled instrument has a measurement tagged tagKey=tagValue.

public bool HasMeasurementTagged(string instrumentName, string tagKey, object? tagValue)

Parameters

instrumentName string

The instrument name.

tagKey string

The tag key to match.

tagValue object

The tag value to match.

Returns

bool

MeasurementCount(string)

Gets the measurement count for a bundled instrument.

public int MeasurementCount(string instrumentName)

Parameters

instrumentName string

The instrument name.

Returns

int

RecordObservable()

Pulls the current values of all bundled observable instruments (gauges).

public void RecordObservable()

Samples(string)

Gets the samples for a bundled histogram.

public IReadOnlyList<double> Samples(string instrumentName)

Parameters

instrumentName string

The instrument name.

Returns

IReadOnlyList<double>