@@ -43,14 +43,14 @@ func InstrumentHandlerInFlight(g prometheus.Gauge, next http.Handler) http.Handl
43
43
44
44
// InstrumentHandlerDuration is a middleware that wraps the provided
45
45
// http.Handler to observe the request duration with the provided ObserverVec.
46
- // The ObserverVec must have zero, one, or two non-const non-curried labels. For
47
- // those, the only allowed label names are "code" and "method". The function
48
- // panics otherwise. The Observe method of the Observer in the ObserverVec is
49
- // called with the request duration in seconds. Partitioning happens by HTTP
50
- // status code and/or HTTP method if the respective instance label names are
51
- // present in the ObserverVec. For unpartitioned observations, use an
52
- // ObserverVec with zero labels. Note that partitioning of Histograms is
53
- // expensive and should be used judiciously.
46
+ // The ObserverVec must have valid metric and label names and must have zero,
47
+ // one, or two non-const non-curried labels. For those, the only allowed label
48
+ // names are "code" and " method". The function panics otherwise. The Observe
49
+ // method of the Observer in the ObserverVec is called with the request duration
50
+ // in seconds. Partitioning happens by HTTP status code and/or HTTP method if
51
+ // the respective instance label names are present in the ObserverVec. For
52
+ // unpartitioned observations, use an ObserverVec with zero labels. Note that
53
+ // partitioning of Histograms is expensive and should be used judiciously.
54
54
//
55
55
// If the wrapped Handler does not set a status code, a status code of 200 is assumed.
56
56
//
@@ -79,12 +79,13 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler) ht
79
79
}
80
80
81
81
// InstrumentHandlerCounter is a middleware that wraps the provided http.Handler
82
- // to observe the request result with the provided CounterVec. The CounterVec
83
- // must have zero, one, or two non-const non-curried labels. For those, the only
84
- // allowed label names are "code" and "method". The function panics
85
- // otherwise. Partitioning of the CounterVec happens by HTTP status code and/or
86
- // HTTP method if the respective instance label names are present in the
87
- // CounterVec. For unpartitioned counting, use a CounterVec with zero labels.
82
+ // to observe the request result with the provided CounterVec. The CounterVec
83
+ // must have valid metric and label names and must have zero, one, or two
84
+ // non-const non-curried labels. For those, the only allowed label names are
85
+ // "code" and "method". The function panics otherwise. Partitioning of the
86
+ // CounterVec happens by HTTP status code and/or HTTP method if the respective
87
+ // instance label names are present in the CounterVec. For unpartitioned
88
+ // counting, use a CounterVec with zero labels.
88
89
//
89
90
// If the wrapped Handler does not set a status code, a status code of 200 is assumed.
90
91
//
@@ -110,14 +111,15 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler)
110
111
111
112
// InstrumentHandlerTimeToWriteHeader is a middleware that wraps the provided
112
113
// http.Handler to observe with the provided ObserverVec the request duration
113
- // until the response headers are written. The ObserverVec must have zero, one,
114
- // or two non-const non-curried labels. For those, the only allowed label names
115
- // are "code" and "method". The function panics otherwise. The Observe method of
116
- // the Observer in the ObserverVec is called with the request duration in
117
- // seconds. Partitioning happens by HTTP status code and/or HTTP method if the
118
- // respective instance label names are present in the ObserverVec. For
119
- // unpartitioned observations, use an ObserverVec with zero labels. Note that
120
- // partitioning of Histograms is expensive and should be used judiciously.
114
+ // until the response headers are written. The ObserverVec must have valid
115
+ // metric and label names and must have zero, one, or two non-const non-curried
116
+ // labels. For those, the only allowed label names are "code" and "method". The
117
+ // function panics otherwise. The Observe method of the Observer in the
118
+ // ObserverVec is called with the request duration in seconds. Partitioning
119
+ // happens by HTTP status code and/or HTTP method if the respective instance
120
+ // label names are present in the ObserverVec. For unpartitioned observations,
121
+ // use an ObserverVec with zero labels. Note that partitioning of Histograms is
122
+ // expensive and should be used judiciously.
121
123
//
122
124
// If the wrapped Handler panics before calling WriteHeader, no value is
123
125
// reported.
@@ -139,15 +141,15 @@ func InstrumentHandlerTimeToWriteHeader(obs prometheus.ObserverVec, next http.Ha
139
141
}
140
142
141
143
// InstrumentHandlerRequestSize is a middleware that wraps the provided
142
- // http.Handler to observe the request size with the provided ObserverVec. The
143
- // ObserverVec must have zero, one, or two non-const non-curried labels. For
144
- // those, the only allowed label names are "code" and "method". The function
145
- // panics otherwise. The Observe method of the Observer in the ObserverVec is
146
- // called with the request size in bytes. Partitioning happens by HTTP status
147
- // code and/or HTTP method if the respective instance label names are present in
148
- // the ObserverVec. For unpartitioned observations, use an ObserverVec with zero
149
- // labels. Note that partitioning of Histograms is expensive and should be used
150
- // judiciously.
144
+ // http.Handler to observe the request size with the provided ObserverVec. The
145
+ // ObserverVec must have valid metric and label names and must have zero, one,
146
+ // or two non-const non-curried labels. For those, the only allowed label names
147
+ // are "code" and "method". The function panics otherwise. The Observe method of
148
+ // the Observer in the ObserverVec is called with the request size in
149
+ // bytes. Partitioning happens by HTTP status code and/or HTTP method if the
150
+ // respective instance label names are present in the ObserverVec. For
151
+ // unpartitioned observations, use an ObserverVec with zero labels. Note that
152
+ // partitioning of Histograms is expensive and should be used judiciously.
151
153
//
152
154
// If the wrapped Handler does not set a status code, a status code of 200 is assumed.
153
155
//
@@ -174,15 +176,15 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler)
174
176
}
175
177
176
178
// InstrumentHandlerResponseSize is a middleware that wraps the provided
177
- // http.Handler to observe the response size with the provided ObserverVec. The
178
- // ObserverVec must have zero, one, or two non-const non-curried labels. For
179
- // those, the only allowed label names are "code" and "method". The function
180
- // panics otherwise. The Observe method of the Observer in the ObserverVec is
181
- // called with the response size in bytes. Partitioning happens by HTTP status
182
- // code and/or HTTP method if the respective instance label names are present in
183
- // the ObserverVec. For unpartitioned observations, use an ObserverVec with zero
184
- // labels. Note that partitioning of Histograms is expensive and should be used
185
- // judiciously.
179
+ // http.Handler to observe the response size with the provided ObserverVec. The
180
+ // ObserverVec must have valid metric and label names and must have zero, one,
181
+ // or two non-const non-curried labels. For those, the only allowed label names
182
+ // are "code" and "method". The function panics otherwise. The Observe method of
183
+ // the Observer in the ObserverVec is called with the response size in
184
+ // bytes. Partitioning happens by HTTP status code and/or HTTP method if the
185
+ // respective instance label names are present in the ObserverVec. For
186
+ // unpartitioned observations, use an ObserverVec with zero labels. Note that
187
+ // partitioning of Histograms is expensive and should be used judiciously.
186
188
//
187
189
// If the wrapped Handler does not set a status code, a status code of 200 is assumed.
188
190
//
@@ -198,6 +200,11 @@ func InstrumentHandlerResponseSize(obs prometheus.ObserverVec, next http.Handler
198
200
})
199
201
}
200
202
203
+ // checkLabels returns whether the provided Collector has a non-const,
204
+ // non-curried label named "code" and/or "method". It panics if the provided
205
+ // Collector does not have a Desc or has more than one Desc or its Desc is
206
+ // invalid. It also panics if the Collector has any non-const, non-curried
207
+ // labels that are not named "code" or "method".
201
208
func checkLabels (c prometheus.Collector ) (code bool , method bool ) {
202
209
// TODO(beorn7): Remove this hacky way to check for instance labels
203
210
// once Descriptors can have their dimensionality queried.
@@ -225,6 +232,10 @@ func checkLabels(c prometheus.Collector) (code bool, method bool) {
225
232
226
233
close (descc )
227
234
235
+ // Make sure the Collector has a valid Desc by registering it with a
236
+ // temporary registry.
237
+ prometheus .NewRegistry ().MustRegister (c )
238
+
228
239
// Create a ConstMetric with the Desc. Since we don't know how many
229
240
// variable labels there are, try for as long as it needs.
230
241
for err := errors .New ("dummy" ); err != nil ; lvs = append (lvs , magicString ) {
0 commit comments