Protocols

The following protocols are available globally.

Library SPI, intended to be implemented by backend libraries

  • 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.

    See more

    Declaration

    Swift

    public protocol MetricsFactory
  • A CounterHandler represents a backend implementation of a Counter.

    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 Counter.

    Implementation requirements

    To implement your own CounterHandler you should respect a few requirements that are necessary so applications work as expected regardless of the selected CounterHandler implementation.

    • The CounterHandler must be a class.
    See more

    Declaration

    Swift

    public protocol CounterHandler : AnyObject
  • A FloatingPointCounterHandler represents a backend implementation of a FloatingPointCounter.

    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 FloatingPointCounter.

    Implementation requirements

    To implement your own FloatingPointCounterHandler you should respect a few requirements that are necessary so applications work as expected regardless of the selected FloatingPointCounterHandler implementation.

    • The FloatingPointCounterHandler must be a class.
    See more

    Declaration

    Swift

    public protocol FloatingPointCounterHandler : AnyObject
  • A RecorderHandler represents a backend implementation of a Recorder.

    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 Recorder.

    Implementation requirements

    To implement your own RecorderHandler you should respect a few requirements that are necessary so applications work as expected regardless of the selected RecorderHandler implementation.

    • The RecorderHandler must be a class.
    See more

    Declaration

    Swift

    public protocol RecorderHandler : AnyObject
  • A TimerHandler represents a backend implementation of a Timer.

    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 Timer.

    Implementation requirements

    To implement your own TimerHandler you should respect a few requirements that are necessary so applications work as expected regardless of the selected TimerHandler implementation.

    • The TimerHandler must be a class.
    See more

    Declaration

    Swift

    public protocol TimerHandler : AnyObject