Skip to content

Commit

Permalink
fix: duplicate metrics collector registration attempted panic
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Feb 19, 2025
1 parent bf2555a commit 7d65b32
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
14 changes: 13 additions & 1 deletion caddy/caddy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,19 @@ localhost:9080 {
mercure {
anonymous
publisher_jwt !ChangeMe!
%s
%[1]s
}
respond 404
}
}
example.com:9080 {
route {
mercure {
anonymous
publisher_jwt !ChangeMe!
%[1]s
}
respond 404
Expand Down
17 changes: 14 additions & 3 deletions metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mercure

import (
"errors"
"net/http"

"github.com/dunglas/mercure/common"
Expand Down Expand Up @@ -62,9 +63,19 @@ func NewPrometheusMetrics(registry prometheus.Registerer) *PrometheusMetrics {
),
}

m.registry.MustRegister(m.subscribers)
m.registry.MustRegister(m.subscribersTotal)
m.registry.MustRegister(m.updatesTotal)
// https://github.com/caddyserver/caddy/pull/6820
if err := m.registry.Register(m.subscribers); err != nil &&
!errors.As(err, &prometheus.AlreadyRegisteredError{}) {
panic(err)
}
if err := m.registry.Register(m.subscribersTotal); err != nil &&
!errors.As(err, &prometheus.AlreadyRegisteredError{}) {
panic(err)
}
if err := m.registry.Register(m.updatesTotal); err != nil &&
!errors.As(err, &prometheus.AlreadyRegisteredError{}) {
panic(err)
}

return m
}
Expand Down

0 comments on commit 7d65b32

Please sign in to comment.