swift-server / swift-prometheus

Prometheus client library for Swift
https://swiftpackageindex.com/swift-server/swift-prometheus
Apache License 2.0
145 stars 32 forks source link

Unregister the metric fully when last labelled metric is unregistered #110

Closed simonjbeaumont closed 8 months ago

simonjbeaumont commented 9 months ago

Motivation

The registry is designed to return the same metric instance for a previously registered metric with the same name if, and only if:

By design, it throws a fatal error if these conditions do not hold for the metric name.

However, the registry also provides methods for unregistering metrics, such that they will not be emitted. A consequence of this unregistration is it allows for the metric name to be reused:

registry.unregisterCounter(registry.makeCounter(name: "name"))
_ = registry.makeGauge(name: "name") // does not crash

But the API has some unexpected behaviour when unregistering metrics that were registered with labels:

registry.unregisterCounter(registry.makeCounter(name: "name", labels: [("a", "1")]))
_ = registry.makeCounter(name: "name", labels: [("b", "1")])  // crashes

This is because the unregister APIs currently only remove the dimensions but do not go as far as to unregister the whole metric once the last such dimension has been unregistered.

Modifications

Result

Unregistering a metric with a label will unregister the metric name if this was the only remaining dimension for that metric.

fabianfett commented 9 months ago

@swift-server-bot test this please

fabianfett commented 9 months ago

@swift-server-bot test this please

fabianfett commented 9 months ago

@swift-server-bot test this please

fabianfett commented 8 months ago

@swift-server-bot test this please