MetricsFactory

public protocol MetricsFactory

The MetricsFactory is the bridge between the MetricsSystem and the metrics backend implementation. MetricsFactory‘s role is to initialize concrete implementations of the various metric types:

Warning

This type is an implementation detail and should not be used directly, unless implementing your own metrics backend. To use the SwiftMetrics API, please refer to the documentation of MetricsSystem.

Destroying metrics

Since some metrics implementations may need to allocate (potentially “heavy”) resources for metrics, destroying metrics offers a signal to libraries when a metric is “known to never be updated again.”

While many metrics are bound to the entire lifetime of an application and thus never need to be destroyed eagerly, some metrics have well defined unique life-cycles, and it may be beneficial to release any resources held by them more eagerly than awaiting the application’s termination. In such cases, a library or application should invoke a metric’s appropriate destroy() method, which in turn results in the corresponding handler that it is backed by to be passed to destroyCounter(handler:), destroyRecorder(handler:) or destroyTimer(handler:) where the factory can decide to free any corresponding resources.

While some libraries may not need to implement this destroying as they may be stateless or similar, libraries using the metrics API should always assume a library WILL make use of this signal, and shall not neglect calling these methods when appropriate.