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

feat: initial implement of the gRPC middleware - logrus, prometheus #44

Merged
merged 8 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
.DS_Store
.vscode
77 changes: 76 additions & 1 deletion server/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,92 @@ package main
import (
"fmt"
"net"
"net/http"

"github.com/codingpot/pr12er/server/pkg/env"
"github.com/codingpot/pr12er/server/pkg/pr12er"
"github.com/codingpot/pr12er/server/pkg/serv"
grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
)

var (
logrusLogger *log.Logger
customFunc grpc_logrus.CodeToLevel
logrusOpts []grpc_logrus.Option
logrusEntry *log.Entry
)

var (
reg = prometheus.NewRegistry()
grpcMetrics = grpc_prometheus.NewServerMetrics()
promServer *http.Server
)

func initPrometheus() {
reg.MustRegister(grpcMetrics)
promServer = &http.Server{
Handler: promhttp.HandlerFor(reg, promhttp.HandlerOpts{}),
Addr: fmt.Sprintf("0.0.0.0:%s", env.PrometheusPort),
}
}

func initGrpcLogrus() {
logrusLogger = log.New()
customFunc = func(code codes.Code) log.Level {
if code == codes.OK {
return log.InfoLevel
}
return log.ErrorLevel
}

// Logrus entry is used, allowing pre-definition of certain fields by the user.
logrusEntry = log.NewEntry(logrusLogger)
// Shared options for the logger, with a custom gRPC code to log level function.
logrusOpts = []grpc_logrus.Option{
grpc_logrus.WithLevels(customFunc),
}
// Make sure that log statements internal to gRPC library are logged using the logrus Logger as well.
grpc_logrus.ReplaceGrpcLogger(logrusEntry)
}

func main() {
log.WithFields(log.Fields{
"Nversion": env.Nversion,
"ServicePort": env.ServicePort,
}).Printf("gRPC server is listening at 0.0.0.0:%s", env.ServicePort)

initGrpcLogrus()
initPrometheus()

lis, err := net.Listen("tcp", fmt.Sprintf(":%s", env.ServicePort))
if err != nil {
log.WithError(err).Fatalf("failed to listen")
}

s := serv.Server{}

grpcServer := grpc.NewServer()
grpcServer := grpc.NewServer(
grpc.ChainUnaryInterceptor(
grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_logrus.UnaryServerInterceptor(logrusEntry, logrusOpts...),
grpc_prometheus.UnaryServerInterceptor,
),
grpc.ChainStreamInterceptor(
grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_logrus.StreamServerInterceptor(logrusEntry, logrusOpts...),
grpc_prometheus.StreamServerInterceptor,
),
)

pr12er.RegisterPr12ErServiceServer(grpcServer, &s)

Expand All @@ -37,6 +98,20 @@ func main() {

reflection.Register(grpcServer)

// prometheus
grpc_prometheus.Register(grpcServer)
grpc_prometheus.EnableHandlingTimeHistogram()
grpcMetrics.InitializeMetrics(grpcServer)
// Register Prometheus metrics handler.
http.Handle("/metrics", promhttp.Handler())

// Start your http server for prometheus.
go func() {
if err := promServer.ListenAndServe(); err != nil {
log.Fatal("Unable to start a prometheus server.")
}
}()

if err := grpcServer.Serve(lis); err != nil {
log.WithError(err).Fatalf("failed to serve")
}
Expand Down
12 changes: 8 additions & 4 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ module github.com/codingpot/pr12er/server
go 1.16

require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/prometheus/client_golang v1.10.0
github.com/prometheus/common v0.25.0 // indirect
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.5.1
golang.org/x/net v0.0.0-20210521195947-fe42d452be8f
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
golang.org/x/net v0.0.0-20210525063256-abc453219eb5
golang.org/x/sys v0.0.0-20210601080250-7ecdf8ef093b // indirect
google.golang.org/genproto v0.0.0-20210601170153-0befbe3492e2 // indirect
google.golang.org/grpc v1.38.0
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0
google.golang.org/protobuf v1.26.0
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading