@@ -63,24 +63,10 @@ public struct Buckets: ExpressibleByArrayLiteral {
6363 }
6464}
6565
66- /// Label type Histograms can use
67- public protocol HistogramLabels : MetricLabels {
68- /// Bucket
69- var le : String { get set }
70- }
71-
72- extension HistogramLabels {
73- /// Creates empty HistogramLabels
74- init ( ) {
75- self . init ( )
76- self . le = " "
77- }
78- }
79-
8066/// Prometheus Histogram metric
8167///
8268/// See https://prometheus.io/docs/concepts/metric_types/#Histogram
83- public class PromHistogram < NumType: DoubleRepresentable , Labels : HistogramLabels > : PromMetric , PrometheusHandled {
69+ public class PromHistogram < NumType: DoubleRepresentable > : PromMetric , PrometheusHandled {
8470 /// Prometheus instance that created this Histogram
8571 internal weak var prometheus : PrometheusClient ?
8672
@@ -93,19 +79,16 @@ public class PromHistogram<NumType: DoubleRepresentable, Labels: HistogramLabels
9379 public let _type : PromMetricType = . histogram
9480
9581 /// Bucketed values for this Histogram
96- private var buckets : [ PromCounter < NumType , EmptyLabels > ] = [ ]
82+ private var buckets : [ PromCounter < NumType > ] = [ ]
9783
9884 /// Buckets used by this Histogram
9985 internal let upperBounds : [ Double ]
10086
101- /// Labels for this Histogram
102- internal let labels : Labels
103-
10487 /// Sub Histograms for this Histogram
105- fileprivate var subHistograms : [ Labels : PromHistogram < NumType , Labels > ] = [ : ]
88+ fileprivate var subHistograms : [ DimensionLabels : PromHistogram < NumType > ] = [ : ]
10689
10790 /// Total value of the Histogram
108- private let sum : PromCounter < NumType , EmptyLabels >
91+ private let sum : PromCounter < NumType >
10992
11093 /// Lock used for thread safety
11194 private let lock : Lock
@@ -115,19 +98,16 @@ public class PromHistogram<NumType: DoubleRepresentable, Labels: HistogramLabels
11598 /// - Parameters:
11699 /// - name: Name of the Histogram
117100 /// - help: Help text of the Histogram
118- /// - labels: Labels for the Histogram
119101 /// - buckets: Buckets to use for the Histogram
120102 /// - p: Prometheus instance creating this Histogram
121- internal init ( _ name: String , _ help: String ? = nil , _ labels : Labels = Labels ( ) , _ buckets: Buckets = . defaultBuckets, _ p: PrometheusClient ) {
103+ internal init ( _ name: String , _ help: String ? = nil , _ buckets: Buckets = . defaultBuckets, _ p: PrometheusClient ) {
122104 self . name = name
123105 self . help = help
124106
125107 self . prometheus = p
126108
127109 self . sum = . init( " \( self . name) _sum " , nil , 0 , p)
128110
129- self . labels = labels
130-
131111 self . upperBounds = buckets. buckets
132112
133113 self . lock = Lock ( )
@@ -142,8 +122,8 @@ public class PromHistogram<NumType: DoubleRepresentable, Labels: HistogramLabels
142122 /// - Returns:
143123 /// Newline separated Prometheus formatted metric string
144124 public func collect( ) -> String {
145- let ( buckets, subHistograms, labels ) = self . lock. withLock {
146- ( self . buckets, self . subHistograms, self . labels )
125+ let ( buckets, subHistograms) = self . lock. withLock {
126+ ( self . buckets, self . subHistograms)
147127 }
148128
149129 var output = [ String] ( )
@@ -157,13 +137,13 @@ public class PromHistogram<NumType: DoubleRepresentable, Labels: HistogramLabels
157137 collectBuckets ( buckets: buckets,
158138 upperBounds: self . upperBounds,
159139 name: self . name,
160- labels: labels ,
140+ labels: nil ,
161141 sum: self . sum. get ( ) ,
162142 into: & output)
163143
164144 subHistograms. forEach { subHistogram in
165145 let ( subHistogramBuckets, subHistogramLabels) = self . lock. withLock {
166- ( subHistogram. value. buckets, subHistogram. value . labels )
146+ ( subHistogram. value. buckets, subHistogram. key )
167147 }
168148 collectBuckets ( buckets: subHistogramBuckets,
169149 upperBounds: subHistogram. value. upperBounds,
@@ -175,22 +155,20 @@ public class PromHistogram<NumType: DoubleRepresentable, Labels: HistogramLabels
175155 return output. joined ( separator: " \n " )
176156 }
177157
178- private func collectBuckets( buckets: [ PromCounter < NumType , EmptyLabels > ] ,
158+ private func collectBuckets( buckets: [ PromCounter < NumType > ] ,
179159 upperBounds: [ Double ] ,
180160 name: String ,
181- labels: Labels ,
161+ labels: DimensionLabels ? ,
182162 sum: NumType ,
183163 into output: inout [ String ] ) {
184- var labels = labels
185164 var acc : NumType = 0
186165 for (i, bound) in upperBounds. enumerated ( ) {
187166 acc += buckets [ i] . get ( )
188- labels. le = bound. description
189- let labelsString = encodeLabels ( labels)
167+ let labelsString = encodeLabels ( EncodableHistogramLabels ( labels: labels, le: bound. description) )
190168 output. append ( " \( name) _bucket \( labelsString) \( acc) " )
191169 }
192170
193- let labelsString = encodeLabels ( labels, [ " le " ] )
171+ let labelsString = encodeLabels ( EncodableHistogramLabels ( labels: labels ) )
194172 output. append ( " \( name) _count \( labelsString) \( acc) " )
195173
196174 output. append ( " \( name) _sum \( labelsString) \( sum) " )
@@ -201,8 +179,8 @@ public class PromHistogram<NumType: DoubleRepresentable, Labels: HistogramLabels
201179 /// - Parameters:
202180 /// - value: Value to observe
203181 /// - labels: Labels to attach to the observed value
204- public func observe( _ value: NumType , _ labels: Labels ? = nil ) {
205- if let labels = labels, type ( of : labels ) != type ( of : EmptyHistogramLabels ( ) ) {
182+ public func observe( _ value: NumType , _ labels: DimensionLabels ? = nil ) {
183+ if let labels = labels {
206184 self . getOrCreateHistogram ( with: labels)
207185 . observe ( value)
208186 }
@@ -222,7 +200,7 @@ public class PromHistogram<NumType: DoubleRepresentable, Labels: HistogramLabels
222200 /// - labels: Labels to attach to the resulting value.
223201 /// - body: Closure to run & record.
224202 @inlinable
225- public func time< T> ( _ labels: Labels ? = nil , _ body: @escaping ( ) throws -> T ) rethrows -> T {
203+ public func time< T> ( _ labels: DimensionLabels ? = nil , _ body: @escaping ( ) throws -> T ) rethrows -> T {
226204 let start = DispatchTime . now ( ) . uptimeNanoseconds
227205 defer {
228206 let delta = Double ( DispatchTime . now ( ) . uptimeNanoseconds - start)
@@ -232,7 +210,7 @@ public class PromHistogram<NumType: DoubleRepresentable, Labels: HistogramLabels
232210 }
233211
234212 /// Helper for histograms & labels
235- fileprivate func getOrCreateHistogram( with labels: Labels ) -> PromHistogram < NumType , Labels > {
213+ fileprivate func getOrCreateHistogram( with labels: DimensionLabels ) -> PromHistogram < NumType > {
236214 let subHistograms = lock. withLock { self . subHistograms }
237215 if let histogram = subHistograms [ labels] {
238216 precondition ( histogram. name == self . name,
@@ -262,7 +240,7 @@ public class PromHistogram<NumType: DoubleRepresentable, Labels: HistogramLabels
262240 return histogram
263241 }
264242 guard let prometheus = prometheus else { fatalError ( " Lingering Histogram " ) }
265- let newHistogram = PromHistogram ( self . name, self . help, labels , Buckets ( self . upperBounds) , prometheus)
243+ let newHistogram = PromHistogram ( self . name, self . help, Buckets ( self . upperBounds) , prometheus)
266244 self . subHistograms [ labels] = newHistogram
267245 return newHistogram
268246 }
0 commit comments