mirror of
https://github.com/rocky-linux/peridot.git
synced 2024-06-13 16:00:15 +00:00
Change interceptor handling and add prometheus metrics to all services
Signed-off-by: Mustafa Gezen <mustafa@ctrliq.com>
This commit is contained in:
parent
8176493bc0
commit
226c0f4c30
|
@ -80,9 +80,7 @@ func (s *Server) interceptor(ctx context.Context, req interface{}, usi *grpc.Una
|
||||||
func (s *Server) Run() {
|
func (s *Server) Run() {
|
||||||
res := utils.NewGRPCServer(
|
res := utils.NewGRPCServer(
|
||||||
&utils.GRPCOptions{
|
&utils.GRPCOptions{
|
||||||
ServerOptions: []grpc.ServerOption{
|
Interceptor: s.interceptor,
|
||||||
grpc.UnaryInterceptor(s.interceptor),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
func(r *utils.Register) {
|
func(r *utils.Register) {
|
||||||
endpoints := []utils.GrpcEndpointRegister{
|
endpoints := []utils.GrpcEndpointRegister{
|
||||||
|
|
|
@ -156,9 +156,9 @@ func (s *Server) Run() {
|
||||||
DialOptions: []grpc.DialOption{
|
DialOptions: []grpc.DialOption{
|
||||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||||
},
|
},
|
||||||
|
Interceptor: s.interceptor,
|
||||||
|
ServerInterceptor: s.serverInterceptor,
|
||||||
ServerOptions: []grpc.ServerOption{
|
ServerOptions: []grpc.ServerOption{
|
||||||
grpc.UnaryInterceptor(s.interceptor),
|
|
||||||
grpc.StreamInterceptor(s.serverInterceptor),
|
|
||||||
grpc.MaxRecvMsgSize(1024 * 1024 * 1024),
|
grpc.MaxRecvMsgSize(1024 * 1024 * 1024),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -175,9 +175,7 @@ func (s *Server) Run() {
|
||||||
res := utils.NewGRPCServer(
|
res := utils.NewGRPCServer(
|
||||||
&utils.GRPCOptions{
|
&utils.GRPCOptions{
|
||||||
Timeout: &timeout,
|
Timeout: &timeout,
|
||||||
ServerOptions: []grpc.ServerOption{
|
Interceptor: s.interceptor,
|
||||||
grpc.UnaryInterceptor(s.interceptor),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
func(r *utils.Register) {
|
func(r *utils.Register) {
|
||||||
endpoints := []utils.GrpcEndpointRegister{
|
endpoints := []utils.GrpcEndpointRegister{
|
||||||
|
|
|
@ -88,9 +88,7 @@ func (s *Server) interceptor(ctx context.Context, req interface{}, usi *grpc.Una
|
||||||
func (s *Server) Run() {
|
func (s *Server) Run() {
|
||||||
res := utils.NewGRPCServer(
|
res := utils.NewGRPCServer(
|
||||||
&utils.GRPCOptions{
|
&utils.GRPCOptions{
|
||||||
ServerOptions: []grpc.ServerOption{
|
Interceptor: s.interceptor,
|
||||||
grpc.UnaryInterceptor(s.interceptor),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
func(r *utils.Register) {
|
func(r *utils.Register) {
|
||||||
endpoints := []utils.GrpcEndpointRegister{
|
endpoints := []utils.GrpcEndpointRegister{
|
||||||
|
|
|
@ -79,9 +79,7 @@ func (s *Server) interceptor(ctx context.Context, req interface{}, usi *grpc.Una
|
||||||
func (s *Server) Run() {
|
func (s *Server) Run() {
|
||||||
res := utils.NewGRPCServer(
|
res := utils.NewGRPCServer(
|
||||||
&utils.GRPCOptions{
|
&utils.GRPCOptions{
|
||||||
ServerOptions: []grpc.ServerOption{
|
Interceptor: s.interceptor,
|
||||||
grpc.UnaryInterceptor(s.interceptor),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
func(r *utils.Register) {
|
func(r *utils.Register) {
|
||||||
err := secparseadminpb.RegisterSecparseAdminHandlerFromEndpoint(
|
err := secparseadminpb.RegisterSecparseAdminHandlerFromEndpoint(
|
||||||
|
|
|
@ -67,9 +67,7 @@ func (s *Server) interceptor(ctx context.Context, req interface{}, usi *grpc.Una
|
||||||
func (s *Server) Run() {
|
func (s *Server) Run() {
|
||||||
res := utils.NewGRPCServer(
|
res := utils.NewGRPCServer(
|
||||||
&utils.GRPCOptions{
|
&utils.GRPCOptions{
|
||||||
ServerOptions: []grpc.ServerOption{
|
Interceptor: s.interceptor,
|
||||||
grpc.UnaryInterceptor(s.interceptor),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
func(r *utils.Register) {
|
func(r *utils.Register) {
|
||||||
err := secparsepb.RegisterSecparseHandlerFromEndpoint(
|
err := secparsepb.RegisterSecparseHandlerFromEndpoint(
|
||||||
|
|
|
@ -32,6 +32,8 @@ package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/grpc-ecosystem/go-grpc-prometheus"
|
||||||
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
"google.golang.org/grpc/credentials/insecure"
|
"google.golang.org/grpc/credentials/insecure"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
"net"
|
"net"
|
||||||
|
@ -42,6 +44,7 @@ import (
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
"github.com/go-chi/chi/middleware"
|
"github.com/go-chi/chi/middleware"
|
||||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||||
|
_ "github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
@ -103,6 +106,8 @@ type GRPCOptions struct {
|
||||||
DialOptions []grpc.DialOption
|
DialOptions []grpc.DialOption
|
||||||
MuxOptions []runtime.ServeMuxOption
|
MuxOptions []runtime.ServeMuxOption
|
||||||
ServerOptions []grpc.ServerOption
|
ServerOptions []grpc.ServerOption
|
||||||
|
Interceptor grpc.UnaryServerInterceptor
|
||||||
|
ServerInterceptor grpc.StreamServerInterceptor
|
||||||
DisableREST bool
|
DisableREST bool
|
||||||
DisableGRPC bool
|
DisableGRPC bool
|
||||||
Timeout *time.Duration
|
Timeout *time.Duration
|
||||||
|
@ -160,7 +165,61 @@ func NewGRPCServer(goptions *GRPCOptions, endpoint func(*Register), serve func(*
|
||||||
}
|
}
|
||||||
opts = append(opts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(1000*1024*1024), grpc.MaxCallSendMsgSize(1000*1024*1024)))
|
opts = append(opts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(1000*1024*1024), grpc.MaxCallSendMsgSize(1000*1024*1024)))
|
||||||
|
|
||||||
serv := grpc.NewServer(options.ServerOptions...)
|
serverOpts := options.ServerOptions
|
||||||
|
// If the server already declares a unary interceptor, let's chain
|
||||||
|
// and make grpc_prometheus run first
|
||||||
|
if options.Interceptor != nil {
|
||||||
|
serverOpts = append(serverOpts, grpc.UnaryInterceptor(
|
||||||
|
func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
||||||
|
n := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
||||||
|
return options.Interceptor(ctx, req, info, handler)
|
||||||
|
}
|
||||||
|
n = func(next grpc.UnaryServerInterceptor) grpc.UnaryServerInterceptor {
|
||||||
|
return func(ctx context.Context, req interface{}, usi *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
||||||
|
_, err := grpc_prometheus.UnaryServerInterceptor(ctx, req, info, handler)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return next(ctx, req, usi, handler)
|
||||||
|
}
|
||||||
|
}(n)
|
||||||
|
|
||||||
|
return n(ctx, req, info, handler)
|
||||||
|
},
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
// Else, only declare prometheus interceptor
|
||||||
|
serverOpts = append(serverOpts, grpc.UnaryInterceptor(grpc_prometheus.UnaryServerInterceptor))
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the server already declares a stream interceptor, let's chain
|
||||||
|
// and make grpc_prometheus run first
|
||||||
|
if options.ServerInterceptor != nil {
|
||||||
|
serverOpts = append(serverOpts, grpc.StreamInterceptor(
|
||||||
|
func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
|
||||||
|
n := func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
|
||||||
|
return options.ServerInterceptor(srv, ss, info, handler)
|
||||||
|
}
|
||||||
|
n = func(next grpc.StreamServerInterceptor) grpc.StreamServerInterceptor {
|
||||||
|
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
|
||||||
|
err := grpc_prometheus.StreamServerInterceptor(srv, ss, info, handler)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return next(srv, ss, info, handler)
|
||||||
|
}
|
||||||
|
}(n)
|
||||||
|
|
||||||
|
return n(srv, ss, info, handler)
|
||||||
|
},
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
// Else, only declare prometheus interceptor
|
||||||
|
serverOpts = append(serverOpts, grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor))
|
||||||
|
}
|
||||||
|
serv := grpc.NewServer(serverOpts...)
|
||||||
|
|
||||||
// background context since this is the "main" app
|
// background context since this is the "main" app
|
||||||
ctx, cancel := context.WithCancel(context.TODO())
|
ctx, cancel := context.WithCancel(context.TODO())
|
||||||
|
@ -200,9 +259,9 @@ func NewGRPCServer(goptions *GRPCOptions, endpoint func(*Register), serve func(*
|
||||||
r.Mount("/", mux)
|
r.Mount("/", mux)
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(2)
|
|
||||||
|
|
||||||
if !options.DisableREST {
|
if !options.DisableREST {
|
||||||
|
wg.Add(1)
|
||||||
go func(wg *sync.WaitGroup) {
|
go func(wg *sync.WaitGroup) {
|
||||||
logrus.Infof("starting http server on port %s", viper.GetString("api.port"))
|
logrus.Infof("starting http server on port %s", viper.GetString("api.port"))
|
||||||
|
|
||||||
|
@ -216,6 +275,8 @@ func NewGRPCServer(goptions *GRPCOptions, endpoint func(*Register), serve func(*
|
||||||
}
|
}
|
||||||
|
|
||||||
if !options.DisableGRPC {
|
if !options.DisableGRPC {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(wg *sync.WaitGroup) {
|
||||||
logrus.Infof("starting grpc server on port %s", viper.GetString("grpc.port"))
|
logrus.Infof("starting grpc server on port %s", viper.GetString("grpc.port"))
|
||||||
registerServer := &RegisterServer{
|
registerServer := &RegisterServer{
|
||||||
Server: serv,
|
Server: serv,
|
||||||
|
@ -224,14 +285,26 @@ func NewGRPCServer(goptions *GRPCOptions, endpoint func(*Register), serve func(*
|
||||||
if serve != nil {
|
if serve != nil {
|
||||||
serve(registerServer)
|
serve(registerServer)
|
||||||
}
|
}
|
||||||
|
grpc_prometheus.Register(serv)
|
||||||
|
|
||||||
err = serv.Serve(lis)
|
err = serv.Serve(lis)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatalf("failed to serve: %v", err)
|
logrus.Fatalf("failed to serve: %v", err)
|
||||||
}
|
}
|
||||||
wg.Done()
|
wg.Done()
|
||||||
|
}(&wg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wg.Add(1)
|
||||||
|
go func(wg *sync.WaitGroup) {
|
||||||
|
promMux := http.NewServeMux()
|
||||||
|
promMux.Handle("/metrics", promhttp.Handler())
|
||||||
|
err := http.ListenAndServe(":7332", promMux)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatalf("could not start prometheus server - %s", err)
|
||||||
|
}
|
||||||
|
}(&wg)
|
||||||
|
|
||||||
return &GRPCServerRes{
|
return &GRPCServerRes{
|
||||||
Cancel: cancel,
|
Cancel: cancel,
|
||||||
WaitGroup: &wg,
|
WaitGroup: &wg,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user