Close #18: Support monitoring TCP services

This commit is contained in:
TwinProduction
2020-10-04 19:49:02 -04:00
parent 6a3f65db7f
commit 3ecfe4d322
7 changed files with 155 additions and 27 deletions

View File

@ -265,3 +265,21 @@ func TestCondition_evaluateWithStatusPatternFailure(t *testing.T) {
t.Errorf("Condition '%s' should have been a failure", condition)
}
}
func TestCondition_evaluateWithConnected(t *testing.T) {
condition := Condition("[CONNECTED] == true")
result := &Result{Connected: true}
condition.evaluate(result)
if !result.ConditionResults[0].Success {
t.Errorf("Condition '%s' should have been a success", condition)
}
}
func TestCondition_evaluateWithConnectedFailure(t *testing.T) {
condition := Condition("[CONNECTED] == true")
result := &Result{Connected: false}
condition.evaluate(result)
if result.ConditionResults[0].Success {
t.Errorf("Condition '%s' should have been a failure", condition)
}
}

View File

@ -9,6 +9,7 @@ import (
"net"
"net/http"
"net/url"
"strings"
"time"
)
@ -136,19 +137,30 @@ func (service *Service) getIp(result *Result) {
}
func (service *Service) call(result *Result) {
request := service.buildRequest()
startTime := time.Now()
response, err := client.GetHttpClient(service.Insecure).Do(request)
if err != nil {
result.Duration = time.Since(startTime)
result.Errors = append(result.Errors, err.Error())
return
isServiceTcp := strings.HasPrefix(service.Url, "tcp://")
var request *http.Request
var response *http.Response
var err error
if !isServiceTcp {
request = service.buildRequest()
}
result.Duration = time.Since(startTime)
result.HttpStatus = response.StatusCode
result.Body, err = ioutil.ReadAll(response.Body)
if err != nil {
result.Errors = append(result.Errors, err.Error())
startTime := time.Now()
if isServiceTcp {
result.Connected = client.CanCreateConnectionToTcpService(strings.TrimPrefix(service.Url, "tcp://"))
result.Duration = time.Since(startTime)
} else {
response, err = client.GetHttpClient(service.Insecure).Do(request)
result.Duration = time.Since(startTime)
if err != nil {
result.Errors = append(result.Errors, err.Error())
return
}
result.HttpStatus = response.StatusCode
result.Connected = response.StatusCode > 0
result.Body, err = ioutil.ReadAll(response.Body)
if err != nil {
result.Errors = append(result.Errors, err.Error())
}
}
}

View File

@ -9,16 +9,37 @@ type HealthStatus struct {
Message string `json:"message,omitempty"`
}
// Result of the evaluation of a Service
type Result struct {
HttpStatus int `json:"status"`
Body []byte `json:"-"`
Hostname string `json:"hostname"`
Ip string `json:"-"`
Duration time.Duration `json:"duration"`
Errors []string `json:"errors"`
// HttpStatus is the HTTP response status code
HttpStatus int `json:"status"`
// Body is the response body
Body []byte `json:"-"`
// Hostname extracted from the Service Url
Hostname string `json:"hostname"`
// Ip resolved from the Service Url
Ip string `json:"-"`
// Connected whether a connection to the host was established successfully
Connected bool `json:"-"`
// Duration time that the request took
Duration time.Duration `json:"duration"`
// Errors encountered during the evaluation of the service's health
Errors []string `json:"errors"`
// ConditionResults results of the service's conditions
ConditionResults []*ConditionResult `json:"condition-results"`
Success bool `json:"success"`
Timestamp time.Time `json:"timestamp"`
// Success whether the result signifies a success or not
Success bool `json:"success"`
// Timestamp when the request was sent
Timestamp time.Time `json:"timestamp"`
}
type ConditionResult struct {

View File

@ -12,6 +12,7 @@ const (
IPPlaceHolder = "[IP]"
ResponseTimePlaceHolder = "[RESPONSE_TIME]"
BodyPlaceHolder = "[BODY]"
ConnectedPlaceHolder = "[CONNECTED]"
LengthFunctionPrefix = "len("
PatternFunctionPrefix = "pat("
@ -35,6 +36,8 @@ func sanitizeAndResolve(list []string, result *Result) []string {
element = strconv.Itoa(int(result.Duration.Milliseconds()))
case BodyPlaceHolder:
element = body
case ConnectedPlaceHolder:
element = strconv.FormatBool(result.Connected)
default:
// if contains the BodyPlaceHolder, then evaluate json path
if strings.Contains(element, BodyPlaceHolder) {