You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

580 lines
19 KiB

  1. // Copyright 2017 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. package promhttp
  14. import (
  15. "errors"
  16. "net/http"
  17. "strconv"
  18. "strings"
  19. "time"
  20. dto "github.com/prometheus/client_model/go"
  21. "github.com/prometheus/client_golang/prometheus"
  22. )
  23. // magicString is used for the hacky label test in checkLabels. Remove once fixed.
  24. const magicString = "zZgWfBxLqvG8kc8IMv3POi2Bb0tZI3vAnBx+gBaFi9FyPzB/CzKUer1yufDa"
  25. // observeWithExemplar is a wrapper for [prometheus.ExemplarAdder.ExemplarObserver],
  26. // which falls back to [prometheus.Observer.Observe] if no labels are provided.
  27. func observeWithExemplar(obs prometheus.Observer, val float64, labels map[string]string) {
  28. if labels == nil {
  29. obs.Observe(val)
  30. return
  31. }
  32. obs.(prometheus.ExemplarObserver).ObserveWithExemplar(val, labels)
  33. }
  34. // addWithExemplar is a wrapper for [prometheus.ExemplarAdder.AddWithExemplar],
  35. // which falls back to [prometheus.Counter.Add] if no labels are provided.
  36. func addWithExemplar(obs prometheus.Counter, val float64, labels map[string]string) {
  37. if labels == nil {
  38. obs.Add(val)
  39. return
  40. }
  41. obs.(prometheus.ExemplarAdder).AddWithExemplar(val, labels)
  42. }
  43. // InstrumentHandlerInFlight is a middleware that wraps the provided
  44. // http.Handler. It sets the provided prometheus.Gauge to the number of
  45. // requests currently handled by the wrapped http.Handler.
  46. //
  47. // See the example for InstrumentHandlerDuration for example usage.
  48. func InstrumentHandlerInFlight(g prometheus.Gauge, next http.Handler) http.Handler {
  49. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  50. g.Inc()
  51. defer g.Dec()
  52. next.ServeHTTP(w, r)
  53. })
  54. }
  55. // InstrumentHandlerDuration is a middleware that wraps the provided
  56. // http.Handler to observe the request duration with the provided ObserverVec.
  57. // The ObserverVec must have valid metric and label names and must have zero,
  58. // one, or two non-const non-curried labels. For those, the only allowed label
  59. // names are "code" and "method". The function panics otherwise. For the "method"
  60. // label a predefined default label value set is used to filter given values.
  61. // Values besides predefined values will count as `unknown` method.
  62. // `WithExtraMethods` can be used to add more methods to the set. The Observe
  63. // method of the Observer in the ObserverVec is called with the request duration
  64. // in seconds. Partitioning happens by HTTP status code and/or HTTP method if
  65. // the respective instance label names are present in the ObserverVec. For
  66. // unpartitioned observations, use an ObserverVec with zero labels. Note that
  67. // partitioning of Histograms is expensive and should be used judiciously.
  68. //
  69. // If the wrapped Handler does not set a status code, a status code of 200 is assumed.
  70. //
  71. // If the wrapped Handler panics, no values are reported.
  72. //
  73. // Note that this method is only guaranteed to never observe negative durations
  74. // if used with Go1.9+.
  75. func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, opts ...Option) http.HandlerFunc {
  76. hOpts := defaultOptions()
  77. for _, o := range opts {
  78. o.apply(hOpts)
  79. }
  80. // Curry the observer with dynamic labels before checking the remaining labels.
  81. code, method := checkLabels(obs.MustCurryWith(hOpts.emptyDynamicLabels()))
  82. if code {
  83. return func(w http.ResponseWriter, r *http.Request) {
  84. now := time.Now()
  85. d := newDelegator(w, nil)
  86. next.ServeHTTP(d, r)
  87. l := labels(code, method, r.Method, d.Status(), hOpts.extraMethods...)
  88. for label, resolve := range hOpts.extraLabelsFromCtx {
  89. l[label] = resolve(r.Context())
  90. }
  91. observeWithExemplar(obs.With(l), time.Since(now).Seconds(), hOpts.getExemplarFn(r.Context()))
  92. }
  93. }
  94. return func(w http.ResponseWriter, r *http.Request) {
  95. now := time.Now()
  96. next.ServeHTTP(w, r)
  97. l := labels(code, method, r.Method, 0, hOpts.extraMethods...)
  98. for label, resolve := range hOpts.extraLabelsFromCtx {
  99. l[label] = resolve(r.Context())
  100. }
  101. observeWithExemplar(obs.With(l), time.Since(now).Seconds(), hOpts.getExemplarFn(r.Context()))
  102. }
  103. }
  104. // InstrumentHandlerCounter is a middleware that wraps the provided http.Handler
  105. // to observe the request result with the provided CounterVec. The CounterVec
  106. // must have valid metric and label names and must have zero, one, or two
  107. // non-const non-curried labels. For those, the only allowed label names are
  108. // "code" and "method". The function panics otherwise. For the "method"
  109. // label a predefined default label value set is used to filter given values.
  110. // Values besides predefined values will count as `unknown` method.
  111. // `WithExtraMethods` can be used to add more methods to the set. Partitioning of the
  112. // CounterVec happens by HTTP status code and/or HTTP method if the respective
  113. // instance label names are present in the CounterVec. For unpartitioned
  114. // counting, use a CounterVec with zero labels.
  115. //
  116. // If the wrapped Handler does not set a status code, a status code of 200 is assumed.
  117. //
  118. // If the wrapped Handler panics, the Counter is not incremented.
  119. //
  120. // See the example for InstrumentHandlerDuration for example usage.
  121. func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler, opts ...Option) http.HandlerFunc {
  122. hOpts := defaultOptions()
  123. for _, o := range opts {
  124. o.apply(hOpts)
  125. }
  126. // Curry the counter with dynamic labels before checking the remaining labels.
  127. code, method := checkLabels(counter.MustCurryWith(hOpts.emptyDynamicLabels()))
  128. if code {
  129. return func(w http.ResponseWriter, r *http.Request) {
  130. d := newDelegator(w, nil)
  131. next.ServeHTTP(d, r)
  132. l := labels(code, method, r.Method, d.Status(), hOpts.extraMethods...)
  133. for label, resolve := range hOpts.extraLabelsFromCtx {
  134. l[label] = resolve(r.Context())
  135. }
  136. addWithExemplar(counter.With(l), 1, hOpts.getExemplarFn(r.Context()))
  137. }
  138. }
  139. return func(w http.ResponseWriter, r *http.Request) {
  140. next.ServeHTTP(w, r)
  141. l := labels(code, method, r.Method, 0, hOpts.extraMethods...)
  142. for label, resolve := range hOpts.extraLabelsFromCtx {
  143. l[label] = resolve(r.Context())
  144. }
  145. addWithExemplar(counter.With(l), 1, hOpts.getExemplarFn(r.Context()))
  146. }
  147. }
  148. // InstrumentHandlerTimeToWriteHeader is a middleware that wraps the provided
  149. // http.Handler to observe with the provided ObserverVec the request duration
  150. // until the response headers are written. The ObserverVec must have valid
  151. // metric and label names and must have zero, one, or two non-const non-curried
  152. // labels. For those, the only allowed label names are "code" and "method". The
  153. // function panics otherwise. For the "method" label a predefined default label
  154. // value set is used to filter given values. Values besides predefined values
  155. // will count as `unknown` method.`WithExtraMethods` can be used to add more
  156. // methods to the set. The Observe method of the Observer in the
  157. // ObserverVec is called with the request duration in seconds. Partitioning
  158. // happens by HTTP status code and/or HTTP method if the respective instance
  159. // label names are present in the ObserverVec. For unpartitioned observations,
  160. // use an ObserverVec with zero labels. Note that partitioning of Histograms is
  161. // expensive and should be used judiciously.
  162. //
  163. // If the wrapped Handler panics before calling WriteHeader, no value is
  164. // reported.
  165. //
  166. // Note that this method is only guaranteed to never observe negative durations
  167. // if used with Go1.9+.
  168. //
  169. // See the example for InstrumentHandlerDuration for example usage.
  170. func InstrumentHandlerTimeToWriteHeader(obs prometheus.ObserverVec, next http.Handler, opts ...Option) http.HandlerFunc {
  171. hOpts := defaultOptions()
  172. for _, o := range opts {
  173. o.apply(hOpts)
  174. }
  175. // Curry the observer with dynamic labels before checking the remaining labels.
  176. code, method := checkLabels(obs.MustCurryWith(hOpts.emptyDynamicLabels()))
  177. return func(w http.ResponseWriter, r *http.Request) {
  178. now := time.Now()
  179. d := newDelegator(w, func(status int) {
  180. l := labels(code, method, r.Method, status, hOpts.extraMethods...)
  181. for label, resolve := range hOpts.extraLabelsFromCtx {
  182. l[label] = resolve(r.Context())
  183. }
  184. observeWithExemplar(obs.With(l), time.Since(now).Seconds(), hOpts.getExemplarFn(r.Context()))
  185. })
  186. next.ServeHTTP(d, r)
  187. }
  188. }
  189. // InstrumentHandlerRequestSize is a middleware that wraps the provided
  190. // http.Handler to observe the request size with the provided ObserverVec. The
  191. // ObserverVec must have valid metric and label names and must have zero, one,
  192. // or two non-const non-curried labels. For those, the only allowed label names
  193. // are "code" and "method". The function panics otherwise. For the "method"
  194. // label a predefined default label value set is used to filter given values.
  195. // Values besides predefined values will count as `unknown` method.
  196. // `WithExtraMethods` can be used to add more methods to the set. The Observe
  197. // method of the Observer in the ObserverVec is called with the request size in
  198. // bytes. Partitioning happens by HTTP status code and/or HTTP method if the
  199. // respective instance label names are present in the ObserverVec. For
  200. // unpartitioned observations, use an ObserverVec with zero labels. Note that
  201. // partitioning of Histograms is expensive and should be used judiciously.
  202. //
  203. // If the wrapped Handler does not set a status code, a status code of 200 is assumed.
  204. //
  205. // If the wrapped Handler panics, no values are reported.
  206. //
  207. // See the example for InstrumentHandlerDuration for example usage.
  208. func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler, opts ...Option) http.HandlerFunc {
  209. hOpts := defaultOptions()
  210. for _, o := range opts {
  211. o.apply(hOpts)
  212. }
  213. // Curry the observer with dynamic labels before checking the remaining labels.
  214. code, method := checkLabels(obs.MustCurryWith(hOpts.emptyDynamicLabels()))
  215. if code {
  216. return func(w http.ResponseWriter, r *http.Request) {
  217. d := newDelegator(w, nil)
  218. next.ServeHTTP(d, r)
  219. size := computeApproximateRequestSize(r)
  220. l := labels(code, method, r.Method, d.Status(), hOpts.extraMethods...)
  221. for label, resolve := range hOpts.extraLabelsFromCtx {
  222. l[label] = resolve(r.Context())
  223. }
  224. observeWithExemplar(obs.With(l), float64(size), hOpts.getExemplarFn(r.Context()))
  225. }
  226. }
  227. return func(w http.ResponseWriter, r *http.Request) {
  228. next.ServeHTTP(w, r)
  229. size := computeApproximateRequestSize(r)
  230. l := labels(code, method, r.Method, 0, hOpts.extraMethods...)
  231. for label, resolve := range hOpts.extraLabelsFromCtx {
  232. l[label] = resolve(r.Context())
  233. }
  234. observeWithExemplar(obs.With(l), float64(size), hOpts.getExemplarFn(r.Context()))
  235. }
  236. }
  237. // InstrumentHandlerResponseSize is a middleware that wraps the provided
  238. // http.Handler to observe the response size with the provided ObserverVec. The
  239. // ObserverVec must have valid metric and label names and must have zero, one,
  240. // or two non-const non-curried labels. For those, the only allowed label names
  241. // are "code" and "method". The function panics otherwise. For the "method"
  242. // label a predefined default label value set is used to filter given values.
  243. // Values besides predefined values will count as `unknown` method.
  244. // `WithExtraMethods` can be used to add more methods to the set. The Observe
  245. // method of the Observer in the ObserverVec is called with the response size in
  246. // bytes. Partitioning happens by HTTP status code and/or HTTP method if the
  247. // respective instance label names are present in the ObserverVec. For
  248. // unpartitioned observations, use an ObserverVec with zero labels. Note that
  249. // partitioning of Histograms is expensive and should be used judiciously.
  250. //
  251. // If the wrapped Handler does not set a status code, a status code of 200 is assumed.
  252. //
  253. // If the wrapped Handler panics, no values are reported.
  254. //
  255. // See the example for InstrumentHandlerDuration for example usage.
  256. func InstrumentHandlerResponseSize(obs prometheus.ObserverVec, next http.Handler, opts ...Option) http.Handler {
  257. hOpts := defaultOptions()
  258. for _, o := range opts {
  259. o.apply(hOpts)
  260. }
  261. // Curry the observer with dynamic labels before checking the remaining labels.
  262. code, method := checkLabels(obs.MustCurryWith(hOpts.emptyDynamicLabels()))
  263. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  264. d := newDelegator(w, nil)
  265. next.ServeHTTP(d, r)
  266. l := labels(code, method, r.Method, d.Status(), hOpts.extraMethods...)
  267. for label, resolve := range hOpts.extraLabelsFromCtx {
  268. l[label] = resolve(r.Context())
  269. }
  270. observeWithExemplar(obs.With(l), float64(d.Written()), hOpts.getExemplarFn(r.Context()))
  271. })
  272. }
  273. // checkLabels returns whether the provided Collector has a non-const,
  274. // non-curried label named "code" and/or "method". It panics if the provided
  275. // Collector does not have a Desc or has more than one Desc or its Desc is
  276. // invalid. It also panics if the Collector has any non-const, non-curried
  277. // labels that are not named "code" or "method".
  278. func checkLabels(c prometheus.Collector) (code, method bool) {
  279. // TODO(beorn7): Remove this hacky way to check for instance labels
  280. // once Descriptors can have their dimensionality queried.
  281. var (
  282. desc *prometheus.Desc
  283. m prometheus.Metric
  284. pm dto.Metric
  285. lvs []string
  286. )
  287. // Get the Desc from the Collector.
  288. descc := make(chan *prometheus.Desc, 1)
  289. c.Describe(descc)
  290. select {
  291. case desc = <-descc:
  292. default:
  293. panic("no description provided by collector")
  294. }
  295. select {
  296. case <-descc:
  297. panic("more than one description provided by collector")
  298. default:
  299. }
  300. close(descc)
  301. // Make sure the Collector has a valid Desc by registering it with a
  302. // temporary registry.
  303. prometheus.NewRegistry().MustRegister(c)
  304. // Create a ConstMetric with the Desc. Since we don't know how many
  305. // variable labels there are, try for as long as it needs.
  306. for err := errors.New("dummy"); err != nil; lvs = append(lvs, magicString) {
  307. m, err = prometheus.NewConstMetric(desc, prometheus.UntypedValue, 0, lvs...)
  308. }
  309. // Write out the metric into a proto message and look at the labels.
  310. // If the value is not the magicString, it is a constLabel, which doesn't interest us.
  311. // If the label is curried, it doesn't interest us.
  312. // In all other cases, only "code" or "method" is allowed.
  313. if err := m.Write(&pm); err != nil {
  314. panic("error checking metric for labels")
  315. }
  316. for _, label := range pm.Label {
  317. name, value := label.GetName(), label.GetValue()
  318. if value != magicString || isLabelCurried(c, name) {
  319. continue
  320. }
  321. switch name {
  322. case "code":
  323. code = true
  324. case "method":
  325. method = true
  326. default:
  327. panic("metric partitioned with non-supported labels")
  328. }
  329. }
  330. return
  331. }
  332. func isLabelCurried(c prometheus.Collector, label string) bool {
  333. // This is even hackier than the label test above.
  334. // We essentially try to curry again and see if it works.
  335. // But for that, we need to type-convert to the two
  336. // types we use here, ObserverVec or *CounterVec.
  337. switch v := c.(type) {
  338. case *prometheus.CounterVec:
  339. if _, err := v.CurryWith(prometheus.Labels{label: "dummy"}); err == nil {
  340. return false
  341. }
  342. case prometheus.ObserverVec:
  343. if _, err := v.CurryWith(prometheus.Labels{label: "dummy"}); err == nil {
  344. return false
  345. }
  346. default:
  347. panic("unsupported metric vec type")
  348. }
  349. return true
  350. }
  351. // emptyLabels is a one-time allocation for non-partitioned metrics to avoid
  352. // unnecessary allocations on each request.
  353. var emptyLabels = prometheus.Labels{}
  354. func labels(code, method bool, reqMethod string, status int, extraMethods ...string) prometheus.Labels {
  355. if !(code || method) {
  356. return emptyLabels
  357. }
  358. labels := prometheus.Labels{}
  359. if code {
  360. labels["code"] = sanitizeCode(status)
  361. }
  362. if method {
  363. labels["method"] = sanitizeMethod(reqMethod, extraMethods...)
  364. }
  365. return labels
  366. }
  367. func computeApproximateRequestSize(r *http.Request) int {
  368. s := 0
  369. if r.URL != nil {
  370. s += len(r.URL.String())
  371. }
  372. s += len(r.Method)
  373. s += len(r.Proto)
  374. for name, values := range r.Header {
  375. s += len(name)
  376. for _, value := range values {
  377. s += len(value)
  378. }
  379. }
  380. s += len(r.Host)
  381. // N.B. r.Form and r.MultipartForm are assumed to be included in r.URL.
  382. if r.ContentLength != -1 {
  383. s += int(r.ContentLength)
  384. }
  385. return s
  386. }
  387. // If the wrapped http.Handler has a known method, it will be sanitized and returned.
  388. // Otherwise, "unknown" will be returned. The known method list can be extended
  389. // as needed by using extraMethods parameter.
  390. func sanitizeMethod(m string, extraMethods ...string) string {
  391. // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods for
  392. // the methods chosen as default.
  393. switch m {
  394. case "GET", "get":
  395. return "get"
  396. case "PUT", "put":
  397. return "put"
  398. case "HEAD", "head":
  399. return "head"
  400. case "POST", "post":
  401. return "post"
  402. case "DELETE", "delete":
  403. return "delete"
  404. case "CONNECT", "connect":
  405. return "connect"
  406. case "OPTIONS", "options":
  407. return "options"
  408. case "NOTIFY", "notify":
  409. return "notify"
  410. case "TRACE", "trace":
  411. return "trace"
  412. case "PATCH", "patch":
  413. return "patch"
  414. default:
  415. for _, method := range extraMethods {
  416. if strings.EqualFold(m, method) {
  417. return strings.ToLower(m)
  418. }
  419. }
  420. return "unknown"
  421. }
  422. }
  423. // If the wrapped http.Handler has not set a status code, i.e. the value is
  424. // currently 0, sanitizeCode will return 200, for consistency with behavior in
  425. // the stdlib.
  426. func sanitizeCode(s int) string {
  427. // See for accepted codes https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
  428. switch s {
  429. case 100:
  430. return "100"
  431. case 101:
  432. return "101"
  433. case 200, 0:
  434. return "200"
  435. case 201:
  436. return "201"
  437. case 202:
  438. return "202"
  439. case 203:
  440. return "203"
  441. case 204:
  442. return "204"
  443. case 205:
  444. return "205"
  445. case 206:
  446. return "206"
  447. case 300:
  448. return "300"
  449. case 301:
  450. return "301"
  451. case 302:
  452. return "302"
  453. case 304:
  454. return "304"
  455. case 305:
  456. return "305"
  457. case 307:
  458. return "307"
  459. case 400:
  460. return "400"
  461. case 401:
  462. return "401"
  463. case 402:
  464. return "402"
  465. case 403:
  466. return "403"
  467. case 404:
  468. return "404"
  469. case 405:
  470. return "405"
  471. case 406:
  472. return "406"
  473. case 407:
  474. return "407"
  475. case 408:
  476. return "408"
  477. case 409:
  478. return "409"
  479. case 410:
  480. return "410"
  481. case 411:
  482. return "411"
  483. case 412:
  484. return "412"
  485. case 413:
  486. return "413"
  487. case 414:
  488. return "414"
  489. case 415:
  490. return "415"
  491. case 416:
  492. return "416"
  493. case 417:
  494. return "417"
  495. case 418:
  496. return "418"
  497. case 500:
  498. return "500"
  499. case 501:
  500. return "501"
  501. case 502:
  502. return "502"
  503. case 503:
  504. return "503"
  505. case 504:
  506. return "504"
  507. case 505:
  508. return "505"
  509. case 428:
  510. return "428"
  511. case 429:
  512. return "429"
  513. case 431:
  514. return "431"
  515. case 511:
  516. return "511"
  517. default:
  518. if s >= 100 && s <= 599 {
  519. return strconv.Itoa(s)
  520. }
  521. return "unknown"
  522. }
  523. }