Skip to content

Commit 70419a3

Browse files
author
beorn7
committed
Add examples for const summary/histogram.
1 parent 9c4fec0 commit 70419a3

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

prometheus/examples_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,56 @@ func ExampleSummaryVec() {
455455
// ]
456456
}
457457

458+
func ExampleConstSummary() {
459+
desc := prometheus.NewDesc(
460+
"http_request_duration_seconds",
461+
"A summary of the HTTP request durations.",
462+
[]string{"code", "method"},
463+
prometheus.Labels{"owner": "example"},
464+
)
465+
466+
// Create a constant summary from values we got from a 3rd party telemetry system.
467+
s := prometheus.MustNewConstSummary(
468+
desc,
469+
4711, 403.34,
470+
map[float64]float64{0.5: 42.3, 0.9: 323.3},
471+
"200", "get",
472+
)
473+
474+
// Just for demonstration, let's check the state of the summary by
475+
// (ab)using its Write method (which is usually only used by Prometheus
476+
// internally).
477+
metric := &dto.Metric{}
478+
s.Write(metric)
479+
fmt.Println(proto.MarshalTextString(metric))
480+
481+
// Output:
482+
// label: <
483+
// name: "code"
484+
// value: "200"
485+
// >
486+
// label: <
487+
// name: "method"
488+
// value: "get"
489+
// >
490+
// label: <
491+
// name: "owner"
492+
// value: "example"
493+
// >
494+
// summary: <
495+
// sample_count: 4711
496+
// sample_sum: 403.34
497+
// quantile: <
498+
// quantile: 0.5
499+
// value: 42.3
500+
// >
501+
// quantile: <
502+
// quantile: 0.9
503+
// value: 323.3
504+
// >
505+
// >
506+
}
507+
458508
func ExampleHistogram() {
459509
temps := prometheus.NewHistogram(prometheus.HistogramOpts{
460510
Name: "pond_temperature_celsius",
@@ -501,6 +551,64 @@ func ExampleHistogram() {
501551
// >
502552
}
503553

554+
func ExampleConstHistogram() {
555+
desc := prometheus.NewDesc(
556+
"http_request_duration_seconds",
557+
"A histogram of the HTTP request durations.",
558+
[]string{"code", "method"},
559+
prometheus.Labels{"owner": "example"},
560+
)
561+
562+
// Create a constant histogram from values we got from a 3rd party telemetry system.
563+
h := prometheus.MustNewConstHistogram(
564+
desc,
565+
4711, 403.34,
566+
map[float64]uint64{25: 121, 50: 2403, 100: 3221, 200: 4233},
567+
"200", "get",
568+
)
569+
570+
// Just for demonstration, let's check the state of the histogram by
571+
// (ab)using its Write method (which is usually only used by Prometheus
572+
// internally).
573+
metric := &dto.Metric{}
574+
h.Write(metric)
575+
fmt.Println(proto.MarshalTextString(metric))
576+
577+
// Output:
578+
// label: <
579+
// name: "code"
580+
// value: "200"
581+
// >
582+
// label: <
583+
// name: "method"
584+
// value: "get"
585+
// >
586+
// label: <
587+
// name: "owner"
588+
// value: "example"
589+
// >
590+
// histogram: <
591+
// sample_count: 4711
592+
// sample_sum: 403.34
593+
// bucket: <
594+
// cumulative_count: 121
595+
// upper_bound: 25
596+
// >
597+
// bucket: <
598+
// cumulative_count: 2403
599+
// upper_bound: 50
600+
// >
601+
// bucket: <
602+
// cumulative_count: 3221
603+
// upper_bound: 100
604+
// >
605+
// bucket: <
606+
// cumulative_count: 4233
607+
// upper_bound: 200
608+
// >
609+
// >
610+
}
611+
504612
func ExamplePushCollectors() {
505613
hostname, _ := os.Hostname()
506614
completionTime := prometheus.NewGauge(prometheus.GaugeOpts{

0 commit comments

Comments
 (0)