func()

in components/google-built-opentelemetry-collector/extension/healthagent/healthagent.go [55:87]


func (hc *healthAgent) isHealthy() bool {
	hc.mu.Lock()
	defer hc.mu.Unlock()
	if !hc.ready {
		hc.logger.Debug("Pipelines are not ready yet")
		return false
	}
	if hc.lastOK == nil && hc.lastError == nil {
		// Should not happen, but let's return HEALTHY since we haven't seen any errors.
		return true
	}
	if hc.lastError == nil {
		// There was never an error => HEALTHY
		return true
	}
	if hc.lastOK == nil {
		// There was never OK => UNHEALTHY
		hc.logger.Infof("There was never OK => UNHEALTHY")
		return false
	}
	// If lastError happenned after lastOk => UNHEALTHY
	// else, if lastError is within (time.Now() - ErrorCheckInterval, time.Now()] => UNHEALTHY
	// else => HEALTHY
	if hc.lastError.Timestamp().After(hc.lastOK.Timestamp()) {
		hc.logger.Infof("lastError happenned after lastOk, hc.lastError: %v, hc.lastOK: %v", hc.lastError.Timestamp(), hc.lastOK.Timestamp())
		return false
	}
	if hc.lastError.Timestamp().After(time.Now().Add(-hc.config.ErrorCheckInterval)) {
		hc.logger.Infof("lastError is within (time.Now() - ErrorCheckInterval, time.Now()], hc.lastError: %v, hc.lastOK: %v", hc.lastError.Timestamp(), hc.lastOK.Timestamp())
		return false
	}
	return true
}