client_reporter.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. )
  8. type clientReporter struct {
  9. metrics *ClientMetrics
  10. rpcType grpcType
  11. serviceName string
  12. methodName string
  13. startTime time.Time
  14. }
  15. func newClientReporter(m *ClientMetrics, rpcType grpcType, fullMethod string) *clientReporter {
  16. r := &clientReporter{
  17. metrics: m,
  18. rpcType: rpcType,
  19. }
  20. if r.metrics.clientHandledHistogramEnabled {
  21. r.startTime = time.Now()
  22. }
  23. r.serviceName, r.methodName = splitMethodName(fullMethod)
  24. r.metrics.clientStartedCounter.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc()
  25. return r
  26. }
  27. func (r *clientReporter) ReceivedMessage() {
  28. r.metrics.clientStreamMsgReceived.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc()
  29. }
  30. func (r *clientReporter) SentMessage() {
  31. r.metrics.clientStreamMsgSent.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc()
  32. }
  33. func (r *clientReporter) Handled(code codes.Code) {
  34. r.metrics.clientHandledCounter.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName, code.String()).Inc()
  35. if r.metrics.clientHandledHistogramEnabled {
  36. r.metrics.clientHandledHistogram.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Observe(time.Since(r.startTime).Seconds())
  37. }
  38. }