Skip to content

Commit

Permalink
story(grpc): add functionality to expose interceptors (#186)
Browse files Browse the repository at this point in the history
* disable internal telemetry for otel

* added propagation to otel settings

* added support for interceptors

* fixed formatting
  • Loading branch information
erictg authored Apr 25, 2024
1 parent 12f5666 commit 3c94b5a
Showing 1 changed file with 45 additions and 11 deletions.
56 changes: 45 additions & 11 deletions grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ type service struct {
}

type runtimeOptions struct {
port uint
logHandler slog.Handler
tc credentials.TransportCredentials
services []service
port uint
logHandler slog.Handler
tc credentials.TransportCredentials
services []service
serverOptions []grpc.ServerOption
}

// RuntimeOption are options for configuring the gRPC runtime.
Expand Down Expand Up @@ -63,6 +64,34 @@ func TransportCredentials(tc credentials.TransportCredentials) RuntimeOption {
}
}

// UnaryInterceptor configures the gRPC server for one unary interceptors, please refer to grpc.UnaryInterceptor for more information
func UnaryInterceptor(f grpc.UnaryServerInterceptor) RuntimeOption {
return func(ro *runtimeOptions) {
ro.serverOptions = append(ro.serverOptions, grpc.UnaryInterceptor(f))
}
}

// ChainUnaryInterceptor configures the gRPC server for multiple unary interceptors, please refer to grpc.ChainUnaryInterceptor for more information
func ChainUnaryInterceptor(interceptors ...grpc.UnaryServerInterceptor) RuntimeOption {
return func(ro *runtimeOptions) {
ro.serverOptions = append(ro.serverOptions, grpc.ChainUnaryInterceptor(interceptors...))
}
}

// StreamInterceptor configures the gRPC server for one server interceptor, please refer to grpc.StreamInterceptor for more information
func StreamInterceptor(i grpc.StreamServerInterceptor) RuntimeOption {
return func(ro *runtimeOptions) {
ro.serverOptions = append(ro.serverOptions, grpc.StreamInterceptor(i))
}
}

// ChainStreamInterceptor configires the gRPC server for multiple server interceptors, please refer to grpc.ChainStreamInterceptor for more information
func ChainStreamInterceptor(interceptors ...grpc.StreamServerInterceptor) RuntimeOption {
return func(ro *runtimeOptions) {
ro.serverOptions = append(ro.serverOptions, grpc.ChainStreamInterceptor(interceptors...))
}
}

type serviceOptions struct {
name string
readiness health.Metric
Expand Down Expand Up @@ -127,20 +156,25 @@ type Runtime struct {
// NewRuntime returns a fully initialized gRPC Runtime.
func NewRuntime(opts ...RuntimeOption) *Runtime {
ro := &runtimeOptions{
port: 8090,
logHandler: noop.LogHandler{},
tc: insecure.NewCredentials(),
port: 8090,
logHandler: noop.LogHandler{},
tc: insecure.NewCredentials(),
serverOptions: []grpc.ServerOption{},
}
for _, opt := range opts {
opt(ro)
}

var healthMonitors []serviceHealthMonitor
s := grpc.NewServer(
grpc.StatsHandler(otelgrpc.NewServerHandler(
otelgrpc.WithMessageEvents(otelgrpc.ReceivedEvents, otelgrpc.SentEvents),
)),
grpc.Creds(ro.tc),
append(ro.serverOptions,
[]grpc.ServerOption{
grpc.StatsHandler(otelgrpc.NewServerHandler(
otelgrpc.WithMessageEvents(otelgrpc.ReceivedEvents, otelgrpc.SentEvents),
)),
grpc.Creds(ro.tc),
}...,
)...,
)
for _, svc := range ro.services {
svc.registerFunc(s)
Expand Down

0 comments on commit 3c94b5a

Please sign in to comment.