You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

http.go 13KB

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