metrics.go 3.0 KB

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