omgrpc

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 18, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

README

omgrpc - OpenMetrics gRPC middleware

Lint Test

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)),
  )
}

Documentation

Overview

Package omgrpc provides helpers to track grpc/openmetrics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InstrumentActiveConns

func InstrumentActiveConns(m openmetrics.GaugeFamily) stats.Handler

InstrumentActiveConns returns default stats.Handler to instrument number of active gRPC connections. It populates no labels.

func InstrumentCallCount

func InstrumentCallCount(m openmetrics.CounterFamily) stats.Handler

InstrumentCallCount returns default stats.Handler to instrument RPC call count. It populates labels it can recognize and leaves others empty:

func InstrumentCallDuration

func InstrumentCallDuration(m openmetrics.HistogramFamily) stats.Handler

InstrumentCallDuration returns default stats.Handler to instrument RPC call duration in units configured for metric. It populates labels it can recognize and leaves others empty:

Types

type CallStats

type CallStats struct {
	IsClient bool // indicates client-side stats
	FailFast bool // only valid for client

	FullMethodName                 string
	IsClientStream, IsServerStream bool
	BeginTime, EndTime             time.Time
	InHeader, InTrailer            metadata.MD
	OutHeader, OutTrailer          metadata.MD
	LocalAddr, RemoteAddr          net.Addr
	BytesRecv, BytesSent           int

	Error error // RPC call error, can be examined with s, _ := grpc/status.FromError(err); s.Code()
}

CallStats holds all the RPC call-related data that can be collected by stats handler.

func (*CallStats) Code

func (s *CallStats) Code() codes.Code

Code is a convenience method / shortcut to return RPC status code.

func (*CallStats) Duration

func (s *CallStats) Duration() time.Duration

Duration is a convenience method that returns RPC call duration.

type CallStatsHandler

type CallStatsHandler func(*CallStats)

CallStatsHandler implements https://pkg.go.dev/google.golang.org/grpc/stats#Handler for RPC calls. CallStats argument is reused (pooled), so pointer cannot be stored - copy instead.

It assumes that stats.Handler methods are never called concurrently.

func (CallStatsHandler) HandleConn

func (h CallStatsHandler) HandleConn(ctx context.Context, stat stats.ConnStats)

HandleConn implements grpc/stats.Handler interface and does nothing.

func (CallStatsHandler) HandleRPC

func (h CallStatsHandler) HandleRPC(ctx context.Context, stat stats.RPCStats)

HandleRPC processes the RPC stats.

func (CallStatsHandler) TagConn

TagConn implements grpc/stats.Handler interface and does nothing.

func (CallStatsHandler) TagRPC

TagRPC attaches omgrpc-internal data to RPC context.

type ConnStats

type ConnStats struct {
	IsClient bool // indicates client-side stats
	Status   ConnStatus

	LocalAddr, RemoteAddr net.Addr
	BytesRecv, BytesSent  int // supported only for server side, only when Connected=false
}

ConnStats holds connection stats.

type ConnStatsHandler

type ConnStatsHandler func(*ConnStats)

ConnStatsHandler implements https://pkg.go.dev/google.golang.org/grpc/stats#Handler for RPC connections. ConnStats argument is reused (pooled), so pointer cannot be stored - copy instead.

It assumes that stats.Handler methods are never called concurrently.

func (ConnStatsHandler) HandleConn

func (h ConnStatsHandler) HandleConn(ctx context.Context, stat stats.ConnStats)

TagRPC attaches omgrpc-internal data to connection context.

func (ConnStatsHandler) HandleRPC

func (h ConnStatsHandler) HandleRPC(ctx context.Context, stat stats.RPCStats)

TagConn tracks server-side

func (ConnStatsHandler) TagConn

TagRPC attaches omgrpc-internal data to connection context.

func (ConnStatsHandler) TagRPC

TagConn implements grpc/stats.Handler interface and does nothing.

type ConnStatus

type ConnStatus int8

ConnStatus holds connection status.

const (
	Connected ConnStatus = iota
	Disconnected
)

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL