http.go 13 KB

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