| 1 | package reverseproxyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/ |
| 2 | |
| 3 | import ( |
| 4 | "errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/" |
| 5 | "runtime/debugscip-go gomod github.com/golang/go/src go1.25.1 `runtime/debug`/" |
| 6 | "syncscip-go gomod github.com/golang/go/src go1.25.1 sync/" |
| 7 | "timescip-go gomod github.com/golang/go/src go1.25.1 time/" |
| 8 | |
| 9 | "github.com/prometheus/client_golang/prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/" |
| 10 | "go.uber.org/zapscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/" |
| 11 | "go.uber.org/zap/zapcorescip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap/zapcore`/" |
| 12 | |
| 13 | "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/" |
| 14 | ) |
| 15 | |
| 16 | var reverseProxyMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics. = struct { |
| 17 | oncescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics:once. syncscip-go gomod github.com/golang/go/src go1.25.1 sync/.Oncescip-go gomod github.com/golang/go/src go1.25.1 sync/Once# |
| 18 | upstreamsHealthyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics:upstreamsHealthy. *prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.GaugeVecscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/GaugeVec# |
| 19 | loggerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics:logger. *zapscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/.Loggerscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/Logger# |
| 20 | }{} |
| 21 | |
| 22 | func initReverseProxyMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/initReverseProxyMetrics().(handler *Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/Handler#, registry *prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Registryscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Registry#) { |
| 23 | const ns, sub = "caddy", "reverse_proxy" |
| 24 | |
| 25 | upstreamsLabels := []string{"upstream"} |
| 26 | reverseProxyMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics..oncescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics:once..Doscip-go gomod github.com/golang/go/src go1.25.1 sync/Once#Do().(func() { |
| 27 | reverseProxyMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics..upstreamsHealthyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics:upstreamsHealthy. = prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.NewGaugeVecscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/NewGaugeVec().(prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.GaugeOptsscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/GaugeOpts#{ |
| 28 | Namespacescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterOpts#Namespace.: ns, |
| 29 | Subsystemscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterOpts#Subsystem.: sub, |
| 30 | Namescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterOpts#Name.: "upstreams_healthy", |
| 31 | Helpscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterOpts#Help.: "Health status of reverse proxy upstreams.", |
| 32 | }, upstreamsLabels) |
| 33 | }) |
| 34 | |
| 35 | // duplicate registration could happen if multiple sites with reverse proxy are configured; so ignore the error because |
| 36 | // there's no good way to capture having multiple sites with reverse proxy. If this happens, the metrics will be |
| 37 | // registered twice, but the second registration will be ignored. |
| 38 | if err := registry.Registerscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Registry#Register().(reverseProxyMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics..upstreamsHealthyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics:upstreamsHealthy.); err != nil && |
| 39 | !errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/.Isscip-go gomod github.com/golang/go/src go1.25.1 errors/Is().(err, prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.AlreadyRegisteredErrorscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/AlreadyRegisteredError#{ |
| 40 | ExistingCollectorscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/AlreadyRegisteredError#ExistingCollector.: reverseProxyMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics..upstreamsHealthyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics:upstreamsHealthy., |
| 41 | NewCollectorscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/AlreadyRegisteredError#NewCollector.: reverseProxyMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics..upstreamsHealthyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics:upstreamsHealthy., |
| 42 | }) { |
| 43 | panic(err) |
| 44 | } |
| 45 | |
| 46 | reverseProxyMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics..loggerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics:logger. = handler.loggerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/Handler#logger..Namedscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/Logger#Named().("reverse_proxy.metrics") |
| 47 | } |
| 48 | |
| 49 | type metricsUpstreamsHealthyUpdaterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/metricsUpstreamsHealthyUpdater# struct { |
| 50 | handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/metricsUpstreamsHealthyUpdater#handler. *Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/Handler# |
| 51 | } |
| 52 | |
| 53 | func newMetricsUpstreamsHealthyUpdaterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/newMetricsUpstreamsHealthyUpdater().(handler *Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/Handler#, ctx caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#) *metricsUpstreamsHealthyUpdaterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/metricsUpstreamsHealthyUpdater# { |
| 54 | initReverseProxyMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/initReverseProxyMetrics().(handler, ctx.GetMetricsRegistryscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#GetMetricsRegistry().()) |
| 55 | reverseProxyMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics..upstreamsHealthyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics:upstreamsHealthy..Resetscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/MetricVec#Reset().() |
| 56 | |
| 57 | return &metricsUpstreamsHealthyUpdaterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/metricsUpstreamsHealthyUpdater#{handler} |
| 58 | } |
| 59 | |
| 60 | func (m *metricsUpstreamsHealthyUpdaterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/metricsUpstreamsHealthyUpdater#) initscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/metricsUpstreamsHealthyUpdater#init().() { |
| 61 | go func() { |
| 62 | defer func() { |
| 63 | if err := recover(); err != nil { |
| 64 | if c := reverseProxyMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics..loggerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics:logger..Checkscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/Logger#Check().(zapcorescip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap/zapcore`/.ErrorLevelscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap/zapcore`/ErrorLevel., "upstreams healthy metrics updater panicked"); c != nil { |
| 65 | c.Writescip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap/zapcore`/CheckedEntry#Write().( |
| 66 | zapscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/.Anyscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/Any().("error", err), |
| 67 | zapscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/.ByteStringscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/ByteString().("stack", debugscip-go gomod github.com/golang/go/src go1.25.1 `runtime/debug`/.Stackscip-go gomod github.com/golang/go/src go1.25.1 `runtime/debug`/Stack().()), |
| 68 | ) |
| 69 | } |
| 70 | } |
| 71 | }() |
| 72 | |
| 73 | m.updatescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/metricsUpstreamsHealthyUpdater#update().() |
| 74 | |
| 75 | ticker := timescip-go gomod github.com/golang/go/src go1.25.1 time/.NewTickerscip-go gomod github.com/golang/go/src go1.25.1 time/NewTicker().(10 * timescip-go gomod github.com/golang/go/src go1.25.1 time/.Secondscip-go gomod github.com/golang/go/src go1.25.1 time/Second.) |
| 76 | for { |
| 77 | select { |
| 78 | case <-ticker.Cscip-go gomod github.com/golang/go/src go1.25.1 time/Ticker#C.: |
| 79 | m.updatescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/metricsUpstreamsHealthyUpdater#update().() |
| 80 | case <-m.handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/metricsUpstreamsHealthyUpdater#handler..ctxscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/Handler#ctx..Donescip-go gomod github.com/golang/go/src go1.25.1 context/Context#Done().(): |
| 81 | ticker.Stopscip-go gomod github.com/golang/go/src go1.25.1 time/Ticker#Stop().() |
| 82 | return |
| 83 | } |
| 84 | } |
| 85 | }() |
| 86 | } |
| 87 | |
| 88 | func (m *metricsUpstreamsHealthyUpdaterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/metricsUpstreamsHealthyUpdater#) updatescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/metricsUpstreamsHealthyUpdater#update().() { |
| 89 | for _, upstream := range m.handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/metricsUpstreamsHealthyUpdater#handler..Upstreamsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/Handler#Upstreams. { |
| 90 | labels := prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Labelsscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Labels#{"upstream": upstream.Dialscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/Upstream#Dial.} |
| 91 | |
| 92 | gaugeValue := 0.0 |
| 93 | if upstream.Healthyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/Upstream#Healthy().() { |
| 94 | gaugeValue = 1.0 |
| 95 | } |
| 96 | |
| 97 | reverseProxyMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics..upstreamsHealthyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/reverseProxyMetrics:upstreamsHealthy..Withscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/GaugeVec#With().(labels).Setscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Gauge#Set().(gaugeValue) |
| 98 | } |
| 99 | } |