Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement: Ability to set gRPC stats handler #17

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions cerbos/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,25 @@ import (
"os"
"time"

effectv1 "github.com/cerbos/cerbos/api/genpb/cerbos/effect/v1"
enginev1 "github.com/cerbos/cerbos/api/genpb/cerbos/engine/v1"
requestv1 "github.com/cerbos/cerbos/api/genpb/cerbos/request/v1"
svcv1 "github.com/cerbos/cerbos/api/genpb/cerbos/svc/v1"
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/retry"
"github.com/rs/xid"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/stats"

effectv1 "github.com/cerbos/cerbos/api/genpb/cerbos/effect/v1"
enginev1 "github.com/cerbos/cerbos/api/genpb/cerbos/engine/v1"
requestv1 "github.com/cerbos/cerbos/api/genpb/cerbos/request/v1"
svcv1 "github.com/cerbos/cerbos/api/genpb/cerbos/svc/v1"

"github.com/cerbos/cerbos-sdk-go/internal"
)

var _ Client[*GRPCClient, PrincipalCtx] = (*GRPCClient)(nil)

type config struct {
statsHandler stats.Handler
address string
tlsAuthority string
tlsCACert string
Expand Down Expand Up @@ -133,6 +136,13 @@ func WithUnaryInterceptors(interceptors ...grpc.UnaryClientInterceptor) Opt {
}
}

// WithStatsHandler sets the gRPC stats handler for the connection.
func WithStatsHandler(handler stats.Handler) Opt {
return func(c *config) {
c.statsHandler = handler
}
}

// New creates a new Cerbos client.
func New(address string, opts ...Opt) (*GRPCClient, error) {
grpcConn, _, err := mkConn(address, opts...)
Expand Down Expand Up @@ -172,6 +182,10 @@ func mkConn(address string, opts ...Opt) (*grpc.ClientConn, *config, error) {
func mkDialOpts(conf *config) ([]grpc.DialOption, error) {
dialOpts := []grpc.DialOption{grpc.WithUserAgent(conf.userAgent)}

if conf.statsHandler != nil {
dialOpts = append(dialOpts, grpc.WithStatsHandler(conf.statsHandler))
}

if conf.connectTimeout > 0 {
dialOpts = append(dialOpts, grpc.WithConnectParams(grpc.ConnectParams{MinConnectTimeout: conf.connectTimeout}))
}
Expand Down