telemetry: Refactor GRPC Server Handler (#756)

* Refactor GRPC server stats handler location
This commit is contained in:
Travis Groth 2020-05-22 13:36:55 -04:00 committed by GitHub
parent e2a7149c36
commit ca5f68e371
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 106 additions and 21 deletions

View file

@ -0,0 +1,39 @@
package telemetry
import (
"context"
"go.opencensus.io/plugin/ocgrpc"
grpcstats "google.golang.org/grpc/stats"
"github.com/pomerium/pomerium/internal/telemetry/metrics"
)
type tagRPCHandler interface {
TagRPC(context.Context, *grpcstats.RPCTagInfo) context.Context
}
// GRPCServerStatsHandler provides a grpc stats.Handler for metrics and tracing for a pomerium service
type GRPCServerStatsHandler struct {
service string
metricsHandler tagRPCHandler
grpcstats.Handler
}
// TagRPC implements grpc.stats.Handler and adds metrics and tracing metadata to the context of a given RPC
func (h *GRPCServerStatsHandler) TagRPC(ctx context.Context, tagInfo *grpcstats.RPCTagInfo) context.Context {
metricCtx := h.metricsHandler.TagRPC(ctx, tagInfo)
handledCtx := h.Handler.TagRPC(metricCtx, tagInfo)
return handledCtx
}
// NewGRPCServerStatsHandler creates a new GRPCServerStatsHandler for a pomerium service
func NewGRPCServerStatsHandler(service string) grpcstats.Handler {
return &GRPCServerStatsHandler{
service: service,
Handler: &ocgrpc.ServerHandler{},
metricsHandler: metrics.NewGRPCServerMetricsHandler(service),
}
}