Minor improvements
This commit is contained in:
parent
10dfdd47a7
commit
09ff879b3b
@ -112,18 +112,12 @@ func (service *Service) ValidateAndSetDefaults() {
|
|||||||
// EvaluateHealth sends a request to the service's URL and evaluates the conditions of the service.
|
// EvaluateHealth sends a request to the service's URL and evaluates the conditions of the service.
|
||||||
func (service *Service) EvaluateHealth() *Result {
|
func (service *Service) EvaluateHealth() *Result {
|
||||||
result := &Result{Success: true, Errors: []string{}}
|
result := &Result{Success: true, Errors: []string{}}
|
||||||
switch {
|
|
||||||
case service.DNS != nil:
|
|
||||||
service.DNS.query(service.URL, result)
|
|
||||||
default:
|
|
||||||
service.getIP(result)
|
service.getIP(result)
|
||||||
if len(result.Errors) == 0 {
|
if len(result.Errors) == 0 {
|
||||||
service.call(result)
|
service.call(result)
|
||||||
} else {
|
} else {
|
||||||
result.Success = false
|
result.Success = false
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for _, condition := range service.Conditions {
|
for _, condition := range service.Conditions {
|
||||||
success := condition.evaluate(result)
|
success := condition.evaluate(result)
|
||||||
if !success {
|
if !success {
|
||||||
@ -150,13 +144,17 @@ func (service *Service) GetAlertsTriggered() []Alert {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (service *Service) getIP(result *Result) {
|
func (service *Service) getIP(result *Result) {
|
||||||
|
if service.DNS != nil {
|
||||||
|
result.Hostname = strings.TrimSuffix(service.URL, ":53")
|
||||||
|
} else {
|
||||||
urlObject, err := url.Parse(service.URL)
|
urlObject, err := url.Parse(service.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.Errors = append(result.Errors, err.Error())
|
result.Errors = append(result.Errors, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
result.Hostname = urlObject.Hostname()
|
result.Hostname = urlObject.Hostname()
|
||||||
ips, err := net.LookupIP(urlObject.Hostname())
|
}
|
||||||
|
ips, err := net.LookupIP(result.Hostname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.Errors = append(result.Errors, err.Error())
|
result.Errors = append(result.Errors, err.Error())
|
||||||
return
|
return
|
||||||
@ -165,15 +163,20 @@ func (service *Service) getIP(result *Result) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (service *Service) call(result *Result) {
|
func (service *Service) call(result *Result) {
|
||||||
isServiceTCP := strings.HasPrefix(service.URL, "tcp://")
|
|
||||||
var request *http.Request
|
var request *http.Request
|
||||||
var response *http.Response
|
var response *http.Response
|
||||||
var err error
|
var err error
|
||||||
if !isServiceTCP {
|
isServiceTCP := strings.HasPrefix(service.URL, "tcp://")
|
||||||
request = service.buildRequest()
|
isServiceDNS := service.DNS != nil
|
||||||
|
isServiceHTTP := !isServiceTCP && !isServiceDNS
|
||||||
|
if isServiceHTTP {
|
||||||
|
request = service.buildHTTPRequest()
|
||||||
}
|
}
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
if isServiceTCP {
|
if isServiceDNS {
|
||||||
|
service.DNS.query(service.URL, result)
|
||||||
|
result.Duration = time.Since(startTime)
|
||||||
|
} else if isServiceTCP {
|
||||||
result.Connected = client.CanCreateConnectionToTCPService(strings.TrimPrefix(service.URL, "tcp://"))
|
result.Connected = client.CanCreateConnectionToTCPService(strings.TrimPrefix(service.URL, "tcp://"))
|
||||||
result.Duration = time.Since(startTime)
|
result.Duration = time.Since(startTime)
|
||||||
} else {
|
} else {
|
||||||
@ -196,7 +199,7 @@ func (service *Service) call(result *Result) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service *Service) buildRequest() *http.Request {
|
func (service *Service) buildHTTPRequest() *http.Request {
|
||||||
var bodyBuffer *bytes.Buffer
|
var bodyBuffer *bytes.Buffer
|
||||||
if service.GraphQL {
|
if service.GraphQL {
|
||||||
graphQlBody := map[string]string{
|
graphQlBody := map[string]string{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user