istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/util/informermetric/informerutil.go (about)

     1  // Copyright Istio Authors
     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 informermetric
    16  
    17  import (
    18  	"sync"
    19  
    20  	"k8s.io/client-go/tools/cache"
    21  
    22  	"istio.io/istio/pkg/cluster"
    23  	"istio.io/istio/pkg/log"
    24  	"istio.io/istio/pkg/monitoring"
    25  )
    26  
    27  var (
    28  	clusterLabel = monitoring.CreateLabel("cluster")
    29  
    30  	errorMetric = monitoring.NewSum(
    31  		"controller_sync_errors_total",
    32  		"Total number of errorMetric syncing controllers.",
    33  	)
    34  
    35  	mu       sync.RWMutex
    36  	handlers = map[cluster.ID]cache.WatchErrorHandler{}
    37  )
    38  
    39  // ErrorHandlerForCluster fetches or creates an ErrorHandler that emits a metric
    40  // and logs when a watch error occurs. For use with SetWatchErrorHandler on SharedInformer.
    41  func ErrorHandlerForCluster(clusterID cluster.ID) cache.WatchErrorHandler {
    42  	mu.RLock()
    43  	handler, ok := handlers[clusterID]
    44  	mu.RUnlock()
    45  	if ok {
    46  		return handler
    47  	}
    48  
    49  	mu.Lock()
    50  	defer mu.Unlock()
    51  	clusterMetric := errorMetric.With(clusterLabel.Value(clusterID.String()))
    52  	h := func(_ *cache.Reflector, err error) {
    53  		clusterMetric.Increment()
    54  		log.Errorf("watch error in cluster %s: %v", clusterID, err)
    55  	}
    56  	handlers[clusterID] = h
    57  	return h
    58  }