1+ import struct Foundation. Date
2+ import Dispatch
13import NIOConcurrencyHelpers
24
35/// Prometheus Gauge metric
46///
57/// See https://prometheus.io/docs/concepts/metric_types/#gauge
6- public class PromGauge < NumType: Numeric , Labels: MetricLabels > : PromMetric , PrometheusHandled {
8+ public class PromGauge < NumType: DoubleRepresentable , Labels: MetricLabels > : PromMetric , PrometheusHandled {
79 /// Prometheus instance that created this Gauge
810 internal weak var prometheus : PrometheusClient ?
911
@@ -68,12 +70,61 @@ public class PromGauge<NumType: Numeric, Labels: MetricLabels>: PromMetric, Prom
6870 }
6971 }
7072
73+ /// Sets the Gauge to the current unixtime in seconds
74+ ///
75+ /// - Parameters:
76+ /// - labels: Labels to attach to the value
77+ ///
78+ /// - Returns: The value of the Gauge attached to the provided labels
79+ @discardableResult
80+ public func setToCurrentTime( _ labels: Labels ? = nil ) -> NumType {
81+ return self . set ( . init( Date ( ) . timeIntervalSince1970) , labels)
82+ }
83+
84+ /// Tracks in progress blocks of code or functions.
85+ ///
86+ /// func someFunc() -> String { return "ABC" }
87+ /// let newFunc = myGauge.trackInprogress(someFunc)
88+ /// newFunc() // returns "ABC" and increments & decrements Gauge
89+ ///
90+ /// - Parameters:
91+ /// - labels: Labels to attach to the value
92+ /// - body: Function to wrap progress tracker around
93+ ///
94+ /// - Returns: The same type of function passed in for `body`, but wrapped to track progress.
95+ @inlinable
96+ public func trackInProgress< T> ( _ labels: Labels ? = nil , _ body: @escaping ( ) throws -> T ) -> ( ( ) throws -> T ) {
97+ return {
98+ self . inc ( )
99+ defer {
100+ self . dec ( )
101+ }
102+ return try body ( )
103+ }
104+ }
105+ /// Time the execution duration of a closure and observe the resulting time in seconds.
106+ ///
107+ /// - parameters:
108+ /// - labels: Labels to attach to the resulting value.
109+ /// - body: Closure to run & record execution time of.
110+ @inlinable
111+ public func time< T> ( _ labels: Labels ? = nil , _ body: @escaping ( ) throws -> T ) rethrows -> T {
112+ let start = DispatchTime . now ( ) . uptimeNanoseconds
113+ defer {
114+ let delta = Double ( DispatchTime . now ( ) . uptimeNanoseconds - start)
115+ self . set ( . init( delta / 1_000_000_000 ) , labels)
116+ }
117+ return try body ( )
118+ }
119+
120+
71121 /// Sets the Gauge
72122 ///
73123 /// - Parameters:
74124 /// - amount: Amount to set the gauge to
75125 /// - labels: Labels to attach to the value
76126 ///
127+ /// - Returns: The value of the Gauge attached to the provided labels
77128 @discardableResult
78129 public func set( _ amount: NumType , _ labels: Labels ? = nil ) -> NumType {
79130 return self . lock. withLock {
@@ -92,8 +143,8 @@ public class PromGauge<NumType: Numeric, Labels: MetricLabels>: PromMetric, Prom
92143 /// - Parameters:
93144 /// - amount: Amount to increment the Gauge with
94145 /// - labels: Labels to attach to the value
95- /// - done: Completion handler
96146 ///
147+ /// - Returns: The value of the Gauge attached to the provided labels
97148 @discardableResult
98149 public func inc( _ amount: NumType , _ labels: Labels ? = nil ) -> NumType {
99150 return self . lock. withLock {
@@ -113,8 +164,8 @@ public class PromGauge<NumType: Numeric, Labels: MetricLabels>: PromMetric, Prom
113164 ///
114165 /// - Parameters:
115166 /// - labels: Labels to attach to the value
116- /// - done: Completion handler
117167 ///
168+ /// - Returns: The value of the Gauge attached to the provided labels
118169 @discardableResult
119170 public func inc( _ labels: Labels ? = nil ) -> NumType {
120171 return self . inc ( 1 , labels)
@@ -125,8 +176,8 @@ public class PromGauge<NumType: Numeric, Labels: MetricLabels>: PromMetric, Prom
125176 /// - Parameters:
126177 /// - amount: Amount to decrement the Gauge with
127178 /// - labels: Labels to attach to the value
128- /// - done: Completion handler
129179 ///
180+ /// - Returns: The value of the Gauge attached to the provided labels
130181 @discardableResult
131182 public func dec( _ amount: NumType , _ labels: Labels ? = nil ) -> NumType {
132183 return self . lock. withLock {
@@ -147,6 +198,7 @@ public class PromGauge<NumType: Numeric, Labels: MetricLabels>: PromMetric, Prom
147198 /// - Parameters:
148199 /// - labels: Labels to attach to the value
149200 ///
201+ /// - Returns: The value of the Gauge attached to the provided labels
150202 @discardableResult
151203 public func dec( _ labels: Labels ? = nil ) -> NumType {
152204 return self . dec ( 1 , labels)
@@ -158,7 +210,6 @@ public class PromGauge<NumType: Numeric, Labels: MetricLabels>: PromMetric, Prom
158210 /// - labels: Labels to get the value for
159211 ///
160212 /// - Returns: The value of the Gauge attached to the provided labels
161- ///
162213 public func get( _ labels: Labels ? = nil ) -> NumType {
163214 return self . lock. withLock {
164215 if let labels = labels {
0 commit comments