Skip to content

Commit 4c92633

Browse files
authored
fix: missing metrics with Caddy 2.9 (#1366)
* fix missing metrics * update tests * use interface instead
1 parent be2e471 commit 4c92633

2 files changed

Lines changed: 21 additions & 9 deletions

File tree

caddy/caddy.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import (
77
"encoding/json"
88
"errors"
99
"fmt"
10-
"github.com/dunglas/frankenphp/internal/fastabs"
11-
"github.com/prometheus/client_golang/prometheus"
1210
"net/http"
1311
"path/filepath"
1412
"strconv"
1513
"strings"
1614

15+
"github.com/dunglas/frankenphp/internal/fastabs"
16+
1717
"github.com/caddyserver/caddy/v2"
1818
"github.com/caddyserver/caddy/v2/caddyconfig"
1919
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
@@ -40,8 +40,6 @@ func init() {
4040
httpcaddyfile.RegisterDirectiveOrder("php_server", "before", "file_server")
4141
}
4242

43-
var metrics = frankenphp.NewPrometheusMetrics(prometheus.DefaultRegisterer)
44-
4543
type workerConfig struct {
4644
// FileName sets the path to the worker script.
4745
FileName string `json:"file_name,omitempty"`
@@ -58,6 +56,8 @@ type FrankenPHPApp struct {
5856
NumThreads int `json:"num_threads,omitempty"`
5957
// Workers configures the worker scripts to start.
6058
Workers []workerConfig `json:"workers,omitempty"`
59+
60+
metrics frankenphp.Metrics
6161
}
6262

6363
// CaddyModule returns the Caddy module information.
@@ -68,11 +68,18 @@ func (f FrankenPHPApp) CaddyModule() caddy.ModuleInfo {
6868
}
6969
}
7070

71+
// Provision sets up the module.
72+
func (f *FrankenPHPApp) Provision(ctx caddy.Context) error {
73+
f.metrics = frankenphp.NewPrometheusMetrics(ctx.GetMetricsRegistry())
74+
75+
return nil
76+
}
77+
7178
func (f *FrankenPHPApp) Start() error {
7279
repl := caddy.NewReplacer()
7380
logger := caddy.Log()
7481

75-
opts := []frankenphp.Option{frankenphp.WithNumThreads(f.NumThreads), frankenphp.WithLogger(logger), frankenphp.WithMetrics(metrics)}
82+
opts := []frankenphp.Option{frankenphp.WithNumThreads(f.NumThreads), frankenphp.WithLogger(logger), frankenphp.WithMetrics(f.metrics)}
7683
for _, w := range f.Workers {
7784
opts = append(opts, frankenphp.WithWorkers(repl.ReplaceKnown(w.FileName, ""), w.Num, w.Env, w.Watch))
7885
}
@@ -671,6 +678,7 @@ func parsePhpServer(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
671678
// Interface guards
672679
var (
673680
_ caddy.App = (*FrankenPHPApp)(nil)
681+
_ caddy.Provisioner = (*FrankenPHPApp)(nil)
674682
_ caddy.Provisioner = (*FrankenPHPModule)(nil)
675683
_ caddyhttp.MiddlewareHandler = (*FrankenPHPModule)(nil)
676684
_ caddyfile.Unmarshaler = (*FrankenPHPModule)(nil)

caddy/caddy_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111
"testing"
1212

1313
"github.com/dunglas/frankenphp"
14-
"github.com/prometheus/client_golang/prometheus"
1514
"github.com/prometheus/client_golang/prometheus/testutil"
1615
"github.com/stretchr/testify/require"
1716

17+
"github.com/caddyserver/caddy/v2"
1818
"github.com/caddyserver/caddy/v2/caddytest"
1919
)
2020

@@ -374,7 +374,9 @@ func TestMetrics(t *testing.T) {
374374
frankenphp_busy_threads 0
375375
`
376376

377-
require.NoError(t, testutil.GatherAndCompare(prometheus.DefaultGatherer, strings.NewReader(expectedMetrics), "frankenphp_total_threads", "frankenphp_busy_threads"))
377+
ctx := caddy.ActiveContext()
378+
379+
require.NoError(t, testutil.GatherAndCompare(ctx.GetMetricsRegistry(), strings.NewReader(expectedMetrics), "frankenphp_total_threads", "frankenphp_busy_threads"))
378380
}
379381

380382
func TestWorkerMetrics(t *testing.T) {
@@ -462,9 +464,10 @@ func TestWorkerMetrics(t *testing.T) {
462464
frankenphp_testdata_index_php_worker_restarts 0
463465
`
464466

467+
ctx := caddy.ActiveContext()
465468
require.NoError(t,
466469
testutil.GatherAndCompare(
467-
prometheus.DefaultGatherer,
470+
ctx.GetMetricsRegistry(),
468471
strings.NewReader(expectedMetrics),
469472
"frankenphp_total_threads",
470473
"frankenphp_busy_threads",
@@ -563,9 +566,10 @@ func TestAutoWorkerConfig(t *testing.T) {
563566
frankenphp_testdata_index_php_worker_restarts 0
564567
`
565568

569+
ctx := caddy.ActiveContext()
566570
require.NoError(t,
567571
testutil.GatherAndCompare(
568-
prometheus.DefaultGatherer,
572+
ctx.GetMetricsRegistry(),
569573
strings.NewReader(expectedMetrics),
570574
"frankenphp_total_threads",
571575
"frankenphp_busy_threads",

0 commit comments

Comments
 (0)