Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: beorn7 <[email protected]>
  • Loading branch information
beorn7 committed Oct 9, 2024
1 parent 5eaa68e commit 564c804
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 80 deletions.
16 changes: 8 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ require (
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 (
Expand All @@ -42,7 +42,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
)
32 changes: 16 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,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 +78,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=
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("msg", "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("msg", "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("msg", "job name is required")
return
}
labels["job"] = job
Expand Down
15 changes: 7 additions & 8 deletions handler/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -58,28 +57,28 @@ 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")
if jobBase64Encoded {
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("msg", "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("msg", "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("msg", "job name is required")
return
}
labels["job"] = job
Expand Down Expand Up @@ -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("msg", "failed to parse text", "source", r.RemoteAddr, "err", err.Error())
return
}
now := time.Now()
Expand Down Expand Up @@ -144,7 +143,7 @@ func Push(
http.StatusBadRequest,
)
}
level.Error(logger).Log(
logger.Error(
"msg", "pushed metrics are invalid or inconsistent with existing metrics",
"method", r.Method,
"source", r.RemoteAddr,
Expand Down
12 changes: 5 additions & 7 deletions handler/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ import (
"html"
"html/template"
"io"
"log/slog"
"net/http"
"strconv"
"time"

"github.com/go-kit/log"
"github.com/go-kit/log/level"

dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/version"
"github.com/prometheus/pushgateway/histogram"
Expand Down Expand Up @@ -58,7 +56,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(
Expand Down Expand Up @@ -90,20 +88,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("msg", "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("msg", "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("msg", "error parsing template", "err", err.Error())
return
}

Expand Down
37 changes: 16 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"io"
"log/slog"
"net/http"
"net/http/pprof"
"net/url"
Expand All @@ -33,35 +34,29 @@ import (
"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)
Expand All @@ -75,22 +70,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{}
)
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("msg", "starting pushgateway", "version", version.Info())
logger.Info("build_context", version.BuildContext())
logger.Debug("msg", "external URL", "url", *externalURL)
logger.Debug("msg", "path prefix used externally", "path", externalPathPrefix)
logger.Debug("msg", "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.
Expand Down Expand Up @@ -124,7 +119,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,
)

Expand Down
Loading

0 comments on commit 564c804

Please sign in to comment.