HTTP reverse proxy for Tailscale

add upstream label to all http metrics

+15 -16
+15 -16
main.go
··· 26 26 ) 27 27 28 28 var ( 29 - requestsInFlight = promauto.NewGauge(prometheus.GaugeOpts{ 30 - Namespace: "tsproxy", 31 - Name: "requests_in_flight", 32 - Help: "Number of requests currently being served by the server.", 33 - }) 29 + requestsInFlight = promauto.NewGaugeVec( 30 + prometheus.GaugeOpts{ 31 + Namespace: "tsproxy", 32 + Name: "requests_in_flight", 33 + Help: "Number of requests currently being served by the server.", 34 + }, 35 + []string{"upstream"}, 36 + ) 34 37 35 38 requests = promauto.NewCounterVec( 36 39 prometheus.CounterOpts{ ··· 38 41 Name: "requests_total", 39 42 Help: "Number of requests received by the server.", 40 43 }, 41 - []string{"code", "method"}, 44 + []string{"upstream", "code", "method"}, 42 45 ) 43 46 44 47 duration = promauto.NewHistogramVec( ··· 48 51 Help: "A histogram of latencies for requests handled by the server.", 49 52 NativeHistogramBucketFactor: 1.1, 50 53 }, 51 - []string{"code", "method"}, 54 + []string{"upstream", "code", "method"}, 52 55 ) 53 56 ) 54 57 ··· 152 155 g.Add(run.SignalHandler(ctx, os.Interrupt, syscall.SIGTERM)) 153 156 154 157 { 155 - ln, err := net.Listen("tcp", net.JoinHostPort(st.Self.TailscaleIPs[0].String(), strconv.Itoa(*port))) 158 + p := strconv.Itoa(*port) 159 + ln, err := net.Listen("tcp", net.JoinHostPort(st.Self.TailscaleIPs[0].String(), p)) 156 160 if err != nil { 157 161 return fmt.Errorf("listen on %d: %w", *port, err) 158 162 } 159 163 defer ln.Close() 160 - 161 - _, p, err := net.SplitHostPort(ln.Addr().String()) 162 - if err != nil { 163 - return err 164 - } 165 164 166 165 http.Handle("/metrics", promhttp.Handler()) 167 166 http.Handle("/sd", serveDiscovery(net.JoinHostPort(st.Self.DNSName, p), targets)) ··· 219 218 220 219 srv := &http.Server{ 221 220 TLSConfig: &tls.Config{GetCertificate: lc.GetCertificate}, 222 - Handler: promhttp.InstrumentHandlerInFlight(requestsInFlight, 223 - promhttp.InstrumentHandlerDuration(duration, 224 - promhttp.InstrumentHandlerCounter(requests, 221 + Handler: promhttp.InstrumentHandlerInFlight(requestsInFlight.With(prometheus.Labels{"upstream": upstream.name}), 222 + promhttp.InstrumentHandlerDuration(duration.MustCurryWith(prometheus.Labels{"upstream": upstream.name}), 223 + promhttp.InstrumentHandlerCounter(requests.MustCurryWith(prometheus.Labels{"upstream": upstream.name}), 225 224 newReverseProxy(log.With(slog.String("upstream", upstream.name)), lc, upstream.backend)))), 226 225 } 227 226