@@ -19,7 +19,7 @@ extension HistogramLabels {
1919/// See https://prometheus.io/docs/concepts/metric_types/#Histogram
2020public class Histogram < NumType: DoubleRepresentable , Labels: HistogramLabels > : Metric , PrometheusHandled {
2121 /// Prometheus instance that created this Histogram
22- internal let prometheus : Prometheus
22+ internal let prometheus : PrometheusClient
2323
2424 /// Name of this Histogram, required
2525 public let name : String
@@ -52,7 +52,7 @@ public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: M
5252 /// - labels: Labels for the Histogram
5353 /// - buckets: Buckets to use for the Histogram
5454 /// - p: Prometheus instance creating this Histogram
55- internal init ( _ name: String , _ help: String ? = nil , _ labels: Labels = Labels ( ) , _ buckets: [ Double ] = defaultBuckets, _ p: Prometheus ) {
55+ internal init ( _ name: String , _ help: String ? = nil , _ labels: Labels = Labels ( ) , _ buckets: [ Double ] = defaultBuckets, _ p: PrometheusClient ) {
5656 self . name = name
5757 self . help = help
5858
@@ -73,44 +73,46 @@ public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: M
7373 ///
7474 /// - Returns:
7575 /// Newline seperated Prometheus formatted metric string
76- public func getMetric( ) -> String {
77- var output = [ String] ( )
78-
79- output. append ( headers)
80-
81- var acc : NumType = 0
82- for (i, bound) in self . upperBounds. enumerated ( ) {
83- acc += buckets [ i] . get ( )
84- labels. le = bound. description
85- let labelsString = encodeLabels ( labels)
86- output. append ( " \( name) _bucket \( labelsString) \( acc) " )
87- }
88-
89- let labelsString = encodeLabels ( labels, [ " le " ] )
90- output. append ( " \( name) _count \( labelsString) \( acc) " )
91-
92- output. append ( " \( name) _sum \( labelsString) \( total. get ( ) ) " )
93-
94- subHistograms. forEach { subHistogram in
76+ public func getMetric( _ done: @escaping ( String ) -> Void ) {
77+ prometheusQueue. async ( flags: . barrier) {
78+ var output = [ String] ( )
79+
80+ output. append ( self . headers)
81+
9582 var acc : NumType = 0
96- for (i, bound) in subHistogram . upperBounds. enumerated ( ) {
97- acc += subHistogram . buckets [ i] . get ( )
98- subHistogram . labels. le = bound. description
99- let labelsString = encodeLabels ( subHistogram . labels)
100- output. append ( " \( subHistogram . name) _bucket \( labelsString) \( acc) " )
83+ for (i, bound) in self . upperBounds. enumerated ( ) {
84+ acc += self . buckets [ i] . get ( )
85+ self . labels. le = bound. description
86+ let labelsString = encodeLabels ( self . labels)
87+ output. append ( " \( self . name) _bucket \( labelsString) \( acc) " )
10188 }
10289
103- let labelsString = encodeLabels ( subHistogram . labels, [ " le " ] )
104- output. append ( " \( subHistogram . name) _count \( labelsString) \( acc) " )
90+ let labelsString = encodeLabels ( self . labels, [ " le " ] )
91+ output. append ( " \( self . name) _count \( labelsString) \( acc) " )
10592
106- output. append ( " \( subHistogram. name) _sum \( labelsString) \( subHistogram. total. get ( ) ) " )
93+ output. append ( " \( self . name) _sum \( labelsString) \( self . total. get ( ) ) " )
94+
95+ self . subHistograms. forEach { subHistogram in
96+ var acc : NumType = 0
97+ for (i, bound) in subHistogram. upperBounds. enumerated ( ) {
98+ acc += subHistogram. buckets [ i] . get ( )
99+ subHistogram. labels. le = bound. description
100+ let labelsString = encodeLabels ( subHistogram. labels)
101+ output. append ( " \( subHistogram. name) _bucket \( labelsString) \( acc) " )
102+ }
103+
104+ let labelsString = encodeLabels ( subHistogram. labels, [ " le " ] )
105+ output. append ( " \( subHistogram. name) _count \( labelsString) \( acc) " )
106+
107+ output. append ( " \( subHistogram. name) _sum \( labelsString) \( subHistogram. total. get ( ) ) " )
108+
109+ subHistogram. labels. le = " "
110+ }
107111
108- subHistogram. labels. le = " "
112+ self . labels. le = " "
113+
114+ done ( output. joined ( separator: " \n " ) )
109115 }
110-
111- self . labels. le = " "
112-
113- return output. joined ( separator: " \n " )
114116 }
115117
116118 /// Observe a value
@@ -119,27 +121,29 @@ public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: M
119121 /// - value: Value to observe
120122 /// - labels: Labels to attach to the observed value
121123 public func observe( _ value: NumType , _ labels: Labels ? = nil ) {
122- if let labels = labels, type ( of: labels) != type ( of: EmptySummaryLabels ( ) ) {
123- let his = prometheus. getOrCreateHistogram ( with: labels, for: self )
124- his. observe ( value)
125- }
126- self . total. inc ( value)
127-
128- for (i, bound) in self . upperBounds. enumerated ( ) {
129- if bound >= value. doubleValue {
130- buckets [ i] . inc ( )
131- return
124+ prometheusQueue. async ( flags: . barrier) {
125+ if let labels = labels, type ( of: labels) != type ( of: EmptySummaryLabels ( ) ) {
126+ let his = self . prometheus. getOrCreateHistogram ( with: labels, for: self )
127+ his. observe ( value)
128+ }
129+ self . total. inc ( value)
130+
131+ for (i, bound) in self . upperBounds. enumerated ( ) {
132+ if bound >= value. doubleValue {
133+ self . buckets [ i] . inc ( )
134+ return
135+ }
132136 }
133137 }
134138 }
135139}
136140
137- extension Prometheus {
141+ extension PrometheusClient {
138142 fileprivate func getOrCreateHistogram< T: Numeric , U: HistogramLabels > ( with labels: U , for his: Histogram < T , U > ) -> Histogram < T , U > {
139143 let histograms = his. subHistograms. filter { ( metric) -> Bool in
140144 guard metric. name == his. name, metric. help == his. help, metric. labels == labels else { return false }
141145 return true
142- }
146+ }
143147 if histograms. count > 2 { fatalError ( " Somehow got 2 histograms with the same data type " ) }
144148 if let histogram = histograms. first {
145149 return histogram
0 commit comments