Skip to content

Commit 46fc72f

Browse files
committed
Renaming and other review feedback
1 parent a34977a commit 46fc72f

File tree

4 files changed

+48
-45
lines changed

4 files changed

+48
-45
lines changed

docs/monitoring.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ NGINX Kubernetes Gateway exports the following metrics:
9191
- nginx_reload_errors_total. Number of unsuccessful NGINX reloads.
9292
- nginx_stale_config. 1 means NKG failed to configure NGINX with the latest version of the configuration, which means
9393
NGINX is running with a stale version.
94-
- nginx_last_reload_milliseconds. Duration in milliseconds of NGINX reloads.
94+
- nginx_last_reload_milliseconds. Duration in milliseconds of NGINX reloads (histogram).
9595
- These metrics have the namespace `nginx_kubernetes_gateway`, and include the label `class` which is set to the
9696
Gateway class of NKG. For example, `nginx_kubernetes_gateway_nginx_reloads_total{class="nginx"}`.
9797

internal/mode/static/manager.go

+18-15
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,9 @@ func StartManager(cfg config.Config) error {
130130
return fmt.Errorf("NGINX is not running: %w", err)
131131
}
132132

133-
var mgrCollector ngxruntime.ManagerCollector
134-
mgrCollector = nkgmetrics.NewManagerFakeCollector()
135-
if cfg.MetricsConfig.Enabled {
136-
mgrCollector, err = configureNginxMetrics(cfg.GatewayClassName)
137-
if err != nil {
138-
return err
139-
}
133+
mgrCollector, err := createAndRegisterMetricsCollectors(cfg.MetricsConfig.Enabled, cfg.GatewayClassName)
134+
if err != nil {
135+
return fmt.Errorf("cannot create and register metrics collectors: %w", err)
140136
}
141137

142138
statusUpdater := status.NewUpdater(status.UpdaterConfig{
@@ -356,19 +352,26 @@ func setInitialConfig(
356352
return updateControlPlane(&config, logger, eventRecorder, configName, logLevelSetter)
357353
}
358354

359-
func configureNginxMetrics(gatewayClassName string) (*nkgmetrics.ManagerMetricsCollector, error) {
360-
constLabels := map[string]string{"class": gatewayClassName}
355+
// createAndRegisterMetricsCollectors creates the NGINX status and NGINX runtime manager collectors, registers them,
356+
// and returns the runtime manager collector to be used in the nginxRuntimeMgr.
357+
func createAndRegisterMetricsCollectors(metricsEnabled bool, gwClassName string) (ngxruntime.ManagerCollector, error) {
358+
if !metricsEnabled {
359+
// return a no-op collector to avoid nil pointer errors when metrics are disabled
360+
return nkgmetrics.NewManagerNoopCollector(), nil
361+
}
362+
constLabels := map[string]string{"class": gwClassName}
363+
361364
ngxCollector, err := nkgmetrics.NewNginxMetricsCollector(constLabels)
362365
if err != nil {
363-
return nil, fmt.Errorf("cannot get NGINX metrics: %w", err)
366+
return nil, fmt.Errorf("cannot create NGINX status metrics collector: %w", err)
364367
}
365-
mgrCollector := nkgmetrics.NewManagerMetricsCollector(constLabels)
366-
if err = metrics.Registry.Register(mgrCollector); err != nil {
367-
return nil, fmt.Errorf("failed to register manager metrics collector: %w", err)
368+
if err := metrics.Registry.Register(ngxCollector); err != nil {
369+
return nil, fmt.Errorf("failed to register NGINX status metrics collector: %w", err)
368370
}
369371

370-
if err = metrics.Registry.Register(ngxCollector); err != nil {
371-
return nil, fmt.Errorf("failed to register NGINX metrics collector: %w", err)
372+
mgrCollector := nkgmetrics.NewManagerMetricsCollector(constLabels)
373+
if err := metrics.Registry.Register(mgrCollector); err != nil {
374+
return nil, fmt.Errorf("failed to register NGINX manager runtime metrics collector: %w", err)
372375
}
373376

374377
return mgrCollector, nil

internal/mode/static/metrics/collector.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,21 @@ func NewManagerMetricsCollector(constLabels map[string]string) *ManagerMetricsCo
4747
Namespace: metricsNamespace,
4848
Help: "Duration in milliseconds of NGINX reloads",
4949
ConstLabels: constLabels,
50-
Buckets: []float64{100.0, 200.0, 300.0, 400.0, 500.0},
50+
Buckets: []float64{500, 1000, 5000, 10000, 30000},
5151
},
5252
),
5353
}
5454
return nc
5555
}
5656

5757
// IncNginxReloadCount increments the counter of successful NGINX reloads and sets the stale config status to false.
58-
func (mc *ManagerMetricsCollector) IncNginxReloadCount() {
58+
func (mc *ManagerMetricsCollector) IncReloadCount() {
5959
mc.reloadsTotal.Inc()
6060
mc.updateConfigStaleStatus(false)
6161
}
6262

6363
// IncNginxReloadErrors increments the counter of NGINX reload errors and sets the stale config status to true.
64-
func (mc *ManagerMetricsCollector) IncNginxReloadErrors() {
64+
func (mc *ManagerMetricsCollector) IncReloadErrors() {
6565
mc.reloadsError.Inc()
6666
mc.updateConfigStaleStatus(true)
6767
}
@@ -75,8 +75,8 @@ func (mc *ManagerMetricsCollector) updateConfigStaleStatus(stale bool) {
7575
mc.configStale.Set(status)
7676
}
7777

78-
// UpdateLastReloadTime updates the last NGINX reload time.
79-
func (mc *ManagerMetricsCollector) UpdateLastReloadTime(duration time.Duration) {
78+
// ObserveLastReloadTime adds the last NGINX reload time to the histogram.
79+
func (mc *ManagerMetricsCollector) ObserveLastReloadTime(duration time.Duration) {
8080
mc.reloadsDuration.Observe(float64(duration / time.Millisecond))
8181
}
8282

@@ -96,20 +96,20 @@ func (mc *ManagerMetricsCollector) Collect(ch chan<- prometheus.Metric) {
9696
mc.reloadsDuration.Collect(ch)
9797
}
9898

99-
// ManagerFakeCollector is a fake collector that will implement ManagerCollector interface.
99+
// ManagerNoopCollector is a no-op collector that will implement ManagerCollector interface.
100100
// Used to initialize the ManagerCollector when metrics are disabled to avoid nil pointer errors.
101-
type ManagerFakeCollector struct{}
101+
type ManagerNoopCollector struct{}
102102

103-
// NewManagerFakeCollector creates a fake collector that implements ManagerCollector interface.
104-
func NewManagerFakeCollector() *ManagerFakeCollector {
105-
return &ManagerFakeCollector{}
103+
// NewManagerNoopCollector creates a no-op collector that implements ManagerCollector interface.
104+
func NewManagerNoopCollector() *ManagerNoopCollector {
105+
return &ManagerNoopCollector{}
106106
}
107107

108-
// IncNginxReloadCount implements a fake IncNginxReloadCount.
109-
func (mc *ManagerFakeCollector) IncNginxReloadCount() {}
108+
// IncReloadCount implements a no-op IncReloadCount.
109+
func (mc *ManagerNoopCollector) IncReloadCount() {}
110110

111-
// IncNginxReloadErrors implements a fake IncNginxReloadErrors.
112-
func (mc *ManagerFakeCollector) IncNginxReloadErrors() {}
111+
// IncReloadErrors implements a no-op IncReloadErrors.
112+
func (mc *ManagerNoopCollector) IncReloadErrors() {}
113113

114-
// UpdateLastReloadTime implements a fake UpdateLastReloadTime.
115-
func (mc *ManagerFakeCollector) UpdateLastReloadTime(_ time.Duration) {}
114+
// ObserveLastReloadTime implements a no-op ObserveLastReloadTime.
115+
func (mc *ManagerNoopCollector) ObserveLastReloadTime(_ time.Duration) {}

internal/mode/static/nginx/runtime/manager.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,27 @@ type Manager interface {
4343

4444
// ManagerCollector is an interface for the metrics of the NGINX runtime manager.
4545
type ManagerCollector interface {
46-
IncNginxReloadCount()
47-
IncNginxReloadErrors()
48-
UpdateLastReloadTime(ms time.Duration)
46+
IncReloadCount()
47+
IncReloadErrors()
48+
ObserveLastReloadTime(ms time.Duration)
4949
}
5050

5151
// ManagerImpl implements Manager.
5252
type ManagerImpl struct {
5353
verifyClient *verifyClient
54-
metricsCollector ManagerCollector
54+
managerCollector ManagerCollector
5555
}
5656

5757
// NewManagerImpl creates a new ManagerImpl.
58-
func NewManagerImpl(mgrCollector ManagerCollector) *ManagerImpl {
58+
func NewManagerImpl(managerCollector ManagerCollector) *ManagerImpl {
5959
return &ManagerImpl{
6060
verifyClient: newVerifyClient(nginxReloadTimeout),
61-
metricsCollector: mgrCollector,
61+
managerCollector: managerCollector,
6262
}
6363
}
6464

6565
func (m *ManagerImpl) Reload(ctx context.Context, configVersion int) error {
66-
t1 := time.Now()
66+
start := time.Now()
6767
// We find the main NGINX PID on every reload because it will change if the NGINX container is restarted.
6868
pid, err := findMainProcess(ctx, os.Stat, os.ReadFile, pidFileTimeout)
6969
if err != nil {
@@ -79,7 +79,7 @@ func (m *ManagerImpl) Reload(ctx context.Context, configVersion int) error {
7979
// send HUP signal to the NGINX main process reload configuration
8080
// See https://nginx.org/en/docs/control.html
8181
if err := syscall.Kill(pid, syscall.SIGHUP); err != nil {
82-
m.metricsCollector.IncNginxReloadErrors()
82+
m.managerCollector.IncReloadErrors()
8383
return fmt.Errorf("failed to send the HUP signal to NGINX main: %w", err)
8484
}
8585

@@ -90,18 +90,18 @@ func (m *ManagerImpl) Reload(ctx context.Context, configVersion int) error {
9090
os.ReadFile,
9191
childProcsTimeout,
9292
); err != nil {
93-
m.metricsCollector.IncNginxReloadErrors()
93+
m.managerCollector.IncReloadErrors()
9494
return fmt.Errorf(noNewWorkersErrFmt, configVersion, err)
9595
}
9696

9797
if err = m.verifyClient.waitForCorrectVersion(ctx, configVersion); err != nil {
98-
m.metricsCollector.IncNginxReloadErrors()
98+
m.managerCollector.IncReloadErrors()
9999
return err
100100
}
101-
m.metricsCollector.IncNginxReloadCount()
101+
m.managerCollector.IncReloadCount()
102102

103-
t2 := time.Now()
104-
m.metricsCollector.UpdateLastReloadTime(t2.Sub(t1))
103+
finish := time.Now()
104+
m.managerCollector.ObserveLastReloadTime(finish.Sub(start))
105105
return nil
106106
}
107107

0 commit comments

Comments
 (0)