metrics.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2015 CoreOS, Inc.
  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 proxy
  15. import (
  16. "net/http"
  17. "strconv"
  18. "time"
  19. "github.com/coreos/etcd/Godeps/_workspace/src/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. requestsHandlingTime = 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 " +
  49. "(GET/PUT etc.).",
  50. Buckets: prometheus.ExponentialBuckets(0.0005, 2, 13),
  51. }, []string{"method"})
  52. )
  53. type forwardingError string
  54. const (
  55. zeroEndpoints forwardingError = "zero_endpoints"
  56. failedSendingRequest forwardingError = "failed_sending_request"
  57. failedGettingResponse forwardingError = "failed_getting_response"
  58. )
  59. func init() {
  60. prometheus.MustRegister(requestsIncoming)
  61. prometheus.MustRegister(requestsHandled)
  62. prometheus.MustRegister(requestsDropped)
  63. prometheus.MustRegister(requestsHandlingTime)
  64. }
  65. func reportIncomingRequest(request *http.Request) {
  66. requestsIncoming.WithLabelValues(request.Method).Inc()
  67. }
  68. func reportRequestHandled(request *http.Request, response *http.Response, startTime time.Time) {
  69. method := request.Method
  70. requestsHandled.WithLabelValues(method, strconv.Itoa(response.StatusCode)).Inc()
  71. requestsHandlingTime.WithLabelValues(method).Observe(time.Since(startTime).Seconds())
  72. }
  73. func reportRequestDropped(request *http.Request, err forwardingError) {
  74. requestsDropped.WithLabelValues(request.Method, string(err)).Inc()
  75. }