Skip to content

Commit

Permalink
added nilaway (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
metachris authored Jun 10, 2024
1 parent e09cf42 commit e057497
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ jobs:
- name: Install golangci-lint
run: go install github.com/golangci/golangci-lint/cmd/[email protected]

- name: Install NilAway
run: go install go.uber.org/nilaway/cmd/nilaway@latest

- name: Lint
run: make lint

Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ lint:
go vet ./...
staticcheck ./...
golangci-lint run
nilaway ./...

.PHONY: fmt
fmt:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Toolbox and building blocks for new Go projects, to get started quickly and righ
* Prometheus metrics
* Using https://pkg.go.dev/github.com/go-chi/chi/v5 for routing
* [Urfave](https://cli.urfave.org/) for cli args
* https://github.com/uber-go/nilaway
* See also:
* Public project setup: https://github.com/flashbots/flashbots-repository-template
* Repository for common Go utilities: https://github.com/flashbots/go-utils
Expand Down
12 changes: 12 additions & 0 deletions cmd/httpserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/flashbots/go-template/common"
"github.com/flashbots/go-template/httpserver"
"github.com/google/uuid"
"github.com/urfave/cli/v2" // imports as package "cli"
)

Expand All @@ -33,6 +34,11 @@ var flags []cli.Flag = []cli.Flag{
Value: false,
Usage: "log debug messages",
},
&cli.BoolFlag{
Name: "log-uid",
Value: false,
Usage: "generate a uuid and add to all log messages",
},
&cli.StringFlag{
Name: "log-service",
Value: "your-project",
Expand All @@ -55,6 +61,7 @@ func main() {
metricsAddr := cCtx.String("metrics-addr")
logJSON := cCtx.Bool("log-json")
logDebug := cCtx.Bool("log-debug")
logUID := cCtx.Bool("log-uid")
logService := cCtx.String("log-service")
drainDuration := time.Duration(cCtx.Int64("drain-seconds")) * time.Second

Expand All @@ -65,6 +72,11 @@ func main() {
Version: common.Version,
})

if logUID {
id := uuid.Must(uuid.NewRandom())
log = log.With("uid", id.String())
}

cfg := &httpserver.HTTPServerConfig{
ListenAddr: listenAddr,
MetricsAddr: metricsAddr,
Expand Down
17 changes: 6 additions & 11 deletions httpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/flashbots/go-template/metrics"
"github.com/flashbots/go-utils/httplogger"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"go.uber.org/atomic"
)

Expand All @@ -28,7 +27,6 @@ type HTTPServerConfig struct {

type Server struct {
cfg *HTTPServerConfig
id uuid.UUID
isReady atomic.Bool
log *slog.Logger

Expand All @@ -37,14 +35,16 @@ type Server struct {
}

func New(cfg *HTTPServerConfig) (srv *Server, err error) {
id := uuid.Must(uuid.NewRandom())
metrics, err := metrics.New(common.PackageName, cfg.MetricsAddr)
if err != nil {
return nil, err
}

srv = &Server{
cfg: cfg,
id: id,
isReady: atomic.Bool{},
log: cfg.Log.With("serverID", id.String()),
log: cfg.Log,
srv: nil,
metrics: metrics,
}
srv.isReady.Swap(true)

Expand All @@ -62,11 +62,6 @@ func New(cfg *HTTPServerConfig) (srv *Server, err error) {
WriteTimeout: cfg.WriteTimeout,
}

srv.metrics, err = metrics.New(common.PackageName, cfg.MetricsAddr)
if err != nil {
return nil, err
}

return srv, nil
}

Expand Down

0 comments on commit e057497

Please sign in to comment.