http.go 14 KB

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