From b43743814eb0186b136e0eb2fa4fa40b00501117 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Wed, 9 Oct 2024 22:56:26 +0200 Subject: [PATCH 1/2] Update dependencies and migrate to log/slog Signed-off-by: beorn7 --- api/v1/api.go | 21 +++++++------- api/v1/api_test.go | 4 +-- go.mod | 18 ++++++------ go.sum | 36 +++++++++++------------ handler/delete.go | 11 ++++--- handler/handler_test.go | 4 +-- handler/push.go | 17 ++++++----- handler/status.go | 14 ++++----- handler/wipe.go | 8 ++---- main.go | 51 ++++++++++++++------------------- storage/diskmetricstore.go | 27 +++++++++-------- storage/diskmetricstore_test.go | 4 +-- 12 files changed, 99 insertions(+), 116 deletions(-) diff --git a/api/v1/api.go b/api/v1/api.go index 9e625c2a..10631ba3 100644 --- a/api/v1/api.go +++ b/api/v1/api.go @@ -15,14 +15,15 @@ package v1 import ( "encoding/json" "fmt" + "log/slog" "net/http" "time" - "github.com/go-kit/log" - "github.com/go-kit/log/level" - dto "github.com/prometheus/client_model/go" + "github.com/prometheus/common/promslog" "github.com/prometheus/common/route" + dto "github.com/prometheus/client_model/go" + "github.com/prometheus/pushgateway/handler" "github.com/prometheus/pushgateway/histogram" "github.com/prometheus/pushgateway/storage" @@ -74,7 +75,7 @@ func setCORS(w http.ResponseWriter) { // API provides registration of handlers for API routes. type API struct { - logger log.Logger + logger *slog.Logger MetricStore storage.MetricStore Flags map[string]string StartTime time.Time @@ -83,13 +84,13 @@ type API struct { // New returns a new API. The log.Logger can be nil, in which case no logging is performed. func New( - l log.Logger, + l *slog.Logger, ms storage.MetricStore, flags map[string]string, buildInfo map[string]string, ) *API { if l == nil { - l = log.NewNopLogger() + l = promslog.NewNopLogger() } return &API{ @@ -175,7 +176,7 @@ func (api *API) respond(w http.ResponseWriter, data interface{}) { Data: data, }) if err != nil { - level.Error(api.logger).Log("msg", "error marshaling JSON", "err", err) + api.logger.Error("error marshaling JSON", "err", err) api.respondError(w, apiError{ typ: errorBadData, err: err, @@ -183,7 +184,7 @@ func (api *API) respond(w http.ResponseWriter, data interface{}) { } if _, err := w.Write(b); err != nil { - level.Error(api.logger).Log("msg", "failed to write data to connection", "err", err) + api.logger.Error("failed to write data to connection", "err", err) } } @@ -208,10 +209,10 @@ func (api *API) respondError(w http.ResponseWriter, apiErr apiError, data interf if err != nil { return } - level.Error(api.logger).Log("msg", "API error", "err", apiErr.Error()) + api.logger.Error("API error", "err", apiErr.Error()) if _, err := w.Write(b); err != nil { - level.Error(api.logger).Log("msg", "failed to write data to connection", "err", err) + api.logger.Error("failed to write data to connection", "err", err) } } diff --git a/api/v1/api_test.go b/api/v1/api_test.go index 2c4cc3d0..00d002a7 100644 --- a/api/v1/api_test.go +++ b/api/v1/api_test.go @@ -21,9 +21,9 @@ import ( "testing" "time" - "github.com/go-kit/log" //nolint:staticcheck // Ignore SA1019. Dependencies use the deprecated package, so we have to, too. "github.com/golang/protobuf/proto" + "github.com/prometheus/common/promslog" dto "github.com/prometheus/client_model/go" @@ -32,7 +32,7 @@ import ( ) var ( - logger = log.NewNopLogger() + logger = promslog.NewNopLogger() testFlags = map[string]string{ "flag1": "value1", "flag2": "value2", diff --git a/go.mod b/go.mod index aa083df1..7931cbda 100644 --- a/go.mod +++ b/go.mod @@ -6,16 +6,15 @@ toolchain go1.23.0 require ( github.com/alecthomas/kingpin/v2 v2.4.0 - github.com/go-kit/log v0.2.1 github.com/golang/protobuf v1.5.4 github.com/golang/snappy v0.0.4 github.com/matttproud/golang_protobuf_extensions v1.0.4 github.com/prometheus/client_golang v1.20.4 github.com/prometheus/client_model v0.6.1 - github.com/prometheus/common v0.59.1 - github.com/prometheus/exporter-toolkit v0.12.0 + github.com/prometheus/common v0.60.0 + github.com/prometheus/exporter-toolkit v0.13.0 github.com/shurcooL/vfsgen v0.0.0-20230704071429-0000e147ea92 - google.golang.org/protobuf v1.34.2 + google.golang.org/protobuf v1.35.1 ) require ( @@ -23,9 +22,9 @@ require ( github.com/mdlayher/socket v0.4.1 // indirect github.com/mdlayher/vsock v1.2.1 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect - golang.org/x/crypto v0.26.0 // indirect - golang.org/x/net v0.28.0 // indirect - golang.org/x/oauth2 v0.22.0 // indirect + golang.org/x/crypto v0.27.0 // indirect + golang.org/x/net v0.29.0 // indirect + golang.org/x/oauth2 v0.23.0 // indirect ) require ( @@ -33,7 +32,6 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect - github.com/go-logfmt/logfmt v0.6.0 // indirect github.com/jpillora/backoff v1.0.0 // indirect github.com/julienschmidt/httprouter v1.3.0 // indirect github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect @@ -42,7 +40,7 @@ require ( github.com/shurcooL/httpfs v0.0.0-20230704072500-f1e31cf0ba5c // indirect github.com/xhit/go-str2duration/v2 v2.1.0 // indirect golang.org/x/sync v0.8.0 // indirect - golang.org/x/sys v0.23.0 // indirect - golang.org/x/text v0.17.0 // indirect + golang.org/x/sys v0.25.0 // indirect + golang.org/x/text v0.18.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 5c70745d..9bb249fb 100644 --- a/go.sum +++ b/go.sum @@ -12,10 +12,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU= -github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0= -github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= -github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= @@ -53,10 +49,10 @@ github.com/prometheus/client_golang v1.20.4 h1:Tgh3Yr67PaOv/uTqloMsCEdeuFTatm5zI github.com/prometheus/client_golang v1.20.4/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE= github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E= github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY= -github.com/prometheus/common v0.59.1 h1:LXb1quJHWm1P6wq/U824uxYi4Sg0oGvNeUm1z5dJoX0= -github.com/prometheus/common v0.59.1/go.mod h1:GpWM7dewqmVYcd7SmRaiWVe9SSqjf0UrwnYnpEZNuT0= -github.com/prometheus/exporter-toolkit v0.12.0 h1:DkE5RcEZR3lQA2QD5JLVQIf41dFKNsVMXFhgqcif7fo= -github.com/prometheus/exporter-toolkit v0.12.0/go.mod h1:fQH0KtTn0yrrS0S82kqppRjDDiwMfIQUwT+RBRRhwUc= +github.com/prometheus/common v0.60.0 h1:+V9PAREWNvJMAuJ1x1BaWl9dewMW4YrHZQbx0sJNllA= +github.com/prometheus/common v0.60.0/go.mod h1:h0LYf1R1deLSKtD4Vdg8gy4RuOvENW2J/h19V5NADQw= +github.com/prometheus/exporter-toolkit v0.13.0 h1:lmA0Q+8IaXgmFRKw09RldZmZdnvu9wwcDLIXGmTPw1c= +github.com/prometheus/exporter-toolkit v0.13.0/go.mod h1:2uop99EZl80KdXhv/MxVI2181fMcwlsumFOqBecGkG0= github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc= github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk= github.com/prometheus/prometheus v0.54.1 h1:vKuwQNjnYN2/mDoWfHXDhAsz/68q/dQDb+YbcEqU7MQ= @@ -78,23 +74,23 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc= github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU= -golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= -golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= -golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= -golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= -golang.org/x/oauth2 v0.22.0 h1:BzDx2FehcG7jJwgWLELCdmLuxk2i+x9UDpSiss2u0ZA= -golang.org/x/oauth2 v0.22.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= +golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= +golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= +golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= +golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= +golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs= +golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= -golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= -golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= +golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg= golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI= -google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= -google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA= +google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/handler/delete.go b/handler/delete.go index c4b963a4..8ccd7953 100644 --- a/handler/delete.go +++ b/handler/delete.go @@ -15,11 +15,10 @@ package handler import ( "fmt" + "log/slog" "net/http" "time" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus/common/route" "github.com/prometheus/pushgateway/storage" @@ -28,7 +27,7 @@ import ( // Delete returns a handler that accepts delete requests. // // The returned handler is already instrumented for Prometheus. -func Delete(ms storage.MetricStore, jobBase64Encoded bool, logger log.Logger) func(http.ResponseWriter, *http.Request) { +func Delete(ms storage.MetricStore, jobBase64Encoded bool, logger *slog.Logger) func(http.ResponseWriter, *http.Request) { instrumentedHandler := InstrumentWithCounter( "delete", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -37,7 +36,7 @@ func Delete(ms storage.MetricStore, jobBase64Encoded bool, logger log.Logger) fu var err error if job, err = decodeBase64(job); err != nil { http.Error(w, fmt.Sprintf("invalid base64 encoding in job name %q: %v", job, err), http.StatusBadRequest) - level.Debug(logger).Log("msg", "invalid base64 encoding in job name", "job", job, "err", err.Error()) + logger.Debug("invalid base64 encoding in job name", "job", job, "err", err.Error()) return } } @@ -45,12 +44,12 @@ func Delete(ms storage.MetricStore, jobBase64Encoded bool, logger log.Logger) fu labels, err := splitLabels(labelsString) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) - level.Debug(logger).Log("msg", "failed to parse URL", "url", labelsString, "err", err.Error()) + logger.Debug("failed to parse URL", "url", labelsString, "err", err.Error()) return } if job == "" { http.Error(w, "job name is required", http.StatusBadRequest) - level.Debug(logger).Log("msg", "job name is required") + logger.Debug("job name is required") return } labels["job"] = job diff --git a/handler/handler_test.go b/handler/handler_test.go index 1f9d388b..5c33a15d 100644 --- a/handler/handler_test.go +++ b/handler/handler_test.go @@ -23,9 +23,9 @@ import ( "testing" "time" - "github.com/go-kit/log" "github.com/matttproud/golang_protobuf_extensions/pbutil" "github.com/prometheus/common/model" + "github.com/prometheus/common/promslog" "github.com/prometheus/common/route" "google.golang.org/protobuf/encoding/prototext" "google.golang.org/protobuf/proto" @@ -35,7 +35,7 @@ import ( "github.com/prometheus/pushgateway/storage" ) -var logger = log.NewNopLogger() +var logger = promslog.NewNopLogger() // MockMetricStore isn't doing any of the validation and sanitation a real // metric store implementation has to do. Those are tested in the storage diff --git a/handler/push.go b/handler/push.go index f6eef1aa..a9f0defc 100644 --- a/handler/push.go +++ b/handler/push.go @@ -17,13 +17,12 @@ import ( "encoding/base64" "fmt" "io" + "log/slog" "mime" "net/http" "strings" "time" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/matttproud/golang_protobuf_extensions/pbutil" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/expfmt" @@ -58,7 +57,7 @@ var ( func Push( ms storage.MetricStore, replace, check, jobBase64Encoded bool, - logger log.Logger, + logger *slog.Logger, ) func(http.ResponseWriter, *http.Request) { handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { job := route.Param(r.Context(), "job") @@ -66,7 +65,7 @@ func Push( var err error if job, err = decodeBase64(job); err != nil { http.Error(w, fmt.Sprintf("invalid base64 encoding in job name %q: %v", job, err), http.StatusBadRequest) - level.Debug(logger).Log("msg", "invalid base64 encoding in job name", "job", job, "err", err.Error()) + logger.Debug("invalid base64 encoding in job name", "job", job, "err", err.Error()) return } } @@ -74,12 +73,12 @@ func Push( labels, err := splitLabels(labelsString) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) - level.Debug(logger).Log("msg", "failed to parse URL", "url", labelsString, "err", err.Error()) + logger.Debug("failed to parse URL", "url", labelsString, "err", err.Error()) return } if job == "" { http.Error(w, "job name is required", http.StatusBadRequest) - level.Debug(logger).Log("msg", "job name is required") + logger.Debug("job name is required") return } labels["job"] = job @@ -109,7 +108,7 @@ func Push( } if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) - level.Debug(logger).Log("msg", "failed to parse text", "source", r.RemoteAddr, "err", err.Error()) + logger.Debug("failed to parse text", "source", r.RemoteAddr, "err", err.Error()) return } now := time.Now() @@ -144,8 +143,8 @@ func Push( http.StatusBadRequest, ) } - level.Error(logger).Log( - "msg", "pushed metrics are invalid or inconsistent with existing metrics", + logger.Error( + "pushed metrics are invalid or inconsistent with existing metrics", "method", r.Method, "source", r.RemoteAddr, "err", err.Error(), diff --git a/handler/status.go b/handler/status.go index c26b7b7e..9fe83c5e 100644 --- a/handler/status.go +++ b/handler/status.go @@ -19,15 +19,15 @@ import ( "html" "html/template" "io" + "log/slog" "net/http" "strconv" "time" - "github.com/go-kit/log" - "github.com/go-kit/log/level" + "github.com/prometheus/common/version" dto "github.com/prometheus/client_model/go" - "github.com/prometheus/common/version" + "github.com/prometheus/pushgateway/histogram" "github.com/prometheus/pushgateway/storage" ) @@ -58,7 +58,7 @@ func Status( root http.FileSystem, flags map[string]string, pathPrefix string, - logger log.Logger, + logger *slog.Logger, ) http.Handler { birth := time.Now() return InstrumentWithCounter( @@ -90,20 +90,20 @@ func Status( f, err := root.Open("template.html") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) - level.Error(logger).Log("msg", "error loading template.html", "err", err.Error()) + logger.Error("error loading template.html", "err", err.Error()) return } defer f.Close() tpl, err := io.ReadAll(f) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) - level.Error(logger).Log("msg", "error reading template.html", "err", err.Error()) + logger.Error("error reading template.html", "err", err.Error()) return } _, err = t.Parse(string(tpl)) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) - level.Error(logger).Log("msg", "error parsing template", "err", err.Error()) + logger.Error("error parsing template", "err", err.Error()) return } diff --git a/handler/wipe.go b/handler/wipe.go index ee916d8e..8e4b362d 100644 --- a/handler/wipe.go +++ b/handler/wipe.go @@ -14,12 +14,10 @@ package handler import ( + "log/slog" "net/http" "time" - "github.com/go-kit/log" - "github.com/go-kit/log/level" - "github.com/prometheus/pushgateway/storage" ) @@ -28,13 +26,13 @@ import ( // The returned handler is already instrumented for Prometheus. func WipeMetricStore( ms storage.MetricStore, - logger log.Logger) http.Handler { + logger *slog.Logger) http.Handler { return InstrumentWithCounter( "wipe", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusAccepted) - level.Debug(logger).Log("msg", "start wiping metric store") + logger.Debug("start wiping metric store") // Delete all metric groups by sending write requests with MetricFamilies equal to nil. for _, group := range ms.GetMetricFamiliesMap() { ms.SubmitWriteRequest(storage.WriteRequest{ diff --git a/main.go b/main.go index 934f9893..1ddee190 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ import ( "context" "fmt" "io" + "log/slog" "net/http" "net/http/pprof" "net/url" @@ -29,39 +30,31 @@ import ( "syscall" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/golang/snappy" "github.com/prometheus/client_golang/prometheus" - versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/model" - "github.com/prometheus/common/promlog" + "github.com/prometheus/common/promslog" "github.com/prometheus/common/route" "github.com/prometheus/common/version" "github.com/prometheus/exporter-toolkit/web" - webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag" + versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version" dto "github.com/prometheus/client_model/go" - promlogflag "github.com/prometheus/common/promlog/flag" + promslogflag "github.com/prometheus/common/promslog/flag" + webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag" - api_v1 "github.com/prometheus/pushgateway/api/v1" "github.com/prometheus/pushgateway/asset" "github.com/prometheus/pushgateway/handler" "github.com/prometheus/pushgateway/storage" + + api_v1 "github.com/prometheus/pushgateway/api/v1" ) func init() { prometheus.MustRegister(versioncollector.NewCollector("pushgateway")) } -// logFunc in an adaptor to plug gokit logging into promhttp.HandlerOpts. -type logFunc func(...interface{}) error - -func (lf logFunc) Println(v ...interface{}) { - lf("msg", fmt.Sprintln(v...)) -} - func main() { var ( app = kingpin.New(filepath.Base(os.Args[0]), "The Pushgateway").UsageWriter(os.Stdout) @@ -75,22 +68,22 @@ func main() { persistenceInterval = app.Flag("persistence.interval", "The minimum interval at which to write out the persistence file.").Default("5m").Duration() pushUnchecked = app.Flag("push.disable-consistency-check", "Do not check consistency of pushed metrics. DANGEROUS.").Default("false").Bool() pushUTF8Names = app.Flag("push.enable-utf8-names", "Allow UTF-8 characters in metric and label names.").Default("false").Bool() - promlogConfig = promlog.Config{} + promlogConfig = promslog.Config{Style: promslog.GoKitStyle} ) - promlogflag.AddFlags(app, &promlogConfig) + promslogflag.AddFlags(app, &promlogConfig) app.Version(version.Print("pushgateway")) app.HelpFlag.Short('h') kingpin.MustParse(app.Parse(os.Args[1:])) - logger := promlog.New(&promlogConfig) + logger := promslog.New(&promlogConfig) *routePrefix = computeRoutePrefix(*routePrefix, *externalURL) externalPathPrefix := computeRoutePrefix("", *externalURL) - level.Info(logger).Log("msg", "starting pushgateway", "version", version.Info()) - level.Info(logger).Log("build_context", version.BuildContext()) - level.Debug(logger).Log("msg", "external URL", "url", *externalURL) - level.Debug(logger).Log("msg", "path prefix used externally", "path", externalPathPrefix) - level.Debug(logger).Log("msg", "path prefix for internal routing", "path", *routePrefix) + logger.Info("starting pushgateway", "version", version.Info()) + logger.Info("Build context", "build_context", version.BuildContext()) + logger.Debug("external URL", "url", *externalURL) + logger.Debug("path prefix used externally", "path", externalPathPrefix) + logger.Debug("path prefix for internal routing", "path", *routePrefix) // flags is used to show command line flags on the status page. // Kingpin default flags are excluded as they would be confusing. @@ -124,7 +117,7 @@ func main() { r.Get( path.Join(*routePrefix, *metricsPath), promhttp.HandlerFor(g, promhttp.HandlerOpts{ - ErrorLog: logFunc(level.Error(logger).Log), + ErrorLog: slog.NewLogLogger(logger.Handler(), slog.LevelError), }).ServeHTTP, ) @@ -206,13 +199,13 @@ func main() { // In the case of a graceful shutdown, do not log the error. if err == http.ErrServerClosed { - level.Info(logger).Log("msg", "HTTP server stopped") + logger.Info("HTTP server stopped") } else { - level.Error(logger).Log("msg", "HTTP server stopped", "err", err) + logger.Error("HTTP server stopped", "err", err) } if err := ms.Shutdown(); err != nil { - level.Error(logger).Log("msg", "problem shutting down metric storage", "err", err) + logger.Error("problem shutting down metric storage", "err", err) } } @@ -277,16 +270,16 @@ func computeRoutePrefix(prefix string, externalURL *url.URL) string { // shutdownServerOnQuit shutdowns the provided server upon closing the provided // quitCh or upon receiving a SIGINT or SIGTERM. -func shutdownServerOnQuit(server *http.Server, quitCh <-chan struct{}, logger log.Logger) error { +func shutdownServerOnQuit(server *http.Server, quitCh <-chan struct{}, logger *slog.Logger) error { notifier := make(chan os.Signal, 1) signal.Notify(notifier, os.Interrupt, syscall.SIGTERM) select { case <-notifier: - level.Info(logger).Log("msg", "received SIGINT/SIGTERM; exiting gracefully...") + logger.Info("received SIGINT/SIGTERM; exiting gracefully...") break case <-quitCh: - level.Warn(logger).Log("msg", "received termination request via web service, exiting gracefully...") + logger.Warn("received termination request via web service, exiting gracefully...") break } return server.Shutdown(context.Background()) diff --git a/storage/diskmetricstore.go b/storage/diskmetricstore.go index 462c9912..b835d914 100644 --- a/storage/diskmetricstore.go +++ b/storage/diskmetricstore.go @@ -17,6 +17,7 @@ import ( "encoding/gob" "errors" "fmt" + "log/slog" "os" "path" "sort" @@ -24,13 +25,11 @@ import ( "sync" "time" - "github.com/go-kit/log" - "github.com/go-kit/log/level" - //nolint:staticcheck // Ignore SA1019. Dependencies use the deprecated package, so we have to, too. "github.com/golang/protobuf/proto" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/common/model" + "github.com/prometheus/common/promslog" dto "github.com/prometheus/client_model/go" ) @@ -55,7 +54,7 @@ type DiskMetricStore struct { metricGroups GroupingKeyToMetricGroup persistenceFile string predefinedHelp map[string]string - logger log.Logger + logger *slog.Logger } type mfStat struct { @@ -80,7 +79,7 @@ func NewDiskMetricStore( persistenceFile string, persistenceInterval time.Duration, gatherPredefinedHelpFrom prometheus.Gatherer, - logger log.Logger, + logger *slog.Logger, ) *DiskMetricStore { // TODO: Do that outside of the constructor to allow the HTTP server to // serve /-/healthy and /-/ready earlier. @@ -93,12 +92,12 @@ func NewDiskMetricStore( logger: logger, } if err := dms.restore(); err != nil { - level.Error(logger).Log("msg", "could not load persisted metrics", "err", err) + logger.Error("could not load persisted metrics", "err", err) } if helpStrings, err := extractPredefinedHelpStrings(gatherPredefinedHelpFrom); err == nil { dms.predefinedHelp = helpStrings } else { - level.Error(logger).Log("msg", "could not gather metrics for predefined help strings", "err", err) + logger.Error("could not gather metrics for predefined help strings", "err", err) } go dms.loop(persistenceInterval) @@ -126,7 +125,7 @@ func (dms *DiskMetricStore) Healthy() error { // considered as healthy. if len(dms.writeQueue) == cap(dms.writeQueue) { err := fmt.Errorf("write queue is full") - level.Warn(dms.logger).Log("msg", err) + dms.logger.Warn(err.Error()) return err } @@ -150,7 +149,7 @@ func (dms *DiskMetricStore) GetMetricFamilies() []*dto.MetricFamily { for name, tmf := range group.Metrics { mf := tmf.GetMetricFamily() if mf == nil { - level.Warn(dms.logger).Log("msg", "storage corruption detected, consider wiping the persistence file") + dms.logger.Warn("storage corruption detected, consider wiping the persistence file") continue } stat, exists := mfStatByName[name] @@ -165,7 +164,7 @@ func (dms *DiskMetricStore) GetMetricFamilies() []*dto.MetricFamily { result[stat.pos] = existingMF } if mf.GetHelp() != existingMF.GetHelp() { - level.Info(dms.logger).Log("msg", "metric families inconsistent help strings", "err", "Metric families have inconsistent help strings. The latter will have priority. This is bad. Fix your pushed metrics!", "new", mf, "old", existingMF) + dms.logger.Info("metric families inconsistent help strings", "err", "Metric families have inconsistent help strings. The latter will have priority. This is bad. Fix your pushed metrics!", "new", mf, "old", existingMF) } // Type inconsistency cannot be fixed here. We will detect it during // gathering anyway, so no reason to log anything here. @@ -173,7 +172,7 @@ func (dms *DiskMetricStore) GetMetricFamilies() []*dto.MetricFamily { } else { copied := false if help, ok := dms.predefinedHelp[name]; ok && mf.GetHelp() != help { - level.Info(dms.logger).Log("msg", "metric families overlap", "err", "Metric family has the same name as a metric family used by the Pushgateway itself but it has a different help string. Changing it to the standard help string. This is bad. Fix your pushed metrics!", "metric_family", mf, "standard_help", help) + dms.logger.Info("metric families overlap", "err", "Metric family has the same name as a metric family used by the Pushgateway itself but it has a different help string. Changing it to the standard help string. This is bad. Fix your pushed metrics!", "metric_family", mf, "standard_help", help) mf = copyMetricFamily(mf) copied = true mf.Help = proto.String(help) @@ -218,9 +217,9 @@ func (dms *DiskMetricStore) loop(persistenceInterval time.Duration) { func() { persistStarted := time.Now() if err := dms.persist(); err != nil { - level.Error(dms.logger).Log("msg", "error persisting metrics", "err", err) + dms.logger.Error("error persisting metrics", "err", err) } else { - level.Info(dms.logger).Log("msg", "metrics persisted", "file", dms.persistenceFile) + dms.logger.Info("metrics persisted", "file", dms.persistenceFile) } persistDone <- persistStarted }, @@ -381,7 +380,7 @@ func (dms *DiskMetricStore) checkWriteRequest(wr WriteRequest) bool { tdms := &DiskMetricStore{ metricGroups: dms.GetMetricFamiliesMap(), predefinedHelp: dms.predefinedHelp, - logger: log.NewNopLogger(), + logger: promslog.NewNopLogger(), } tdms.processWriteRequest(wr) diff --git a/storage/diskmetricstore_test.go b/storage/diskmetricstore_test.go index f80d9372..7fe0c36c 100644 --- a/storage/diskmetricstore_test.go +++ b/storage/diskmetricstore_test.go @@ -22,11 +22,11 @@ import ( "testing" "time" - "github.com/go-kit/log" //nolint:staticcheck // Ignore SA1019. Dependencies use the deprecated package, so we have to, too. "github.com/golang/protobuf/proto" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/common/model" + "github.com/prometheus/common/promslog" dto "github.com/prometheus/client_model/go" @@ -34,7 +34,7 @@ import ( ) var ( - logger = log.NewNopLogger() + logger = promslog.NewNopLogger() // Example metric families. Keep labels sorted lexicographically! mf1a = &dto.MetricFamily{ Name: proto.String("mf1"), From b8c26919de016b8b0ce4d40564dd001df7c874a3 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Thu, 10 Oct 2024 17:09:39 +0200 Subject: [PATCH 2/2] Migrate from pbutil to protodelim To be precise: From github.com/matttproud/golang_protobuf_extensions/pbutil to google.golang.org/protobuf/encoding/protodelim . Signed-off-by: beorn7 --- go.mod | 1 - go.sum | 4 ---- handler/handler_test.go | 14 +++++++------- handler/push.go | 9 +++++++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index 7931cbda..7832cf14 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,6 @@ require ( github.com/alecthomas/kingpin/v2 v2.4.0 github.com/golang/protobuf v1.5.4 github.com/golang/snappy v0.0.4 - github.com/matttproud/golang_protobuf_extensions v1.0.4 github.com/prometheus/client_golang v1.20.4 github.com/prometheus/client_model v0.6.1 github.com/prometheus/common v0.60.0 diff --git a/go.sum b/go.sum index 9bb249fb..42e5f60b 100644 --- a/go.sum +++ b/go.sum @@ -13,7 +13,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= @@ -32,8 +31,6 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= -github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= -github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U= github.com/mdlayher/socket v0.4.1/go.mod h1:cAqeGjoufqdxWkD7DkpyS+wcefOtmu5OQ8KuoJGIReA= github.com/mdlayher/vsock v1.2.1 h1:pC1mTJTvjo1r9n9fbm7S1j04rCgCzhCOS5DY0zqHlnQ= @@ -80,7 +77,6 @@ golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs= golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= -golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= diff --git a/handler/handler_test.go b/handler/handler_test.go index 5c33a15d..eb9cdbfc 100644 --- a/handler/handler_test.go +++ b/handler/handler_test.go @@ -23,10 +23,10 @@ import ( "testing" "time" - "github.com/matttproud/golang_protobuf_extensions/pbutil" "github.com/prometheus/common/model" "github.com/prometheus/common/promslog" "github.com/prometheus/common/route" + "google.golang.org/protobuf/encoding/protodelim" "google.golang.org/protobuf/encoding/prototext" "google.golang.org/protobuf/proto" @@ -335,7 +335,7 @@ func TestPush(t *testing.T) { // With job name and instance name and protobuf content. mms.lastWriteRequest = storage.WriteRequest{} buf := &bytes.Buffer{} - _, err = pbutil.WriteDelimited(buf, &dto.MetricFamily{ + _, err = protodelim.MarshalTo(buf, &dto.MetricFamily{ Name: proto.String("some_metric"), Type: dto.MetricType_UNTYPED.Enum(), Metric: []*dto.Metric{ @@ -350,7 +350,7 @@ func TestPush(t *testing.T) { t.Fatal(err) } - _, err = pbutil.WriteDelimited(buf, &dto.MetricFamily{ + _, err = protodelim.MarshalTo(buf, &dto.MetricFamily{ Name: proto.String("another_metric"), Type: dto.MetricType_UNTYPED.Enum(), Metric: []*dto.Metric{ @@ -365,7 +365,7 @@ func TestPush(t *testing.T) { t.Fatal(err) } - _, err = pbutil.WriteDelimited(buf, &dto.MetricFamily{ + _, err = protodelim.MarshalTo(buf, &dto.MetricFamily{ Name: proto.String("histogram_metric"), Type: dto.MetricType_HISTOGRAM.Enum(), Metric: []*dto.Metric{ @@ -518,7 +518,7 @@ func TestPushUTF8(t *testing.T) { // With job name, instance name, UTF-8 escaped label name in params, UTF-8 metric names and protobuf content. mms.lastWriteRequest = storage.WriteRequest{} buf := &bytes.Buffer{} - _, err = pbutil.WriteDelimited(buf, &dto.MetricFamily{ + _, err = protodelim.MarshalTo(buf, &dto.MetricFamily{ Name: proto.String("some.metric"), Type: dto.MetricType_UNTYPED.Enum(), Metric: []*dto.Metric{ @@ -533,7 +533,7 @@ func TestPushUTF8(t *testing.T) { t.Fatal(err) } - _, err = pbutil.WriteDelimited(buf, &dto.MetricFamily{ + _, err = protodelim.MarshalTo(buf, &dto.MetricFamily{ Name: proto.String("another.metric"), Type: dto.MetricType_UNTYPED.Enum(), Metric: []*dto.Metric{ @@ -548,7 +548,7 @@ func TestPushUTF8(t *testing.T) { t.Fatal(err) } - _, err = pbutil.WriteDelimited(buf, &dto.MetricFamily{ + _, err = protodelim.MarshalTo(buf, &dto.MetricFamily{ Name: proto.String("histogram.metric"), Type: dto.MetricType_HISTOGRAM.Enum(), Metric: []*dto.Metric{ diff --git a/handler/push.go b/handler/push.go index a9f0defc..45a64b46 100644 --- a/handler/push.go +++ b/handler/push.go @@ -14,6 +14,7 @@ package handler import ( + "bufio" "encoding/base64" "fmt" "io" @@ -23,11 +24,11 @@ import ( "strings" "time" - "github.com/matttproud/golang_protobuf_extensions/pbutil" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/expfmt" "github.com/prometheus/common/model" "github.com/prometheus/common/route" + "google.golang.org/protobuf/encoding/protodelim" dto "github.com/prometheus/client_model/go" @@ -89,9 +90,13 @@ func Push( ctParams["encoding"] == "delimited" && ctParams["proto"] == "io.prometheus.client.MetricFamily" { metricFamilies = map[string]*dto.MetricFamily{} + unmarshaler := protodelim.UnmarshalOptions{ + MaxSize: -1, + } + in := bufio.NewReader(r.Body) for { mf := &dto.MetricFamily{} - if _, err = pbutil.ReadDelimited(r.Body, mf); err != nil { + if err = unmarshaler.UnmarshalFrom(in, mf); err != nil { if err == io.EOF { err = nil }