client.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright 2016 Michal Witkowski. All Rights Reserved.
  2. // See LICENSE for licensing terms.
  3. // gRPC Prometheus monitoring interceptors for client-side gRPC.
  4. package grpc_prometheus
  5. import (
  6. prom "github.com/prometheus/client_golang/prometheus"
  7. )
  8. var (
  9. // DefaultClientMetrics is the default instance of ClientMetrics. It is
  10. // intended to be used in conjunction the default Prometheus metrics
  11. // registry.
  12. DefaultClientMetrics = NewClientMetrics()
  13. // UnaryClientInterceptor is a gRPC client-side interceptor that provides Prometheus monitoring for Unary RPCs.
  14. UnaryClientInterceptor = DefaultClientMetrics.UnaryClientInterceptor()
  15. // StreamClientInterceptor is a gRPC client-side interceptor that provides Prometheus monitoring for Streaming RPCs.
  16. StreamClientInterceptor = DefaultClientMetrics.StreamClientInterceptor()
  17. )
  18. func init() {
  19. prom.MustRegister(DefaultClientMetrics.clientStartedCounter)
  20. prom.MustRegister(DefaultClientMetrics.clientHandledCounter)
  21. prom.MustRegister(DefaultClientMetrics.clientStreamMsgReceived)
  22. prom.MustRegister(DefaultClientMetrics.clientStreamMsgSent)
  23. }
  24. // EnableClientHandlingTimeHistogram turns on recording of handling time of
  25. // RPCs. Histogram metrics can be very expensive for Prometheus to retain and
  26. // query. This function acts on the DefaultClientMetrics variable and the
  27. // default Prometheus metrics registry.
  28. func EnableClientHandlingTimeHistogram(opts ...HistogramOption) {
  29. DefaultClientMetrics.EnableClientHandlingTimeHistogram(opts...)
  30. prom.Register(DefaultClientMetrics.clientHandledHistogram)
  31. }