Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Decouple registration from grpc.Server implementation #17

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions server.go
Expand Up @@ -30,11 +30,15 @@ func init() {
prom.MustRegister(DefaultServerMetrics.serverStreamMsgSent)
}

// Register takes a gRPC server and pre-initializes all counters to 0. This
// allows for easier monitoring in Prometheus (no missing metrics), and should
// be called *after* all services have been registered with the server. This
// function acts on the DefaultServerMetrics variable.
func Register(server *grpc.Server) {
// prometheusServer defines the interface that is required for grpc_prometheus to Register.
type prometheusServer interface {
GetServiceInfo() map[string]grpc.ServiceInfo
}

// Register takes a prometheusServer and pre-initializes all counters to 0.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general please talk about gRPC.Server and mention ("or any compatible type"), so it is clear for most users.

// This allows for easier monitoring in Prometheus (no missing metrics), and
// should be called *after* all services have been registered with the server.
func Register(server prometheusServer) {
DefaultServerMetrics.InitializeMetrics(server)
}

Expand Down
4 changes: 2 additions & 2 deletions server_metrics.go
Expand Up @@ -129,9 +129,9 @@ func (m *ServerMetrics) StreamServerInterceptor() func(srv interface{}, ss grpc.
}

// InitializeMetrics initializes all metrics, with their appropriate null
// value, for all gRPC methods registered on a gRPC server. This is useful, to
// value, for all gRPC methods registered on a prometheusServer. This is useful, to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep this as gRPC server.

// ensure that all metrics exist when collecting and querying.
func (m *ServerMetrics) InitializeMetrics(server *grpc.Server) {
func (m *ServerMetrics) InitializeMetrics(server prometheusServer) {
serviceInfo := server.GetServiceInfo()
for serviceName, info := range serviceInfo {
for _, mInfo := range info.Methods {
Expand Down