instrument_server.go 17 KB

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