MCPcopy
hub / github.com/prometheus/client_golang / ExampleHistogram

Function ExampleHistogram

prometheus/examples_test.go:439–461  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

437}
438
439func ExampleHistogram() {
440 temps := prometheus.NewHistogram(prometheus.HistogramOpts{
441 Name: "pond_temperature_celsius",
442 Help: "The temperature of the frog pond.", // Sorry, we can't measure how badly it smells.
443 Buckets: prometheus.LinearBuckets(20, 5, 5), // 5 buckets, each 5 centigrade wide.
444 })
445
446 // Simulate some observations.
447 for i := 0; i < 1000; i++ {
448 temps.Observe(30 + math.Floor(120*math.Sin(float64(i)*0.1))/10)
449 }
450
451 // Just for demonstration, let's check the state of the histogram by
452 // (ab)using its Write method (which is usually only used by Prometheus
453 // internally).
454 metric := &dto.Metric{}
455 temps.Write(metric)
456
457 fmt.Println(toNormalizedJSON(sanitizeMetric(metric)))
458
459 // Output:
460 // {"histogram":{"sampleCount":"1000","sampleSum":29969.50000000001,"bucket":[{"cumulativeCount":"192","upperBound":20},{"cumulativeCount":"366","upperBound":25},{"cumulativeCount":"501","upperBound":30},{"cumulativeCount":"638","upperBound":35},{"cumulativeCount":"816","upperBound":40}],"createdTimestamp":"1970-01-01T00:00:10Z"}}
461}
462
463func ExampleNewConstHistogram() {
464 desc := prometheus.NewDesc(

Callers

nothing calls this directly

Calls 7

ObserveMethod · 0.95
NewHistogramFunction · 0.92
LinearBucketsFunction · 0.92
toNormalizedJSONFunction · 0.85
sanitizeMetricFunction · 0.85
WriteMethod · 0.65
PrintlnMethod · 0.65

Tested by

no test coverage detected