client_reporter.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2016 Michal Witkowski. All Rights Reserved.
  2. // See LICENSE for licensing terms.
  3. package grpc_prometheus
  4. import (
  5. "time"
  6. "google.golang.org/grpc/codes"
  7. prom "github.com/prometheus/client_golang/prometheus"
  8. )
  9. var (
  10. clientStartedCounter = prom.NewCounterVec(
  11. prom.CounterOpts{
  12. Namespace: "grpc",
  13. Subsystem: "client",
  14. Name: "started_total",
  15. Help: "Total number of RPCs started on the client.",
  16. }, []string{"grpc_type", "grpc_service", "grpc_method"})
  17. clientHandledCounter = prom.NewCounterVec(
  18. prom.CounterOpts{
  19. Namespace: "grpc",
  20. Subsystem: "client",
  21. Name: "handled_total",
  22. Help: "Total number of RPCs completed by the client, regardless of success or failure.",
  23. }, []string{"grpc_type", "grpc_service", "grpc_method", "grpc_code"})
  24. clientStreamMsgReceived = prom.NewCounterVec(
  25. prom.CounterOpts{
  26. Namespace: "grpc",
  27. Subsystem: "client",
  28. Name: "msg_received_total",
  29. Help: "Total number of RPC stream messages received by the client.",
  30. }, []string{"grpc_type", "grpc_service", "grpc_method"})
  31. clientStreamMsgSent = prom.NewCounterVec(
  32. prom.CounterOpts{
  33. Namespace: "grpc",
  34. Subsystem: "client",
  35. Name: "msg_sent_total",
  36. Help: "Total number of gRPC stream messages sent by the client.",
  37. }, []string{"grpc_type", "grpc_service", "grpc_method"})
  38. clientHandledHistogramEnabled = false
  39. clientHandledHistogramOpts = prom.HistogramOpts{
  40. Namespace: "grpc",
  41. Subsystem: "client",
  42. Name: "handling_seconds",
  43. Help: "Histogram of response latency (seconds) of the gRPC until it is finished by the application.",
  44. Buckets: prom.DefBuckets,
  45. }
  46. clientHandledHistogram *prom.HistogramVec
  47. )
  48. func init() {
  49. prom.MustRegister(clientStartedCounter)
  50. prom.MustRegister(clientHandledCounter)
  51. prom.MustRegister(clientStreamMsgReceived)
  52. prom.MustRegister(clientStreamMsgSent)
  53. }
  54. // EnableClientHandlingTimeHistogram turns on recording of handling time of RPCs.
  55. // Histogram metrics can be very expensive for Prometheus to retain and query.
  56. func EnableClientHandlingTimeHistogram(opts ...HistogramOption) {
  57. for _, o := range opts {
  58. o(&clientHandledHistogramOpts)
  59. }
  60. if !clientHandledHistogramEnabled {
  61. clientHandledHistogram = prom.NewHistogramVec(
  62. clientHandledHistogramOpts,
  63. []string{"grpc_type", "grpc_service", "grpc_method"},
  64. )
  65. prom.Register(clientHandledHistogram)
  66. }
  67. clientHandledHistogramEnabled = true
  68. }
  69. type clientReporter struct {
  70. rpcType grpcType
  71. serviceName string
  72. methodName string
  73. startTime time.Time
  74. }
  75. func newClientReporter(rpcType grpcType, fullMethod string) *clientReporter {
  76. r := &clientReporter{rpcType: rpcType}
  77. if clientHandledHistogramEnabled {
  78. r.startTime = time.Now()
  79. }
  80. r.serviceName, r.methodName = splitMethodName(fullMethod)
  81. clientStartedCounter.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc()
  82. return r
  83. }
  84. func (r *clientReporter) ReceivedMessage() {
  85. clientStreamMsgReceived.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc()
  86. }
  87. func (r *clientReporter) SentMessage() {
  88. clientStreamMsgSent.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc()
  89. }
  90. func (r *clientReporter) Handled(code codes.Code) {
  91. clientHandledCounter.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName, code.String()).Inc()
  92. if clientHandledHistogramEnabled {
  93. clientHandledHistogram.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Observe(time.Since(r.startTime).Seconds())
  94. }
  95. }