diff --git a/caddy/caddy.go b/caddy/caddy.go index 19657578d..eb28db5ee 100644 --- a/caddy/caddy.go +++ b/caddy/caddy.go @@ -7,13 +7,13 @@ import ( "encoding/json" "errors" "fmt" - "github.com/dunglas/frankenphp/internal/fastabs" - "github.com/prometheus/client_golang/prometheus" "net/http" "path/filepath" "strconv" "strings" + "github.com/dunglas/frankenphp/internal/fastabs" + "github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2/caddyconfig" "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" @@ -40,8 +40,6 @@ func init() { httpcaddyfile.RegisterDirectiveOrder("php_server", "before", "file_server") } -var metrics = frankenphp.NewPrometheusMetrics(prometheus.DefaultRegisterer) - type workerConfig struct { // FileName sets the path to the worker script. FileName string `json:"file_name,omitempty"` @@ -58,6 +56,8 @@ type FrankenPHPApp struct { NumThreads int `json:"num_threads,omitempty"` // Workers configures the worker scripts to start. Workers []workerConfig `json:"workers,omitempty"` + + metrics frankenphp.Metrics } // CaddyModule returns the Caddy module information. @@ -68,11 +68,18 @@ func (f FrankenPHPApp) CaddyModule() caddy.ModuleInfo { } } +// Provision sets up the module. +func (f *FrankenPHPApp) Provision(ctx caddy.Context) error { + f.metrics = frankenphp.NewPrometheusMetrics(ctx.GetMetricsRegistry()) + + return nil +} + func (f *FrankenPHPApp) Start() error { repl := caddy.NewReplacer() logger := caddy.Log() - opts := []frankenphp.Option{frankenphp.WithNumThreads(f.NumThreads), frankenphp.WithLogger(logger), frankenphp.WithMetrics(metrics)} + opts := []frankenphp.Option{frankenphp.WithNumThreads(f.NumThreads), frankenphp.WithLogger(logger), frankenphp.WithMetrics(f.metrics)} for _, w := range f.Workers { opts = append(opts, frankenphp.WithWorkers(repl.ReplaceKnown(w.FileName, ""), w.Num, w.Env, w.Watch)) } @@ -671,6 +678,7 @@ func parsePhpServer(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error) // Interface guards var ( _ caddy.App = (*FrankenPHPApp)(nil) + _ caddy.Provisioner = (*FrankenPHPApp)(nil) _ caddy.Provisioner = (*FrankenPHPModule)(nil) _ caddyhttp.MiddlewareHandler = (*FrankenPHPModule)(nil) _ caddyfile.Unmarshaler = (*FrankenPHPModule)(nil) diff --git a/caddy/caddy_test.go b/caddy/caddy_test.go index 71880fb5a..3334b66db 100644 --- a/caddy/caddy_test.go +++ b/caddy/caddy_test.go @@ -11,10 +11,10 @@ import ( "testing" "github.com/dunglas/frankenphp" - "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/testutil" "github.com/stretchr/testify/require" + "github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2/caddytest" ) @@ -374,7 +374,9 @@ func TestMetrics(t *testing.T) { frankenphp_busy_threads 0 ` - require.NoError(t, testutil.GatherAndCompare(prometheus.DefaultGatherer, strings.NewReader(expectedMetrics), "frankenphp_total_threads", "frankenphp_busy_threads")) + ctx := caddy.ActiveContext() + + require.NoError(t, testutil.GatherAndCompare(ctx.GetMetricsRegistry(), strings.NewReader(expectedMetrics), "frankenphp_total_threads", "frankenphp_busy_threads")) } func TestWorkerMetrics(t *testing.T) { @@ -462,9 +464,10 @@ func TestWorkerMetrics(t *testing.T) { frankenphp_testdata_index_php_worker_restarts 0 ` + ctx := caddy.ActiveContext() require.NoError(t, testutil.GatherAndCompare( - prometheus.DefaultGatherer, + ctx.GetMetricsRegistry(), strings.NewReader(expectedMetrics), "frankenphp_total_threads", "frankenphp_busy_threads", @@ -563,9 +566,10 @@ func TestAutoWorkerConfig(t *testing.T) { frankenphp_testdata_index_php_worker_restarts 0 ` + ctx := caddy.ActiveContext() require.NoError(t, testutil.GatherAndCompare( - prometheus.DefaultGatherer, + ctx.GetMetricsRegistry(), strings.NewReader(expectedMetrics), "frankenphp_total_threads", "frankenphp_busy_threads",