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:
Counter->CounterHandlerFloatingPointCounter->FloatingPointCounterHandlerRecorder->RecorderHandler
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.
-
Create a backing
CounterHandler.Declaration
Swift
func makeCounter(label: String, dimensions: [(String, String)]) -> CounterHandlerParameters
labelThe label for the
CounterHandler.dimensionsThe dimensions for the
CounterHandler. -
makeFloatingPointCounter(label:Default implementationdimensions: ) Create a backing
FloatingPointCounterHandler.Default Implementation
Create a default backing
FloatingPointCounterHandlerfor backends which do not naively support floating point counters.The created FloatingPointCounterHandler is a wrapper around a backend’s CounterHandler which accumulates floating point values and records increments to an underlying CounterHandler after crossing integer boundaries.
Declaration
Swift
func makeFloatingPointCounter(label: String, dimensions: [(String, String)]) -> FloatingPointCounterHandlerParameters
labelThe label for the
FloatingPointCounterHandler.dimensionsThe dimensions for the
FloatingPointCounterHandler. -
Create a backing
RecorderHandler.Declaration
Swift
func makeRecorder(label: String, dimensions: [(String, String)], aggregate: Bool) -> RecorderHandlerParameters
labelThe label for the
RecorderHandler.dimensionsThe dimensions for the
RecorderHandler.aggregateIs data aggregation expected.
-
Create a backing
TimerHandler.Declaration
Swift
func makeTimer(label: String, dimensions: [(String, String)]) -> TimerHandlerParameters
labelThe label for the
TimerHandler.dimensionsThe dimensions for the
TimerHandler. -
Invoked when the corresponding
Counter‘sdestroy()function is invoked. Upon receiving this signal the factory may eagerly release any resources related to this counter.Declaration
Swift
func destroyCounter(_ handler: CounterHandler)Parameters
handlerThe handler to be destroyed.
-
destroyFloatingPointCounter(_:Default implementation) Invoked when the corresponding
FloatingPointCounter‘sdestroy()function is invoked. Upon receiving this signal the factory may eagerly release any resources related to this counter.Default Implementation
Invoked when the corresponding
FloatingPointCounter‘sdestroy()function is invoked. Upon receiving this signal the factory may eagerly release any resources related to this counter.destroyFloatingPointCountermust be implemented ifmakeFloatingPointCounteris implemented.Declaration
Swift
func destroyFloatingPointCounter(_ handler: FloatingPointCounterHandler)Parameters
handlerThe handler to be destroyed.
-
Invoked when the corresponding
Recorder‘sdestroy()function is invoked. Upon receiving this signal the factory may eagerly release any resources related to this recorder.Declaration
Swift
func destroyRecorder(_ handler: RecorderHandler)Parameters
handlerThe handler to be destroyed.
-
Invoked when the corresponding
Timer‘sdestroy()function is invoked. Upon receiving this signal the factory may eagerly release any resources related to this timer.Declaration
Swift
func destroyTimer(_ handler: TimerHandler)Parameters
handlerThe handler to be destroyed.
View on GitHub
Install in Dash
MetricsFactory Protocol Reference