Skip to content

Commit

Permalink
cmd/stupgrades: Basic request metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
calmh committed Jun 3, 2024
1 parent 25eec4d commit 6d72e81
Showing 1 changed file with 54 additions and 7 deletions.
61 changes: 54 additions & 7 deletions cmd/stupgrades/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@ import (
"time"

"github.com/alecthomas/kong"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
_ "github.com/syncthing/syncthing/lib/automaxprocs"
"github.com/syncthing/syncthing/lib/build"
"github.com/syncthing/syncthing/lib/httpcache"
"github.com/syncthing/syncthing/lib/upgrade"
)

type cli struct {
Listen string `default:":8080" help:"Listen address"`
URL string `short:"u" default:"https://api.github.com/repos/syncthing/syncthing/releases?per_page=25" help:"GitHub releases url"`
Forward []string `short:"f" help:"Forwarded pages, format: /path->https://example/com/url"`
CacheTime time.Duration `default:"15m" help:"Cache time"`
Listen string `default:":8080" help:"Listen address"`
MetricsListen string `default:":8081" help:"Listen address for metrics"`
URL string `short:"u" default:"https://api.github.com/repos/syncthing/syncthing/releases?per_page=25" help:"GitHub releases url"`
Forward []string `short:"f" help:"Forwarded pages, format: /path->https://example/com/url"`
CacheTime time.Duration `default:"15m" help:"Cache time"`
}

func main() {
Expand All @@ -41,17 +46,59 @@ func main() {
}

func server(params *cli) error {
http.Handle("/meta.json", httpcache.SinglePath(&githubReleases{url: params.URL}, params.CacheTime))
if params.MetricsListen != "" {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
go func() {
log.Println("Listening for metrics on", params.MetricsListen)
if err := http.ListenAndServe(params.MetricsListen, mux); err != nil {
log.Fatalf("Failed to start metrics server: %v", err)
}
}()
}

mux := http.NewServeMux()
mux.Handle("/meta.json", withVersionCounter(httpcache.SinglePath(&githubReleases{url: params.URL}, params.CacheTime)))

for _, fwd := range params.Forward {
path, url, ok := strings.Cut(fwd, "->")
if !ok {
return fmt.Errorf("invalid forward: %q", fwd)
}
http.Handle(path, httpcache.SinglePath(&proxy{url: url}, params.CacheTime))
log.Println("Forwarding", path, "to", url)
mux.Handle(path, httpcache.SinglePath(&proxy{url: url}, params.CacheTime))
}

srv := &http.Server{
Addr: params.Listen,
Handler: mux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
srv.SetKeepAlivesEnabled(false)
return srv.ListenAndServe()
}

return http.ListenAndServe(params.Listen, nil)
var versionCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "upgrade_check_requests_total",
}, []string{"version"})

func withVersionCounter(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ua := r.Header.Get("User-Agent")
ver, err := build.ParseVersion(ua)
if err != nil {
versionCounter.WithLabelValues("unknown").Inc()
} else {
v := ver.Tag
v = strings.TrimPrefix(v, "v")
if idx := strings.Index(v, "-"); idx != -1 {
v = v[:idx]
}
versionCounter.WithLabelValues(v).Inc()
}
next.ServeHTTP(w, r)
})
}

type githubReleases struct {
Expand Down

0 comments on commit 6d72e81

Please sign in to comment.