metrics.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  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 proxy
  14. import (
  15. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus"
  16. "net/http"
  17. "strconv"
  18. "time"
  19. )
  20. var (
  21. requestsIncoming = prometheus.NewCounterVec(
  22. prometheus.CounterOpts{
  23. Namespace: "etcd",
  24. Subsystem: "proxy",
  25. Name: "requests_total",
  26. Help: "Counter requests incoming by method.",
  27. }, []string{"method"})
  28. requestsHandled = prometheus.NewCounterVec(
  29. prometheus.CounterOpts{
  30. Namespace: "etcd",
  31. Subsystem: "proxy",
  32. Name: "handled_total",
  33. Help: "Counter of requests fully handled (by authoratitave servers)",
  34. }, []string{"method", "code"})
  35. requestsDropped = prometheus.NewCounterVec(
  36. prometheus.CounterOpts{
  37. Namespace: "etcd",
  38. Subsystem: "proxy",
  39. Name: "dropped_total",
  40. Help: "Counter of requests dropped on the proxy.",
  41. }, []string{"method", "proxying_error"})
  42. requestsHandlingTime = prometheus.NewHistogramVec(
  43. prometheus.HistogramOpts{
  44. Namespace: "etcd",
  45. Subsystem: "proxy",
  46. Name: "handling_duration_seconds",
  47. Help: "Bucketed histogram of handling time of successful events (non-watches), by method " +
  48. "(GET/PUT etc.).",
  49. Buckets: prometheus.ExponentialBuckets(0.0005, 2, 13),
  50. }, []string{"method"})
  51. )
  52. type forwardingError string
  53. const (
  54. zeroEndpoints forwardingError = "zero_endpoints"
  55. failedSendingRequest forwardingError = "failed_sending_request"
  56. failedGettingResponse forwardingError = "failed_getting_response"
  57. )
  58. func init() {
  59. prometheus.MustRegister(requestsIncoming)
  60. prometheus.MustRegister(requestsHandled)
  61. prometheus.MustRegister(requestsDropped)
  62. prometheus.MustRegister(requestsHandlingTime)
  63. }
  64. func reportIncomingRequest(request *http.Request) {
  65. requestsIncoming.WithLabelValues(request.Method).Inc()
  66. }
  67. func reportRequestHandled(request *http.Request, response *http.Response, startTime time.Time) {
  68. method := request.Method
  69. requestsHandled.WithLabelValues(method, strconv.Itoa(response.StatusCode)).Inc()
  70. requestsHandlingTime.WithLabelValues(method).Observe(time.Since(startTime).Seconds())
  71. }
  72. func reportRequestDropped(request *http.Request, err forwardingError) {
  73. requestsDropped.WithLabelValues(request.Method, string(err)).Inc()
  74. }