Skip to content

Commit d830b8d

Browse files
committed
metrics: optional process_start_time
The process_start_time as registered by csi-lib-utils and thus Kubernetes component base conflicts with the one from the Prometheus Golang collector, leading to this error: gathered metric family process_start_time_seconds has help "[ALPHA] Start time of the process since unix epoch in seconds." but should have "Start time of the process since unix epoch in seconds." Allowing processes to control where they get the metric from solves this conflict.
1 parent a833d13 commit d830b8d

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

metrics/metrics.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,22 @@ func WithLabels(labels map[string]string) MetricsManagerOption {
145145
}
146146
}
147147

148+
// WithProcessStartTime controlls whether process_start_time_seconds is registered
149+
// in the registry of the metrics manager. It's enabled by default out of convenience
150+
// (no need to do anything special in most sidecars) but should be disabled in more
151+
// complex scenarios (more than one metrics manager per process, metric already
152+
// provided elsewhere like via the Prometheus Golang collector).
153+
//
154+
// In particular, registering this metric via metric manager and thus the Kubernetes
155+
// component base conflicts with the Prometheus Golang collector (gathered metric family
156+
// process_start_time_seconds has help "[ALPHA] Start time of the process since unix epoch in seconds."
157+
// but should have "Start time of the process since unix epoch in seconds."
158+
func WithProcessStartTime(registerProcessStartTime bool) MetricsManagerOption {
159+
return func(cmm *csiMetricsManager) {
160+
cmm.registerProcessStartTime = registerProcessStartTime
161+
}
162+
}
163+
148164
// NewCSIMetricsManagerForSidecar creates and registers metrics for CSI Sidecars and
149165
// returns an object that can be used to trigger the metrics. It uses "csi_sidecar"
150166
// as subsystem.
@@ -177,18 +193,22 @@ func NewCSIMetricsManagerForPlugin(driverName string) CSIMetricsManager {
177193
// If unknown, leave empty, and use SetDriverName method to update later.
178194
func NewCSIMetricsManagerWithOptions(driverName string, options ...MetricsManagerOption) CSIMetricsManager {
179195
cmm := csiMetricsManager{
180-
registry: metrics.NewKubeRegistry(),
181-
subsystem: SubsystemSidecar,
182-
stabilityLevel: metrics.ALPHA,
196+
registry: metrics.NewKubeRegistry(),
197+
subsystem: SubsystemSidecar,
198+
stabilityLevel: metrics.ALPHA,
199+
registerProcessStartTime: true,
183200
}
184201

185-
// https://github.com/open-telemetry/opentelemetry-collector/issues/969
186-
// Add process_start_time_seconds into the metric to let the start time be parsed correctly
187-
metrics.RegisterProcessStartTime(cmm.registry.Register)
188-
189202
for _, option := range options {
190203
option(&cmm)
191204
}
205+
206+
if cmm.registerProcessStartTime {
207+
// https://github.com/open-telemetry/opentelemetry-collector/issues/969
208+
// Add process_start_time_seconds into the metric to let the start time be parsed correctly
209+
metrics.RegisterProcessStartTime(cmm.registry.Register)
210+
}
211+
192212
labels := []string{labelCSIDriverName, labelCSIOperationName, labelGrpcStatusCode}
193213
labels = append(labels, cmm.additionalLabelNames...)
194214
for _, label := range cmm.additionalLabels {
@@ -219,6 +239,7 @@ type csiMetricsManager struct {
219239
additionalLabelNames []string
220240
additionalLabels []label
221241
csiOperationsLatencyMetric *metrics.HistogramVec
242+
registerProcessStartTime bool
222243
}
223244

224245
type label struct {

0 commit comments

Comments
 (0)