Skip to content

Commit

Permalink
Server consolidation
Browse files Browse the repository at this point in the history
Stuttering removed - GRPCServer renamed to just Server and
similarly for http and restproxyserver.

grpcserver now accepts 'port' as an argument instead of getting
the value from the environment.

metricsServer creation is now a single method

AB#9031
  • Loading branch information
eccles committed Apr 10, 2024
1 parent 3a6c628 commit d2c9851
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 98 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
.viminfo
.local/
.vscode/
.bash_history
.viminfo
59 changes: 29 additions & 30 deletions grpcserver/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"

env "github.com/datatrails/go-datatrails-common/environment"
"github.com/datatrails/go-datatrails-common/grpchealth"
grpcHealth "google.golang.org/grpc/health/grpc_health_v1"
)
Expand All @@ -26,7 +25,7 @@ type RegisterServer func(*grpcServer)

func defaultRegisterServer(g *grpcServer) {}

type GRPCServer struct {
type Server struct {
name string
log Logger
listenStr string
Expand All @@ -38,34 +37,34 @@ type GRPCServer struct {
reflection bool
}

type GRPCServerOption func(*GRPCServer)
type ServerOption func(*Server)

func WithAppendedInterceptor(i grpcUnaryServerInterceptor) GRPCServerOption {
return func(g *GRPCServer) {
func WithAppendedInterceptor(i grpcUnaryServerInterceptor) ServerOption {
return func(g *Server) {
g.interceptors = append(g.interceptors, i)
}
}

func WithPrependedInterceptor(i grpcUnaryServerInterceptor) GRPCServerOption {
return func(g *GRPCServer) {
func WithPrependedInterceptor(i grpcUnaryServerInterceptor) ServerOption {
return func(g *Server) {
g.interceptors = append([]grpcUnaryServerInterceptor{i}, g.interceptors...)
}
}

func WithRegisterServer(r RegisterServer) GRPCServerOption {
return func(g *GRPCServer) {
func WithRegisterServer(r RegisterServer) ServerOption {
return func(g *Server) {
g.register = r
}
}

func WithoutHealth() GRPCServerOption {
return func(g *GRPCServer) {
func WithoutHealth() ServerOption {
return func(g *Server) {
g.health = false
}
}

func WithReflection(r bool) GRPCServerOption {
return func(g *GRPCServer) {
func WithReflection(r bool) ServerOption {
return func(g *Server) {
g.reflection = r
}
}
Expand All @@ -77,21 +76,21 @@ func tracingFilter(ctx context.Context, fullMethodName string) bool {
return true
}

// New creates a new GRPCServer that is bound to a specific GRPC API. This object complies with
// New creates a new Server that is bound to a specific GRPC API. This object complies with
// the standard Listener service and can be managed by the startup.Listeners object.
func New(log Logger, name string, opts ...GRPCServerOption) GRPCServer {
listenStr := fmt.Sprintf(":%s", env.GetOrFatal("PORT"))

g := GRPCServer{
name: strings.ToLower(name),
listenStr: listenStr,
register: defaultRegisterServer,
interceptors: []grpc.UnaryServerInterceptor{
grpc_otrace.UnaryServerInterceptor(grpc_otrace.WithFilterFunc(tracingFilter)),
grpc_validator.UnaryServerInterceptor(),
},
health: true,
func New(log Logger, name string, port string, opts ...ServerOption) *Server {
var g Server

listenStr := fmt.Sprintf(":%s", port)

g.name = strings.ToLower(name)
g.listenStr = listenStr
g.register = defaultRegisterServer
g.interceptors = []grpc.UnaryServerInterceptor{
grpc_otrace.UnaryServerInterceptor(grpc_otrace.WithFilterFunc(tracingFilter)),
grpc_validator.UnaryServerInterceptor(),
}
g.health = true
for _, opt := range opts {
opt(&g)
}
Expand All @@ -115,15 +114,15 @@ func New(log Logger, name string, opts ...GRPCServerOption) GRPCServer {

g.server = server
g.log = log.WithIndex("grpcserver", g.String())
return g
return &g
}

func (g *GRPCServer) String() string {
func (g *Server) String() string {
// No logging in this method please.
return fmt.Sprintf("%s%s", g.name, g.listenStr)
}

func (g *GRPCServer) Listen() error {
func (g *Server) Listen() error {
listen, err := net.Listen("tcp", g.listenStr)
if err != nil {
return fmt.Errorf("failed to listen %s: %w", g, err)
Expand All @@ -141,7 +140,7 @@ func (g *GRPCServer) Listen() error {
return nil
}

func (g *GRPCServer) Shutdown(_ context.Context) error {
func (g *Server) Shutdown(_ context.Context) error {
g.log.Infof("Shutdown")
if g.healthService != nil {
g.healthService.NotReady() // readiness
Expand Down
13 changes: 5 additions & 8 deletions httpserver/httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,16 @@ func WithOptionalHandlers(handlers ...HandleChainFunc) ServerOption {

// New creates a new httpserver.
func New(log Logger, name string, port string, h http.Handler, opts ...ServerOption) *Server {
s := Server{
server: http.Server{
Addr: ":" + port,
},
handler: h,
name: strings.ToLower(name),
var s Server
s.server = http.Server{
Addr: ":" + port,
}
s.handler = h
s.name = strings.ToLower(name)
s.log = log.WithIndex("httpserver", s.String())
for _, opt := range opts {
opt(&s)
}
// It is preferable to return a copy rather than a reference. Unfortunately http.Server has an
// internal mutex and this cannot or should not be copied so we will return a reference instead.
return &s
}

Expand Down
28 changes: 28 additions & 0 deletions metrics/httpserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package metrics

import (
"fmt"

"github.com/datatrails/go-datatrails-common/httpserver"
)

type HTTPServer struct {
*httpserver.Server
metrics *Metrics
}

func NewServer(log Logger, serviceName string, port string, opts ...MetricsOption) HTTPServer {
m := New(
log,
serviceName,
port,
)
return HTTPServer{
httpserver.New(log, fmt.Sprintf("metrics %s", serviceName), port, m.newPromHandler()),
m,
}
}

func (h *HTTPServer) Metrics() *Metrics {
return h.metrics
}
9 changes: 1 addition & 8 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,9 @@ func (m *Metrics) Register(cs ...prometheus.Collector) {
m.registry.MustRegister(cs...)
}

func (m *Metrics) Port() string {
if m != nil {
return m.port
}
return ""
}

// NewPromHandler - this handler is used on the endpoint that serves metrics endpoint
// which is provided on a different port to the service.
// The default InstrumentMetricHandler is suppressed.
func (m *Metrics) NewPromHandler() http.Handler {
func (m *Metrics) newPromHandler() http.Handler {
return promhttp.HandlerFor(m.registry, promhttp.HandlerOpts{})
}
Loading

0 comments on commit d2c9851

Please sign in to comment.