Counters
Counters are just that: they count numbers together. You create a counter, pass it a value and it will add it to the existing total. How is this different than a gauge?
The intention from counters in metrics is to track the rate of change. It’s not about the absolute value like a gauge.
You can increase a counter in small increments quickly, or large steps less frequently: +1 every second for 5 seconds, or +5 every 5 seconds. Both will produce the same rate of change.
Metrics counter isn’t continuous like a gauge. It doesn’t keep syncing the value with the dashboard and there is no absolute value sent or stored.
Creating a counter is nearly identical to a gauge:
let openTelemetry = OpenTelemetry.instance
let meter = openTelemetry.meterProvider.meterBuilder(name: "NAME_OF_METRICS_GROUP").build()
var counter = meter.counterBuilder(name: "NAME_OF_THE_COUNTER").build()
counter.add(value: 1.0)
An easy way to imagine the usage of a counter is counting your typing speed:
- OpenTelemetry Metrics will upload the added telemetry every 5 seconds (based on how you configure your exporter).
- You start typing, which each letter you add 1 to your counter.
- With each telemetry upload a new total is sent to the dashboard.
- The slower you type, the smaller those increments are. It doesn’t matter how many characters you entered at the end because what’s measured is your speed so time is a factor.
Note that this counter is only a positive counter. Using it to measure how fast your memory is increasing will be fine if you know that it will move in one direction only, and you are not freeing up any memory in your app… (not a good practice tho :] )