Debian Codegraph @

prometheus/example_metricvec_test.go

raw
1 // Copyright 2020 The Prometheus Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package prometheus_test
15
16 import (
17 "fmt"
18
19 "google.golang.org/protobuf/proto"
20
21 dto "github.com/prometheus/client_model/go"
22
23 "github.com/prometheus/client_golang/prometheus"
24 )
25
26 // Info implements an info pseudo-metric, which is modeled as a Gauge that
27 // always has a value of 1. In practice, you would just use a Gauge directly,
28 // but for this example, we pretend it would be useful to have a “native”
29 // implementation.
30 type Info struct {
31 desc *prometheus.Desc
32 labelPairs []*dto.LabelPair
33 }
34
35 func (i Info) Desc() *prometheus.Desc {
36 return i.desc
37 }
38
39 func (i Info) Write(out *dto.Metric) error {
40 out.Label = i.labelPairs
41 out.Gauge = &dto.Gauge{Value: proto.Float64(1)}
42 return nil
43 }
44
45 // InfoVec is the vector version for Info. As an info metric never changes, we
46 // wouldn't really need to wrap GetMetricWithLabelValues and GetMetricWith
47 // because Info has no additional methods compared to the vanilla Metric that
48 // the unwrapped MetricVec methods return. However, to demonstrate all there is
49 // to do to fully implement a vector for a custom Metric implementation, we do
50 // it in this example anyway.
51 type InfoVec struct {
52 *prometheus.MetricVec
53 }
54
55 func NewInfoVec(name, help string, labelNames []string) *InfoVec {
56 desc := prometheus.NewDesc(name, help, labelNames, nil)
57 return &InfoVec{
58 MetricVec: prometheus.NewMetricVec(desc, func(lvs ...string) prometheus.Metric {
59 if len(lvs) != len(labelNames) {
60 panic("inconsistent label cardinality")
61 }
62 return Info{desc: desc, labelPairs: prometheus.MakeLabelPairs(desc, lvs)}
63 }),
64 }
65 }
66
67 func (v *InfoVec) GetMetricWithLabelValues(lvs ...string) (Info, error) {
68 metric, err := v.MetricVec.GetMetricWithLabelValues(lvs...)
69 return metric.(Info), err
70 }
71
72 func (v *InfoVec) GetMetricWith(labels prometheus.Labels) (Info, error) {
73 metric, err := v.MetricVec.GetMetricWith(labels)
74 return metric.(Info), err
75 }
76
77 func (v *InfoVec) WithLabelValues(lvs ...string) Info {
78 i, err := v.GetMetricWithLabelValues(lvs...)
79 if err != nil {
80 panic(err)
81 }
82 return i
83 }
84
85 func (v *InfoVec) With(labels prometheus.Labels) Info {
86 i, err := v.GetMetricWith(labels)
87 if err != nil {
88 panic(err)
89 }
90 return i
91 }
92
93 func (v *InfoVec) CurryWith(labels prometheus.Labels) (*InfoVec, error) {
94 vec, err := v.MetricVec.CurryWith(labels)
95 if vec != nil {
96 return &InfoVec{vec}, err
97 }
98 return nil, err
99 }
100
101 func (v *InfoVec) MustCurryWith(labels prometheus.Labels) *InfoVec {
102 vec, err := v.CurryWith(labels)
103 if err != nil {
104 panic(err)
105 }
106 return vec
107 }
108
109 func ExampleMetricVec() {
110 infoVec := NewInfoVec(
111 "library_version_info",
112 "Versions of the libraries used in this binary.",
113 []string{"library", "version"},
114 )
115
116 infoVec.WithLabelValues("prometheus/client_golang", "1.7.1")
117 infoVec.WithLabelValues("k8s.io/client-go", "0.18.8")
118
119 // Just for demonstration, let's check the state of the InfoVec by
120 // registering it with a custom registry and then let it collect the
121 // metrics.
122 reg := prometheus.NewRegistry()
123 reg.MustRegister(infoVec)
124
125 metricFamilies, err := reg.Gather()
126 if err != nil || len(metricFamilies) != 1 {
127 panic("unexpected behavior of custom test registry")
128 }
129 fmt.Println(toNormalizedJSON(metricFamilies[0]))
130
131 // Output:
132 // {"name":"library_version_info","help":"Versions of the libraries used in this binary.","type":"GAUGE","metric":[{"label":[{"name":"library","value":"k8s.io/client-go"},{"name":"version","value":"0.18.8"}],"gauge":{"value":1}},{"label":[{"name":"library","value":"prometheus/client_golang"},{"name":"version","value":"1.7.1"}],"gauge":{"value":1}}]}
133 }