metrics.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 v2http
  15. import (
  16. "strconv"
  17. "time"
  18. "net/http"
  19. "github.com/coreos/etcd/etcdserver/api/v2http/httptypes"
  20. "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. "github.com/coreos/etcd/etcdserver/v2error"
  22. "github.com/prometheus/client_golang/prometheus"
  23. )
  24. var (
  25. incomingEvents = prometheus.NewCounterVec(
  26. prometheus.CounterOpts{
  27. Namespace: "etcd",
  28. Subsystem: "http",
  29. Name: "received_total",
  30. Help: "Counter of requests received into the system (successfully parsed and authd).",
  31. }, []string{"method"})
  32. failedEvents = prometheus.NewCounterVec(
  33. prometheus.CounterOpts{
  34. Namespace: "etcd",
  35. Subsystem: "http",
  36. Name: "failed_total",
  37. Help: "Counter of handle failures of requests (non-watches), by method (GET/PUT etc.) and code (400, 500 etc.).",
  38. }, []string{"method", "code"})
  39. successfulEventsHandlingTime = prometheus.NewHistogramVec(
  40. prometheus.HistogramOpts{
  41. Namespace: "etcd",
  42. Subsystem: "http",
  43. Name: "successful_duration_seconds",
  44. Help: "Bucketed histogram of processing time (s) of successfully handled requests (non-watches), by method (GET/PUT etc.).",
  45. Buckets: prometheus.ExponentialBuckets(0.0005, 2, 13),
  46. }, []string{"method"})
  47. )
  48. func init() {
  49. prometheus.MustRegister(incomingEvents)
  50. prometheus.MustRegister(failedEvents)
  51. prometheus.MustRegister(successfulEventsHandlingTime)
  52. }
  53. func reportRequestReceived(request etcdserverpb.Request) {
  54. incomingEvents.WithLabelValues(methodFromRequest(request)).Inc()
  55. }
  56. func reportRequestCompleted(request etcdserverpb.Request, startTime time.Time) {
  57. method := methodFromRequest(request)
  58. successfulEventsHandlingTime.WithLabelValues(method).Observe(time.Since(startTime).Seconds())
  59. }
  60. func reportRequestFailed(request etcdserverpb.Request, err error) {
  61. method := methodFromRequest(request)
  62. failedEvents.WithLabelValues(method, strconv.Itoa(codeFromError(err))).Inc()
  63. }
  64. func methodFromRequest(request etcdserverpb.Request) string {
  65. if request.Method == "GET" && request.Quorum {
  66. return "QGET"
  67. }
  68. return request.Method
  69. }
  70. func codeFromError(err error) int {
  71. if err == nil {
  72. return http.StatusInternalServerError
  73. }
  74. switch e := err.(type) {
  75. case *v2error.Error:
  76. return e.StatusCode()
  77. case *httptypes.HTTPError:
  78. return e.Code
  79. default:
  80. return http.StatusInternalServerError
  81. }
  82. }