@@ -14,6 +14,11 @@ public enum PrometheusExporterExtensions {
1414 static let prometheusSummaryQuantileLabelName = " quantile "
1515 static let prometheusSummaryQuantileLabelValueForMin = " 0 "
1616 static let prometheusSummaryQuantileLabelValueForMax = " 1 "
17+ static let prometheusHistogramType = " histogram "
18+ static let prometheusHistogramSumPostFix = " _sum "
19+ static let prometheusHistogramCountPostFix = " _count "
20+ static let prometheusHistogramBucketPostFix = " _bucket "
21+ static let prometheusHistogramLeLabelName = " le "
1722
1823 static func writeMetricsCollection( exporter: PrometheusExporter ) -> String {
1924 var output = " "
@@ -45,8 +50,20 @@ public enum PrometheusExporterExtensions {
4550 let min = summary. min
4651 let max = summary. max
4752 output += PrometheusExporterExtensions . writeSummary ( prometheusMetric: prometheusMetric, timeStamp: now, labels: labels, metricName: metric. name, sum: Double ( sum) , count: count, min: Double ( min) , max: Double ( max) )
48- case . intHistogram, . doubleHistogram:
49- break
53+ case . intHistogram:
54+ let histogram = metricData as! HistogramData < Int >
55+ let count = histogram. count
56+ let sum = histogram. sum
57+ let bucketsBoundaries = histogram. buckets. boundaries. map { Double ( $0) }
58+ let bucketsCounts = histogram. buckets. counts
59+ output += PrometheusExporterExtensions . writeHistogram ( prometheusMetric: prometheusMetric, timeStamp: now, labels: labels, metricName: metric. name, sum: Double ( sum) , count: count, bucketsBoundaries: bucketsBoundaries, bucketsCounts: bucketsCounts)
60+ case . doubleHistogram:
61+ let histogram = metricData as! HistogramData < Double >
62+ let count = histogram. count
63+ let sum = histogram. sum
64+ let bucketsBoundaries = histogram. buckets. boundaries
65+ let bucketsCounts = histogram. buckets. counts
66+ output += PrometheusExporterExtensions . writeHistogram ( prometheusMetric: prometheusMetric, timeStamp: now, labels: labels, metricName: metric. name, sum: Double ( sum) , count: count, bucketsBoundaries: bucketsBoundaries, bucketsCounts: bucketsCounts)
5067 }
5168 }
5269 }
@@ -79,4 +96,24 @@ public enum PrometheusExporterExtensions {
7996 }
8097 return prometheusMetric. write ( timeStamp: timeStamp)
8198 }
99+
100+ private static func writeHistogram( prometheusMetric: PrometheusMetric , timeStamp: String , labels: [ String : String ] , metricName: String , sum: Double , count: Int , bucketsBoundaries: Array < Double > , bucketsCounts: Array < Int > ) -> String {
101+ var prometheusMetric = prometheusMetric
102+ prometheusMetric. type = prometheusHistogramType
103+ labels. forEach {
104+ prometheusMetric. values. append ( PrometheusValue ( name: metricName + prometheusHistogramSumPostFix, labels: [ $0. key: $0. value] , value: sum) )
105+ prometheusMetric. values. append ( PrometheusValue ( name: metricName + prometheusHistogramCountPostFix, labels: [ $0. key: $0. value] , value: Double ( count) ) )
106+ for i in 0 ..< bucketsBoundaries. count {
107+ prometheusMetric. values. append ( PrometheusValue ( name: metricName,
108+ labels: [ $0. key: $0. value,
109+ prometheusHistogramLeLabelName: String ( format: " %f " , bucketsBoundaries [ i] ) ] ,
110+ value: Double ( bucketsCounts [ i] ) ) )
111+ }
112+ prometheusMetric. values. append ( PrometheusValue ( name: metricName,
113+ labels: [ $0. key: $0. value,
114+ prometheusHistogramLeLabelName: " +Inf " ] ,
115+ value: Double ( bucketsCounts [ bucketsBoundaries. count] ) ) )
116+ }
117+ return prometheusMetric. write ( timeStamp: timeStamp)
118+ }
82119}
0 commit comments