metrics.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package httpproxy
  15. import (
  16. "net/http"
  17. "strconv"
  18. "time"
  19. "github.com/prometheus/client_golang/prometheus"
  20. )
  21. var (
  22. requestsIncoming = prometheus.NewCounterVec(
  23. prometheus.CounterOpts{
  24. Namespace: "etcd",
  25. Subsystem: "proxy",
  26. Name: "requests_total",
  27. Help: "Counter requests incoming by method.",
  28. }, []string{"method"})
  29. requestsHandled = prometheus.NewCounterVec(
  30. prometheus.CounterOpts{
  31. Namespace: "etcd",
  32. Subsystem: "proxy",
  33. Name: "handled_total",
  34. Help: "Counter of requests fully handled (by authoratitave servers)",
  35. }, []string{"method", "code"})
  36. requestsDropped = prometheus.NewCounterVec(
  37. prometheus.CounterOpts{
  38. Namespace: "etcd",
  39. Subsystem: "proxy",
  40. Name: "dropped_total",
  41. Help: "Counter of requests dropped on the proxy.",
  42. }, []string{"method", "proxying_error"})
  43. requestsHandlingSec = prometheus.NewHistogramVec(
  44. prometheus.HistogramOpts{
  45. Namespace: "etcd",
  46. Subsystem: "proxy",
  47. Name: "handling_duration_seconds",
  48. Help: "Bucketed histogram of handling time of successful events (non-watches), by method (GET/PUT etc.).",
  49. // lowest bucket start of upper bound 0.0005 sec (0.5 ms) with factor 2
  50. // highest bucket start of 0.0005 sec * 2^12 == 2.048 sec
  51. Buckets: prometheus.ExponentialBuckets(0.0005, 2, 13),
  52. }, []string{"method"})
  53. )
  54. type forwardingError string
  55. const (
  56. zeroEndpoints forwardingError = "zero_endpoints"
  57. failedSendingRequest forwardingError = "failed_sending_request"
  58. failedGettingResponse forwardingError = "failed_getting_response"
  59. )
  60. func init() {
  61. prometheus.MustRegister(requestsIncoming)
  62. prometheus.MustRegister(requestsHandled)
  63. prometheus.MustRegister(requestsDropped)
  64. prometheus.MustRegister(requestsHandlingSec)
  65. }
  66. func reportIncomingRequest(request *http.Request) {
  67. requestsIncoming.WithLabelValues(request.Method).Inc()
  68. }
  69. func reportRequestHandled(request *http.Request, response *http.Response, startTime time.Time) {
  70. method := request.Method
  71. requestsHandled.WithLabelValues(method, strconv.Itoa(response.StatusCode)).Inc()
  72. requestsHandlingSec.WithLabelValues(method).Observe(time.Since(startTime).Seconds())
  73. }
  74. func reportRequestDropped(request *http.Request, err forwardingError) {
  75. requestsDropped.WithLabelValues(request.Method, string(err)).Inc()
  76. }