Skip to content

Commit

Permalink
Merge pull request #698 from prometheus/beorn7/release
Browse files Browse the repository at this point in the history
Migrate to slog and protodelim
  • Loading branch information
SuperQ authored Oct 10, 2024
2 parents 5eaa68e + b8c2691 commit 84b5b2c
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 130 deletions.
21 changes: 11 additions & 10 deletions api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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{
Expand Down Expand Up @@ -175,15 +176,15 @@ 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,
}, "")
}

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)
}
}

Expand All @@ -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)
}
}

Expand Down
4 changes: 2 additions & 2 deletions api/v1/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -32,7 +32,7 @@ import (
)

var (
logger = log.NewNopLogger()
logger = promslog.NewNopLogger()
testFlags = map[string]string{
"flag1": "value1",
"flag2": "value2",
Expand Down
19 changes: 8 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,31 @@ 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 (
github.com/klauspost/compress v1.17.9 // indirect
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 (
github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30 // indirect
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
Expand All @@ -42,7 +39,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
)
40 changes: 16 additions & 24 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ 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=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
Expand All @@ -36,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=
Expand All @@ -53,10 +46,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=
Expand All @@ -78,23 +71,22 @@ 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/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
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.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=
Expand Down
11 changes: 5 additions & 6 deletions handler/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand All @@ -37,20 +36,20 @@ 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
}
}
labelsString := route.Param(r.Context(), "labels")
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
Expand Down
18 changes: 9 additions & 9 deletions handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ 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/protodelim"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"

Expand All @@ -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
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand Down
Loading

0 comments on commit 84b5b2c

Please sign in to comment.