@@ -151,7 +151,10 @@ func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (written int, err e
151151 n , err = w .WriteString (" summary\n " )
152152 case dto .MetricType_UNTYPED :
153153 n , err = w .WriteString (" untyped\n " )
154- case dto .MetricType_HISTOGRAM :
154+ case dto .MetricType_HISTOGRAM , dto .MetricType_GAUGE_HISTOGRAM :
155+ // The classic Prometheus text format has no notion of a gauge
156+ // histogram. We render a gauge histogram in the same way as a
157+ // regular histogram.
155158 n , err = w .WriteString (" histogram\n " )
156159 default :
157160 return written , fmt .Errorf ("unknown metric type %s" , metricType .String ())
@@ -223,18 +226,22 @@ func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (written int, err e
223226 w , name , "_count" , metric , "" , 0 ,
224227 float64 (metric .Summary .GetSampleCount ()),
225228 )
226- case dto .MetricType_HISTOGRAM :
229+ case dto .MetricType_HISTOGRAM , dto . MetricType_GAUGE_HISTOGRAM :
227230 if metric .Histogram == nil {
228231 return written , fmt .Errorf (
229232 "expected histogram in metric %s %s" , name , metric ,
230233 )
231234 }
232235 infSeen := false
233236 for _ , b := range metric .Histogram .Bucket {
237+ v := b .GetCumulativeCountFloat ()
238+ if v == 0 {
239+ v = float64 (b .GetCumulativeCount ())
240+ }
234241 n , err = writeSample (
235242 w , name , "_bucket" , metric ,
236243 model .BucketLabel , b .GetUpperBound (),
237- float64 ( b . GetCumulativeCount ()) ,
244+ v ,
238245 )
239246 written += n
240247 if err != nil {
@@ -245,10 +252,14 @@ func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (written int, err e
245252 }
246253 }
247254 if ! infSeen {
255+ v := metric .Histogram .GetSampleCountFloat ()
256+ if v == 0 {
257+ v = float64 (metric .Histogram .GetSampleCount ())
258+ }
248259 n , err = writeSample (
249260 w , name , "_bucket" , metric ,
250261 model .BucketLabel , math .Inf (+ 1 ),
251- float64 ( metric . Histogram . GetSampleCount ()) ,
262+ v ,
252263 )
253264 written += n
254265 if err != nil {
@@ -263,10 +274,11 @@ func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (written int, err e
263274 if err != nil {
264275 return
265276 }
266- n , err = writeSample (
267- w , name , "_count" , metric , "" , 0 ,
268- float64 (metric .Histogram .GetSampleCount ()),
269- )
277+ v := metric .Histogram .GetSampleCountFloat ()
278+ if v == 0 {
279+ v = float64 (metric .Histogram .GetSampleCount ())
280+ }
281+ n , err = writeSample (w , name , "_count" , metric , "" , 0 , v )
270282 default :
271283 return written , fmt .Errorf (
272284 "unexpected type in metric %s %s" , name , metric ,
0 commit comments