The registry is designed to return the same metric instance for a previously registered metric with the same name if, and only if:
the metric type matches;
the metric label keys match (it's fine for the labels to differ); and
in the case of histograms, the buckets match.
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:
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
Unregister the metric fully when last labelled metric is unregistered
Add test, testUnregisterReregisterWithoutLabels, which passed before this patch.
Add test, testUnregisterReregisterWithLabels, which failed before this patch, but now passes.
Result
Unregistering a metric with a label will unregister the metric name if this was the only remaining dimension for that metric.
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:
But the API has some unexpected behaviour when unregistering metrics that were registered with labels:
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
testUnregisterReregisterWithoutLabels
, which passed before this patch.testUnregisterReregisterWithLabels
, which failed before this patch, but now passes.Result
Unregistering a metric with a label will unregister the metric name if this was the only remaining dimension for that metric.