http.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // Copyright 2014 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 prometheus
  14. import (
  15. "bufio"
  16. "bytes"
  17. "compress/gzip"
  18. "fmt"
  19. "io"
  20. "net"
  21. "net/http"
  22. "strconv"
  23. "strings"
  24. "sync"
  25. "time"
  26. "github.com/prometheus/common/expfmt"
  27. )
  28. // TODO(beorn7): Remove this whole file. It is a partial mirror of
  29. // promhttp/http.go (to avoid circular import chains) where everything HTTP
  30. // related should live. The functions here are just for avoiding
  31. // breakage. Everything is deprecated.
  32. const (
  33. contentTypeHeader = "Content-Type"
  34. contentLengthHeader = "Content-Length"
  35. contentEncodingHeader = "Content-Encoding"
  36. acceptEncodingHeader = "Accept-Encoding"
  37. )
  38. var bufPool sync.Pool
  39. func getBuf() *bytes.Buffer {
  40. buf := bufPool.Get()
  41. if buf == nil {
  42. return &bytes.Buffer{}
  43. }
  44. return buf.(*bytes.Buffer)
  45. }
  46. func giveBuf(buf *bytes.Buffer) {
  47. buf.Reset()
  48. bufPool.Put(buf)
  49. }
  50. // Handler returns an HTTP handler for the DefaultGatherer. It is
  51. // already instrumented with InstrumentHandler (using "prometheus" as handler
  52. // name).
  53. //
  54. // Deprecated: Please note the issues described in the doc comment of
  55. // InstrumentHandler. You might want to consider using promhttp.Handler instead
  56. // (which is not instrumented, but can be instrumented with the tooling provided
  57. // in package promhttp).
  58. func Handler() http.Handler {
  59. return InstrumentHandler("prometheus", UninstrumentedHandler())
  60. }
  61. // UninstrumentedHandler returns an HTTP handler for the DefaultGatherer.
  62. //
  63. // Deprecated: Use promhttp.Handler instead. See there for further documentation.
  64. func UninstrumentedHandler() http.Handler {
  65. return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  66. mfs, err := DefaultGatherer.Gather()
  67. if err != nil {
  68. http.Error(w, "An error has occurred during metrics collection:\n\n"+err.Error(), http.StatusInternalServerError)
  69. return
  70. }
  71. contentType := expfmt.Negotiate(req.Header)
  72. buf := getBuf()
  73. defer giveBuf(buf)
  74. writer, encoding := decorateWriter(req, buf)
  75. enc := expfmt.NewEncoder(writer, contentType)
  76. var lastErr error
  77. for _, mf := range mfs {
  78. if err := enc.Encode(mf); err != nil {
  79. lastErr = err
  80. http.Error(w, "An error has occurred during metrics encoding:\n\n"+err.Error(), http.StatusInternalServerError)
  81. return
  82. }
  83. }
  84. if closer, ok := writer.(io.Closer); ok {
  85. closer.Close()
  86. }
  87. if lastErr != nil && buf.Len() == 0 {
  88. http.Error(w, "No metrics encoded, last error:\n\n"+lastErr.Error(), http.StatusInternalServerError)
  89. return
  90. }
  91. header := w.Header()
  92. header.Set(contentTypeHeader, string(contentType))
  93. header.Set(contentLengthHeader, fmt.Sprint(buf.Len()))
  94. if encoding != "" {
  95. header.Set(contentEncodingHeader, encoding)
  96. }
  97. w.Write(buf.Bytes())
  98. })
  99. }
  100. // decorateWriter wraps a writer to handle gzip compression if requested. It
  101. // returns the decorated writer and the appropriate "Content-Encoding" header
  102. // (which is empty if no compression is enabled).
  103. func decorateWriter(request *http.Request, writer io.Writer) (io.Writer, string) {
  104. header := request.Header.Get(acceptEncodingHeader)
  105. parts := strings.Split(header, ",")
  106. for _, part := range parts {
  107. part := strings.TrimSpace(part)
  108. if part == "gzip" || strings.HasPrefix(part, "gzip;") {
  109. return gzip.NewWriter(writer), "gzip"
  110. }
  111. }
  112. return writer, ""
  113. }
  114. var instLabels = []string{"method", "code"}
  115. type nower interface {
  116. Now() time.Time
  117. }
  118. type nowFunc func() time.Time
  119. func (n nowFunc) Now() time.Time {
  120. return n()
  121. }
  122. var now nower = nowFunc(func() time.Time {
  123. return time.Now()
  124. })
  125. func nowSeries(t ...time.Time) nower {
  126. return nowFunc(func() time.Time {
  127. defer func() {
  128. t = t[1:]
  129. }()
  130. return t[0]
  131. })
  132. }
  133. // InstrumentHandler wraps the given HTTP handler for instrumentation. It
  134. // registers four metric collectors (if not already done) and reports HTTP
  135. // metrics to the (newly or already) registered collectors: http_requests_total
  136. // (CounterVec), http_request_duration_microseconds (Summary),
  137. // http_request_size_bytes (Summary), http_response_size_bytes (Summary). Each
  138. // has a constant label named "handler" with the provided handlerName as
  139. // value. http_requests_total is a metric vector partitioned by HTTP method
  140. // (label name "method") and HTTP status code (label name "code").
  141. //
  142. // Deprecated: InstrumentHandler has several issues. Use the tooling provided in
  143. // package promhttp instead. The issues are the following:
  144. //
  145. // - It uses Summaries rather than Histograms. Summaries are not useful if
  146. // aggregation across multiple instances is required.
  147. //
  148. // - It uses microseconds as unit, which is deprecated and should be replaced by
  149. // seconds.
  150. //
  151. // - The size of the request is calculated in a separate goroutine. Since this
  152. // calculator requires access to the request header, it creates a race with
  153. // any writes to the header performed during request handling.
  154. // httputil.ReverseProxy is a prominent example for a handler
  155. // performing such writes.
  156. //
  157. // - It has additional issues with HTTP/2, cf.
  158. // https://github.com/prometheus/client_golang/issues/272.
  159. func InstrumentHandler(handlerName string, handler http.Handler) http.HandlerFunc {
  160. return InstrumentHandlerFunc(handlerName, handler.ServeHTTP)
  161. }
  162. // InstrumentHandlerFunc wraps the given function for instrumentation. It
  163. // otherwise works in the same way as InstrumentHandler (and shares the same
  164. // issues).
  165. //
  166. // Deprecated: InstrumentHandlerFunc is deprecated for the same reasons as
  167. // InstrumentHandler is. Use the tooling provided in package promhttp instead.
  168. func InstrumentHandlerFunc(handlerName string, handlerFunc func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
  169. return InstrumentHandlerFuncWithOpts(
  170. SummaryOpts{
  171. Subsystem: "http",
  172. ConstLabels: Labels{"handler": handlerName},
  173. Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
  174. },
  175. handlerFunc,
  176. )
  177. }
  178. // InstrumentHandlerWithOpts works like InstrumentHandler (and shares the same
  179. // issues) but provides more flexibility (at the cost of a more complex call
  180. // syntax). As InstrumentHandler, this function registers four metric
  181. // collectors, but it uses the provided SummaryOpts to create them. However, the
  182. // fields "Name" and "Help" in the SummaryOpts are ignored. "Name" is replaced
  183. // by "requests_total", "request_duration_microseconds", "request_size_bytes",
  184. // and "response_size_bytes", respectively. "Help" is replaced by an appropriate
  185. // help string. The names of the variable labels of the http_requests_total
  186. // CounterVec are "method" (get, post, etc.), and "code" (HTTP status code).
  187. //
  188. // If InstrumentHandlerWithOpts is called as follows, it mimics exactly the
  189. // behavior of InstrumentHandler:
  190. //
  191. // prometheus.InstrumentHandlerWithOpts(
  192. // prometheus.SummaryOpts{
  193. // Subsystem: "http",
  194. // ConstLabels: prometheus.Labels{"handler": handlerName},
  195. // },
  196. // handler,
  197. // )
  198. //
  199. // Technical detail: "requests_total" is a CounterVec, not a SummaryVec, so it
  200. // cannot use SummaryOpts. Instead, a CounterOpts struct is created internally,
  201. // and all its fields are set to the equally named fields in the provided
  202. // SummaryOpts.
  203. //
  204. // Deprecated: InstrumentHandlerWithOpts is deprecated for the same reasons as
  205. // InstrumentHandler is. Use the tooling provided in package promhttp instead.
  206. func InstrumentHandlerWithOpts(opts SummaryOpts, handler http.Handler) http.HandlerFunc {
  207. return InstrumentHandlerFuncWithOpts(opts, handler.ServeHTTP)
  208. }
  209. // InstrumentHandlerFuncWithOpts works like InstrumentHandlerFunc (and shares
  210. // the same issues) but provides more flexibility (at the cost of a more complex
  211. // call syntax). See InstrumentHandlerWithOpts for details how the provided
  212. // SummaryOpts are used.
  213. //
  214. // Deprecated: InstrumentHandlerFuncWithOpts is deprecated for the same reasons
  215. // as InstrumentHandler is. Use the tooling provided in package promhttp instead.
  216. func InstrumentHandlerFuncWithOpts(opts SummaryOpts, handlerFunc func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
  217. reqCnt := NewCounterVec(
  218. CounterOpts{
  219. Namespace: opts.Namespace,
  220. Subsystem: opts.Subsystem,
  221. Name: "requests_total",
  222. Help: "Total number of HTTP requests made.",
  223. ConstLabels: opts.ConstLabels,
  224. },
  225. instLabels,
  226. )
  227. if err := Register(reqCnt); err != nil {
  228. if are, ok := err.(AlreadyRegisteredError); ok {
  229. reqCnt = are.ExistingCollector.(*CounterVec)
  230. } else {
  231. panic(err)
  232. }
  233. }
  234. opts.Name = "request_duration_microseconds"
  235. opts.Help = "The HTTP request latencies in microseconds."
  236. reqDur := NewSummary(opts)
  237. if err := Register(reqDur); err != nil {
  238. if are, ok := err.(AlreadyRegisteredError); ok {
  239. reqDur = are.ExistingCollector.(Summary)
  240. } else {
  241. panic(err)
  242. }
  243. }
  244. opts.Name = "request_size_bytes"
  245. opts.Help = "The HTTP request sizes in bytes."
  246. reqSz := NewSummary(opts)
  247. if err := Register(reqSz); err != nil {
  248. if are, ok := err.(AlreadyRegisteredError); ok {
  249. reqSz = are.ExistingCollector.(Summary)
  250. } else {
  251. panic(err)
  252. }
  253. }
  254. opts.Name = "response_size_bytes"
  255. opts.Help = "The HTTP response sizes in bytes."
  256. resSz := NewSummary(opts)
  257. if err := Register(resSz); err != nil {
  258. if are, ok := err.(AlreadyRegisteredError); ok {
  259. resSz = are.ExistingCollector.(Summary)
  260. } else {
  261. panic(err)
  262. }
  263. }
  264. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  265. now := time.Now()
  266. delegate := &responseWriterDelegator{ResponseWriter: w}
  267. out := computeApproximateRequestSize(r)
  268. _, cn := w.(http.CloseNotifier)
  269. _, fl := w.(http.Flusher)
  270. _, hj := w.(http.Hijacker)
  271. _, rf := w.(io.ReaderFrom)
  272. var rw http.ResponseWriter
  273. if cn && fl && hj && rf {
  274. rw = &fancyResponseWriterDelegator{delegate}
  275. } else {
  276. rw = delegate
  277. }
  278. handlerFunc(rw, r)
  279. elapsed := float64(time.Since(now)) / float64(time.Microsecond)
  280. method := sanitizeMethod(r.Method)
  281. code := sanitizeCode(delegate.status)
  282. reqCnt.WithLabelValues(method, code).Inc()
  283. reqDur.Observe(elapsed)
  284. resSz.Observe(float64(delegate.written))
  285. reqSz.Observe(float64(<-out))
  286. })
  287. }
  288. func computeApproximateRequestSize(r *http.Request) <-chan int {
  289. // Get URL length in current go routine for avoiding a race condition.
  290. // HandlerFunc that runs in parallel may modify the URL.
  291. s := 0
  292. if r.URL != nil {
  293. s += len(r.URL.String())
  294. }
  295. out := make(chan int, 1)
  296. go func() {
  297. s += len(r.Method)
  298. s += len(r.Proto)
  299. for name, values := range r.Header {
  300. s += len(name)
  301. for _, value := range values {
  302. s += len(value)
  303. }
  304. }
  305. s += len(r.Host)
  306. // N.B. r.Form and r.MultipartForm are assumed to be included in r.URL.
  307. if r.ContentLength != -1 {
  308. s += int(r.ContentLength)
  309. }
  310. out <- s
  311. close(out)
  312. }()
  313. return out
  314. }
  315. type responseWriterDelegator struct {
  316. http.ResponseWriter
  317. handler, method string
  318. status int
  319. written int64
  320. wroteHeader bool
  321. }
  322. func (r *responseWriterDelegator) WriteHeader(code int) {
  323. r.status = code
  324. r.wroteHeader = true
  325. r.ResponseWriter.WriteHeader(code)
  326. }
  327. func (r *responseWriterDelegator) Write(b []byte) (int, error) {
  328. if !r.wroteHeader {
  329. r.WriteHeader(http.StatusOK)
  330. }
  331. n, err := r.ResponseWriter.Write(b)
  332. r.written += int64(n)
  333. return n, err
  334. }
  335. type fancyResponseWriterDelegator struct {
  336. *responseWriterDelegator
  337. }
  338. func (f *fancyResponseWriterDelegator) CloseNotify() <-chan bool {
  339. return f.ResponseWriter.(http.CloseNotifier).CloseNotify()
  340. }
  341. func (f *fancyResponseWriterDelegator) Flush() {
  342. f.ResponseWriter.(http.Flusher).Flush()
  343. }
  344. func (f *fancyResponseWriterDelegator) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  345. return f.ResponseWriter.(http.Hijacker).Hijack()
  346. }
  347. func (f *fancyResponseWriterDelegator) ReadFrom(r io.Reader) (int64, error) {
  348. if !f.wroteHeader {
  349. f.WriteHeader(http.StatusOK)
  350. }
  351. n, err := f.ResponseWriter.(io.ReaderFrom).ReadFrom(r)
  352. f.written += n
  353. return n, err
  354. }
  355. func sanitizeMethod(m string) string {
  356. switch m {
  357. case "GET", "get":
  358. return "get"
  359. case "PUT", "put":
  360. return "put"
  361. case "HEAD", "head":
  362. return "head"
  363. case "POST", "post":
  364. return "post"
  365. case "DELETE", "delete":
  366. return "delete"
  367. case "CONNECT", "connect":
  368. return "connect"
  369. case "OPTIONS", "options":
  370. return "options"
  371. case "NOTIFY", "notify":
  372. return "notify"
  373. default:
  374. return strings.ToLower(m)
  375. }
  376. }
  377. func sanitizeCode(s int) string {
  378. switch s {
  379. case 100:
  380. return "100"
  381. case 101:
  382. return "101"
  383. case 200:
  384. return "200"
  385. case 201:
  386. return "201"
  387. case 202:
  388. return "202"
  389. case 203:
  390. return "203"
  391. case 204:
  392. return "204"
  393. case 205:
  394. return "205"
  395. case 206:
  396. return "206"
  397. case 300:
  398. return "300"
  399. case 301:
  400. return "301"
  401. case 302:
  402. return "302"
  403. case 304:
  404. return "304"
  405. case 305:
  406. return "305"
  407. case 307:
  408. return "307"
  409. case 400:
  410. return "400"
  411. case 401:
  412. return "401"
  413. case 402:
  414. return "402"
  415. case 403:
  416. return "403"
  417. case 404:
  418. return "404"
  419. case 405:
  420. return "405"
  421. case 406:
  422. return "406"
  423. case 407:
  424. return "407"
  425. case 408:
  426. return "408"
  427. case 409:
  428. return "409"
  429. case 410:
  430. return "410"
  431. case 411:
  432. return "411"
  433. case 412:
  434. return "412"
  435. case 413:
  436. return "413"
  437. case 414:
  438. return "414"
  439. case 415:
  440. return "415"
  441. case 416:
  442. return "416"
  443. case 417:
  444. return "417"
  445. case 418:
  446. return "418"
  447. case 500:
  448. return "500"
  449. case 501:
  450. return "501"
  451. case 502:
  452. return "502"
  453. case 503:
  454. return "503"
  455. case 504:
  456. return "504"
  457. case 505:
  458. return "505"
  459. case 428:
  460. return "428"
  461. case 429:
  462. return "429"
  463. case 431:
  464. return "431"
  465. case 511:
  466. return "511"
  467. default:
  468. return strconv.Itoa(s)
  469. }
  470. }