Documentation ¶
Overview ¶
Package grpcprom provides Prometheus instrumentation for gRPC clients and servers.
The following metrics are provided:
grpc_client_connections_open [gauge] Number of gRPC client connections open. grpc_client_connections_total [counter] Total number of gRPC client connections opened. grpc_client_requests_pending{grpc_type,grpc_service,grpc_method} [gauge] Number of gRPC client requests pending. grpc_client_requests_total{grpc_type,grpc_service,grpc_method,grpc_code} [counter] Total number of gRPC client requests completed. grpc_client_latency_seconds{grpc_type,grpc_service,grpc_method,grpc_code} [histogram] Latency of gRPC client requests. grpc_client_recv_bytes{grpc_type,grpc_service,grpc_method,grpc_frame} [histogram] Bytes received in gRPC client responses. grpc_client_sent_bytes{grpc_type,grpc_service,grpc_method,grpc_frame} [histogram] Bytes sent in gRPC client requests. grpc_server_connections_open [gauge] Number of gRPC server connections open. grpc_server_connections_total [counter] Total number of gRPC server connections opened. grpc_server_requests_pending{grpc_type,grpc_service,grpc_method} [gauge] Number of gRPC server requests pending. grpc_server_requests_total{grpc_type,grpc_service,grpc_method,grpc_code} [counter] Total number of gRPC server requests completed. grpc_server_latency_seconds{grpc_type,grpc_service,grpc_method,grpc_code} [histogram] Latency of gRPC server requests. grpc_server_recv_bytes{grpc_type,grpc_service,grpc_method,grpc_frame} [histogram] Bytes received in gRPC server requests. grpc_server_sent_bytes{grpc_type,grpc_service,grpc_method,grpc_frame} [histogram] Bytes sent in gRPC server responses.
Example ¶
registry := prometheus.NewRegistry() registry.MustRegister(collectors.NewGoCollector()) registry.MustRegister(collectors.NewBuildInfoCollector()) registry.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})) // Create gRPC client metrics and register with Prometheus. clientMetrics := grpcprom.NewClientMetrics() registry.MustRegister(clientMetrics) // Instrument gRPC client(s). backendConn, err := grpc.Dial(backendAddr, grpc.WithStatsHandler(clientMetrics.StatsHandler()), grpc.WithStreamInterceptor(clientMetrics.StreamInterceptor()), grpc.WithUnaryInterceptor(clientMetrics.UnaryInterceptor()), grpc.WithDefaultCallOptions( grpc.WaitForReady(true), ), ) check(err) // Create gRPC server metrics and register with Prometheus. serverMetrics := grpcprom.NewServerMetrics() registry.MustRegister(serverMetrics) // Instrument gRPC server and initialize metrics. grpcSrv := grpc.NewServer( grpc.StatsHandler(serverMetrics.StatsHandler()), grpc.StreamInterceptor(serverMetrics.StreamInterceptor()), grpc.UnaryInterceptor(serverMetrics.UnaryInterceptor()), ) fepb.RegisterFrontendServer(grpcSrv, &FrontendServer{ BackendClient: bepb.NewBackendClient(backendConn), }) serverMetrics.Init(grpcSrv, codes.OK) // Serve metrics. httpLis, err := net.Listen("tcp", httpAddr) check(err) httpSrv := http.NewServeMux() httpSrv.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{})) go http.Serve(httpLis, httpSrv) // Serve gRPC. grpcLis, err := net.Listen("tcp", grpcAddr) check(err) check(grpcSrv.Serve(grpcLis))
Output:
Index ¶
- Variables
- type ClientMetrics
- func (m *ClientMetrics) Collect(ch chan<- prometheus.Metric)
- func (m *ClientMetrics) Describe(ch chan<- *prometheus.Desc)
- func (m *ClientMetrics) Init(srv *grpc.Server, codes ...codes.Code)
- func (m *ClientMetrics) StatsHandler() stats.Handler
- func (m *ClientMetrics) StreamInterceptor() grpc.StreamClientInterceptor
- func (m *ClientMetrics) UnaryInterceptor() grpc.UnaryClientInterceptor
- type HistogramOption
- type MetricOption
- type Option
- func ConnectionsOpen(opts ...MetricOption) Option
- func ConnectionsTotal(opts ...MetricOption) Option
- func LatencySeconds(opts ...HistogramOption) Option
- func RecvBytes(opts ...HistogramOption) Option
- func RequestsPending(opts ...MetricOption) Option
- func RequestsTotal(opts ...MetricOption) Option
- func SentBytes(opts ...HistogramOption) Option
- type ServerMetrics
- func (m *ServerMetrics) Collect(ch chan<- prometheus.Metric)
- func (m *ServerMetrics) Describe(ch chan<- *prometheus.Desc)
- func (m *ServerMetrics) Init(srv *grpc.Server, codes ...codes.Code)
- func (m *ServerMetrics) StatsHandler() stats.Handler
- func (m *ServerMetrics) StreamInterceptor() grpc.StreamServerInterceptor
- func (m *ServerMetrics) UnaryInterceptor() grpc.UnaryServerInterceptor
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var AllCodes = []codes.Code{ codes.OK, codes.Canceled, codes.Unknown, codes.InvalidArgument, codes.DeadlineExceeded, codes.NotFound, codes.AlreadyExists, codes.PermissionDenied, codes.ResourceExhausted, codes.FailedPrecondition, codes.Aborted, codes.OutOfRange, codes.Unimplemented, codes.Internal, codes.Unavailable, codes.DataLoss, codes.Unauthenticated, }
AllCodes is a slice of all gRPC codes.
var DefaultBytesBuckets = []float64{0, 32, 64, 128, 256, 512, 1024, 2048, 8192, 32768, 131072, 524288}
DefaultBytesBuckets are the default bytes histogram buckets.
var DefaultLatencyBuckets = []float64{0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10}
DefaultLatencyBuckets are the default latency histogram buckets.
Functions ¶
This section is empty.
Types ¶
type ClientMetrics ¶ added in v0.2.0
type ClientMetrics struct {
// contains filtered or unexported fields
}
ClientMetrics is a collection of gRPC client metrics.
func NewClientMetrics ¶ added in v0.2.0
func NewClientMetrics(options ...Option) *ClientMetrics
NewClientMetrics returns new ClientMetrics with the given options.
func (*ClientMetrics) Collect ¶ added in v0.2.0
func (m *ClientMetrics) Collect(ch chan<- prometheus.Metric)
Collect sends each collected metric via the provided channel and returns once the last metric has been sent.
func (*ClientMetrics) Describe ¶ added in v0.2.0
func (m *ClientMetrics) Describe(ch chan<- *prometheus.Desc)
Describe sends the super-set of all possible descriptors of metrics to the provided channel and returns once the last descriptor has been sent.
func (*ClientMetrics) Init ¶ added in v0.2.0
func (m *ClientMetrics) Init(srv *grpc.Server, codes ...codes.Code)
Init initializes the metrics for srv with the given codes.
func (*ClientMetrics) StatsHandler ¶ added in v0.2.0
func (m *ClientMetrics) StatsHandler() stats.Handler
StatsHandler returns a gRPC stats handler.
func (*ClientMetrics) StreamInterceptor ¶ added in v0.2.0
func (m *ClientMetrics) StreamInterceptor() grpc.StreamClientInterceptor
StreamInterceptor returns a gRPC client stream interceptor.
func (*ClientMetrics) UnaryInterceptor ¶ added in v0.2.0
func (m *ClientMetrics) UnaryInterceptor() grpc.UnaryClientInterceptor
UnaryInterceptor returns a gRPC client unary interceptor.
type HistogramOption ¶ added in v0.2.0
type HistogramOption interface {
// contains filtered or unexported methods
}
A HistogramOption applies an option to a histogram.
func Buckets ¶ added in v0.2.0
func Buckets(v []float64) HistogramOption
Buckets returns a HistogramOption that sets the histogram's buckets.
func NoBuckets ¶ added in v0.2.0
func NoBuckets() HistogramOption
NoBuckets returns a HistogramOption that disables the histogram's buckets. Instead, only sum and count will be collected.
type MetricOption ¶ added in v0.2.0
type MetricOption interface { HistogramOption // contains filtered or unexported methods }
A MetricOption applies an option to a metric.
func Disable ¶ added in v0.2.0
func Disable() MetricOption
Disable returns a MetricOption that disables the metric.
type Option ¶ added in v0.2.0
type Option interface {
// contains filtered or unexported methods
}
An Option applies an option.
func ConnectionsOpen ¶ added in v0.2.0
func ConnectionsOpen(opts ...MetricOption) Option
ConnectionsOpen returns an Option that applies the given MetricOptions to the connections_open metric.
func ConnectionsTotal ¶ added in v0.2.0
func ConnectionsTotal(opts ...MetricOption) Option
ConnectionsTotal returns an Option that applies the given MetricOptions to the connections_total metric.
func LatencySeconds ¶ added in v0.2.0
func LatencySeconds(opts ...HistogramOption) Option
LatencySeconds returns an Option that applies the given HistogramOption to the latency_seconds metric.
func RecvBytes ¶ added in v0.2.0
func RecvBytes(opts ...HistogramOption) Option
RecvBytes returns an Option that applies the given HistogramOption to the recv_bytes metric.
func RequestsPending ¶ added in v0.2.0
func RequestsPending(opts ...MetricOption) Option
RequestsPending returns an Option that applies the given MetricOptions to the requests_pending metric.
func RequestsTotal ¶ added in v0.2.0
func RequestsTotal(opts ...MetricOption) Option
RequestsTotal returns an Option that applies the given MetricOptions to the requests_total metric.
func SentBytes ¶ added in v0.2.0
func SentBytes(opts ...HistogramOption) Option
SentBytes returns an Option that applies the given HistogramOption to the sent_bytes metric.
type ServerMetrics ¶ added in v0.2.0
type ServerMetrics struct {
// contains filtered or unexported fields
}
ServerMetrics is a collection of gRPC server metrics.
func NewServerMetrics ¶ added in v0.2.0
func NewServerMetrics(options ...Option) *ServerMetrics
NewServerMetrics returns new ServerMetrics with the given options.
func (*ServerMetrics) Collect ¶ added in v0.2.0
func (m *ServerMetrics) Collect(ch chan<- prometheus.Metric)
Collect sends each collected metric via the provided channel and returns once the last metric has been sent.
func (*ServerMetrics) Describe ¶ added in v0.2.0
func (m *ServerMetrics) Describe(ch chan<- *prometheus.Desc)
Describe sends the super-set of all possible descriptors of metrics to the provided channel and returns once the last descriptor has been sent.
func (*ServerMetrics) Init ¶ added in v0.2.0
func (m *ServerMetrics) Init(srv *grpc.Server, codes ...codes.Code)
Init initializes the metrics for srv with the given codes.
func (*ServerMetrics) StatsHandler ¶ added in v0.2.0
func (m *ServerMetrics) StatsHandler() stats.Handler
StatsHandler returns a gRPC stats handler.
func (*ServerMetrics) StreamInterceptor ¶ added in v0.2.0
func (m *ServerMetrics) StreamInterceptor() grpc.StreamServerInterceptor
StreamInterceptor returns a gRPC server stream interceptor.
func (*ServerMetrics) UnaryInterceptor ¶ added in v0.2.0
func (m *ServerMetrics) UnaryInterceptor() grpc.UnaryServerInterceptor
UnaryInterceptor returns a gRPC server unary interceptor.