Skip to content

Commit c699476

Browse files
authored
Merge pull request #189 from AndrewSirenko/legacy-metrics
Let CSIMetricsManager serve additional registries
2 parents e0fa89d + dd22ce6 commit c699476

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.23.1
44

55
require (
66
github.com/container-storage-interface/spec v1.11.0
7+
github.com/prometheus/client_golang v1.20.5
78
github.com/stretchr/testify v1.10.0
89
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.58.0
910
go.opentelemetry.io/otel/trace v1.33.0
@@ -43,7 +44,6 @@ require (
4344
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
4445
github.com/pkg/errors v0.9.1 // indirect
4546
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
46-
github.com/prometheus/client_golang v1.20.5 // indirect
4747
github.com/prometheus/client_model v0.6.1 // indirect
4848
github.com/prometheus/common v0.61.0 // indirect
4949
github.com/prometheus/procfs v0.15.1 // indirect

metrics/metrics.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"strings"
2626
"time"
2727

28+
"github.com/prometheus/client_golang/prometheus"
2829
"google.golang.org/grpc/codes"
2930
"google.golang.org/grpc/status"
3031
"k8s.io/component-base/metrics"
@@ -91,6 +92,11 @@ type CSIMetricsManager interface {
9192
// value is defined in the metrics manager
9293
HaveAdditionalLabel(name string) bool
9394

95+
// WithAdditionalRegistry can be used to ensure additional non-CSI registries are served through RegisterToServer
96+
//
97+
// registry - Any registry which implements Gather() (e.g. metrics.KubeRegistry, prometheus.Registry, etc.)
98+
WithAdditionalRegistry(registry prometheus.Gatherer) CSIMetricsManager
99+
94100
// SetDriverName is called to update the CSI driver name. This should be done
95101
// as soon as possible, otherwise metrics recorded by this manager will be
96102
// recorded with an "unknown-driver" driver_name.
@@ -242,11 +248,6 @@ func NewCSIMetricsManagerWithOptions(driverName string, options ...MetricsManage
242248
// https://github.com/open-telemetry/opentelemetry-collector/issues/969
243249
// Add process_start_time_seconds into the metric to let the start time be parsed correctly
244250
metrics.RegisterProcessStartTime(cmm.registry.Register)
245-
// TODO: This is a bug in component-base library. We need to remove this after upgrade component-base dependency
246-
// BugFix: https://github.com/kubernetes/kubernetes/pull/96435
247-
// The first call to RegisterProcessStartTime can only create the metric, so we need a second call to actually
248-
// register the metric.
249-
metrics.RegisterProcessStartTime(cmm.registry.Register)
250251
}
251252

252253
labels := []string{labelCSIDriverName, labelCSIOperationName, labelGrpcStatusCode}
@@ -266,6 +267,9 @@ func NewCSIMetricsManagerWithOptions(driverName string, options ...MetricsManage
266267
)
267268
cmm.SetDriverName(driverName)
268269
cmm.registerMetrics()
270+
cmm.gatherers = prometheus.Gatherers{
271+
cmm.GetRegistry(),
272+
}
269273
return &cmm
270274
}
271275

@@ -278,6 +282,7 @@ type csiMetricsManager struct {
278282
driverName string
279283
additionalLabelNames []string
280284
additionalLabels []label
285+
gatherers prometheus.Gatherers
281286
csiOperationsLatencyMetric *metrics.HistogramVec
282287
registerProcessStartTime bool
283288
}
@@ -367,6 +372,14 @@ func (cmm *csiMetricsManager) HaveAdditionalLabel(name string) bool {
367372
return false
368373
}
369374

375+
// WithAdditionalRegistry can be used to ensure additional non-CSI registries are served through RegisterToServer
376+
//
377+
// registry - Any registry which implements Gather() (e.g. metrics.KubeRegistry, prometheus.Registry, etc.)
378+
func (cmm *csiMetricsManager) WithAdditionalRegistry(registry prometheus.Gatherer) CSIMetricsManager {
379+
cmm.gatherers = append(cmm.gatherers, registry)
380+
return cmm
381+
}
382+
370383
// RecordMetrics passes the stored values as to the implementation.
371384
func (cmmv *csiMetricsManagerWithValues) RecordMetrics(
372385
operationName string,
@@ -390,7 +403,7 @@ func (cmm *csiMetricsManager) SetDriverName(driverName string) {
390403
// given server at the specified address/path.
391404
func (cmm *csiMetricsManager) RegisterToServer(s Server, metricsPath string) {
392405
s.Handle(metricsPath, metrics.HandlerFor(
393-
cmm.GetRegistry(),
406+
cmm.gatherers,
394407
metrics.HandlerOpts{
395408
ErrorHandling: metrics.ContinueOnError}))
396409
}

0 commit comments

Comments
 (0)