Skip to content

Commit 5037009

Browse files
authored
Add metrics (#119)
1 parent f9f0036 commit 5037009

File tree

10 files changed

+256
-138
lines changed

10 files changed

+256
-138
lines changed

.github/workflows/docker.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
steps:
2323

2424
- name: Set up Go 1.20
25-
uses: actions/setup-go@v3
25+
uses: actions/setup-go@v4
2626
with:
2727
go-version: "1.20"
2828
id: go
@@ -87,7 +87,7 @@ jobs:
8787
steps:
8888

8989
- name: Set up Go 1.20
90-
uses: actions/setup-go@v3
90+
uses: actions/setup-go@v4
9191
with:
9292
go-version: "1.20"
9393
id: go

api/v1/apiv1connect/ipam.connect.go

Lines changed: 62 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1/ipam.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/server/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ func main() {
2727
Usage: "gRPC server endpoint",
2828
EnvVars: []string{"GOIPAM_GRPC_SERVER_ENDPOINT"},
2929
},
30+
&cli.StringFlag{
31+
Name: "metrics-endpoint",
32+
Value: "localhost:2112",
33+
Usage: "metrics endpoint",
34+
EnvVars: []string{"GOIPAM_METRICS_ENDPOINT"},
35+
},
3036
&cli.StringFlag{
3137
Name: "log-level",
3238
Value: "info",
@@ -291,6 +297,7 @@ func getConfig(ctx *cli.Context) config {
291297

292298
return config{
293299
GrpcServerEndpoint: ctx.String("grpc-server-endpoint"),
300+
MetricsEndpoint: ctx.String("metrics-endpoint"),
294301
Log: zlog.Sugar(),
295302
}
296303
}

cmd/server/server.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import (
44
"net/http"
55
"time"
66

7+
"github.com/bufbuild/connect-go"
8+
otelconnect "github.com/bufbuild/connect-opentelemetry-go"
79
compress "github.com/klauspost/connect-compress"
810
goipam "github.com/metal-stack/go-ipam"
911
"github.com/metal-stack/go-ipam/api/v1/apiv1connect"
1012
"github.com/metal-stack/go-ipam/pkg/service"
1113
"github.com/metal-stack/v"
14+
"github.com/prometheus/client_golang/prometheus/promhttp"
15+
"go.opentelemetry.io/otel/exporters/prometheus"
16+
"go.opentelemetry.io/otel/sdk/metric"
1217

1318
grpchealth "github.com/bufbuild/connect-grpchealth-go"
1419
grpcreflect "github.com/bufbuild/connect-grpcreflect-go"
@@ -20,6 +25,7 @@ import (
2025

2126
type config struct {
2227
GrpcServerEndpoint string
28+
MetricsEndpoint string
2329
Log *zap.SugaredLogger
2430
Storage goipam.Storage
2531
}
@@ -40,10 +46,44 @@ func newServer(c config) *server {
4046
}
4147
func (s *server) Run() error {
4248
s.log.Infow("starting go-ipam", "version", v.V, "backend", s.storage.Name())
49+
50+
// The exporter embeds a default OpenTelemetry Reader and
51+
// implements prometheus.Collector, allowing it to be used as
52+
// both a Reader and Collector.
53+
exporter, err := prometheus.New()
54+
if err != nil {
55+
return err
56+
}
57+
provider := metric.NewMeterProvider(metric.WithReader(exporter))
58+
59+
// Start the prometheus HTTP server and pass the exporter Collector to it
60+
go func() {
61+
s.log.Infof("serving metrics at %s/metrics", s.c.MetricsEndpoint)
62+
metricsServer := http.NewServeMux()
63+
metricsServer.Handle("/metrics", promhttp.Handler())
64+
ms := &http.Server{
65+
Addr: s.c.MetricsEndpoint,
66+
Handler: metricsServer,
67+
ReadHeaderTimeout: time.Minute,
68+
}
69+
err := ms.ListenAndServe()
70+
if err != nil {
71+
s.log.Errorw("unable to start metric endpoint", "error", err)
72+
return
73+
}
74+
}()
75+
4376
mux := http.NewServeMux()
4477
// The generated constructors return a path and a plain net/http
4578
// handler.
46-
mux.Handle(apiv1connect.NewIpamServiceHandler(service.New(s.log, s.ipamer)))
79+
mux.Handle(
80+
apiv1connect.NewIpamServiceHandler(
81+
service.New(s.log, s.ipamer),
82+
connect.WithInterceptors(
83+
otelconnect.NewInterceptor(otelconnect.WithMeterProvider(provider)),
84+
),
85+
),
86+
)
4787

4888
mux.Handle(grpchealth.NewHandler(
4989
grpchealth.NewStaticChecker(apiv1connect.IpamServiceName),
@@ -62,6 +102,6 @@ func (s *server) Run() error {
62102
ReadHeaderTimeout: 1 * time.Minute,
63103
}
64104

65-
err := server.ListenAndServe()
105+
err = server.ListenAndServe()
66106
return err
67107
}

go.mod

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,80 +4,97 @@ go 1.20
44

55
require (
66
github.com/avast/retry-go/v4 v4.3.4
7-
github.com/bufbuild/connect-go v1.7.0
8-
github.com/bufbuild/connect-grpchealth-go v1.1.0
9-
github.com/bufbuild/connect-grpcreflect-go v1.0.0
7+
github.com/bufbuild/connect-go v1.9.0
8+
github.com/bufbuild/connect-grpchealth-go v1.1.1
9+
github.com/bufbuild/connect-grpcreflect-go v1.1.0
10+
github.com/bufbuild/connect-opentelemetry-go v0.4.0
1011
github.com/jmoiron/sqlx v1.3.5
1112
github.com/klauspost/connect-compress v1.0.0
1213
github.com/lib/pq v1.10.9
1314
github.com/metal-stack/v v1.0.3
14-
github.com/redis/go-redis/v9 v9.0.4
15-
github.com/stretchr/testify v1.8.2
16-
github.com/testcontainers/testcontainers-go v0.20.1
17-
github.com/urfave/cli/v2 v2.25.3
15+
github.com/prometheus/client_golang v1.16.0
16+
github.com/redis/go-redis/v9 v9.0.5
17+
github.com/stretchr/testify v1.8.4
18+
github.com/testcontainers/testcontainers-go v0.21.0
19+
github.com/urfave/cli/v2 v2.25.7
1820
go.etcd.io/etcd/client/v3 v3.5.9
19-
go.mongodb.org/mongo-driver v1.11.6
21+
go.mongodb.org/mongo-driver v1.12.0
22+
go.opentelemetry.io/otel/exporters/prometheus v0.39.0
23+
go.opentelemetry.io/otel/sdk/metric v0.39.0
2024
go.uber.org/zap v1.24.0
2125
go4.org/netipx v0.0.0-20230303233057-f1b76eb4bb35
22-
golang.org/x/net v0.10.0
23-
golang.org/x/sync v0.2.0
24-
google.golang.org/protobuf v1.30.0
26+
golang.org/x/net v0.12.0
27+
golang.org/x/sync v0.3.0
28+
google.golang.org/protobuf v1.31.0
2529
)
2630

2731
require (
2832
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
2933
github.com/Microsoft/go-winio v0.6.1 // indirect
30-
github.com/benbjohnson/clock v1.3.4 // indirect
34+
github.com/benbjohnson/clock v1.3.5 // indirect
35+
github.com/beorn7/perks v1.0.1 // indirect
3136
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
3237
github.com/cespare/xxhash/v2 v2.2.0 // indirect
33-
github.com/containerd/containerd v1.7.1 // indirect
38+
github.com/containerd/containerd v1.7.2 // indirect
3439
github.com/coreos/go-semver v0.3.1 // indirect
3540
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
3641
github.com/cpuguy83/dockercfg v0.3.1 // indirect
3742
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
3843
github.com/davecgh/go-spew v1.1.1 // indirect
3944
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
4045
github.com/docker/distribution v2.8.2+incompatible // indirect
41-
github.com/docker/docker v24.0.0+incompatible // indirect
46+
github.com/docker/docker v24.0.4+incompatible // indirect
4247
github.com/docker/go-connections v0.4.0 // indirect
4348
github.com/docker/go-units v0.5.0 // indirect
49+
github.com/go-logr/logr v1.2.4 // indirect
50+
github.com/go-logr/stdr v1.2.2 // indirect
4451
github.com/gogo/protobuf v1.3.2 // indirect
4552
github.com/golang/protobuf v1.5.3 // indirect
4653
github.com/golang/snappy v0.0.4 // indirect
4754
github.com/google/uuid v1.3.0 // indirect
48-
github.com/imdario/mergo v0.3.13 // indirect
49-
github.com/klauspost/compress v1.16.5 // indirect
50-
github.com/kr/text v0.2.0 // indirect
55+
github.com/imdario/mergo v0.3.15 // indirect
56+
github.com/klauspost/compress v1.16.7 // indirect
5157
github.com/magiconair/properties v1.8.7 // indirect
58+
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
5259
github.com/moby/patternmatcher v0.5.0 // indirect
5360
github.com/moby/sys/sequential v0.5.0 // indirect
5461
github.com/moby/term v0.5.0 // indirect
5562
github.com/montanaflynn/stats v0.7.1 // indirect
5663
github.com/morikuni/aec v1.0.0 // indirect
5764
github.com/opencontainers/go-digest v1.0.0 // indirect
58-
github.com/opencontainers/image-spec v1.1.0-rc3 // indirect
65+
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
5966
github.com/opencontainers/runc v1.1.7 // indirect
6067
github.com/pkg/errors v0.9.1 // indirect
6168
github.com/pmezard/go-difflib v1.0.0 // indirect
69+
github.com/prometheus/client_model v0.4.0 // indirect
70+
github.com/prometheus/common v0.44.0 // indirect
71+
github.com/prometheus/procfs v0.11.0 // indirect
6272
github.com/russross/blackfriday/v2 v2.1.0 // indirect
63-
github.com/sirupsen/logrus v1.9.1 // indirect
73+
github.com/sirupsen/logrus v1.9.3 // indirect
6474
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
6575
github.com/xdg-go/scram v1.1.2 // indirect
6676
github.com/xdg-go/stringprep v1.0.4 // indirect
6777
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
6878
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
6979
go.etcd.io/etcd/api/v3 v3.5.9 // indirect
7080
go.etcd.io/etcd/client/pkg/v3 v3.5.9 // indirect
81+
go.opentelemetry.io/otel v1.16.0 // indirect
82+
go.opentelemetry.io/otel/metric v1.16.0 // indirect
83+
go.opentelemetry.io/otel/sdk v1.16.0 // indirect
84+
go.opentelemetry.io/otel/trace v1.16.0 // indirect
7185
go.uber.org/atomic v1.11.0 // indirect
7286
go.uber.org/goleak v1.2.1 // indirect
7387
go.uber.org/multierr v1.11.0 // indirect
74-
golang.org/x/crypto v0.9.0 // indirect
75-
golang.org/x/mod v0.10.0 // indirect
76-
golang.org/x/sys v0.8.0 // indirect
77-
golang.org/x/text v0.9.0 // indirect
78-
golang.org/x/tools v0.9.1 // indirect
79-
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
80-
google.golang.org/grpc v1.55.0 // indirect
88+
golang.org/x/crypto v0.11.0 // indirect
89+
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect
90+
golang.org/x/mod v0.12.0 // indirect
91+
golang.org/x/sys v0.10.0 // indirect
92+
golang.org/x/text v0.11.0 // indirect
93+
golang.org/x/tools v0.11.0 // indirect
94+
google.golang.org/genproto v0.0.0-20230706204954-ccb25ca9f130 // indirect
95+
google.golang.org/genproto/googleapis/api v0.0.0-20230706204954-ccb25ca9f130 // indirect
96+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230706204954-ccb25ca9f130 // indirect
97+
google.golang.org/grpc v1.56.2 // indirect
8198
gopkg.in/yaml.v3 v3.0.1 // indirect
82-
gotest.tools/v3 v3.4.0 // indirect
99+
gotest.tools/v3 v3.5.0 // indirect
83100
)

0 commit comments

Comments
 (0)