Skip to content

Commit

Permalink
metrics, pprof: support reloading services with SIGHUP
Browse files Browse the repository at this point in the history
Reload prometheus and pprof services, if the config is updated.

Closes #1868.

Signed-off-by: Andrey Butusov <[email protected]>
  • Loading branch information
End-rey committed Nov 22, 2024
1 parent d03873a commit b79f58f
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ attribute, which is used for container domain name in NNS contracts (#2954)
- New `peapod-to-fstree` tool providing peapod-to-fstree data migration (#3013)
- Reloading node attributes with SIGHUP (#3005)
- Reloading pool sizes (#3018)
- Reloading pprof/metrics services with SIGHUP (#3016)

### Fixed
- Do not search for tombstones when handling their expiration, use local indexes instead (#2929)
Expand Down
38 changes: 37 additions & 1 deletion cmd/neofs-node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ type internals struct {
closers []func()
// services that are useful for debug (e.g. when a regular closer does not
// close), must be close at the very end of application life cycle
veryLastClosers map[string]func()
veryLastClosersLock sync.RWMutex
veryLastClosers map[string]func()

apiVersion version.Version
healthStatus atomic.Int32
Expand Down Expand Up @@ -871,6 +872,9 @@ func (c *cfg) configWatcher(ctx context.Context) {
case <-ch:
c.log.Info("SIGHUP has been received, rereading configuration...")

oldMetrics := writeMetricConfig(c.cfgReader)
oldProfiler := writeProfilerConfig(c.cfgReader)

Check warning on line 877 in cmd/neofs-node/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/config.go#L875-L877

Added lines #L875 - L877 were not covered by tests
err := c.readConfig(c.cfgReader)
if err != nil {
c.log.Error("configuration reading", zap.Error(err))
Expand All @@ -881,6 +885,11 @@ func (c *cfg) configWatcher(ctx context.Context) {

c.reloadObjectPoolSizes()

// Prometheus and pprof

// nolint:contextcheck
c.reloadMetricsAndPprof(oldMetrics, oldProfiler)

Check warning on line 892 in cmd/neofs-node/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/config.go#L888-L892

Added lines #L888 - L892 were not covered by tests
// Logger

err = c.internals.logLevel.UnmarshalText([]byte(c.logger.level))
Expand Down Expand Up @@ -965,3 +974,30 @@ func writeSystemAttributes(c *cfg) error {

return nil
}

func (c *cfg) reloadMetricsAndPprof(oldMetrics metricConfig, oldProfiler profilerConfig) {
c.veryLastClosersLock.Lock()
defer c.veryLastClosersLock.Unlock()

// Metrics

if oldMetrics.isUpdated(c.cfgReader) {
if closer, ok := c.veryLastClosers[metricName]; ok {
closer()
}
delete(c.veryLastClosers, metricName)

preRunAndLog(c, metricName, initMetrics(c))

Check warning on line 990 in cmd/neofs-node/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/config.go#L978-L990

Added lines #L978 - L990 were not covered by tests
}

//Profiler

if oldProfiler.isUpdated(c.cfgReader) {
if closer, ok := c.veryLastClosers[profilerName]; ok {
closer()
}
delete(c.veryLastClosers, profilerName)

preRunAndLog(c, profilerName, initProfiler(c))

Check warning on line 1001 in cmd/neofs-node/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/config.go#L995-L1001

Added lines #L995 - L1001 were not covered by tests
}
}
3 changes: 3 additions & 0 deletions cmd/neofs-node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,12 @@ func shutdown(c *cfg) {
for _, closer := range c.closers {
closer()
}

c.veryLastClosersLock.RLock()

Check warning on line 188 in cmd/neofs-node/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/main.go#L188

Added line #L188 was not covered by tests
for _, lastCloser := range c.veryLastClosers {
lastCloser()
}
c.veryLastClosersLock.RUnlock()

Check warning on line 192 in cmd/neofs-node/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/main.go#L192

Added line #L192 was not covered by tests

c.log.Debug("waiting for all processes to stop")

Expand Down
23 changes: 23 additions & 0 deletions cmd/neofs-node/metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import (
"time"

"github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
metricsconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/metrics"
httputil "github.com/nspcc-dev/neofs-node/pkg/util/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand All @@ -25,3 +28,23 @@ func initMetrics(c *cfg) *httputil.Server {

return srv
}

type metricConfig struct {
enabled bool
shutdownTimeout time.Duration
address string
}

func writeMetricConfig(c *config.Config) metricConfig {
return metricConfig{
enabled: metricsconfig.Enabled(c),
shutdownTimeout: metricsconfig.ShutdownTimeout(c),
address: metricsconfig.Address(c),
}

Check warning on line 43 in cmd/neofs-node/metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/metrics.go#L38-L43

Added lines #L38 - L43 were not covered by tests
}

func (m1 metricConfig) isUpdated(c *config.Config) bool {
return m1.enabled != metricsconfig.Enabled(c) ||
m1.shutdownTimeout != metricsconfig.ShutdownTimeout(c) ||
m1.address != metricsconfig.Address(c)

Check warning on line 49 in cmd/neofs-node/metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/metrics.go#L46-L49

Added lines #L46 - L49 were not covered by tests
}
23 changes: 23 additions & 0 deletions cmd/neofs-node/pprof.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import (
"time"

"github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
profilerconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/profiler"
httputil "github.com/nspcc-dev/neofs-node/pkg/util/http"
)
Expand All @@ -24,3 +27,23 @@ func initProfiler(c *cfg) *httputil.Server {

return srv
}

type profilerConfig struct {
enabled bool
shutdownTimeout time.Duration
address string
}

func writeProfilerConfig(c *config.Config) profilerConfig {
return profilerConfig{
enabled: profilerconfig.Enabled(c),
shutdownTimeout: profilerconfig.ShutdownTimeout(c),
address: profilerconfig.Address(c),
}

Check warning on line 42 in cmd/neofs-node/pprof.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/pprof.go#L37-L42

Added lines #L37 - L42 were not covered by tests
}

func (m1 profilerConfig) isUpdated(c *config.Config) bool {
return m1.enabled != profilerconfig.Enabled(c) ||
m1.shutdownTimeout != profilerconfig.ShutdownTimeout(c) ||
m1.address != profilerconfig.Address(c)

Check warning on line 48 in cmd/neofs-node/pprof.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/pprof.go#L45-L48

Added lines #L45 - L48 were not covered by tests
}

0 comments on commit b79f58f

Please sign in to comment.