UpDownCounter implements Instrument
UpDownCounter instrument for recording increments and decrements.
Unlike Counter, UpDownCounter supports negative values and can both increase and decrease. Use for tracking values that can go up and down.
Example usage:
$counter = $meter->createUpDownCounter('queue.size', 'items', 'Current queue size');
$counter->add(5, ['queue.name' => 'tasks']); // Added 5 items
$counter->add(-2, ['queue.name' => 'tasks']); // Removed 2 items
Tags
Interfaces
- Instrument
- Base interface for metric instruments.
Methods
- __construct() : mixed
- add() : void
- Add a value to the counter (can be negative).
- collect() : array<string|int, Metric>
- Collect aggregated metrics for export.
- description() : string|null
- Get the instrument description.
- name() : string
- Get the instrument name.
- unit() : string|null
- Get the unit of measurement.
Methods
__construct()
public
__construct(string $name, resource $resource, InstrumentationScope $scope, ClockInterface $clock[, AggregationTemporality $temporality = AggregationTemporality::CUMULATIVE ][, ExemplarFilter $exemplarFilter = new TraceBasedExemplarFilter() ][, null|string $unit = null ][, null|string $description = null ]) : mixed
Parameters
- $name : string
-
Instrument name
- $resource : resource
-
The resource context for this instrument
- $scope : InstrumentationScope
-
Instrumentation scope that created this instrument
- $clock : ClockInterface
-
Clock for timestamps
- $temporality : AggregationTemporality = AggregationTemporality::CUMULATIVE
-
Aggregation temporality
- $exemplarFilter : ExemplarFilter = new TraceBasedExemplarFilter()
-
Filter for exemplar sampling
- $unit : null|string = null
-
Unit of measurement
- $description : null|string = null
-
Human-readable description
add()
Add a value to the counter (can be negative).
public
add(float|int $amount[, array<string, bool|float|int|string> $attributes = [] ][, null|SpanContext $context = null ]) : void
Parameters
- $amount : float|int
-
Amount to add (positive or negative)
- $attributes : array<string, bool|float|int|string> = []
-
Categorization attributes
- $context : null|SpanContext = null
-
Optional span context for exemplar capture
collect()
Collect aggregated metrics for export.
public
collect() : array<string|int, Metric>
Returns all aggregated metrics since the last collection, then resets the internal aggregation state.
Return values
array<string|int, Metric> —Aggregated metrics ready for export
description()
Get the instrument description.
public
description() : string|null
Return values
string|nullname()
Get the instrument name.
public
name() : string
Return values
stringunit()
Get the unit of measurement.
public
unit() : string|null