Skip to content

Commit

Permalink
Add metrics (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Jul 11, 2023
1 parent f9f0036 commit 5037009
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 138 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
steps:

- name: Set up Go 1.20
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: "1.20"
id: go
Expand Down Expand Up @@ -87,7 +87,7 @@ jobs:
steps:

- name: Set up Go 1.20
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: "1.20"
id: go
Expand Down
92 changes: 62 additions & 30 deletions api/v1/apiv1connect/ipam.connect.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/v1/ipam.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func main() {
Usage: "gRPC server endpoint",
EnvVars: []string{"GOIPAM_GRPC_SERVER_ENDPOINT"},
},
&cli.StringFlag{
Name: "metrics-endpoint",
Value: "localhost:2112",
Usage: "metrics endpoint",
EnvVars: []string{"GOIPAM_METRICS_ENDPOINT"},
},
&cli.StringFlag{
Name: "log-level",
Value: "info",
Expand Down Expand Up @@ -291,6 +297,7 @@ func getConfig(ctx *cli.Context) config {

return config{
GrpcServerEndpoint: ctx.String("grpc-server-endpoint"),
MetricsEndpoint: ctx.String("metrics-endpoint"),
Log: zlog.Sugar(),
}
}
44 changes: 42 additions & 2 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import (
"net/http"
"time"

"github.com/bufbuild/connect-go"
otelconnect "github.com/bufbuild/connect-opentelemetry-go"
compress "github.com/klauspost/connect-compress"
goipam "github.com/metal-stack/go-ipam"
"github.com/metal-stack/go-ipam/api/v1/apiv1connect"
"github.com/metal-stack/go-ipam/pkg/service"
"github.com/metal-stack/v"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/sdk/metric"

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

type config struct {
GrpcServerEndpoint string
MetricsEndpoint string
Log *zap.SugaredLogger
Storage goipam.Storage
}
Expand All @@ -40,10 +46,44 @@ func newServer(c config) *server {
}
func (s *server) Run() error {
s.log.Infow("starting go-ipam", "version", v.V, "backend", s.storage.Name())

// The exporter embeds a default OpenTelemetry Reader and
// implements prometheus.Collector, allowing it to be used as
// both a Reader and Collector.
exporter, err := prometheus.New()
if err != nil {
return err
}
provider := metric.NewMeterProvider(metric.WithReader(exporter))

// Start the prometheus HTTP server and pass the exporter Collector to it
go func() {
s.log.Infof("serving metrics at %s/metrics", s.c.MetricsEndpoint)
metricsServer := http.NewServeMux()
metricsServer.Handle("/metrics", promhttp.Handler())
ms := &http.Server{
Addr: s.c.MetricsEndpoint,
Handler: metricsServer,
ReadHeaderTimeout: time.Minute,
}
err := ms.ListenAndServe()
if err != nil {
s.log.Errorw("unable to start metric endpoint", "error", err)
return
}
}()

mux := http.NewServeMux()
// The generated constructors return a path and a plain net/http
// handler.
mux.Handle(apiv1connect.NewIpamServiceHandler(service.New(s.log, s.ipamer)))
mux.Handle(
apiv1connect.NewIpamServiceHandler(
service.New(s.log, s.ipamer),
connect.WithInterceptors(
otelconnect.NewInterceptor(otelconnect.WithMeterProvider(provider)),
),
),
)

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

err := server.ListenAndServe()
err = server.ListenAndServe()
return err
}
71 changes: 44 additions & 27 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,80 +4,97 @@ go 1.20

require (
github.com/avast/retry-go/v4 v4.3.4
github.com/bufbuild/connect-go v1.7.0
github.com/bufbuild/connect-grpchealth-go v1.1.0
github.com/bufbuild/connect-grpcreflect-go v1.0.0
github.com/bufbuild/connect-go v1.9.0
github.com/bufbuild/connect-grpchealth-go v1.1.1
github.com/bufbuild/connect-grpcreflect-go v1.1.0
github.com/bufbuild/connect-opentelemetry-go v0.4.0
github.com/jmoiron/sqlx v1.3.5
github.com/klauspost/connect-compress v1.0.0
github.com/lib/pq v1.10.9
github.com/metal-stack/v v1.0.3
github.com/redis/go-redis/v9 v9.0.4
github.com/stretchr/testify v1.8.2
github.com/testcontainers/testcontainers-go v0.20.1
github.com/urfave/cli/v2 v2.25.3
github.com/prometheus/client_golang v1.16.0
github.com/redis/go-redis/v9 v9.0.5
github.com/stretchr/testify v1.8.4
github.com/testcontainers/testcontainers-go v0.21.0
github.com/urfave/cli/v2 v2.25.7
go.etcd.io/etcd/client/v3 v3.5.9
go.mongodb.org/mongo-driver v1.11.6
go.mongodb.org/mongo-driver v1.12.0
go.opentelemetry.io/otel/exporters/prometheus v0.39.0
go.opentelemetry.io/otel/sdk/metric v0.39.0
go.uber.org/zap v1.24.0
go4.org/netipx v0.0.0-20230303233057-f1b76eb4bb35
golang.org/x/net v0.10.0
golang.org/x/sync v0.2.0
google.golang.org/protobuf v1.30.0
golang.org/x/net v0.12.0
golang.org/x/sync v0.3.0
google.golang.org/protobuf v1.31.0
)

require (
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/benbjohnson/clock v1.3.4 // indirect
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/containerd/containerd v1.7.1 // indirect
github.com/containerd/containerd v1.7.2 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v24.0.0+incompatible // indirect
github.com/docker/docker v24.0.4+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/klauspost/compress v1.16.5 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/moby/patternmatcher v0.5.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc3 // indirect
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
github.com/opencontainers/runc v1.1.7 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sirupsen/logrus v1.9.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
go.etcd.io/etcd/api/v3 v3.5.9 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.9 // indirect
go.opentelemetry.io/otel v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/sdk v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/goleak v1.2.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/tools v0.9.1 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.55.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/tools v0.11.0 // indirect
google.golang.org/genproto v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/grpc v1.56.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.4.0 // indirect
gotest.tools/v3 v3.5.0 // indirect
)
Loading

0 comments on commit 5037009

Please sign in to comment.