|
| 1 | +/* |
| 2 | +Copyright 2022 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +//Package metrics implements custom metrics for CAPC |
| 18 | +package metrics |
| 19 | + |
| 20 | +import ( |
| 21 | + "github.com/prometheus/client_golang/prometheus" |
| 22 | + "regexp" |
| 23 | + crtlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics" |
| 24 | +) |
| 25 | + |
| 26 | +// AcsCustomMetrics encapsulates all CloudStack custom metrics defined for the controller. |
| 27 | +type AcsCustomMetrics struct { |
| 28 | + acsReconciliationErrorCount *prometheus.CounterVec |
| 29 | + errorCodeRegexp *regexp.Regexp |
| 30 | +} |
| 31 | + |
| 32 | +// NewCustomMetrics constructs an AcsCustomMetrics with all desired CloudStack custom metrics and any supporting resources. |
| 33 | +func NewCustomMetrics() AcsCustomMetrics { |
| 34 | + customMetrics := AcsCustomMetrics{} |
| 35 | + customMetrics.acsReconciliationErrorCount = prometheus.NewCounterVec( |
| 36 | + prometheus.CounterOpts{ |
| 37 | + Name: "acs_reconciliation_errors", |
| 38 | + Help: "Count of reconciliation errors caused by ACS issues, bucketed by error code", |
| 39 | + }, |
| 40 | + []string{"acs_error_code"}, |
| 41 | + ) |
| 42 | + crtlmetrics.Registry.MustRegister(customMetrics.acsReconciliationErrorCount) |
| 43 | + |
| 44 | + // ACS standard error messages of the form "CloudStack API error 431 (CSExceptionErrorCode: 9999):..." |
| 45 | + // This regexp is used to extract CSExceptionCodes from the message. |
| 46 | + customMetrics.errorCodeRegexp, _ = regexp.Compile(".+CSExceptionErrorCode: ([0-9]+).+") |
| 47 | + |
| 48 | + return customMetrics |
| 49 | +} |
| 50 | + |
| 51 | +// IncrementAcsReconciliationErrors accepts a CloudStack error message and increments the custom |
| 52 | +// acs_reconciliation_errors counter, labeled with the error code if present in the error message. |
| 53 | +func (m *AcsCustomMetrics) IncrementAcsReconciliationErrors(acsError error) { |
| 54 | + matches := m.errorCodeRegexp.FindStringSubmatch(acsError.Error()) |
| 55 | + if len(matches) > 1 { |
| 56 | + m.acsReconciliationErrorCount.WithLabelValues(matches[1]).Inc() |
| 57 | + } else { |
| 58 | + m.acsReconciliationErrorCount.WithLabelValues("No error code").Inc() |
| 59 | + } |
| 60 | +} |
0 commit comments