diff --git a/caddy/caddy_test.go b/caddy/caddy_test.go index 86987239c7..74fff323ed 100644 --- a/caddy/caddy_test.go +++ b/caddy/caddy_test.go @@ -744,7 +744,7 @@ func TestMetrics(t *testing.T) { // Check metrics expectedMetrics := ` # HELP frankenphp_total_threads Total number of PHP threads - # TYPE frankenphp_total_threads counter + # TYPE frankenphp_total_threads gauge frankenphp_total_threads ` + cpus + ` # HELP frankenphp_busy_threads Number of busy PHP threads @@ -820,7 +820,7 @@ func TestWorkerMetrics(t *testing.T) { // Check metrics expectedMetrics := ` # HELP frankenphp_total_threads Total number of PHP threads - # TYPE frankenphp_total_threads counter + # TYPE frankenphp_total_threads gauge frankenphp_total_threads ` + cpus + ` # HELP frankenphp_busy_threads Number of busy PHP threads @@ -977,7 +977,7 @@ func TestNamedWorkerMetrics(t *testing.T) { // Check metrics expectedMetrics := ` # HELP frankenphp_total_threads Total number of PHP threads - # TYPE frankenphp_total_threads counter + # TYPE frankenphp_total_threads gauge frankenphp_total_threads ` + cpus + ` # HELP frankenphp_busy_threads Number of busy PHP threads @@ -1073,7 +1073,7 @@ func TestAutoWorkerConfig(t *testing.T) { // Check metrics expectedMetrics := ` # HELP frankenphp_total_threads Total number of PHP threads - # TYPE frankenphp_total_threads counter + # TYPE frankenphp_total_threads gauge frankenphp_total_threads ` + cpus + ` # HELP frankenphp_busy_threads Number of busy PHP threads @@ -1440,7 +1440,7 @@ func TestMultiWorkersMetrics(t *testing.T) { // Check metrics expectedMetrics := ` # HELP frankenphp_total_threads Total number of PHP threads - # TYPE frankenphp_total_threads counter + # TYPE frankenphp_total_threads gauge frankenphp_total_threads ` + cpus + ` # HELP frankenphp_busy_threads Number of busy PHP threads diff --git a/metrics.go b/metrics.go index 34ff6c350f..baab7bbc90 100644 --- a/metrics.go +++ b/metrics.go @@ -83,7 +83,7 @@ func (n nullMetrics) DequeuedRequest() {} type PrometheusMetrics struct { registry prometheus.Registerer - totalThreads prometheus.Counter + totalThreads prometheus.Gauge busyThreads prometheus.Gauge totalWorkers *prometheus.GaugeVec busyWorkers *prometheus.GaugeVec @@ -258,7 +258,7 @@ func (m *PrometheusMetrics) TotalThreads(num int) { m.mu.RLock() defer m.mu.RUnlock() - m.totalThreads.Add(float64(num)) + m.totalThreads.Set(float64(num)) } func (m *PrometheusMetrics) StartRequest() { @@ -380,7 +380,7 @@ func NewPrometheusMetrics(registry prometheus.Registerer) *PrometheusMetrics { m := &PrometheusMetrics{ registry: registry, - totalThreads: prometheus.NewCounter(prometheus.CounterOpts{ + totalThreads: prometheus.NewGauge(prometheus.GaugeOpts{ Name: "frankenphp_total_threads", Help: "Total number of PHP threads", }), diff --git a/metrics_test.go b/metrics_test.go index bfe77f17e1..846a926569 100644 --- a/metrics_test.go +++ b/metrics_test.go @@ -10,15 +10,36 @@ import ( "github.com/stretchr/testify/require" ) +const ( + totalThreadsMetricName = "frankenphp_total_threads" + initialTotalThreadsMetric = 3 + reconfiguredThreadsMetric = 2 + expectedTotalThreadsMetrics = ` + # HELP frankenphp_total_threads Total number of PHP threads + # TYPE frankenphp_total_threads gauge + frankenphp_total_threads 2 + ` +) + func createPrometheusMetrics() *PrometheusMetrics { return &PrometheusMetrics{ registry: prometheus.NewRegistry(), - totalThreads: prometheus.NewCounter(prometheus.CounterOpts{Name: "frankenphp_total_threads"}), + totalThreads: prometheus.NewGauge(prometheus.GaugeOpts{Name: "frankenphp_total_threads"}), busyThreads: prometheus.NewGauge(prometheus.GaugeOpts{Name: "frankenphp_busy_threads"}), queueDepth: prometheus.NewGauge(prometheus.GaugeOpts{Name: "frankenphp_queue_depth"}), } } +func TestPrometheusMetrics_TotalThreadsReportsCurrentValue(t *testing.T) { + registry := prometheus.NewRegistry() + m := NewPrometheusMetrics(registry) + + m.TotalThreads(initialTotalThreadsMetric) + m.TotalThreads(reconfiguredThreadsMetric) + + require.NoError(t, testutil.GatherAndCompare(registry, strings.NewReader(expectedTotalThreadsMetrics), totalThreadsMetricName)) +} + func TestPrometheusMetrics_TotalWorkers(t *testing.T) { m := createPrometheusMetrics()