Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions caddy/caddy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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",
}),
Expand Down
23 changes: 22 additions & 1 deletion metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Loading