Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix missing metrics #1366

Merged
merged 3 commits into from
Feb 12, 2025
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
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
Loading