Ensure service name is passed to grpc metrics handlers (#510)

This commit is contained in:
Travis Groth 2020-02-21 06:25:43 -05:00 committed by GitHub
parent 3654f44384
commit 87d3d8c798
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 17 deletions

View file

@ -150,6 +150,7 @@ func newGRPCServer(opt config.Options, as *authorize.Authorize, cs *cache.Cache,
} }
so := &pgrpc.ServerOptions{ so := &pgrpc.ServerOptions{
Addr: opt.GRPCAddr, Addr: opt.GRPCAddr,
ServiceName: opt.Services,
KeepaliveParams: keepalive.ServerParameters{ KeepaliveParams: keepalive.ServerParameters{
MaxConnectionAge: opt.GRPCServerMaxConnectionAge, MaxConnectionAge: opt.GRPCServerMaxConnectionAge,
MaxConnectionAgeGrace: opt.GRPCServerMaxConnectionAgeGrace, MaxConnectionAgeGrace: opt.GRPCServerMaxConnectionAgeGrace,

1
go.sum
View file

@ -510,6 +510,7 @@ google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ij
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.25.1 h1:wdKvqQk7IttEw92GoRyKG2IDrUIpgpj6H6m81yfeMW0= google.golang.org/grpc v1.25.1 h1:wdKvqQk7IttEw92GoRyKG2IDrUIpgpj6H6m81yfeMW0=
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
google.golang.org/grpc v1.27.1 h1:zvIju4sqAGvwKspUQOhwnpcqSbzi7/H6QomNNjTL4sk=
google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

View file

@ -43,6 +43,9 @@ type Options struct {
// WithInsecure disables transport security for this ClientConn. // WithInsecure disables transport security for this ClientConn.
// Note that transport security is required unless WithInsecure is set. // Note that transport security is required unless WithInsecure is set.
WithInsecure bool WithInsecure bool
// ServiceName specifies the service name for telemetry exposition
ServiceName string
} }
// NewGRPCClientConn returns a new gRPC pomerium service client connection. // NewGRPCClientConn returns a new gRPC pomerium service client connection.
@ -57,7 +60,7 @@ func NewGRPCClientConn(opts *Options) (*grpc.ClientConn, error) {
connAddr = fmt.Sprintf("%s:%d", connAddr, defaultGRPCPort) connAddr = fmt.Sprintf("%s:%d", connAddr, defaultGRPCPort)
} }
dialOptions := []grpc.DialOption{ dialOptions := []grpc.DialOption{
grpc.WithChainUnaryInterceptor(metrics.GRPCClientInterceptor(connAddr), grpcTimeoutInterceptor(opts.RequestTimeout)), grpc.WithChainUnaryInterceptor(metrics.GRPCClientInterceptor(opts.ServiceName), grpcTimeoutInterceptor(opts.RequestTimeout)),
grpc.WithStatsHandler(&ocgrpc.ClientHandler{}), grpc.WithStatsHandler(&ocgrpc.ClientHandler{}),
grpc.WithDefaultCallOptions([]grpc.CallOption{grpc.WaitForReady(true)}...), grpc.WithDefaultCallOptions([]grpc.CallOption{grpc.WaitForReady(true)}...),
} }

View file

@ -29,7 +29,7 @@ func NewServer(opt *ServerOptions, registrationFn func(s *grpc.Server), wg *sync
return nil, err return nil, err
} }
grpcOpts := []grpc.ServerOption{ grpcOpts := []grpc.ServerOption{
grpc.StatsHandler(metrics.NewGRPCServerStatsHandler(opt.Addr)), grpc.StatsHandler(metrics.NewGRPCServerStatsHandler(opt.ServiceName)),
grpc.KeepaliveParams(opt.KeepaliveParams), grpc.KeepaliveParams(opt.KeepaliveParams),
} }
@ -70,7 +70,11 @@ type ServerOptions struct {
// This should be used only for testing. // This should be used only for testing.
InsecureServer bool InsecureServer bool
// KeepaliveParams sets GRPC keepalive.ServerParameters
KeepaliveParams keepalive.ServerParameters KeepaliveParams keepalive.ServerParameters
// ServiceName specifies the service name for telemetry exposition
ServiceName string
} }
var defaultServerOptions = &ServerOptions{ var defaultServerOptions = &ServerOptions{

View file

@ -131,10 +131,10 @@ func GRPCClientInterceptor(service string) grpc.UnaryClientInterceptor {
taggedCtx, tagErr := tag.New( taggedCtx, tagErr := tag.New(
ctx, ctx,
tag.Insert(TagKeyService, service), tag.Upsert(TagKeyService, service),
tag.Insert(TagKeyHost, cc.Target()), tag.Upsert(TagKeyHost, cc.Target()),
tag.Insert(TagKeyGRPCMethod, rpcMethod), tag.Upsert(TagKeyGRPCMethod, rpcMethod),
tag.Insert(TagKeyGRPCService, rpcService), tag.Upsert(TagKeyGRPCService, rpcService),
) )
if tagErr != nil { if tagErr != nil {
log.Warn().Err(tagErr).Str("context", "GRPCClientInterceptor").Msg("telemetry/metrics: failed to create context") log.Warn().Err(tagErr).Str("context", "GRPCClientInterceptor").Msg("telemetry/metrics: failed to create context")
@ -170,9 +170,9 @@ func (h *GRPCServerStatsHandler) TagRPC(ctx context.Context, tagInfo *grpcstats.
taggedCtx, tagErr := tag.New( taggedCtx, tagErr := tag.New(
handledCtx, handledCtx,
tag.Insert(TagKeyService, h.service), tag.Upsert(TagKeyService, h.service),
tag.Insert(TagKeyGRPCMethod, rpcMethod), tag.Upsert(TagKeyGRPCMethod, rpcMethod),
tag.Insert(TagKeyGRPCService, rpcService), tag.Upsert(TagKeyGRPCService, rpcService),
) )
if tagErr != nil { if tagErr != nil {
log.Warn().Err(tagErr).Str("context", "GRPCServerStatsHandler").Msg("telemetry/metrics: failed to create context") log.Warn().Err(tagErr).Str("context", "GRPCServerStatsHandler").Msg("telemetry/metrics: failed to create context")

View file

@ -112,9 +112,9 @@ func HTTPMetricsHandler(service string) func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx, tagErr := tag.New( ctx, tagErr := tag.New(
r.Context(), r.Context(),
tag.Insert(TagKeyService, service), tag.Upsert(TagKeyService, service),
tag.Insert(TagKeyHost, r.Host), tag.Upsert(TagKeyHost, r.Host),
tag.Insert(TagKeyHTTPMethod, r.Method), tag.Upsert(TagKeyHTTPMethod, r.Method),
) )
if tagErr != nil { if tagErr != nil {
log.Warn().Err(tagErr).Str("context", "HTTPMetricsHandler").Msg("telemetry/metrics: failed to create metrics tag") log.Warn().Err(tagErr).Str("context", "HTTPMetricsHandler").Msg("telemetry/metrics: failed to create metrics tag")
@ -139,10 +139,10 @@ func HTTPMetricsRoundTripper(service string, destination string) func(next http.
return tripper.RoundTripperFunc(func(r *http.Request) (*http.Response, error) { return tripper.RoundTripperFunc(func(r *http.Request) (*http.Response, error) {
ctx, tagErr := tag.New( ctx, tagErr := tag.New(
r.Context(), r.Context(),
tag.Insert(TagKeyService, service), tag.Upsert(TagKeyService, service),
tag.Insert(TagKeyHost, r.Host), tag.Upsert(TagKeyHost, r.Host),
tag.Insert(TagKeyHTTPMethod, r.Method), tag.Upsert(TagKeyHTTPMethod, r.Method),
tag.Insert(TagKeyDestination, destination), tag.Upsert(TagKeyDestination, destination),
) )
if tagErr != nil { if tagErr != nil {
log.Warn().Err(tagErr).Str("context", "HTTPMetricsRoundTripper").Msg("telemetry/metrics: failed to create metrics tag") log.Warn().Err(tagErr).Str("context", "HTTPMetricsRoundTripper").Msg("telemetry/metrics: failed to create metrics tag")