Skip to content

Commit

Permalink
fix: missing metrics with Caddy 2.9 (#1366)
Browse files Browse the repository at this point in the history
* fix missing metrics

* update tests

* use interface instead
  • Loading branch information
IndraGunawan authored Feb 12, 2025
1 parent be2e471 commit 4c92633
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
18 changes: 13 additions & 5 deletions caddy/caddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"`
Expand All @@ -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.
Expand All @@ -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))
}
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions caddy/caddy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 4c92633

Please sign in to comment.