github.com/cilium/cilium@v1.16.2/pkg/k8s/metrics/metrics.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package metrics
     5  
     6  import (
     7  	"github.com/cilium/cilium/pkg/lock"
     8  	"github.com/cilium/cilium/pkg/time"
     9  )
    10  
    11  var (
    12  	// LastInteraction is the time at which the last apiserver interaction
    13  	// occurred
    14  	LastInteraction eventTimestamper
    15  	// LastSuccessInteraction is the time at which we have received a successful
    16  	// k8s apiserver reply (i.e. a response code 2xx or 4xx).
    17  	LastSuccessInteraction eventTimestamper
    18  )
    19  
    20  type eventTimestamper struct {
    21  	timestamp time.Time
    22  	lock      lock.RWMutex
    23  }
    24  
    25  // Reset sets the timestamp to the current time
    26  func (e *eventTimestamper) Reset() {
    27  	e.lock.Lock()
    28  	e.timestamp = time.Now()
    29  	e.lock.Unlock()
    30  }
    31  
    32  // Time returns the timestamp as set per Reset()
    33  func (e *eventTimestamper) Time() time.Time {
    34  	e.lock.RLock()
    35  	t := e.timestamp
    36  	e.lock.RUnlock()
    37  	return t
    38  }