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 a01d888
Showing 1 changed file with 51 additions and 7 deletions.
58 changes: 51 additions & 7 deletions cmd/stupgrades/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ 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/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 +45,57 @@ 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) {
ver := "unknown"
ua := r.Header.Get("User-Agent")
parts := strings.SplitN(ua, " ", 3)
if len(parts) == 3 && parts[0] == "syncthing" {
ver = strings.TrimPrefix(parts[1], "v")
if idx := strings.Index(ver, "-"); idx != -1 {
ver = ver[:idx]
}
}
versionCounter.WithLabelValues(ver).Inc()
next.ServeHTTP(w, r)
})
}

type githubReleases struct {
Expand Down

0 comments on commit a01d888

Please sign in to comment.