http.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 non instrumented).
  57. func Handler() http.Handler {
  58. return InstrumentHandler("prometheus", UninstrumentedHandler())
  59. }
  60. // UninstrumentedHandler returns an HTTP handler for the DefaultGatherer.
  61. //
  62. // Deprecated: Use promhttp.Handler 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"+err.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. func nowSeries(t ...time.Time) nower {
  125. return nowFunc(func() time.Time {
  126. defer func() {
  127. t = t[1:]
  128. }()
  129. return t[0]
  130. })
  131. }
  132. // InstrumentHandler wraps the given HTTP handler for instrumentation. It
  133. // registers four metric collectors (if not already done) and reports HTTP
  134. // metrics to the (newly or already) registered collectors: http_requests_total
  135. // (CounterVec), http_request_duration_microseconds (Summary),
  136. // http_request_size_bytes (Summary), http_response_size_bytes (Summary). Each
  137. // has a constant label named "handler" with the provided handlerName as
  138. // value. http_requests_total is a metric vector partitioned by HTTP method
  139. // (label name "method") and HTTP status code (label name "code").
  140. //
  141. // Deprecated: InstrumentHandler has several issues:
  142. //
  143. // - It uses Summaries rather than Histograms. Summaries are not useful if
  144. // aggregation across multiple instances is required.
  145. //
  146. // - It uses microseconds as unit, which is deprecated and should be replaced by
  147. // seconds.
  148. //
  149. // - The size of the request is calculated in a separate goroutine. Since this
  150. // calculator requires access to the request header, it creates a race with
  151. // any writes to the header performed during request handling.
  152. // httputil.ReverseProxy is a prominent example for a handler
  153. // performing such writes.
  154. //
  155. // Upcoming versions of this package will provide ways of instrumenting HTTP
  156. // handlers that are more flexible and have fewer issues. Please prefer direct
  157. // instrumentation in the meantime.
  158. func InstrumentHandler(handlerName string, handler http.Handler) http.HandlerFunc {
  159. return InstrumentHandlerFunc(handlerName, handler.ServeHTTP)
  160. }
  161. // InstrumentHandlerFunc wraps the given function for instrumentation. It
  162. // otherwise works in the same way as InstrumentHandler (and shares the same
  163. // issues).
  164. //
  165. // Deprecated: InstrumentHandlerFunc is deprecated for the same reasons as
  166. // InstrumentHandler is.
  167. func InstrumentHandlerFunc(handlerName string, handlerFunc func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
  168. return InstrumentHandlerFuncWithOpts(
  169. SummaryOpts{
  170. Subsystem: "http",
  171. ConstLabels: Labels{"handler": handlerName},
  172. },
  173. handlerFunc,
  174. )
  175. }
  176. // InstrumentHandlerWithOpts works like InstrumentHandler (and shares the same
  177. // issues) but provides more flexibility (at the cost of a more complex call
  178. // syntax). As InstrumentHandler, this function registers four metric
  179. // collectors, but it uses the provided SummaryOpts to create them. However, the
  180. // fields "Name" and "Help" in the SummaryOpts are ignored. "Name" is replaced
  181. // by "requests_total", "request_duration_microseconds", "request_size_bytes",
  182. // and "response_size_bytes", respectively. "Help" is replaced by an appropriate
  183. // help string. The names of the variable labels of the http_requests_total
  184. // CounterVec are "method" (get, post, etc.), and "code" (HTTP status code).
  185. //
  186. // If InstrumentHandlerWithOpts is called as follows, it mimics exactly the
  187. // behavior of InstrumentHandler:
  188. //
  189. // prometheus.InstrumentHandlerWithOpts(
  190. // prometheus.SummaryOpts{
  191. // Subsystem: "http",
  192. // ConstLabels: prometheus.Labels{"handler": handlerName},
  193. // },
  194. // handler,
  195. // )
  196. //
  197. // Technical detail: "requests_total" is a CounterVec, not a SummaryVec, so it
  198. // cannot use SummaryOpts. Instead, a CounterOpts struct is created internally,
  199. // and all its fields are set to the equally named fields in the provided
  200. // SummaryOpts.
  201. //
  202. // Deprecated: InstrumentHandlerWithOpts is deprecated for the same reasons as
  203. // InstrumentHandler is.
  204. func InstrumentHandlerWithOpts(opts SummaryOpts, handler http.Handler) http.HandlerFunc {
  205. return InstrumentHandlerFuncWithOpts(opts, handler.ServeHTTP)
  206. }
  207. // InstrumentHandlerFuncWithOpts works like InstrumentHandlerFunc (and shares
  208. // the same issues) but provides more flexibility (at the cost of a more complex
  209. // call syntax). See InstrumentHandlerWithOpts for details how the provided
  210. // SummaryOpts are used.
  211. //
  212. // Deprecated: InstrumentHandlerFuncWithOpts is deprecated for the same reasons
  213. // as InstrumentHandler is.
  214. func InstrumentHandlerFuncWithOpts(opts SummaryOpts, handlerFunc func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
  215. reqCnt := NewCounterVec(
  216. CounterOpts{
  217. Namespace: opts.Namespace,
  218. Subsystem: opts.Subsystem,
  219. Name: "requests_total",
  220. Help: "Total number of HTTP requests made.",
  221. ConstLabels: opts.ConstLabels,
  222. },
  223. instLabels,
  224. )
  225. opts.Name = "request_duration_microseconds"
  226. opts.Help = "The HTTP request latencies in microseconds."
  227. reqDur := NewSummary(opts)
  228. opts.Name = "request_size_bytes"
  229. opts.Help = "The HTTP request sizes in bytes."
  230. reqSz := NewSummary(opts)
  231. opts.Name = "response_size_bytes"
  232. opts.Help = "The HTTP response sizes in bytes."
  233. resSz := NewSummary(opts)
  234. regReqCnt := MustRegisterOrGet(reqCnt).(*CounterVec)
  235. regReqDur := MustRegisterOrGet(reqDur).(Summary)
  236. regReqSz := MustRegisterOrGet(reqSz).(Summary)
  237. regResSz := MustRegisterOrGet(resSz).(Summary)
  238. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  239. now := time.Now()
  240. delegate := &responseWriterDelegator{ResponseWriter: w}
  241. out := make(chan int)
  242. urlLen := 0
  243. if r.URL != nil {
  244. urlLen = len(r.URL.String())
  245. }
  246. go computeApproximateRequestSize(r, out, urlLen)
  247. _, cn := w.(http.CloseNotifier)
  248. _, fl := w.(http.Flusher)
  249. _, hj := w.(http.Hijacker)
  250. _, rf := w.(io.ReaderFrom)
  251. var rw http.ResponseWriter
  252. if cn && fl && hj && rf {
  253. rw = &fancyResponseWriterDelegator{delegate}
  254. } else {
  255. rw = delegate
  256. }
  257. handlerFunc(rw, r)
  258. elapsed := float64(time.Since(now)) / float64(time.Microsecond)
  259. method := sanitizeMethod(r.Method)
  260. code := sanitizeCode(delegate.status)
  261. regReqCnt.WithLabelValues(method, code).Inc()
  262. regReqDur.Observe(elapsed)
  263. regResSz.Observe(float64(delegate.written))
  264. regReqSz.Observe(float64(<-out))
  265. })
  266. }
  267. func computeApproximateRequestSize(r *http.Request, out chan int, s int) {
  268. s += len(r.Method)
  269. s += len(r.Proto)
  270. for name, values := range r.Header {
  271. s += len(name)
  272. for _, value := range values {
  273. s += len(value)
  274. }
  275. }
  276. s += len(r.Host)
  277. // N.B. r.Form and r.MultipartForm are assumed to be included in r.URL.
  278. if r.ContentLength != -1 {
  279. s += int(r.ContentLength)
  280. }
  281. out <- s
  282. }
  283. type responseWriterDelegator struct {
  284. http.ResponseWriter
  285. handler, method string
  286. status int
  287. written int64
  288. wroteHeader bool
  289. }
  290. func (r *responseWriterDelegator) WriteHeader(code int) {
  291. r.status = code
  292. r.wroteHeader = true
  293. r.ResponseWriter.WriteHeader(code)
  294. }
  295. func (r *responseWriterDelegator) Write(b []byte) (int, error) {
  296. if !r.wroteHeader {
  297. r.WriteHeader(http.StatusOK)
  298. }
  299. n, err := r.ResponseWriter.Write(b)
  300. r.written += int64(n)
  301. return n, err
  302. }
  303. type fancyResponseWriterDelegator struct {
  304. *responseWriterDelegator
  305. }
  306. func (f *fancyResponseWriterDelegator) CloseNotify() <-chan bool {
  307. return f.ResponseWriter.(http.CloseNotifier).CloseNotify()
  308. }
  309. func (f *fancyResponseWriterDelegator) Flush() {
  310. f.ResponseWriter.(http.Flusher).Flush()
  311. }
  312. func (f *fancyResponseWriterDelegator) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  313. return f.ResponseWriter.(http.Hijacker).Hijack()
  314. }
  315. func (f *fancyResponseWriterDelegator) ReadFrom(r io.Reader) (int64, error) {
  316. if !f.wroteHeader {
  317. f.WriteHeader(http.StatusOK)
  318. }
  319. n, err := f.ResponseWriter.(io.ReaderFrom).ReadFrom(r)
  320. f.written += n
  321. return n, err
  322. }
  323. func sanitizeMethod(m string) string {
  324. switch m {
  325. case "GET", "get":
  326. return "get"
  327. case "PUT", "put":
  328. return "put"
  329. case "HEAD", "head":
  330. return "head"
  331. case "POST", "post":
  332. return "post"
  333. case "DELETE", "delete":
  334. return "delete"
  335. case "CONNECT", "connect":
  336. return "connect"
  337. case "OPTIONS", "options":
  338. return "options"
  339. case "NOTIFY", "notify":
  340. return "notify"
  341. default:
  342. return strings.ToLower(m)
  343. }
  344. }
  345. func sanitizeCode(s int) string {
  346. switch s {
  347. case 100:
  348. return "100"
  349. case 101:
  350. return "101"
  351. case 200:
  352. return "200"
  353. case 201:
  354. return "201"
  355. case 202:
  356. return "202"
  357. case 203:
  358. return "203"
  359. case 204:
  360. return "204"
  361. case 205:
  362. return "205"
  363. case 206:
  364. return "206"
  365. case 300:
  366. return "300"
  367. case 301:
  368. return "301"
  369. case 302:
  370. return "302"
  371. case 304:
  372. return "304"
  373. case 305:
  374. return "305"
  375. case 307:
  376. return "307"
  377. case 400:
  378. return "400"
  379. case 401:
  380. return "401"
  381. case 402:
  382. return "402"
  383. case 403:
  384. return "403"
  385. case 404:
  386. return "404"
  387. case 405:
  388. return "405"
  389. case 406:
  390. return "406"
  391. case 407:
  392. return "407"
  393. case 408:
  394. return "408"
  395. case 409:
  396. return "409"
  397. case 410:
  398. return "410"
  399. case 411:
  400. return "411"
  401. case 412:
  402. return "412"
  403. case 413:
  404. return "413"
  405. case 414:
  406. return "414"
  407. case 415:
  408. return "415"
  409. case 416:
  410. return "416"
  411. case 417:
  412. return "417"
  413. case 418:
  414. return "418"
  415. case 500:
  416. return "500"
  417. case 501:
  418. return "501"
  419. case 502:
  420. return "502"
  421. case 503:
  422. return "503"
  423. case 504:
  424. return "504"
  425. case 505:
  426. return "505"
  427. case 428:
  428. return "428"
  429. case 429:
  430. return "429"
  431. case 431:
  432. return "431"
  433. case 511:
  434. return "511"
  435. default:
  436. return strconv.Itoa(s)
  437. }
  438. }