Skip to content

Commit 909cee1

Browse files
authored
Build with go-1.21 and use slog instead of zap for logging (#122)
1 parent 1346cc6 commit 909cee1

File tree

10 files changed

+112
-123
lines changed

10 files changed

+112
-123
lines changed

.github/workflows/docker.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ jobs:
2121
runs-on: ubuntu-latest
2222
steps:
2323

24-
- name: Set up Go 1.20
24+
- name: Set up Go 1.21
2525
uses: actions/setup-go@v4
2626
with:
27-
go-version: "1.20"
27+
go-version: "1.21"
2828
id: go
2929

3030
- name: Check out code into the Go module directory
@@ -86,10 +86,10 @@ jobs:
8686
runs-on: ubuntu-latest
8787
steps:
8888

89-
- name: Set up Go 1.20
89+
- name: Set up Go 1.21
9090
uses: actions/setup-go@v4
9191
with:
92-
go-version: "1.20"
92+
go-version: "1.21"
9393
id: go
9494

9595
- name: Check out code into the Go module directory

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.20-alpine as builder
1+
FROM golang:1.21-alpine as builder
22
RUN apk add \
33
binutils \
44
gcc \

cmd/server/main.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"log/slog"
78
"os"
89

910
goipam "github.com/metal-stack/go-ipam"
1011
"github.com/metal-stack/v"
1112
"github.com/urfave/cli/v2"
1213
"go.mongodb.org/mongo-driver/mongo/options"
13-
"go.uber.org/zap"
14-
"go.uber.org/zap/zapcore"
1514
)
1615

1716
func main() {
@@ -283,21 +282,19 @@ func main() {
283282
}
284283

285284
func getConfig(ctx *cli.Context) config {
286-
cfg := zap.NewProductionConfig()
287-
level, err := zap.ParseAtomicLevel(ctx.String("log-level"))
288-
if err != nil {
289-
panic(err)
285+
opts := &slog.HandlerOptions{
286+
Level: slog.LevelInfo,
290287
}
291-
cfg.Level = level
292-
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
293-
zlog, err := cfg.Build()
294-
if err != nil {
295-
panic(err)
288+
switch ctx.String("log-level") {
289+
case "debug":
290+
opts.Level = slog.LevelDebug
291+
case "error":
292+
opts.Level = slog.LevelError
296293
}
297294

298295
return config{
299296
GrpcServerEndpoint: ctx.String("grpc-server-endpoint"),
300297
MetricsEndpoint: ctx.String("metrics-endpoint"),
301-
Log: zlog.Sugar(),
298+
Log: slog.New(slog.NewJSONHandler(os.Stdout, opts)),
302299
}
303300
}

cmd/server/server.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"fmt"
5+
"log/slog"
46
"net/http"
57
"time"
68

@@ -18,22 +20,21 @@ import (
1820
"connectrpc.com/grpchealth"
1921
"connectrpc.com/grpcreflect"
2022

21-
"go.uber.org/zap"
2223
"golang.org/x/net/http2"
2324
"golang.org/x/net/http2/h2c"
2425
)
2526

2627
type config struct {
2728
GrpcServerEndpoint string
2829
MetricsEndpoint string
29-
Log *zap.SugaredLogger
30+
Log *slog.Logger
3031
Storage goipam.Storage
3132
}
3233
type server struct {
3334
c config
3435
ipamer goipam.Ipamer
3536
storage goipam.Storage
36-
log *zap.SugaredLogger
37+
log *slog.Logger
3738
}
3839

3940
func newServer(c config) *server {
@@ -45,7 +46,7 @@ func newServer(c config) *server {
4546
}
4647
}
4748
func (s *server) Run() error {
48-
s.log.Infow("starting go-ipam", "version", v.V, "backend", s.storage.Name())
49+
s.log.Info("starting go-ipam", "version", v.V, "backend", s.storage.Name())
4950

5051
// The exporter embeds a default OpenTelemetry Reader and
5152
// implements prometheus.Collector, allowing it to be used as
@@ -58,7 +59,7 @@ func (s *server) Run() error {
5859

5960
// Start the prometheus HTTP server and pass the exporter Collector to it
6061
go func() {
61-
s.log.Infof("serving metrics at %s/metrics", s.c.MetricsEndpoint)
62+
s.log.Info("serving metrics", "at", fmt.Sprintf("%s/metrics", s.c.MetricsEndpoint))
6263
metricsServer := http.NewServeMux()
6364
metricsServer.Handle("/metrics", promhttp.Handler())
6465
ms := &http.Server{
@@ -68,7 +69,7 @@ func (s *server) Run() error {
6869
}
6970
err := ms.ListenAndServe()
7071
if err != nil {
71-
s.log.Errorw("unable to start metric endpoint", "error", err)
72+
s.log.Error("unable to start metric endpoint", "error", err)
7273
return
7374
}
7475
}()

go.mod

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
module github.com/metal-stack/go-ipam
22

3-
go 1.20
3+
go 1.21
44

55
require (
66
connectrpc.com/connect v1.11.0
77
connectrpc.com/grpchealth v1.2.0
88
connectrpc.com/grpcreflect v1.2.0
99
connectrpc.com/otelconnect v0.5.0
10-
github.com/avast/retry-go/v4 v4.3.4
10+
github.com/avast/retry-go/v4 v4.5.0
1111
github.com/jmoiron/sqlx v1.3.5
1212
github.com/klauspost/connect-compress/v2 v2.0.0
1313
github.com/lib/pq v1.10.9
1414
github.com/metal-stack/v v1.0.3
1515
github.com/prometheus/client_golang v1.16.0
1616
github.com/redis/go-redis/v9 v9.0.5
1717
github.com/stretchr/testify v1.8.4
18-
github.com/testcontainers/testcontainers-go v0.21.0
18+
github.com/testcontainers/testcontainers-go v0.23.0
1919
github.com/urfave/cli/v2 v2.25.7
2020
go.etcd.io/etcd/client/v3 v3.5.9
2121
go.mongodb.org/mongo-driver v1.12.1
2222
go.opentelemetry.io/otel/exporters/prometheus v0.39.0
2323
go.opentelemetry.io/otel/sdk/metric v0.39.0
24-
go.uber.org/zap v1.25.0
2524
go4.org/netipx v0.0.0-20230728184502-ec4c8b891b28
26-
golang.org/x/net v0.13.0
25+
golang.org/x/net v0.14.0
2726
golang.org/x/sync v0.3.0
2827
google.golang.org/protobuf v1.31.0
2928
)
3029

3130
require (
31+
dario.cat/mergo v1.0.0 // indirect
3232
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
3333
github.com/Microsoft/go-winio v0.6.1 // indirect
3434
github.com/benbjohnson/clock v1.3.5 // indirect
@@ -52,7 +52,6 @@ require (
5252
github.com/golang/protobuf v1.5.3 // indirect
5353
github.com/golang/snappy v0.0.4 // indirect
5454
github.com/google/uuid v1.3.0 // indirect
55-
github.com/imdario/mergo v0.3.15 // indirect
5655
github.com/klauspost/compress v1.16.7 // indirect
5756
github.com/magiconair/properties v1.8.7 // indirect
5857
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
@@ -63,7 +62,7 @@ require (
6362
github.com/morikuni/aec v1.0.0 // indirect
6463
github.com/opencontainers/go-digest v1.0.0 // indirect
6564
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
66-
github.com/opencontainers/runc v1.1.8 // indirect
65+
github.com/opencontainers/runc v1.1.9 // indirect
6766
github.com/pkg/errors v0.9.1 // indirect
6867
github.com/pmezard/go-difflib v1.0.0 // indirect
6968
github.com/prometheus/client_model v0.4.0 // indirect
@@ -84,16 +83,16 @@ require (
8483
go.opentelemetry.io/otel/trace v1.16.0 // indirect
8584
go.uber.org/goleak v1.2.1 // indirect
8685
go.uber.org/multierr v1.11.0 // indirect
87-
golang.org/x/crypto v0.11.0 // indirect
88-
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b // indirect
86+
go.uber.org/zap v1.25.0 // indirect
87+
golang.org/x/crypto v0.12.0 // indirect
88+
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb // indirect
8989
golang.org/x/mod v0.12.0 // indirect
90-
golang.org/x/sys v0.10.0 // indirect
91-
golang.org/x/text v0.11.0 // indirect
92-
golang.org/x/tools v0.11.1 // indirect
93-
google.golang.org/genproto v0.0.0-20230731193218-e0aa005b6bdf // indirect
94-
google.golang.org/genproto/googleapis/api v0.0.0-20230731193218-e0aa005b6bdf // indirect
95-
google.golang.org/genproto/googleapis/rpc v0.0.0-20230731193218-e0aa005b6bdf // indirect
90+
golang.org/x/sys v0.11.0 // indirect
91+
golang.org/x/text v0.12.0 // indirect
92+
golang.org/x/tools v0.12.0 // indirect
93+
google.golang.org/genproto v0.0.0-20230815205213-6bfd019c3878 // indirect
94+
google.golang.org/genproto/googleapis/api v0.0.0-20230815205213-6bfd019c3878 // indirect
95+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230815205213-6bfd019c3878 // indirect
9696
google.golang.org/grpc v1.57.0 // indirect
9797
gopkg.in/yaml.v3 v3.0.1 // indirect
98-
gotest.tools/v3 v3.5.0 // indirect
9998
)

0 commit comments

Comments
 (0)