# README
omgrpc - OpenMetrics gRPC middleware
Quickstart
import (
"github.com/bsm/omgrpc"
"github.com/bsm/openmetrics"
"google.golang.org/grpc"
)
func initGRPCServer(
fooServer yourproto.FooServer,
callCount openmetrics.CounterFamily, // with tags: "method", "status"
callDuration openmetrics.HistogramFamily, // with tags: "method", "status"
activeConns openmetrics.GaugeFamily, // no tags
) *grpc.Server {
server := grpc.NewServer(
grpc.WithStats(omgrpc.InstrumentCallCount(callCount)),
grpc.WithStats(omgrpc.InstrumentCallDuration(callDuration)),
grpc.WithStats(omgrpc.InstrumentActiveConns(activeConns)),
)
yourproto.RegisterFooServer(server, fooServer)
return server
}
func initGRPCClient(
ctx context.Context,
target string,
callCount openmetrics.CounterFamily, // with tags: "method", "status"
callDuration openmetrics.HistogramFamily, // with tags: "method", "status"
activeConns openmetrics.GaugeFamily, // no tags
) (*grpc.Conn, error) {
return grpc.DialContext(
ctx,
target,
grpc.WithStatsHandler(omgrpc.InstrumentCallCount(callCount)),
grpc.WithStatsHandler(omgrpc.InstrumentCallDuration(callDuration)),
grpc.WithStatsHandler(omgrpc.InstrumentActiveConns(activeConns)),
)
}
# Functions
InstrumentActiveConns returns default stats.Handler to instrument number of active gRPC connections.
InstrumentCallCount returns default stats.Handler to instrument RPC call count.
InstrumentCallDuration returns default stats.Handler to instrument RPC call duration in units configured for metric.
# Type aliases
CallStatsHandler implements https://pkg.go.dev/google.golang.org/grpc/stats#Handler for RPC calls.
ConnStatsHandler implements https://pkg.go.dev/google.golang.org/grpc/stats#Handler for RPC connections.
ConnStatus holds connection status.