Use csi-lib-utils v0.10.0

This commit is contained in:
Chris Henzie
2021-08-09 10:22:50 -07:00
parent 0c4830b4c9
commit c858a697ef
5 changed files with 55 additions and 11 deletions

View File

@@ -84,7 +84,8 @@ func ExitOnConnectionLoss() func() bool {
if err := ioutil.WriteFile(terminationLogPath, []byte(terminationMsg), 0644); err != nil {
klog.Errorf("%s: %s", terminationLogPath, err)
}
klog.Fatalf(terminationMsg)
klog.Exit(terminationMsg)
// Not reached.
return false
}
}
@@ -150,7 +151,7 @@ func connect(
return nil, errors.New("OnConnectionLoss callback only supported for unix:// addresses")
}
klog.Infof("Connecting to %s", address)
klog.V(5).Infof("Connecting to %s", address)
// Connect in background.
var conn *grpc.ClientConn
@@ -191,6 +192,13 @@ type ExtendedCSIMetricsManager struct {
metrics.CSIMetricsManager
}
type AdditionalInfo struct {
Migrated string
}
type AdditionalInfoKeyType struct{}
var AdditionalInfoKey AdditionalInfoKeyType
// RecordMetricsClientInterceptor is a gPRC unary interceptor for recording metrics for CSI operations
// in a gRPC client.
func (cmm ExtendedCSIMetricsManager) RecordMetricsClientInterceptor(
@@ -203,11 +211,35 @@ func (cmm ExtendedCSIMetricsManager) RecordMetricsClientInterceptor(
start := time.Now()
err := invoker(ctx, method, req, reply, cc, opts...)
duration := time.Since(start)
cmm.RecordMetrics(
var cmmBase metrics.CSIMetricsManager
cmmBase = cmm
if cmm.HaveAdditionalLabel(metrics.LabelMigrated) {
// record migration status
additionalInfo := ctx.Value(AdditionalInfoKey)
migrated := "false"
if additionalInfo != nil {
additionalInfoVal, ok := additionalInfo.(AdditionalInfo)
if !ok {
klog.Errorf("Failed to record migrated status, cannot convert additional info %v", additionalInfo)
return err
}
migrated = additionalInfoVal.Migrated
}
cmmv, metricsErr := cmm.WithLabelValues(map[string]string{metrics.LabelMigrated: migrated})
if metricsErr != nil {
klog.Errorf("Failed to record migrated status, error: %v", metricsErr)
} else {
cmmBase = cmmv
}
}
// Record the default metric
cmmBase.RecordMetrics(
method, /* operationName */
err, /* operationErr */
duration, /* operationDuration */
)
return err
}

View File

@@ -47,6 +47,9 @@ const (
labelGrpcStatusCode = "grpc_status_code"
unknownCSIDriverName = "unknown-driver"
// LabelMigrated is the Label that indicate whether this is a CSI migration operation
LabelMigrated = "migrated"
// CSI Operation Latency with status code total - Histogram Metric
operationsLatencyMetricName = "operations_seconds"
operationsLatencyHelp = "Container Storage Interface operation duration with gRPC error code status total"
@@ -83,6 +86,10 @@ type CSIMetricsManager interface {
// and then accumulates values.
WithLabelValues(labels map[string]string) (CSIMetricsManager, error)
// HaveAdditionalLabel can be used to check if the additional label
// value is defined in the metrics manager
HaveAdditionalLabel(name string) bool
// SetDriverName is called to update the CSI driver name. This should be done
// as soon as possible, otherwise metrics recorded by this manager will be
// recorded with an "unknown-driver" driver_name.
@@ -149,6 +156,13 @@ func WithLabels(labels map[string]string) MetricsManagerOption {
}
}
// WithMigration adds the migrated field to the current metrics label
func WithMigration() MetricsManagerOption {
return func(cmm *csiMetricsManager) {
cmm.additionalLabelNames = append(cmm.additionalLabelNames, LabelMigrated)
}
}
// WithProcessStartTime controlls whether process_start_time_seconds is registered
// in the registry of the metrics manager. It's enabled by default out of convenience
// (no need to do anything special in most sidecars) but should be disabled in more
@@ -316,7 +330,7 @@ func (cmmv *csiMetricsManagerWithValues) WithLabelValues(labels map[string]strin
}
// Now add all new values.
for name, value := range labels {
if !extended.haveAdditionalLabel(name) {
if !extended.HaveAdditionalLabel(name) {
return nil, fmt.Errorf("label %q was not defined via WithLabelNames", name)
}
if v, ok := extended.additionalValues[name]; ok {
@@ -327,7 +341,7 @@ func (cmmv *csiMetricsManagerWithValues) WithLabelValues(labels map[string]strin
return extended, nil
}
func (cmm *csiMetricsManager) haveAdditionalLabel(name string) bool {
func (cmm *csiMetricsManager) HaveAdditionalLabel(name string) bool {
for _, n := range cmm.additionalLabelNames {
if n == name {
return true