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

Add metrics version #1248

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ ENV CGO_LDFLAGS="-L/usr/local/lib -lssl -lcrypto -lreadline -largon2 -lcurl -lon
RUN echo $CGO_LDFLAGS

WORKDIR /go/src/app/caddy/frankenphp
RUN GOBIN=/usr/local/bin go install -tags 'nobadger,nomysql,nopgx' -ldflags "-w -s -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $PHP_VERSION Caddy'" && \
RUN GOBIN=/usr/local/bin go install -tags 'nobadger,nomysql,nopgx' -ldflags "-w -s -X 'github.com/dunglas/frankenphp.VersionString=$FRANKENPHP_VERSION' -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $PHP_VERSION Caddy'" && \
setcap cap_net_bind_service=+ep /usr/local/bin/frankenphp && \
cp Caddyfile /etc/caddy/Caddyfile && \
frankenphp version
Expand Down
2 changes: 1 addition & 1 deletion alpine.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ ENV CGO_CPPFLAGS=$PHP_CPPFLAGS
ENV CGO_LDFLAGS="-lssl -lcrypto -lreadline -largon2 -lcurl -lonig -lz $PHP_LDFLAGS"

WORKDIR /go/src/app/caddy/frankenphp
RUN GOBIN=/usr/local/bin go install -tags 'nobadger,nomysql,nopgx' -ldflags "-w -s -extldflags '-Wl,-z,stack-size=0x80000' -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $PHP_VERSION Caddy'" && \
RUN GOBIN=/usr/local/bin go install -tags 'nobadger,nomysql,nopgx' -ldflags "-w -s -extldflags '-Wl,-z,stack-size=0x80000' -X 'github.com/dunglas/frankenphp.VersionString=$FRANKENPHP_VERSION' -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $PHP_VERSION Caddy'" && \
setcap cap_net_bind_service=+ep /usr/local/bin/frankenphp && \
([ -z "${NO_COMPRESS}" ] && upx --best /usr/local/bin/frankenphp || true) && \
frankenphp version
Expand Down
2 changes: 1 addition & 1 deletion build-static.sh
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ fi

cd caddy/frankenphp/
go env
go build -buildmode=pie -tags "cgo,netgo,osusergo,static_build,nobadger,nomysql,nopgx" -ldflags "-linkmode=external -extldflags '-static-pie ${extraExtldflags}' ${extraLdflags} -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP ${FRANKENPHP_VERSION} PHP ${LIBPHP_VERSION} Caddy'" -o "../../dist/${bin}"
go build -buildmode=pie -tags "cgo,netgo,osusergo,static_build,nobadger,nomysql,nopgx" -ldflags "-linkmode=external -extldflags '-static-pie ${extraExtldflags}' ${extraLdflags} -X 'github.com/dunglas/frankenphp.VersionString=${FRANKENPHP_VERSION}' -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP ${FRANKENPHP_VERSION} PHP ${LIBPHP_VERSION} Caddy'" -o "../../dist/${bin}"
cd ../..

if [ -d "${EMBED}" ]; then
Expand Down
5 changes: 5 additions & 0 deletions caddy/caddy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,10 @@ func TestWorkerMetrics(t *testing.T) {
# HELP frankenphp_testdata_index_php_worker_restarts Number of PHP worker restarts for this worker
# TYPE frankenphp_testdata_index_php_worker_restarts counter
frankenphp_testdata_index_php_worker_restarts 0

# HELP frankenphp_version Current version of FrankenPHP
# TYPE frankenphp_version gauge
frankenphp_version{php_version="` + frankenphp.Version().Version + `",version="dev"} 1
`

require.NoError(t,
Expand All @@ -474,6 +478,7 @@ func TestWorkerMetrics(t *testing.T) {
"frankenphp_testdata_index_php_worker_crashes",
"frankenphp_testdata_index_php_worker_restarts",
"frankenphp_testdata_index_php_ready_workers",
"frankenphp_version",
))
}

Expand Down
1 change: 1 addition & 0 deletions docs/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ When [Caddy metrics](https://caddyserver.com/docs/metrics) are enabled, FrankenP
* `frankenphp_[worker]_worker_restarts`: The number of times a worker has been deliberately restarted.
* `frankenphp_total_threads`: The total number of PHP threads.
* `frankenphp_busy_threads`: The number of PHP threads currently processing a request (running workers always consume a thread).
* `frankenphp_version`: The current version of FrankenPHP and PHP as a label.

For worker metrics, the `[worker]` placeholder is replaced by the worker script path in the Caddyfile.
2 changes: 1 addition & 1 deletion frankenphp.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func Init(options ...Option) error {
}

if c := logger.Check(zapcore.InfoLevel, "FrankenPHP started 🐘"); c != nil {
c.Write(zap.String("php_version", Version().Version), zap.Int("num_threads", opt.numThreads))
c.Write(zap.String("php_version", Version().Version), zap.Int("num_threads", opt.numThreads), zap.String("frankenphp_version", VersionString))
}
if EmbeddedAppPath != "" {
if c := logger.Check(zapcore.InfoLevel, "embedded PHP app 📦"); c != nil {
Expand Down
11 changes: 11 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

var metricsNameRegex = regexp.MustCompile(`\W+`)
var metricsNameFixRegex = regexp.MustCompile(`^_+|_+$`)
var VersionString = "dev"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe could you use https://pkg.go.dev/runtime/debug#ReadBuildInfo instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the version of the FrankenPHP module (and of all other installed modules) should already be exposed by Caddy: https://github.com/caddyserver/caddy/blob/v2.8.4/metrics.go#L15

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the string currently output from a production instance:

go_build_info{checksum="",path="github.com/dunglas/frankenphp/caddy",version="(devel)"} 1

It doesn't seem too useful :(

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum this looks like an issue in my release script 😅 (because of the replace). Isn't the version of lib (not the Caddy module) displayed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean, but that is the only thing in the metrics.

Copy link
Owner

@dunglas dunglas Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean we should have the version of github.com/dunglas/frankenphp too

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no dependencies listed, just the top-level.


const (
StopReasonCrash = iota
Expand Down Expand Up @@ -85,6 +86,7 @@ type PrometheusMetrics struct {
workerRestarts map[string]prometheus.Counter
workerRequestTime map[string]prometheus.Counter
workerRequestCount map[string]prometheus.Counter
version *prometheus.GaugeVec
mu sync.Mutex
}

Expand Down Expand Up @@ -292,6 +294,8 @@ func (m *PrometheusMetrics) Shutdown() {
m.workerCrashes = map[string]prometheus.Counter{}
m.readyWorkers = map[string]prometheus.Gauge{}

// version does not change while running/restarting/stopping

m.registry.MustRegister(m.totalThreads)
m.registry.MustRegister(m.busyThreads)
}
Expand Down Expand Up @@ -325,8 +329,15 @@ func NewPrometheusMetrics(registry prometheus.Registerer) *PrometheusMetrics {
workerRestarts: map[string]prometheus.Counter{},
workerCrashes: map[string]prometheus.Counter{},
readyWorkers: map[string]prometheus.Gauge{},
version: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "frankenphp_version",
Help: "Current version of FrankenPHP",
}, []string{"version", "php_version"}),
}

m.registry.MustRegister(m.version)
m.version.WithLabelValues(VersionString, Version().Version).Set(1)

m.registry.MustRegister(m.totalThreads)
m.registry.MustRegister(m.busyThreads)

Expand Down
Loading