metrics.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2016 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 v3rpc
  15. import (
  16. "strings"
  17. "time"
  18. "github.com/prometheus/client_golang/prometheus"
  19. "golang.org/x/net/context"
  20. "google.golang.org/grpc"
  21. )
  22. var (
  23. receivedCounter = prometheus.NewCounterVec(
  24. prometheus.CounterOpts{
  25. Namespace: "etcd",
  26. Subsystem: "grpc",
  27. Name: "requests_total",
  28. Help: "Counter of received requests.",
  29. }, []string{"grpc_service", "grpc_method"})
  30. failedCounter = prometheus.NewCounterVec(
  31. prometheus.CounterOpts{
  32. Namespace: "etcd",
  33. Subsystem: "grpc",
  34. Name: "requests_failed_total",
  35. Help: "Counter of failed requests.",
  36. }, []string{"grpc_service", "grpc_method", "grpc_code"})
  37. handlingDuration = prometheus.NewHistogramVec(
  38. prometheus.HistogramOpts{
  39. Namespace: "etcd",
  40. Subsystem: "grpc",
  41. Name: "unary_requests_duration_seconds",
  42. Help: "Bucketed histogram of processing time (s) of handled unary (non-stream) requests.",
  43. Buckets: prometheus.ExponentialBuckets(0.0005, 2, 13),
  44. }, []string{"grpc_service", "grpc_method"})
  45. )
  46. func metricsUnaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
  47. service, method := splitMethodName(info.FullMethod)
  48. receivedCounter.WithLabelValues(service, method).Inc()
  49. start := time.Now()
  50. resp, err = handler(ctx, req)
  51. if err != nil {
  52. failedCounter.WithLabelValues(service, method, grpc.Code(err).String()).Inc()
  53. }
  54. handlingDuration.WithLabelValues(service, method).Observe(time.Since(start).Seconds())
  55. return resp, err
  56. }
  57. func metricsStreamInterceptor(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
  58. service, method := splitMethodName(info.FullMethod)
  59. receivedCounter.WithLabelValues(service, method).Inc()
  60. err := handler(srv, ss)
  61. if err != nil {
  62. failedCounter.WithLabelValues(service, method, grpc.Code(err).String()).Inc()
  63. }
  64. return err
  65. }
  66. func splitMethodName(fullMethodName string) (string, string) {
  67. fullMethodName = strings.TrimPrefix(fullMethodName, "/") // remove leading slash
  68. if i := strings.Index(fullMethodName, "/"); i >= 0 {
  69. return fullMethodName[:i], fullMethodName[i+1:]
  70. }
  71. return "unknown", "unknown"
  72. }
  73. func init() {
  74. prometheus.MustRegister(receivedCounter)
  75. prometheus.MustRegister(failedCounter)
  76. prometheus.MustRegister(handlingDuration)
  77. }