blob: 322f99046fd7ef65427b166922bff28e9104432f [file] [log] [blame]
sslobodrd046be82019-01-16 10:02:22 -05001// Copyright 2016 Michal Witkowski. All Rights Reserved.
2// See LICENSE for licensing terms.
3
4// gRPC Prometheus monitoring interceptors for server-side gRPC.
5
6package grpc_prometheus
7
8import (
9 prom "github.com/prometheus/client_golang/prometheus"
10 "google.golang.org/grpc"
11)
12
13var (
14 // DefaultServerMetrics is the default instance of ServerMetrics. It is
15 // intended to be used in conjunction the default Prometheus metrics
16 // registry.
17 DefaultServerMetrics = NewServerMetrics()
18
19 // UnaryServerInterceptor is a gRPC server-side interceptor that provides Prometheus monitoring for Unary RPCs.
20 UnaryServerInterceptor = DefaultServerMetrics.UnaryServerInterceptor()
21
22 // StreamServerInterceptor is a gRPC server-side interceptor that provides Prometheus monitoring for Streaming RPCs.
23 StreamServerInterceptor = DefaultServerMetrics.StreamServerInterceptor()
24)
25
26func init() {
27 prom.MustRegister(DefaultServerMetrics.serverStartedCounter)
28 prom.MustRegister(DefaultServerMetrics.serverHandledCounter)
29 prom.MustRegister(DefaultServerMetrics.serverStreamMsgReceived)
30 prom.MustRegister(DefaultServerMetrics.serverStreamMsgSent)
31}
32
33// Register takes a gRPC server and pre-initializes all counters to 0. This
34// allows for easier monitoring in Prometheus (no missing metrics), and should
35// be called *after* all services have been registered with the server. This
36// function acts on the DefaultServerMetrics variable.
37func Register(server *grpc.Server) {
38 DefaultServerMetrics.InitializeMetrics(server)
39}
40
41// EnableHandlingTimeHistogram turns on recording of handling time
42// of RPCs. Histogram metrics can be very expensive for Prometheus
43// to retain and query. This function acts on the DefaultServerMetrics
44// variable and the default Prometheus metrics registry.
45func EnableHandlingTimeHistogram(opts ...HistogramOption) {
46 DefaultServerMetrics.EnableHandlingTimeHistogram(opts...)
47 prom.Register(DefaultServerMetrics.serverHandledHistogram)
48}