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.

instrument_server.go 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. func labels(code, method bool, reqMethod string, status int, extraMethods ...string) prometheus.Labels {
  352. labels := prometheus.Labels{}
  353. if !(code || method) {
  354. return labels
  355. }
  356. if code {
  357. labels["code"] = sanitizeCode(status)
  358. }
  359. if method {
  360. labels["method"] = sanitizeMethod(reqMethod, extraMethods...)
  361. }
  362. return labels
  363. }
  364. func computeApproximateRequestSize(r *http.Request) int {
  365. s := 0
  366. if r.URL != nil {
  367. s += len(r.URL.String())
  368. }
  369. s += len(r.Method)
  370. s += len(r.Proto)
  371. for name, values := range r.Header {
  372. s += len(name)
  373. for _, value := range values {
  374. s += len(value)
  375. }
  376. }
  377. s += len(r.Host)
  378. // N.B. r.Form and r.MultipartForm are assumed to be included in r.URL.
  379. if r.ContentLength != -1 {
  380. s += int(r.ContentLength)
  381. }
  382. return s
  383. }
  384. // If the wrapped http.Handler has a known method, it will be sanitized and returned.
  385. // Otherwise, "unknown" will be returned. The known method list can be extended
  386. // as needed by using extraMethods parameter.
  387. func sanitizeMethod(m string, extraMethods ...string) string {
  388. // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods for
  389. // the methods chosen as default.
  390. switch m {
  391. case "GET", "get":
  392. return "get"
  393. case "PUT", "put":
  394. return "put"
  395. case "HEAD", "head":
  396. return "head"
  397. case "POST", "post":
  398. return "post"
  399. case "DELETE", "delete":
  400. return "delete"
  401. case "CONNECT", "connect":
  402. return "connect"
  403. case "OPTIONS", "options":
  404. return "options"
  405. case "NOTIFY", "notify":
  406. return "notify"
  407. case "TRACE", "trace":
  408. return "trace"
  409. case "PATCH", "patch":
  410. return "patch"
  411. default:
  412. for _, method := range extraMethods {
  413. if strings.EqualFold(m, method) {
  414. return strings.ToLower(m)
  415. }
  416. }
  417. return "unknown"
  418. }
  419. }
  420. // If the wrapped http.Handler has not set a status code, i.e. the value is
  421. // currently 0, sanitizeCode will return 200, for consistency with behavior in
  422. // the stdlib.
  423. func sanitizeCode(s int) string {
  424. // See for accepted codes https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
  425. switch s {
  426. case 100:
  427. return "100"
  428. case 101:
  429. return "101"
  430. case 200, 0:
  431. return "200"
  432. case 201:
  433. return "201"
  434. case 202:
  435. return "202"
  436. case 203:
  437. return "203"
  438. case 204:
  439. return "204"
  440. case 205:
  441. return "205"
  442. case 206:
  443. return "206"
  444. case 300:
  445. return "300"
  446. case 301:
  447. return "301"
  448. case 302:
  449. return "302"
  450. case 304:
  451. return "304"
  452. case 305:
  453. return "305"
  454. case 307:
  455. return "307"
  456. case 400:
  457. return "400"
  458. case 401:
  459. return "401"
  460. case 402:
  461. return "402"
  462. case 403:
  463. return "403"
  464. case 404:
  465. return "404"
  466. case 405:
  467. return "405"
  468. case 406:
  469. return "406"
  470. case 407:
  471. return "407"
  472. case 408:
  473. return "408"
  474. case 409:
  475. return "409"
  476. case 410:
  477. return "410"
  478. case 411:
  479. return "411"
  480. case 412:
  481. return "412"
  482. case 413:
  483. return "413"
  484. case 414:
  485. return "414"
  486. case 415:
  487. return "415"
  488. case 416:
  489. return "416"
  490. case 417:
  491. return "417"
  492. case 418:
  493. return "418"
  494. case 500:
  495. return "500"
  496. case 501:
  497. return "501"
  498. case 502:
  499. return "502"
  500. case 503:
  501. return "503"
  502. case 504:
  503. return "504"
  504. case 505:
  505. return "505"
  506. case 428:
  507. return "428"
  508. case 429:
  509. return "429"
  510. case 431:
  511. return "431"
  512. case 511:
  513. return "511"
  514. default:
  515. if s >= 100 && s <= 599 {
  516. return strconv.Itoa(s)
  517. }
  518. return "unknown"
  519. }
  520. }