Skip to content

Commit

Permalink
Add NegControllerErrorCount metrics
Browse files Browse the repository at this point in the history
Add NegControllerErrorCount metrics to track the count of all errors
from NEG controller, and counts of server errors from GCE/K8s.
  • Loading branch information
sawsa307 committed May 13, 2023
1 parent c7c7171 commit 55df15e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/neg/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ func (c *Controller) handleErr(err error, key interface{}) {
}

msg := fmt.Sprintf("error processing service %q: %v", key, err)
metrics.PublishNegControllerErrorCountMetrics(err)
c.logger.Error(nil, msg)
if service, exists, err := c.serviceLister.GetByKey(key.(string)); err != nil {
c.logger.Error(err, "Failed to retrieve service from store", "service", key.(string))
Expand Down
32 changes: 32 additions & 0 deletions pkg/neg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/prometheus/client_golang/prometheus"
"k8s.io/ingress-gce/pkg/utils"
)

const (
Expand Down Expand Up @@ -49,6 +50,10 @@ const (
ipv6EndpointType = "IPv6"
dualStackEndpointType = "DualStack"
migrationEndpointType = "Migration"

gceServerError = "GCE_server_error"
k8sServerError = "K8s_server_error"
totalNegControllerError = "total_neg_controller_error"
)

type syncType string
Expand Down Expand Up @@ -211,6 +216,17 @@ var (
},
[]string{"endpoint_type"},
)

// NegControllerErrorCount tracks the count of server errors(GCE/K8s) and
// all errors from NEG controller.
NegControllerErrorCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: negControllerSubsystem,
Name: "neg_controller_error_count",
Help: "Counts of server errors and NEG controller errors.",
},
[]string{"error_type"},
)
)

var register sync.Once
Expand All @@ -234,6 +250,7 @@ func RegisterMetrics() {
prometheus.MustRegister(DualStackMigrationLongestUnfinishedDuration)
prometheus.MustRegister(DualStackMigrationServiceCount)
prometheus.MustRegister(SyncerCountByEndpointType)
prometheus.MustRegister(NegControllerErrorCount)

RegisterSyncerMetrics()
})
Expand Down Expand Up @@ -279,6 +296,21 @@ func PublishDegradedModeCorrectnessMetrics(count int, endpointType string, negTy
DegradeModeCorrectness.WithLabelValues(negType, endpointType).Observe(float64(count))
}

// PublishNegControllerErrorCountMetrics publishes collected metrics
// for neg controller errors.
func PublishNegControllerErrorCountMetrics(err error) {
if err == nil {
return
}
NegControllerErrorCount.WithLabelValues(totalNegControllerError).Inc()
if utils.IsGCEServerError(err) {
NegControllerErrorCount.WithLabelValues(gceServerError).Inc()
}
if utils.IsK8sServerError(err) {
NegControllerErrorCount.WithLabelValues(k8sServerError).Inc()
}
}

func getResult(err error) string {
if err != nil {
return resultError
Expand Down
34 changes: 34 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,40 @@ func GetErrorType(err error) string {
return ""
}

// IsGCEServerError returns true if the error is GCE server error
func IsGCEServerError(err error) bool {
if err == nil {
return false
}
var gerr *googleapi.Error
if !errors.As(err, &gerr) {
return false
}
for {
if apiErr, ok := err.(*googleapi.Error); ok {
return apiErr.Code >= http.StatusInternalServerError
}
err = errors.Unwrap(err)
}
}

// IsK8sServerError returns true if the error is K8s server error
func IsK8sServerError(err error) bool {
if err == nil {
return false
}
var k8serr *k8serrors.StatusError
if !errors.As(err, &k8serr) {
return false
}
for {
if apiErr, ok := err.(*k8serrors.StatusError); ok {
return apiErr.ErrStatus.Code >= http.StatusInternalServerError
}
err = errors.Unwrap(err)
}
}

// PrettyJson marshals an object in a human-friendly format.
func PrettyJson(data interface{}) (string, error) {
buffer := new(bytes.Buffer)
Expand Down

0 comments on commit 55df15e

Please sign in to comment.