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
->CounterHandler
FloatingPointCounter
->FloatingPointCounterHandler
Recorder
->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)]) -> CounterHandler
Parameters
label
The label for the
CounterHandler
.dimensions
The dimensions for the
CounterHandler
. -
makeFloatingPointCounter(label:
Default implementationdimensions: ) Create a backing
FloatingPointCounterHandler
.Default Implementation
Create a default backing
FloatingPointCounterHandler
for 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)]) -> FloatingPointCounterHandler
Parameters
label
The label for the
FloatingPointCounterHandler
.dimensions
The dimensions for the
FloatingPointCounterHandler
. -
Create a backing
RecorderHandler
.Declaration
Swift
func makeRecorder(label: String, dimensions: [(String, String)], aggregate: Bool) -> RecorderHandler
Parameters
label
The label for the
RecorderHandler
.dimensions
The dimensions for the
RecorderHandler
.aggregate
Is data aggregation expected.
-
Create a backing
TimerHandler
.Declaration
Swift
func makeTimer(label: String, dimensions: [(String, String)]) -> TimerHandler
Parameters
label
The label for the
TimerHandler
.dimensions
The 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
handler
The 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.destroyFloatingPointCounter
must be implemented ifmakeFloatingPointCounter
is implemented.Declaration
Swift
func destroyFloatingPointCounter(_ handler: FloatingPointCounterHandler)
Parameters
handler
The 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
handler
The 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
handler
The handler to be destroyed.