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: 9 additions & 1 deletion caddy/caddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,17 @@ 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())
f.logger = ctx.Logger()

if httpApp, err := ctx.AppIfConfigured("http"); err == nil {
if httpApp.(*caddyhttp.App).Metrics != nil {
f.metrics = frankenphp.NewPrometheusMetrics(ctx.GetMetricsRegistry())
}
} else {
// if the http module is not configured (this should never happen) then collect the metrics by default
f.metrics = frankenphp.NewPrometheusMetrics(ctx.GetMetricsRegistry())
}

return nil
}

Expand Down
84 changes: 84 additions & 0 deletions caddy/caddy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ func TestMetrics(t *testing.T) {
admin localhost:2999
http_port `+testPort+`
https_port 9443
metrics

frankenphp
}
Expand Down Expand Up @@ -405,6 +406,7 @@ func TestWorkerMetrics(t *testing.T) {
admin localhost:2999
http_port `+testPort+`
https_port 9443
metrics

frankenphp {
worker ../testdata/index.php 2
Expand Down Expand Up @@ -502,6 +504,7 @@ func TestNamedWorkerMetrics(t *testing.T) {
admin localhost:2999
http_port `+testPort+`
https_port 9443
metrics

frankenphp {
worker {
Expand Down Expand Up @@ -594,6 +597,7 @@ func TestAutoWorkerConfig(t *testing.T) {
admin localhost:2999
http_port `+testPort+`
https_port 9443
metrics

frankenphp {
worker ../testdata/index.php
Expand Down Expand Up @@ -873,6 +877,7 @@ func TestMultiWorkersMetrics(t *testing.T) {
admin localhost:2999
http_port `+testPort+`
https_port 9443
metrics

frankenphp {
worker {
Expand Down Expand Up @@ -979,6 +984,7 @@ func TestMultiWorkersMetricsWithDuplicateName(t *testing.T) {
admin localhost:2999
http_port `+testPort+`
https_port 9443
metrics

frankenphp {
worker {
Expand Down Expand Up @@ -1073,3 +1079,81 @@ func TestMultiWorkersMetricsWithDuplicateName(t *testing.T) {
"frankenphp_ready_workers",
))
}

func TestDisabledMetrics(t *testing.T) {
var wg sync.WaitGroup
tester := caddytest.NewTester(t)
tester.InitServer(`
{
skip_install_trust
admin localhost:2999
http_port `+testPort+`
https_port 9443

frankenphp {
worker {
name service1
file ../testdata/index.php
num 2
}
worker {
name service1
file ../testdata/ini.php
num 3
}
}
}

localhost:`+testPort+` {
route {
php {
root ../testdata
}
}
}

example.com:`+testPort+` {
route {
php {
root ../testdata
}
}
}
`, "caddyfile")

// Make some requests
for i := 0; i < 10; i++ {
wg.Add(1)
go func(i int) {
tester.AssertGetResponse(fmt.Sprintf("http://localhost:"+testPort+"/index.php?i=%d", i), http.StatusOK, fmt.Sprintf("I am by birth a Genevese (%d)", i))
wg.Done()
}(i)
}
wg.Wait()

// Fetch metrics
resp, err := http.Get("http://localhost:2999/metrics")
require.NoError(t, err, "failed to fetch metrics")
defer resp.Body.Close()

// Read and parse metrics
metrics := new(bytes.Buffer)
_, err = metrics.ReadFrom(resp.Body)
require.NoError(t, err, "failed to read metrics")

ctx := caddy.ActiveContext()
count, err := testutil.GatherAndCount(
ctx.GetMetricsRegistry(),
"frankenphp_busy_threads",
"frankenphp_busy_workers",
"frankenphp_queue_depth",
"frankenphp_ready_workers",
"frankenphp_total_threads",
"frankenphp_total_workers",
"frankenphp_worker_request_count",
"frankenphp_worker_request_time",
)

require.NoError(t, err, "failed to count metrics")
require.Zero(t, count, "metrics should be missing")
}
1 change: 0 additions & 1 deletion testdata/performance/k6.Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
root /go/src/app/testdata
php {
root /go/src/app/testdata
enable_root_symlink false
}
}
}
2 changes: 1 addition & 1 deletion testdata/performance/perf-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ select filename in ./testdata/performance/*.js; do

sleep 10

docker run --entrypoint "" -it -v .:/app -w /app \
docker run --entrypoint "" -it --rm -v .:/app -w /app \
--add-host "host.docker.internal:host-gateway" \
grafana/k6:latest \
k6 run -e "CADDY_HOSTNAME=$CADDY_HOSTNAME:8125" "./$filename"
Expand Down