Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoints.onErrorAdd #743

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ You can then configure alerts to be triggered when an endpoint is unhealthy once
| `endpoints[].ui.hide-url` | Whether to ensure the URL is not displayed in the results. Useful if the URL contains a token. | `false` |
| `endpoints[].ui.dont-resolve-failed-conditions` | Whether to resolve failed conditions for the UI. | `false` |
| `endpoints[].ui.badge.reponse-time` | List of response time thresholds. Each time a threshold is reached, the badge has a different color. | `[50, 200, 300, 500, 750]` |
| `endpoints[].onErrorAdd` | Add whole response Body ("[BODY]") or part of JSON ("[BODY].error") when an error occurs. | `""` |


### External Endpoints
Expand Down
26 changes: 26 additions & 0 deletions config/endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/TwiN/gatus/v5/jsonpath"
"io"
"net"
"net/http"
Expand Down Expand Up @@ -121,6 +122,9 @@ type Endpoint struct {

// NumberOfSuccessesInARow is the number of successful evaluations in a row
NumberOfSuccessesInARow int `yaml:"-"`

// OnErrorAdd is the message or [BODY] or part of [BODY].json to add to the result's errors if the endpoint fails
OnErrorAdd string `yaml:"onErrorAdd,omitempty"`
}

// IsEnabled returns whether the endpoint is enabled or not
Expand Down Expand Up @@ -303,6 +307,25 @@ func (e *Endpoint) EvaluateHealth() *Result {
if e.UIConfig.HideConditions {
result.ConditionResults = nil
}

if endpoint.OnErrorAdd != "" && !result.Success {
if endpoint.OnErrorAdd == BodyPlaceholder && len(result.Body) > 0 {
result.Errors = append(result.Errors, string(result.Body))
} else if strings.Contains(endpoint.OnErrorAdd, BodyPlaceholder) && len(result.Body) > 0 {
resolvedElement, resolvedElementLength, err := jsonpath.Eval(strings.TrimPrefix(
strings.TrimPrefix(endpoint.OnErrorAdd, BodyPlaceholder), "."), result.Body)
if err != nil {
result.Errors = append(result.Errors, "Decoding of endpoint.OnErrorAdd failed: "+err.Error())
} else {
if resolvedElementLength > 0 {
result.Errors = append(result.Errors, resolvedElement)
}
}
} else {
result.Errors = append(result.Errors, endpoint.OnErrorAdd)
}
}

return result
}

Expand Down Expand Up @@ -422,6 +445,9 @@ func (e *Endpoint) buildHTTPRequest() *http.Request {

// needsToReadBody checks if there's any condition that requires the response Body to be read
func (e *Endpoint) needsToReadBody() bool {
if strings.Contains(e.OnErrorAdd, BodyPlaceholder) {
return true
}
for _, condition := range e.Conditions {
if condition.hasBodyPlaceholder() {
return true
Expand Down
23 changes: 23 additions & 0 deletions config/endpoint/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,29 @@ func TestEndpoint(t *testing.T) {
},
MockRoundTripper: nil,
},
{
Name: "failed-status-with-error-from-body",
Endpoint: Endpoint{
Name: "website-health",
URL: "https://twin.sh/health",
Conditions: []Condition{"[STATUS] == 200"},
OnErrorAdd: "[BODY].error",
},
ExpectedResult: &Result{
Success: false,
Connected: true,
Hostname: "twin.sh",
ConditionResults: []*ConditionResult{
{Condition: "[STATUS] (502) == 200", Success: false},
},
DomainExpiration: 0, // Because there's no [DOMAIN_EXPIRATION] condition, this is not resolved, so it should be 0.
Errors: []string{"something went wrong"},
},
MockRoundTripper: test.MockRoundTripper(func(r *http.Request) *http.Response {
return &http.Response{StatusCode: http.StatusBadGateway, Body: io.NopCloser(strings.NewReader(
"{\"error\":\"something went wrong\"}"))}
}),
},
}
for _, scenario := range scenarios {
t.Run(scenario.Name, func(t *testing.T) {
Expand Down