server_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 serverReporter struct {
  9. metrics *ServerMetrics
  10. rpcType grpcType
  11. serviceName string
  12. methodName string
  13. startTime time.Time
  14. }
  15. func newServerReporter(m *ServerMetrics, rpcType grpcType, fullMethod string) *serverReporter {
  16. r := &serverReporter{
  17. metrics: m,
  18. rpcType: rpcType,
  19. }
  20. if r.metrics.serverHandledHistogramEnabled {
  21. r.startTime = time.Now()
  22. }
  23. r.serviceName, r.methodName = splitMethodName(fullMethod)
  24. r.metrics.serverStartedCounter.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc()
  25. return r
  26. }
  27. func (r *serverReporter) ReceivedMessage() {
  28. r.metrics.serverStreamMsgReceived.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc()
  29. }
  30. func (r *serverReporter) SentMessage() {
  31. r.metrics.serverStreamMsgSent.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc()
  32. }
  33. func (r *serverReporter) Handled(code codes.Code) {
  34. r.metrics.serverHandledCounter.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName, code.String()).Inc()
  35. if r.metrics.serverHandledHistogramEnabled {
  36. r.metrics.serverHandledHistogram.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Observe(time.Since(r.startTime).Seconds())
  37. }
  38. }