vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/throttle/base/metric_health.go (about)

     1  /*
     2   Copyright 2017 GitHub Inc.
     3  
     4   Licensed under MIT License. See https://github.com/github/freno/blob/master/LICENSE
     5  */
     6  
     7  package base
     8  
     9  import (
    10  	"time"
    11  )
    12  
    13  // MetricHealth is a health status for a metric, and more specifically,
    14  // when it was last checked to be "OK"
    15  type MetricHealth struct {
    16  	LastHealthyAt           time.Time
    17  	SecondsSinceLastHealthy int64
    18  }
    19  
    20  // NewMetricHealth returns a MetricHealth
    21  func NewMetricHealth(lastHealthyAt time.Time) *MetricHealth {
    22  	result := &MetricHealth{
    23  		LastHealthyAt:           lastHealthyAt,
    24  		SecondsSinceLastHealthy: int64(time.Since(lastHealthyAt).Seconds()),
    25  	}
    26  	return result
    27  }
    28  
    29  // MetricHealthMap maps metric names to metric healths
    30  type MetricHealthMap map[string](*MetricHealth)
    31  
    32  // Aggregate another map into this map, take the worst metric of the two
    33  func (m MetricHealthMap) Aggregate(other MetricHealthMap) MetricHealthMap {
    34  	for metricName, otherHealth := range other {
    35  		if currentHealth, ok := m[metricName]; ok {
    36  			if currentHealth.SecondsSinceLastHealthy < otherHealth.SecondsSinceLastHealthy {
    37  				m[metricName] = otherHealth
    38  			}
    39  		} else {
    40  			m[metricName] = otherHealth
    41  		}
    42  	}
    43  	return m
    44  }