Skip to content

Instantly share code, notes, and snippets.

@johscheuer
Created November 28, 2016 12:19
Show Gist options
  • Save johscheuer/7dbc435dc7591840508cb40559efea6e to your computer and use it in GitHub Desktop.
Save johscheuer/7dbc435dc7591840508cb40559efea6e to your computer and use it in GitHub Desktop.
func getHostnameFromConnection(connection, defaultHost string) string {
host, _, err := net.SplitHostPort(connection)
if err != nil {
host = defaultHost
fmt.Println(err)
}
return host
}
func (redisDB RedisDB) GetHealthStatus() map[string]string {
result := map[string]string{"self": okString}
hostname, err := os.Hostname()
if err != nil { //TODO we'll just ignore any errors :)
hostname = "UNKNOWN"
}
redisMasterHost := getHostnameFromConnection(redisDB.master, "redis-master")
redisSlaveHost := getHostnameFromConnection(redisDB.slave, "redis-slave")
var wg sync.WaitGroup
results := make(chan *checkConnectionResult, 2)
wg.Add(2)
go func() {
results <- checkConnections(redisMasterHost, hostname, redisDB.master, redisDB.masterPassword)
wg.Done()
}()
go func() {
results <- checkConnections(redisSlaveHost, hostname, redisDB.slave, redisDB.slavePassword)
wg.Done()
}()
wg.Wait()
close(results)
// Merge Results
for res := range results {
if res.name == redisMasterHost {
redisMastersTotal.WithLabelValues(hostname, redisDB.appVersion).Set(float64(res.total))
redisMastersHealthyTotal.WithLabelValues(hostname, redisDB.appVersion).Set(float64(res.healthy))
}
if res.name == redisSlaveHost {
redisSlavesTotal.WithLabelValues(hostname, redisDB.appVersion).Set(float64(res.total))
redisSlavesHealthyTotal.WithLabelValues(hostname, redisDB.appVersion).Set(float64(res.healthy))
}
for k, v := range res.results {
result[k] = v
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment