Skip to content

Commit 36659fa

Browse files
committed
Merge pull request #145 from prometheus/beorn7/doc
Document the possibility to create "empty" metrics in a metric vector.
2 parents 3b16b46 + 7abba84 commit 36659fa

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

prometheus/examples_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ func ExampleSummaryVec() {
392392
temps.WithLabelValues("lithobates-catesbeianus").Observe(32 + math.Floor(100*math.Cos(float64(i)*0.11))/10)
393393
}
394394

395+
// Create a Summary without any observations.
396+
temps.WithLabelValues("leiopelma-hochstetteri")
397+
395398
// Just for demonstration, let's check the state of the summary vector
396399
// by (ab)using its Collect method and the Write method of its elements
397400
// (which is usually only used by Prometheus internally - code like the
@@ -414,6 +417,26 @@ func ExampleSummaryVec() {
414417
// Output:
415418
// [label: <
416419
// name: "species"
420+
// value: "leiopelma-hochstetteri"
421+
// >
422+
// summary: <
423+
// sample_count: 0
424+
// sample_sum: 0
425+
// quantile: <
426+
// quantile: 0.5
427+
// value: nan
428+
// >
429+
// quantile: <
430+
// quantile: 0.9
431+
// value: nan
432+
// >
433+
// quantile: <
434+
// quantile: 0.99
435+
// value: nan
436+
// >
437+
// >
438+
// label: <
439+
// name: "species"
417440
// value: "lithobates-catesbeianus"
418441
// >
419442
// summary: <

prometheus/vec.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ func (m *MetricVec) Collect(ch chan<- Metric) {
5858
// GetMetricWithLabelValues returns the Metric for the given slice of label
5959
// values (same order as the VariableLabels in Desc). If that combination of
6060
// label values is accessed for the first time, a new Metric is created.
61+
//
62+
// It is possible to call this method without using the returned Metric to only
63+
// create the new Metric but leave it at its start value (e.g. a Summary or
64+
// Histogram without any observations). See also the SummaryVec example.
65+
//
6166
// Keeping the Metric for later use is possible (and should be considered if
6267
// performance is critical), but keep in mind that Reset, DeleteLabelValues and
6368
// Delete can be used to delete the Metric from the MetricVec. In that case, the
@@ -87,8 +92,9 @@ func (m *MetricVec) GetMetricWithLabelValues(lvs ...string) (Metric, error) {
8792

8893
// GetMetricWith returns the Metric for the given Labels map (the label names
8994
// must match those of the VariableLabels in Desc). If that label map is
90-
// accessed for the first time, a new Metric is created. Implications of keeping
91-
// the Metric are the same as for GetMetricWithLabelValues.
95+
// accessed for the first time, a new Metric is created. Implications of
96+
// creating a Metric without using it and keeping the Metric for later use are
97+
// the same as for GetMetricWithLabelValues.
9298
//
9399
// An error is returned if the number and names of the Labels are inconsistent
94100
// with those of the VariableLabels in Desc.

0 commit comments

Comments
 (0)