Counter implements Instrument
Counter instrument for recording non-negative increments.
Counters are monotonically increasing - they only go up. Use for counting occurrences: requests, errors, items processed.
Example usage:
$counter = $meter->createCounter('http.requests', 'requests', 'Total HTTP requests');
$counter->add(1, ['http.method' => 'GET']);
$counter->add(1, ['http.method' => 'POST']);
Tags
Interfaces
- Instrument
- Base interface for metric instruments.
Methods
- __construct() : mixed
- add() : void
- Add a non-negative value to the counter.
- 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 non-negative value to the counter.
public
add(float|int $amount[, array<string, bool|float|int|string> $attributes = [] ][, null|SpanContext $context = null ]) : void
Parameters
- $amount : float|int
-
Amount to add (must be >= 0)
- $attributes : array<string, bool|float|int|string> = []
-
Categorization attributes
- $context : null|SpanContext = null
-
Optional span context for exemplar capture
Tags
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