Skip to content

Commit

Permalink
remove need for renaming workers
Browse files Browse the repository at this point in the history
  • Loading branch information
withinboredom committed Sep 8, 2024
1 parent 72d2f8f commit cbdd9bf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 44 deletions.
3 changes: 3 additions & 0 deletions caddy/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/dunglas/vulcain/caddy v1.0.5
github.com/prometheus/client_golang v1.20.2
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
go.uber.org/zap v1.27.0
)

Expand Down Expand Up @@ -43,6 +44,7 @@ require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/chzyer/readline v1.5.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dgraph-io/badger v1.6.2 // indirect
github.com/dgraph-io/badger/v2 v2.2007.4 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
Expand Down Expand Up @@ -121,6 +123,7 @@ require (
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/pires/go-proxyproto v0.7.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
Expand Down
69 changes: 28 additions & 41 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package frankenphp

import (
"github.com/prometheus/client_golang/prometheus"
"path/filepath"
"regexp"
"sync"
"time"
Expand All @@ -26,7 +27,6 @@ type Metrics interface {
// StopWorkerRequest Collects stopped worker requests
StopWorkerRequest(name string, duration time.Duration)
StartWorkerRequest(name string)
RenameWorker(oldName, newName string)
Shutdown()
}

Expand Down Expand Up @@ -56,9 +56,6 @@ func (n nullMetrics) StopWorkerRequest(name string, duration time.Duration) {
func (n nullMetrics) StartWorkerRequest(name string) {
}

func (n nullMetrics) RenameWorker(oldName, newName string) {
}

func (n nullMetrics) Shutdown() {
}

Expand Down Expand Up @@ -93,48 +90,63 @@ func (m *PrometheusMetrics) StopWorker(name string) {
m.totalWorkers[name].Dec()
}

func (m *PrometheusMetrics) getIdentity(name string) (string, error) {
actualName, err := filepath.Abs(name)
if err != nil {
return name, err
}

return actualName, nil
}

func (m *PrometheusMetrics) TotalWorkers(name string, num int) {
m.mu.Lock()
defer m.mu.Unlock()

identity, err := m.getIdentity(name)
if err != nil {
// do not create metrics, let error propagate when worker is started
return
}

subsystem := getWorkerNameForMetrics(name)

if _, ok := m.totalWorkers[name]; !ok {
m.totalWorkers[name] = prometheus.NewGauge(prometheus.GaugeOpts{
if _, ok := m.totalWorkers[identity]; !ok {
m.totalWorkers[identity] = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "frankenphp",
Subsystem: subsystem,
Name: "total_workers",
Help: "Total number of PHP workers for this worker",
})
m.registry.MustRegister(m.totalWorkers[name])
m.registry.MustRegister(m.totalWorkers[identity])
}

if _, ok := m.busyWorkers[name]; !ok {
m.busyWorkers[name] = prometheus.NewGauge(prometheus.GaugeOpts{
if _, ok := m.busyWorkers[identity]; !ok {
m.busyWorkers[identity] = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "frankenphp",
Subsystem: subsystem,
Name: "busy_workers",
Help: "Number of busy PHP workers for this worker",
})
m.registry.MustRegister(m.busyWorkers[name])
m.registry.MustRegister(m.busyWorkers[identity])
}

if _, ok := m.workerRequestTime[name]; !ok {
m.workerRequestTime[name] = prometheus.NewCounter(prometheus.CounterOpts{
if _, ok := m.workerRequestTime[identity]; !ok {
m.workerRequestTime[identity] = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "frankenphp",
Subsystem: subsystem,
Name: "worker_request_time",
})
m.registry.MustRegister(m.workerRequestTime[name])
m.registry.MustRegister(m.workerRequestTime[identity])
}

if _, ok := m.workerRequestCount[name]; !ok {
m.workerRequestCount[name] = prometheus.NewCounter(prometheus.CounterOpts{
if _, ok := m.workerRequestCount[identity]; !ok {
m.workerRequestCount[identity] = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "frankenphp",
Subsystem: subsystem,
Name: "worker_request_count",
})
m.registry.MustRegister(m.workerRequestCount[name])
m.registry.MustRegister(m.workerRequestCount[identity])
}
}

Expand Down Expand Up @@ -167,31 +179,6 @@ func (m *PrometheusMetrics) StartWorkerRequest(name string) {
m.busyWorkers[name].Inc()
}

func (m *PrometheusMetrics) RenameWorker(oldName, newName string) {
m.mu.Lock()
defer m.mu.Unlock()

if _, ok := m.totalWorkers[oldName]; ok {
m.totalWorkers[newName] = m.totalWorkers[oldName]
delete(m.totalWorkers, oldName)
}

if _, ok := m.busyWorkers[oldName]; ok {
m.busyWorkers[newName] = m.busyWorkers[oldName]
delete(m.busyWorkers, oldName)
}

if _, ok := m.workerRequestTime[oldName]; ok {
m.workerRequestTime[newName] = m.workerRequestTime[oldName]
delete(m.workerRequestTime, oldName)
}

if _, ok := m.workerRequestCount[oldName]; ok {
m.workerRequestCount[newName] = m.workerRequestCount[oldName]
delete(m.workerRequestCount, oldName)
}
}

func (m *PrometheusMetrics) Shutdown() {
m.registry.Unregister(m.totalThreads)
m.registry.Unregister(m.busyThreads)
Expand Down
3 changes: 2 additions & 1 deletion metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ func TestPrometheusMetrics_TotalWorkers(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m.TotalWorkers(tt.worker, tt.num)
_, ok := m.totalWorkers[tt.worker]
actualName, _ := m.getIdentity(tt.worker)
_, ok := m.totalWorkers[actualName]
require.True(t, ok)
})
}
Expand Down
2 changes: 0 additions & 2 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ func startWorkers(fileName string, nbWorkers int, env PreparedEnv) error {
return fmt.Errorf("workers %q: %w", fileName, err)
}

metrics.RenameWorker(fileName, absFileName)

if _, ok := workersRequestChans.Load(absFileName); ok {
return fmt.Errorf("workers %q: already started", absFileName)
}
Expand Down

0 comments on commit cbdd9bf

Please sign in to comment.