...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/metrics/reconciler_metrics.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/metrics

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package metrics
    16  
    17  import (
    18  	"context"
    19  	"log"
    20  	"sync/atomic"
    21  	"time"
    22  
    23  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/errors"
    24  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
    25  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/metrics"
    26  	"go.opencensus.io/stats"
    27  	"go.opencensus.io/tag"
    28  	"k8s.io/apimachinery/pkg/runtime/schema"
    29  )
    30  
    31  var ResourceNameLabel bool
    32  
    33  type ReconcilerMetrics struct {
    34  	// atomic counter for occupied workers
    35  	occupiedWorkers   int64
    36  	ResourceNameLabel bool
    37  }
    38  
    39  func (r *ReconcilerMetrics) RecordReconcileWorkers(ctx context.Context, gvk schema.GroupVersionKind) {
    40  	atomic.AddInt64(&r.occupiedWorkers, 1)
    41  	openCensusContext, _ := tag.New(ctx, tag.Insert(metrics.KindTag, gvk.GroupKind().String()))
    42  	stats.Record(openCensusContext, metrics.MReconcileTotalWorkers.M(k8s.ControllerMaxConcurrentReconciles))
    43  	stats.Record(openCensusContext, metrics.MReconcileOccupiedWorkers.M(atomic.LoadInt64(&r.occupiedWorkers)))
    44  }
    45  
    46  func (r *ReconcilerMetrics) RecordReconcileMetrics(ctx context.Context, gvk schema.GroupVersionKind, ns, name string, startTime time.Time, reconcileErr *error) {
    47  	if reconcileErr == nil {
    48  		log.Println("ERROR: the pointer to reconcile error is nil. Skip recording reconcile metrics")
    49  		return
    50  	}
    51  	status := "OK"
    52  	if *reconcileErr != nil {
    53  		status = "ERROR"
    54  	}
    55  	openCensusContext, _ := tag.New(ctx, tag.Insert(metrics.KindTag, gvk.GroupKind().String()), tag.Insert(metrics.NamespaceTag, ns), tag.Insert(metrics.StatusTag, status))
    56  	if r.ResourceNameLabel {
    57  		openCensusContext, _ = tag.New(openCensusContext, tag.Insert(metrics.ResourceNameTag, name))
    58  	}
    59  	stats.Record(openCensusContext, metrics.MReconcileRequests.M(1), metrics.MReconcileDuration.M(time.Since(startTime).Seconds()))
    60  	r.RecordInternalErrors(ctx, gvk, ns, reconcileErr)
    61  }
    62  
    63  func (r *ReconcilerMetrics) RecordInternalErrors(ctx context.Context, gvk schema.GroupVersionKind, ns string, reconcileErr *error) {
    64  	if reconcileErr == nil {
    65  		log.Println("ERROR: the pointer to reconcile error is nil. Skip recording reconcile metrics")
    66  		return
    67  	}
    68  	err := *reconcileErr
    69  	if err == nil {
    70  		return
    71  	}
    72  	if _, ok := errors.AsInternalError(err); !ok {
    73  		return
    74  	}
    75  	openCensusContext, _ := tag.New(ctx, tag.Insert(metrics.KindTag, gvk.GroupKind().String()), tag.Insert(metrics.NamespaceTag, ns))
    76  	stats.Record(openCensusContext, metrics.MInternalErrors.M(1))
    77  }
    78  
    79  func (r *ReconcilerMetrics) AfterReconcile() {
    80  	atomic.AddInt64(&r.occupiedWorkers, -1)
    81  }
    82  

View as plain text