Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c094c06e56 | ||
|
|
f961bf961e | ||
|
|
404a3cea64 | ||
|
|
d000460a99 | ||
|
|
455fae05c1 | ||
|
|
85dff34350 | ||
|
|
99fa632021 | ||
|
|
cafcc9d45b | ||
|
|
dc929dac70 | ||
|
|
42825b62fb | ||
|
|
a89bb392ed | ||
|
|
e7c4d03c22 | ||
|
|
4e6bf91651 | ||
|
|
de31a7a62e | ||
|
|
7f0543ebd2 | ||
|
|
9b893aa4e0 | ||
|
|
50435f4030 | ||
|
|
9649d80388 | ||
|
|
8c3ab1eac2 | ||
|
|
cdb5ba080a | ||
|
|
0a145da912 | ||
|
|
b603cdb0ea | ||
|
|
11d1f24ceb | ||
|
|
c74472d332 | ||
|
|
78a1262e7c | ||
|
|
7ff8907eda | ||
|
|
1d21f5889d | ||
|
|
d7d904ae5f | ||
|
|
7390895514 | ||
|
|
2873d96b9f | ||
|
|
ea9623f695 | ||
|
|
9cdef02bdc | ||
|
|
16229592a2 | ||
|
|
52ad4ee9e5 | ||
|
|
b47c6dc408 | ||
|
|
8e2a2c4dbc | ||
|
|
8698736e7d | ||
|
|
cd1430f043 | ||
|
|
79bef8d391 | ||
|
|
9196f57487 | ||
|
|
1e0d9e184c | ||
|
|
bc42497cb7 | ||
|
|
b0b0ab574d | ||
|
|
e369484e5f | ||
|
|
d18618449f | ||
|
|
9186049589 | ||
|
|
4362831f71 | ||
|
|
99f558d43e | ||
|
|
4a3d5944b6 | ||
|
|
688456d7cf | ||
|
|
431fb3e9f2 | ||
|
|
a1679ddc5e | ||
|
|
d8d8e8720b | ||
|
|
4a0d9d058a | ||
|
|
c8ccf9b352 | ||
|
|
45c966fbca | ||
|
|
d8d4756ef3 | ||
|
|
1e9c54cc0f | ||
|
|
80570688e1 | ||
|
|
43e6e3e8f5 | ||
|
|
8b4c5c20f3 | ||
|
|
6f6db36b0f | ||
|
|
467874de10 | ||
|
|
8337f41425 | ||
|
|
601d676e34 | ||
|
|
fbb5d48bf7 | ||
|
|
119b80edc0 | ||
|
|
99e8cfb1ce | ||
|
|
dcbbec7931 | ||
|
|
2ccd656386 | ||
|
|
5755f3a699 | ||
|
|
911d809376 | ||
|
|
752c872d3b | ||
|
|
67a3e4e330 | ||
|
|
668ed3b1a2 | ||
|
|
dc6cb8fc1d | ||
|
|
f1aa5191bf | ||
|
|
bc6ca2ebd0 | ||
|
|
30801938b2 | ||
|
|
ddddd405bb | ||
|
|
2207dd9c32 | ||
|
|
3204a79eb6 | ||
|
|
e9ac115a95 | ||
|
|
c90c786f39 | ||
|
|
f10e2ac639 | ||
|
|
c2d899f2a3 | ||
|
|
7415d8e361 | ||
|
|
298dcc4790 | ||
|
|
2f2890c093 | ||
|
|
e463aec5f6 | ||
|
|
6b3e11a47c | ||
|
|
0985e3bed8 | ||
|
|
6d8fd267de | ||
|
|
e89bb932ea | ||
|
|
77737dbab6 | ||
|
|
271c3dc91d | ||
|
|
5860a27ab5 | ||
|
|
819093cb7e |
No files matched your search
@@ -3,3 +3,4 @@ Dockerfile
|
|||||||
.github
|
.github
|
||||||
.idea
|
.idea
|
||||||
.git
|
.git
|
||||||
|
web/app
|
||||||
File renamed without changes.
@@ -15,16 +15,18 @@ jobs:
|
|||||||
timeout-minutes: 5
|
timeout-minutes: 5
|
||||||
steps:
|
steps:
|
||||||
- name: Set up Go 1.15
|
- name: Set up Go 1.15
|
||||||
uses: actions/setup-go@v1
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: 1.15
|
go-version: 1.15
|
||||||
id: go
|
|
||||||
- name: Check out code into the Go module directory
|
- name: Check out code into the Go module directory
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
- name: Build binary to make sure it works
|
- name: Build binary to make sure it works
|
||||||
run: go build -mod vendor
|
run: go build -mod vendor
|
||||||
- name: Test
|
- name: Test
|
||||||
run: sudo go test -mod vendor ./... -race -coverprofile=coverage.txt -covermode=atomic
|
# We're using "sudo" because one of the tests leverages ping, which requires super-user privileges.
|
||||||
|
# As for the 'env "PATH=$PATH" "GOROOT=$GOROOT"', we need it to use the same "go" executable that
|
||||||
|
# was configured by the "Set up Go 1.15" step (otherwise, it'd use sudo's "go" executable)
|
||||||
|
run: sudo env "PATH=$PATH" "GOROOT=$GOROOT" go test -mod vendor ./... -race -coverprofile=coverage.txt -covermode=atomic
|
||||||
- name: Codecov
|
- name: Codecov
|
||||||
uses: codecov/codecov-action@v1.0.14
|
uses: codecov/codecov-action@v1.0.14
|
||||||
with:
|
with:
|
||||||
|
|||||||
+2
-2
@@ -1,4 +1,4 @@
|
|||||||
bin
|
|
||||||
.idea
|
.idea
|
||||||
.vscode
|
.vscode
|
||||||
gatus
|
gatus
|
||||||
|
db.db
|
||||||
+1
-1
@@ -13,7 +13,7 @@ RUN CGO_ENABLED=0 GOOS=linux go build -mod vendor -a -installsuffix cgo -o gatus
|
|||||||
FROM scratch
|
FROM scratch
|
||||||
COPY --from=builder /app/gatus .
|
COPY --from=builder /app/gatus .
|
||||||
COPY --from=builder /app/config.yaml ./config/config.yaml
|
COPY --from=builder /app/config.yaml ./config/config.yaml
|
||||||
COPY --from=builder /app/static static/
|
COPY --from=builder /app/web/static ./web/static
|
||||||
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
|
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
|
||||||
ENV PORT=8080
|
ENV PORT=8080
|
||||||
EXPOSE ${PORT}
|
EXPOSE ${PORT}
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
docker-build:
|
||||||
|
docker build -t twinproduction/gatus:latest .
|
||||||
|
|
||||||
|
docker-build-and-run:
|
||||||
|
docker build -t twinproduction/gatus:latest . && docker run -p 8080:8080 --name gatus twinproduction/gatus:latest
|
||||||
|
|
||||||
|
build-frontend:
|
||||||
|
npm --prefix web/app run build
|
||||||
|
|
||||||
|
run-frontend:
|
||||||
|
npm --prefix web/app run serve
|
||||||
|
|
||||||
|
test:
|
||||||
|
go test ./alerting/... ./client/... ./config/... ./controller/... ./core/... ./jsonpath/... ./pattern/... ./security/... ./storage/... ./util/... ./watchdog/... -cover
|
||||||
@@ -2,6 +2,7 @@ package custom
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -15,11 +16,12 @@ import (
|
|||||||
// AlertProvider is the configuration necessary for sending an alert using a custom HTTP request
|
// AlertProvider is the configuration necessary for sending an alert using a custom HTTP request
|
||||||
// Technically, all alert providers should be reachable using the custom alert provider
|
// Technically, all alert providers should be reachable using the custom alert provider
|
||||||
type AlertProvider struct {
|
type AlertProvider struct {
|
||||||
URL string `yaml:"url"`
|
URL string `yaml:"url"`
|
||||||
Method string `yaml:"method,omitempty"`
|
Method string `yaml:"method,omitempty"`
|
||||||
Insecure bool `yaml:"insecure,omitempty"`
|
Insecure bool `yaml:"insecure,omitempty"`
|
||||||
Body string `yaml:"body,omitempty"`
|
Body string `yaml:"body,omitempty"`
|
||||||
Headers map[string]string `yaml:"headers,omitempty"`
|
Headers map[string]string `yaml:"headers,omitempty"`
|
||||||
|
Placeholders map[string]map[string]string `yaml:"placeholders,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsValid returns whether the provider's configuration is valid
|
// IsValid returns whether the provider's configuration is valid
|
||||||
@@ -32,10 +34,25 @@ func (provider *AlertProvider) ToCustomAlertProvider(service *core.Service, aler
|
|||||||
return provider
|
return provider
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAlertStatePlaceholderValue returns the Placeholder value for ALERT_TRIGGERED_OR_RESOLVED if configured
|
||||||
|
func (provider *AlertProvider) GetAlertStatePlaceholderValue(resolved bool) string {
|
||||||
|
status := "TRIGGERED"
|
||||||
|
if resolved {
|
||||||
|
status = "RESOLVED"
|
||||||
|
}
|
||||||
|
if _, ok := provider.Placeholders["ALERT_TRIGGERED_OR_RESOLVED"]; ok {
|
||||||
|
if val, ok := provider.Placeholders["ALERT_TRIGGERED_OR_RESOLVED"][status]; ok {
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return status
|
||||||
|
}
|
||||||
|
|
||||||
func (provider *AlertProvider) buildHTTPRequest(serviceName, alertDescription string, resolved bool) *http.Request {
|
func (provider *AlertProvider) buildHTTPRequest(serviceName, alertDescription string, resolved bool) *http.Request {
|
||||||
body := provider.Body
|
body := provider.Body
|
||||||
providerURL := provider.URL
|
providerURL := provider.URL
|
||||||
method := provider.Method
|
method := provider.Method
|
||||||
|
|
||||||
if strings.Contains(body, "[ALERT_DESCRIPTION]") {
|
if strings.Contains(body, "[ALERT_DESCRIPTION]") {
|
||||||
body = strings.ReplaceAll(body, "[ALERT_DESCRIPTION]", alertDescription)
|
body = strings.ReplaceAll(body, "[ALERT_DESCRIPTION]", alertDescription)
|
||||||
}
|
}
|
||||||
@@ -44,9 +61,9 @@ func (provider *AlertProvider) buildHTTPRequest(serviceName, alertDescription st
|
|||||||
}
|
}
|
||||||
if strings.Contains(body, "[ALERT_TRIGGERED_OR_RESOLVED]") {
|
if strings.Contains(body, "[ALERT_TRIGGERED_OR_RESOLVED]") {
|
||||||
if resolved {
|
if resolved {
|
||||||
body = strings.ReplaceAll(body, "[ALERT_TRIGGERED_OR_RESOLVED]", "RESOLVED")
|
body = strings.ReplaceAll(body, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetAlertStatePlaceholderValue(true))
|
||||||
} else {
|
} else {
|
||||||
body = strings.ReplaceAll(body, "[ALERT_TRIGGERED_OR_RESOLVED]", "TRIGGERED")
|
body = strings.ReplaceAll(body, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetAlertStatePlaceholderValue(false))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if strings.Contains(providerURL, "[ALERT_DESCRIPTION]") {
|
if strings.Contains(providerURL, "[ALERT_DESCRIPTION]") {
|
||||||
@@ -57,9 +74,9 @@ func (provider *AlertProvider) buildHTTPRequest(serviceName, alertDescription st
|
|||||||
}
|
}
|
||||||
if strings.Contains(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]") {
|
if strings.Contains(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]") {
|
||||||
if resolved {
|
if resolved {
|
||||||
providerURL = strings.ReplaceAll(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]", "RESOLVED")
|
providerURL = strings.ReplaceAll(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetAlertStatePlaceholderValue(true))
|
||||||
} else {
|
} else {
|
||||||
providerURL = strings.ReplaceAll(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]", "TRIGGERED")
|
providerURL = strings.ReplaceAll(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetAlertStatePlaceholderValue(false))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(method) == 0 {
|
if len(method) == 0 {
|
||||||
@@ -76,6 +93,9 @@ func (provider *AlertProvider) buildHTTPRequest(serviceName, alertDescription st
|
|||||||
// Send a request to the alert provider and return the body
|
// Send a request to the alert provider and return the body
|
||||||
func (provider *AlertProvider) Send(serviceName, alertDescription string, resolved bool) ([]byte, error) {
|
func (provider *AlertProvider) Send(serviceName, alertDescription string, resolved bool) ([]byte, error) {
|
||||||
if os.Getenv("MOCK_ALERT_PROVIDER") == "true" {
|
if os.Getenv("MOCK_ALERT_PROVIDER") == "true" {
|
||||||
|
if os.Getenv("MOCK_ALERT_PROVIDER_ERROR") == "true" {
|
||||||
|
return nil, errors.New("error")
|
||||||
|
}
|
||||||
return []byte("{}"), nil
|
return []byte("{}"), nil
|
||||||
}
|
}
|
||||||
request := provider.buildHTTPRequest(serviceName, alertDescription, resolved)
|
request := provider.buildHTTPRequest(serviceName, alertDescription, resolved)
|
||||||
|
|||||||
@@ -62,9 +62,49 @@ func TestAlertProvider_ToCustomAlertProvider(t *testing.T) {
|
|||||||
provider := AlertProvider{URL: "http://example.com"}
|
provider := AlertProvider{URL: "http://example.com"}
|
||||||
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, true)
|
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, true)
|
||||||
if customAlertProvider == nil {
|
if customAlertProvider == nil {
|
||||||
t.Error("customAlertProvider shouldn't have been nil")
|
t.Fatal("customAlertProvider shouldn't have been nil")
|
||||||
}
|
}
|
||||||
if customAlertProvider != customAlertProvider {
|
if customAlertProvider.URL != "http://example.com" {
|
||||||
t.Error("customAlertProvider should've been equal to customAlertProvider")
|
t.Error("expected URL to be http://example.com, got", customAlertProvider.URL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAlertProvider_buildHTTPRequestWithCustomPlaceholder(t *testing.T) {
|
||||||
|
const (
|
||||||
|
ExpectedURL = "http://example.com/service-name?event=test&description=alert-description"
|
||||||
|
ExpectedBody = "service-name,alert-description,test"
|
||||||
|
)
|
||||||
|
customAlertProvider := &AlertProvider{
|
||||||
|
URL: "http://example.com/[SERVICE_NAME]?event=[ALERT_TRIGGERED_OR_RESOLVED]&description=[ALERT_DESCRIPTION]",
|
||||||
|
Body: "[SERVICE_NAME],[ALERT_DESCRIPTION],[ALERT_TRIGGERED_OR_RESOLVED]",
|
||||||
|
Headers: nil,
|
||||||
|
Placeholders: map[string]map[string]string{
|
||||||
|
"ALERT_TRIGGERED_OR_RESOLVED": {
|
||||||
|
"RESOLVED": "test",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
request := customAlertProvider.buildHTTPRequest("service-name", "alert-description", true)
|
||||||
|
if request.URL.String() != ExpectedURL {
|
||||||
|
t.Error("expected URL to be", ExpectedURL, "was", request.URL.String())
|
||||||
|
}
|
||||||
|
body, _ := ioutil.ReadAll(request.Body)
|
||||||
|
if string(body) != ExpectedBody {
|
||||||
|
t.Error("expected body to be", ExpectedBody, "was", string(body))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAlertProvider_GetAlertStatePlaceholderValueDefaults(t *testing.T) {
|
||||||
|
customAlertProvider := &AlertProvider{
|
||||||
|
URL: "http://example.com/[SERVICE_NAME]?event=[ALERT_TRIGGERED_OR_RESOLVED]&description=[ALERT_DESCRIPTION]",
|
||||||
|
Body: "[SERVICE_NAME],[ALERT_DESCRIPTION],[ALERT_TRIGGERED_OR_RESOLVED]",
|
||||||
|
Headers: nil,
|
||||||
|
Placeholders: nil,
|
||||||
|
}
|
||||||
|
if customAlertProvider.GetAlertStatePlaceholderValue(true) != "RESOLVED" {
|
||||||
|
t.Error("expected RESOLVED, got", customAlertProvider.GetAlertStatePlaceholderValue(true))
|
||||||
|
}
|
||||||
|
if customAlertProvider.GetAlertStatePlaceholderValue(false) != "TRIGGERED" {
|
||||||
|
t.Error("expected TRIGGERED, got", customAlertProvider.GetAlertStatePlaceholderValue(false))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -38,7 +38,7 @@ func (provider *AlertProvider) ToCustomAlertProvider(service *core.Service, aler
|
|||||||
} else {
|
} else {
|
||||||
prefix = ":x:"
|
prefix = ":x:"
|
||||||
}
|
}
|
||||||
results += fmt.Sprintf("%s - `%s`\n", prefix, conditionResult.Condition)
|
results += fmt.Sprintf("%s - `%s`\\n", prefix, conditionResult.Condition)
|
||||||
}
|
}
|
||||||
return &custom.AlertProvider{
|
return &custom.AlertProvider{
|
||||||
URL: provider.WebhookURL,
|
URL: provider.WebhookURL,
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package mattermost
|
package mattermost
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -19,23 +21,45 @@ func TestAlertProvider_IsValid(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAlertProvider_ToCustomAlertProviderWithResolvedAlert(t *testing.T) {
|
func TestAlertProvider_ToCustomAlertProviderWithResolvedAlert(t *testing.T) {
|
||||||
provider := AlertProvider{WebhookURL: "http://example.com"}
|
provider := AlertProvider{WebhookURL: "http://example.org"}
|
||||||
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{ConditionResults: []*core.ConditionResult{{Condition: "SUCCESSFUL_CONDITION", Success: true}}}, true)
|
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{ConditionResults: []*core.ConditionResult{{Condition: "SUCCESSFUL_CONDITION", Success: true}}}, true)
|
||||||
if customAlertProvider == nil {
|
if customAlertProvider == nil {
|
||||||
t.Error("customAlertProvider shouldn't have been nil")
|
t.Fatal("customAlertProvider shouldn't have been nil")
|
||||||
}
|
}
|
||||||
if !strings.Contains(customAlertProvider.Body, "resolved") {
|
if !strings.Contains(customAlertProvider.Body, "resolved") {
|
||||||
t.Error("customAlertProvider.Body should've contained the substring resolved")
|
t.Error("customAlertProvider.Body should've contained the substring resolved")
|
||||||
}
|
}
|
||||||
|
if customAlertProvider.URL != "http://example.org" {
|
||||||
|
t.Errorf("expected URL to be %s, got %s", "http://example.org", customAlertProvider.URL)
|
||||||
|
}
|
||||||
|
if customAlertProvider.Method != http.MethodPost {
|
||||||
|
t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method)
|
||||||
|
}
|
||||||
|
body := make(map[string]interface{})
|
||||||
|
err := json.Unmarshal([]byte(customAlertProvider.Body), &body)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("expected body to be valid JSON, got error:", err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAlertProvider_ToCustomAlertProviderWithTriggeredAlert(t *testing.T) {
|
func TestAlertProvider_ToCustomAlertProviderWithTriggeredAlert(t *testing.T) {
|
||||||
provider := AlertProvider{WebhookURL: "http://example.com"}
|
provider := AlertProvider{WebhookURL: "http://example.org"}
|
||||||
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{ConditionResults: []*core.ConditionResult{{Condition: "UNSUCCESSFUL_CONDITION", Success: false}}}, false)
|
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{ConditionResults: []*core.ConditionResult{{Condition: "UNSUCCESSFUL_CONDITION", Success: false}}}, false)
|
||||||
if customAlertProvider == nil {
|
if customAlertProvider == nil {
|
||||||
t.Error("customAlertProvider shouldn't have been nil")
|
t.Fatal("customAlertProvider shouldn't have been nil")
|
||||||
}
|
}
|
||||||
if !strings.Contains(customAlertProvider.Body, "triggered") {
|
if !strings.Contains(customAlertProvider.Body, "triggered") {
|
||||||
t.Error("customAlertProvider.Body should've contained the substring triggered")
|
t.Error("customAlertProvider.Body should've contained the substring triggered")
|
||||||
}
|
}
|
||||||
|
if customAlertProvider.URL != "http://example.org" {
|
||||||
|
t.Errorf("expected URL to be %s, got %s", "http://example.org", customAlertProvider.URL)
|
||||||
|
}
|
||||||
|
if customAlertProvider.Method != http.MethodPost {
|
||||||
|
t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method)
|
||||||
|
}
|
||||||
|
body := make(map[string]interface{})
|
||||||
|
err := json.Unmarshal([]byte(customAlertProvider.Body), &body)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("expected body to be valid JSON, got error:", err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package messagebird
|
package messagebird
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -30,11 +32,22 @@ func TestAlertProvider_ToCustomAlertProviderWithResolvedAlert(t *testing.T) {
|
|||||||
}
|
}
|
||||||
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, true)
|
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, true)
|
||||||
if customAlertProvider == nil {
|
if customAlertProvider == nil {
|
||||||
t.Error("customAlertProvider shouldn't have been nil")
|
t.Fatal("customAlertProvider shouldn't have been nil")
|
||||||
}
|
}
|
||||||
if !strings.Contains(customAlertProvider.Body, "RESOLVED") {
|
if !strings.Contains(customAlertProvider.Body, "RESOLVED") {
|
||||||
t.Error("customAlertProvider.Body should've contained the substring RESOLVED")
|
t.Error("customAlertProvider.Body should've contained the substring RESOLVED")
|
||||||
}
|
}
|
||||||
|
if customAlertProvider.URL != "https://rest.messagebird.com/messages" {
|
||||||
|
t.Errorf("expected URL to be %s, got %s", "https://rest.messagebird.com/messages", customAlertProvider.URL)
|
||||||
|
}
|
||||||
|
if customAlertProvider.Method != http.MethodPost {
|
||||||
|
t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method)
|
||||||
|
}
|
||||||
|
body := make(map[string]interface{})
|
||||||
|
err := json.Unmarshal([]byte(customAlertProvider.Body), &body)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("expected body to be valid JSON, got error:", err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAlertProvider_ToCustomAlertProviderWithTriggeredAlert(t *testing.T) {
|
func TestAlertProvider_ToCustomAlertProviderWithTriggeredAlert(t *testing.T) {
|
||||||
@@ -45,9 +58,20 @@ func TestAlertProvider_ToCustomAlertProviderWithTriggeredAlert(t *testing.T) {
|
|||||||
}
|
}
|
||||||
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, false)
|
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, false)
|
||||||
if customAlertProvider == nil {
|
if customAlertProvider == nil {
|
||||||
t.Error("customAlertProvider shouldn't have been nil")
|
t.Fatal("customAlertProvider shouldn't have been nil")
|
||||||
}
|
}
|
||||||
if !strings.Contains(customAlertProvider.Body, "TRIGGERED") {
|
if !strings.Contains(customAlertProvider.Body, "TRIGGERED") {
|
||||||
t.Error("customAlertProvider.Body should've contained the substring TRIGGERED")
|
t.Error("customAlertProvider.Body should've contained the substring TRIGGERED")
|
||||||
}
|
}
|
||||||
|
if customAlertProvider.URL != "https://rest.messagebird.com/messages" {
|
||||||
|
t.Errorf("expected URL to be %s, got %s", "https://rest.messagebird.com/messages", customAlertProvider.URL)
|
||||||
|
}
|
||||||
|
if customAlertProvider.Method != http.MethodPost {
|
||||||
|
t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method)
|
||||||
|
}
|
||||||
|
body := make(map[string]interface{})
|
||||||
|
err := json.Unmarshal([]byte(customAlertProvider.Body), &body)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("expected body to be valid JSON, got error:", err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package pagerduty
|
package pagerduty
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -22,20 +24,42 @@ func TestAlertProvider_ToCustomAlertProviderWithResolvedAlert(t *testing.T) {
|
|||||||
provider := AlertProvider{IntegrationKey: "00000000000000000000000000000000"}
|
provider := AlertProvider{IntegrationKey: "00000000000000000000000000000000"}
|
||||||
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, true)
|
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, true)
|
||||||
if customAlertProvider == nil {
|
if customAlertProvider == nil {
|
||||||
t.Error("customAlertProvider shouldn't have been nil")
|
t.Fatal("customAlertProvider shouldn't have been nil")
|
||||||
}
|
}
|
||||||
if !strings.Contains(customAlertProvider.Body, "RESOLVED") {
|
if !strings.Contains(customAlertProvider.Body, "RESOLVED") {
|
||||||
t.Error("customAlertProvider.Body should've contained the substring RESOLVED")
|
t.Error("customAlertProvider.Body should've contained the substring RESOLVED")
|
||||||
}
|
}
|
||||||
|
if customAlertProvider.URL != "https://events.pagerduty.com/v2/enqueue" {
|
||||||
|
t.Errorf("expected URL to be %s, got %s", "https://events.pagerduty.com/v2/enqueue", customAlertProvider.URL)
|
||||||
|
}
|
||||||
|
if customAlertProvider.Method != http.MethodPost {
|
||||||
|
t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method)
|
||||||
|
}
|
||||||
|
body := make(map[string]interface{})
|
||||||
|
err := json.Unmarshal([]byte(customAlertProvider.Body), &body)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("expected body to be valid JSON, got error:", err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAlertProvider_ToCustomAlertProviderWithTriggeredAlert(t *testing.T) {
|
func TestAlertProvider_ToCustomAlertProviderWithTriggeredAlert(t *testing.T) {
|
||||||
provider := AlertProvider{IntegrationKey: "00000000000000000000000000000000"}
|
provider := AlertProvider{IntegrationKey: "00000000000000000000000000000000"}
|
||||||
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, false)
|
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, false)
|
||||||
if customAlertProvider == nil {
|
if customAlertProvider == nil {
|
||||||
t.Error("customAlertProvider shouldn't have been nil")
|
t.Fatal("customAlertProvider shouldn't have been nil")
|
||||||
}
|
}
|
||||||
if !strings.Contains(customAlertProvider.Body, "TRIGGERED") {
|
if !strings.Contains(customAlertProvider.Body, "TRIGGERED") {
|
||||||
t.Error("customAlertProvider.Body should've contained the substring TRIGGERED")
|
t.Error("customAlertProvider.Body should've contained the substring TRIGGERED")
|
||||||
}
|
}
|
||||||
|
if customAlertProvider.URL != "https://events.pagerduty.com/v2/enqueue" {
|
||||||
|
t.Errorf("expected URL to be %s, got %s", "https://events.pagerduty.com/v2/enqueue", customAlertProvider.URL)
|
||||||
|
}
|
||||||
|
if customAlertProvider.Method != http.MethodPost {
|
||||||
|
t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method)
|
||||||
|
}
|
||||||
|
body := make(map[string]interface{})
|
||||||
|
err := json.Unmarshal([]byte(customAlertProvider.Body), &body)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("expected body to be valid JSON, got error:", err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -31,11 +31,11 @@ func (provider *AlertProvider) ToCustomAlertProvider(service *core.Service, aler
|
|||||||
for _, conditionResult := range result.ConditionResults {
|
for _, conditionResult := range result.ConditionResults {
|
||||||
var prefix string
|
var prefix string
|
||||||
if conditionResult.Success {
|
if conditionResult.Success {
|
||||||
prefix = ":heavy_check_mark:"
|
prefix = ":white_check_mark:"
|
||||||
} else {
|
} else {
|
||||||
prefix = ":x:"
|
prefix = ":x:"
|
||||||
}
|
}
|
||||||
results += fmt.Sprintf("%s - `%s`\n", prefix, conditionResult.Condition)
|
results += fmt.Sprintf("%s - `%s`\\n", prefix, conditionResult.Condition)
|
||||||
}
|
}
|
||||||
return &custom.AlertProvider{
|
return &custom.AlertProvider{
|
||||||
URL: provider.WebhookURL,
|
URL: provider.WebhookURL,
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package slack
|
package slack
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -22,20 +24,42 @@ func TestAlertProvider_ToCustomAlertProviderWithResolvedAlert(t *testing.T) {
|
|||||||
provider := AlertProvider{WebhookURL: "http://example.com"}
|
provider := AlertProvider{WebhookURL: "http://example.com"}
|
||||||
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{ConditionResults: []*core.ConditionResult{{Condition: "SUCCESSFUL_CONDITION", Success: true}}}, true)
|
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{ConditionResults: []*core.ConditionResult{{Condition: "SUCCESSFUL_CONDITION", Success: true}}}, true)
|
||||||
if customAlertProvider == nil {
|
if customAlertProvider == nil {
|
||||||
t.Error("customAlertProvider shouldn't have been nil")
|
t.Fatal("customAlertProvider shouldn't have been nil")
|
||||||
}
|
}
|
||||||
if !strings.Contains(customAlertProvider.Body, "resolved") {
|
if !strings.Contains(customAlertProvider.Body, "resolved") {
|
||||||
t.Error("customAlertProvider.Body should've contained the substring resolved")
|
t.Error("customAlertProvider.Body should've contained the substring resolved")
|
||||||
}
|
}
|
||||||
|
if customAlertProvider.URL != "http://example.com" {
|
||||||
|
t.Errorf("expected URL to be %s, got %s", "http://example.com", customAlertProvider.URL)
|
||||||
|
}
|
||||||
|
if customAlertProvider.Method != http.MethodPost {
|
||||||
|
t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method)
|
||||||
|
}
|
||||||
|
body := make(map[string]interface{})
|
||||||
|
err := json.Unmarshal([]byte(customAlertProvider.Body), &body)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("expected body to be valid JSON, got error:", err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAlertProvider_ToCustomAlertProviderWithTriggeredAlert(t *testing.T) {
|
func TestAlertProvider_ToCustomAlertProviderWithTriggeredAlert(t *testing.T) {
|
||||||
provider := AlertProvider{WebhookURL: "http://example.com"}
|
provider := AlertProvider{WebhookURL: "http://example.com"}
|
||||||
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{ConditionResults: []*core.ConditionResult{{Condition: "UNSUCCESSFUL_CONDITION", Success: false}}}, false)
|
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{ConditionResults: []*core.ConditionResult{{Condition: "UNSUCCESSFUL_CONDITION", Success: false}}}, false)
|
||||||
if customAlertProvider == nil {
|
if customAlertProvider == nil {
|
||||||
t.Error("customAlertProvider shouldn't have been nil")
|
t.Fatal("customAlertProvider shouldn't have been nil")
|
||||||
}
|
}
|
||||||
if !strings.Contains(customAlertProvider.Body, "triggered") {
|
if !strings.Contains(customAlertProvider.Body, "triggered") {
|
||||||
t.Error("customAlertProvider.Body should've contained the substring triggered")
|
t.Error("customAlertProvider.Body should've contained the substring triggered")
|
||||||
}
|
}
|
||||||
|
if customAlertProvider.URL != "http://example.com" {
|
||||||
|
t.Errorf("expected URL to be %s, got %s", "http://example.com", customAlertProvider.URL)
|
||||||
|
}
|
||||||
|
if customAlertProvider.Method != http.MethodPost {
|
||||||
|
t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method)
|
||||||
|
}
|
||||||
|
body := make(map[string]interface{})
|
||||||
|
err := json.Unmarshal([]byte(customAlertProvider.Body), &body)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("expected body to be valid JSON, got error:", err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package twilio
|
package twilio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -26,31 +27,49 @@ func TestTwilioAlertProvider_IsValid(t *testing.T) {
|
|||||||
func TestAlertProvider_ToCustomAlertProviderWithResolvedAlert(t *testing.T) {
|
func TestAlertProvider_ToCustomAlertProviderWithResolvedAlert(t *testing.T) {
|
||||||
provider := AlertProvider{
|
provider := AlertProvider{
|
||||||
SID: "1",
|
SID: "1",
|
||||||
Token: "1",
|
Token: "2",
|
||||||
From: "1",
|
From: "3",
|
||||||
To: "1",
|
To: "4",
|
||||||
}
|
}
|
||||||
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, true)
|
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{Name: "service-name"}, &core.Alert{Description: "alert-description"}, &core.Result{}, true)
|
||||||
if customAlertProvider == nil {
|
if customAlertProvider == nil {
|
||||||
t.Error("customAlertProvider shouldn't have been nil")
|
t.Fatal("customAlertProvider shouldn't have been nil")
|
||||||
}
|
}
|
||||||
if !strings.Contains(customAlertProvider.Body, "RESOLVED") {
|
if !strings.Contains(customAlertProvider.Body, "RESOLVED") {
|
||||||
t.Error("customAlertProvider.Body should've contained the substring RESOLVED")
|
t.Error("customAlertProvider.Body should've contained the substring RESOLVED")
|
||||||
}
|
}
|
||||||
|
if customAlertProvider.URL != "https://api.twilio.com/2010-04-01/Accounts/1/Messages.json" {
|
||||||
|
t.Errorf("expected URL to be %s, got %s", "https://api.twilio.com/2010-04-01/Accounts/1/Messages.json", customAlertProvider.URL)
|
||||||
|
}
|
||||||
|
if customAlertProvider.Method != http.MethodPost {
|
||||||
|
t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method)
|
||||||
|
}
|
||||||
|
if customAlertProvider.Body != "Body=RESOLVED%3A+service-name+-+alert-description&From=3&To=4" {
|
||||||
|
t.Errorf("expected body to be %s, got %s", "Body=RESOLVED%3A+service-name+-+alert-description&From=3&To=4", customAlertProvider.Body)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAlertProvider_ToCustomAlertProviderWithTriggeredAlert(t *testing.T) {
|
func TestAlertProvider_ToCustomAlertProviderWithTriggeredAlert(t *testing.T) {
|
||||||
provider := AlertProvider{
|
provider := AlertProvider{
|
||||||
SID: "1",
|
SID: "4",
|
||||||
Token: "1",
|
Token: "3",
|
||||||
From: "1",
|
From: "2",
|
||||||
To: "1",
|
To: "1",
|
||||||
}
|
}
|
||||||
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{}, &core.Alert{}, &core.Result{}, false)
|
customAlertProvider := provider.ToCustomAlertProvider(&core.Service{Name: "service-name"}, &core.Alert{Description: "alert-description"}, &core.Result{}, false)
|
||||||
if customAlertProvider == nil {
|
if customAlertProvider == nil {
|
||||||
t.Error("customAlertProvider shouldn't have been nil")
|
t.Fatal("customAlertProvider shouldn't have been nil")
|
||||||
}
|
}
|
||||||
if !strings.Contains(customAlertProvider.Body, "TRIGGERED") {
|
if !strings.Contains(customAlertProvider.Body, "TRIGGERED") {
|
||||||
t.Error("customAlertProvider.Body should've contained the substring TRIGGERED")
|
t.Error("customAlertProvider.Body should've contained the substring TRIGGERED")
|
||||||
}
|
}
|
||||||
|
if customAlertProvider.URL != "https://api.twilio.com/2010-04-01/Accounts/4/Messages.json" {
|
||||||
|
t.Errorf("expected URL to be %s, got %s", "https://api.twilio.com/2010-04-01/Accounts/4/Messages.json", customAlertProvider.URL)
|
||||||
|
}
|
||||||
|
if customAlertProvider.Method != http.MethodPost {
|
||||||
|
t.Errorf("expected method to be %s, got %s", http.MethodPost, customAlertProvider.Method)
|
||||||
|
}
|
||||||
|
if customAlertProvider.Body != "Body=TRIGGERED%3A+service-name+-+alert-description&From=2&To=1" {
|
||||||
|
t.Errorf("expected body to be %s, got %s", "Body=TRIGGERED%3A+service-name+-+alert-description&From=2&To=1", customAlertProvider.Body)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -27,6 +27,7 @@ func GetHTTPClient(insecure bool) *http.Client {
|
|||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
MaxIdleConns: 100,
|
MaxIdleConns: 100,
|
||||||
MaxIdleConnsPerHost: 20,
|
MaxIdleConnsPerHost: 20,
|
||||||
|
Proxy: http.ProxyFromEnvironment,
|
||||||
TLSClientConfig: &tls.Config{
|
TLSClientConfig: &tls.Config{
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
},
|
},
|
||||||
@@ -41,6 +42,7 @@ func GetHTTPClient(insecure bool) *http.Client {
|
|||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
MaxIdleConns: 100,
|
MaxIdleConns: 100,
|
||||||
MaxIdleConnsPerHost: 20,
|
MaxIdleConnsPerHost: 20,
|
||||||
|
Proxy: http.ProxyFromEnvironment,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -1,14 +1,14 @@
|
|||||||
services:
|
services:
|
||||||
- name: frontend
|
- name: front-end
|
||||||
group: core
|
group: core
|
||||||
url: "https://twinnation.org/health"
|
url: "https://twinnation.org/health"
|
||||||
interval: 1m
|
interval: 1m
|
||||||
conditions:
|
conditions:
|
||||||
- "[STATUS] == 200"
|
- "[STATUS] == 200"
|
||||||
- "[BODY].status == UP"
|
- "[BODY].status == UP"
|
||||||
- "[RESPONSE_TIME] < 70"
|
- "[RESPONSE_TIME] < 150"
|
||||||
|
|
||||||
- name: backend
|
- name: back-end
|
||||||
group: core
|
group: core
|
||||||
url: "http://example.org/"
|
url: "http://example.org/"
|
||||||
interval: 5m
|
interval: 5m
|
||||||
|
|||||||
+27
-5
@@ -11,6 +11,8 @@ import (
|
|||||||
"github.com/TwinProduction/gatus/core"
|
"github.com/TwinProduction/gatus/core"
|
||||||
"github.com/TwinProduction/gatus/k8s"
|
"github.com/TwinProduction/gatus/k8s"
|
||||||
"github.com/TwinProduction/gatus/security"
|
"github.com/TwinProduction/gatus/security"
|
||||||
|
"github.com/TwinProduction/gatus/storage"
|
||||||
|
"github.com/TwinProduction/gatus/util"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -28,9 +30,6 @@ const (
|
|||||||
|
|
||||||
// DefaultPort is the default port the service will listen on
|
// DefaultPort is the default port the service will listen on
|
||||||
DefaultPort = 8080
|
DefaultPort = 8080
|
||||||
|
|
||||||
// DefaultContextRoot is the default context root of the web application
|
|
||||||
DefaultContextRoot = "/"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -74,8 +73,11 @@ type Config struct {
|
|||||||
// Kubernetes is the Kubernetes configuration
|
// Kubernetes is the Kubernetes configuration
|
||||||
Kubernetes *k8s.Config `yaml:"kubernetes"`
|
Kubernetes *k8s.Config `yaml:"kubernetes"`
|
||||||
|
|
||||||
|
// Storage is the configuration for how the data is stored
|
||||||
|
Storage *storage.Config `yaml:"storage"`
|
||||||
|
|
||||||
// Web is the configuration for the web listener
|
// Web is the configuration for the web listener
|
||||||
Web *webConfig `yaml:"web"`
|
Web *WebConfig `yaml:"web"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the configuration, or panics if the configuration hasn't loaded yet
|
// Get returns the configuration, or panics if the configuration hasn't loaded yet
|
||||||
@@ -147,13 +149,33 @@ func parseAndValidateConfigBytes(yamlBytes []byte) (config *Config, err error) {
|
|||||||
validateServicesConfig(config)
|
validateServicesConfig(config)
|
||||||
validateKubernetesConfig(config)
|
validateKubernetesConfig(config)
|
||||||
validateWebConfig(config)
|
validateWebConfig(config)
|
||||||
|
validateStorageConfig(config)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func validateStorageConfig(config *Config) {
|
||||||
|
if config.Storage == nil {
|
||||||
|
config.Storage = &storage.Config{}
|
||||||
|
}
|
||||||
|
err := storage.Initialize(config.Storage)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
// Remove all ServiceStatus that represent services which no longer exist in the configuration
|
||||||
|
var keys []string
|
||||||
|
for _, service := range config.Services {
|
||||||
|
keys = append(keys, util.ConvertGroupAndServiceToKey(service.Group, service.Name))
|
||||||
|
}
|
||||||
|
numberOfServiceStatusesDeleted := storage.Get().DeleteAllServiceStatusesNotInKeys(keys)
|
||||||
|
if numberOfServiceStatusesDeleted > 0 {
|
||||||
|
log.Printf("[config][validateStorageConfig] Deleted %d service statuses because their matching services no longer existed", numberOfServiceStatusesDeleted)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func validateWebConfig(config *Config) {
|
func validateWebConfig(config *Config) {
|
||||||
if config.Web == nil {
|
if config.Web == nil {
|
||||||
config.Web = &webConfig{Address: DefaultAddress, Port: DefaultPort, ContextRoot: DefaultContextRoot}
|
config.Web = &WebConfig{Address: DefaultAddress, Port: DefaultPort}
|
||||||
} else {
|
} else {
|
||||||
config.Web.validateAndSetDefaults()
|
config.Web.validateAndSetDefaults()
|
||||||
}
|
}
|
||||||
|
|||||||
+116
-65
@@ -42,7 +42,10 @@ func TestLoadDefaultConfigurationFile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestParseAndValidateConfigBytes(t *testing.T) {
|
func TestParseAndValidateConfigBytes(t *testing.T) {
|
||||||
config, err := parseAndValidateConfigBytes([]byte(`
|
file := t.TempDir() + "/test.db"
|
||||||
|
config, err := parseAndValidateConfigBytes([]byte(fmt.Sprintf(`
|
||||||
|
storage:
|
||||||
|
file: %s
|
||||||
services:
|
services:
|
||||||
- name: twinnation
|
- name: twinnation
|
||||||
url: https://twinnation.org/health
|
url: https://twinnation.org/health
|
||||||
@@ -54,9 +57,9 @@ services:
|
|||||||
conditions:
|
conditions:
|
||||||
- "[STATUS] != 400"
|
- "[STATUS] != 400"
|
||||||
- "[STATUS] != 500"
|
- "[STATUS] != 500"
|
||||||
`))
|
`, file)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("No error should've been returned")
|
t.Error("expected no error, got", err.Error())
|
||||||
}
|
}
|
||||||
if config == nil {
|
if config == nil {
|
||||||
t.Fatal("Config shouldn't have been nil")
|
t.Fatal("Config shouldn't have been nil")
|
||||||
@@ -99,7 +102,7 @@ services:
|
|||||||
- "[STATUS] == 200"
|
- "[STATUS] == 200"
|
||||||
`))
|
`))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("No error should've been returned")
|
t.Error("expected no error, got", err.Error())
|
||||||
}
|
}
|
||||||
if config == nil {
|
if config == nil {
|
||||||
t.Fatal("Config shouldn't have been nil")
|
t.Fatal("Config shouldn't have been nil")
|
||||||
@@ -119,9 +122,6 @@ services:
|
|||||||
if config.Web.Port != DefaultPort {
|
if config.Web.Port != DefaultPort {
|
||||||
t.Errorf("Port should have been %d, because it is the default value", DefaultPort)
|
t.Errorf("Port should have been %d, because it is the default value", DefaultPort)
|
||||||
}
|
}
|
||||||
if config.Web.ContextRoot != DefaultContextRoot {
|
|
||||||
t.Errorf("ContextRoot should have been %s, because it is the default value", DefaultContextRoot)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseAndValidateConfigBytesWithAddress(t *testing.T) {
|
func TestParseAndValidateConfigBytesWithAddress(t *testing.T) {
|
||||||
@@ -135,7 +135,7 @@ services:
|
|||||||
- "[STATUS] == 200"
|
- "[STATUS] == 200"
|
||||||
`))
|
`))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("No error should've been returned")
|
t.Error("expected no error, got", err.Error())
|
||||||
}
|
}
|
||||||
if config == nil {
|
if config == nil {
|
||||||
t.Fatal("Config shouldn't have been nil")
|
t.Fatal("Config shouldn't have been nil")
|
||||||
@@ -165,12 +165,12 @@ web:
|
|||||||
port: 12345
|
port: 12345
|
||||||
services:
|
services:
|
||||||
- name: twinnation
|
- name: twinnation
|
||||||
url: https://twinnation.org/actuator/health
|
url: https://twinnation.org/health
|
||||||
conditions:
|
conditions:
|
||||||
- "[STATUS] == 200"
|
- "[STATUS] == 200"
|
||||||
`))
|
`))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("No error should've been returned")
|
t.Error("expected no error, got", err.Error())
|
||||||
}
|
}
|
||||||
if config == nil {
|
if config == nil {
|
||||||
t.Fatal("Config shouldn't have been nil")
|
t.Fatal("Config shouldn't have been nil")
|
||||||
@@ -178,17 +178,15 @@ services:
|
|||||||
if config.Metrics {
|
if config.Metrics {
|
||||||
t.Error("Metrics should've been false by default")
|
t.Error("Metrics should've been false by default")
|
||||||
}
|
}
|
||||||
if config.Services[0].URL != "https://twinnation.org/actuator/health" {
|
if config.Services[0].URL != "https://twinnation.org/health" {
|
||||||
t.Errorf("URL should have been %s", "https://twinnation.org/actuator/health")
|
t.Errorf("URL should have been %s", "https://twinnation.org/health")
|
||||||
}
|
}
|
||||||
if config.Services[0].Interval != 60*time.Second {
|
if config.Services[0].Interval != 60*time.Second {
|
||||||
t.Errorf("Interval should have been %s, because it is the default value", 60*time.Second)
|
t.Errorf("Interval should have been %s, because it is the default value", 60*time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.Web.Address != DefaultAddress {
|
if config.Web.Address != DefaultAddress {
|
||||||
t.Errorf("Bind address should have been %s, because it is the default value", DefaultAddress)
|
t.Errorf("Bind address should have been %s, because it is the default value", DefaultAddress)
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.Web.Port != 12345 {
|
if config.Web.Port != 12345 {
|
||||||
t.Errorf("Port should have been %d, because it is specified in config", 12345)
|
t.Errorf("Port should have been %d, because it is specified in config", 12345)
|
||||||
}
|
}
|
||||||
@@ -206,7 +204,7 @@ services:
|
|||||||
- "[STATUS] == 200"
|
- "[STATUS] == 200"
|
||||||
`))
|
`))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("No error should've been returned")
|
t.Error("expected no error, got", err.Error())
|
||||||
}
|
}
|
||||||
if config == nil {
|
if config == nil {
|
||||||
t.Fatal("Config shouldn't have been nil")
|
t.Fatal("Config shouldn't have been nil")
|
||||||
@@ -228,44 +226,6 @@ services:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseAndValidateConfigBytesWithPortAndHostAndContextRoot(t *testing.T) {
|
|
||||||
config, err := parseAndValidateConfigBytes([]byte(`
|
|
||||||
web:
|
|
||||||
port: 12345
|
|
||||||
address: 127.0.0.1
|
|
||||||
context-root: /deeply/nested/down=/their
|
|
||||||
services:
|
|
||||||
- name: twinnation
|
|
||||||
url: https://twinnation.org/health
|
|
||||||
conditions:
|
|
||||||
- "[STATUS] == 200"
|
|
||||||
`))
|
|
||||||
if err != nil {
|
|
||||||
t.Error("No error should've been returned")
|
|
||||||
}
|
|
||||||
if config == nil {
|
|
||||||
t.Fatal("Config shouldn't have been nil")
|
|
||||||
}
|
|
||||||
if config.Metrics {
|
|
||||||
t.Error("Metrics should've been false by default")
|
|
||||||
}
|
|
||||||
if config.Services[0].URL != "https://twinnation.org/health" {
|
|
||||||
t.Errorf("URL should have been %s", "https://twinnation.org/health")
|
|
||||||
}
|
|
||||||
if config.Services[0].Interval != 60*time.Second {
|
|
||||||
t.Errorf("Interval should have been %s, because it is the default value", 60*time.Second)
|
|
||||||
}
|
|
||||||
if config.Web.Address != "127.0.0.1" {
|
|
||||||
t.Errorf("Bind address should have been %s, because it is specified in config", "127.0.0.1")
|
|
||||||
}
|
|
||||||
if config.Web.Port != 12345 {
|
|
||||||
t.Errorf("Port should have been %d, because it is specified in config", 12345)
|
|
||||||
}
|
|
||||||
if config.Web.ContextRoot != "/deeply/nested/down=/their/" {
|
|
||||||
t.Errorf("Port should have been %s, because it is specified in config", "/deeply/nested/down=/their/")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseAndValidateConfigBytesWithInvalidPort(t *testing.T) {
|
func TestParseAndValidateConfigBytesWithInvalidPort(t *testing.T) {
|
||||||
defer func() { recover() }()
|
defer func() { recover() }()
|
||||||
_, _ = parseAndValidateConfigBytes([]byte(`
|
_, _ = parseAndValidateConfigBytes([]byte(`
|
||||||
@@ -293,7 +253,7 @@ services:
|
|||||||
- "[STATUS] == 200"
|
- "[STATUS] == 200"
|
||||||
`))
|
`))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("No error should've been returned")
|
t.Error("expected no error, got", err.Error())
|
||||||
}
|
}
|
||||||
if config == nil {
|
if config == nil {
|
||||||
t.Fatal("Config shouldn't have been nil")
|
t.Fatal("Config shouldn't have been nil")
|
||||||
@@ -313,9 +273,6 @@ services:
|
|||||||
if config.Web.Port != DefaultPort {
|
if config.Web.Port != DefaultPort {
|
||||||
t.Errorf("Port should have been %d, because it is the default value", DefaultPort)
|
t.Errorf("Port should have been %d, because it is the default value", DefaultPort)
|
||||||
}
|
}
|
||||||
if config.Web.ContextRoot != DefaultContextRoot {
|
|
||||||
t.Errorf("ContextRoot should have been %s, because it is the default value", DefaultContextRoot)
|
|
||||||
}
|
|
||||||
if userAgent := config.Services[0].Headers["User-Agent"]; userAgent != "Test/2.0" {
|
if userAgent := config.Services[0].Headers["User-Agent"]; userAgent != "Test/2.0" {
|
||||||
t.Errorf("User-Agent should've been %s, got %s", "Test/2.0", userAgent)
|
t.Errorf("User-Agent should've been %s, got %s", "Test/2.0", userAgent)
|
||||||
}
|
}
|
||||||
@@ -334,7 +291,7 @@ services:
|
|||||||
- "[STATUS] == 200"
|
- "[STATUS] == 200"
|
||||||
`))
|
`))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("No error should've been returned")
|
t.Error("expected no error, got", err.Error())
|
||||||
}
|
}
|
||||||
if config == nil {
|
if config == nil {
|
||||||
t.Fatal("Config shouldn't have been nil")
|
t.Fatal("Config shouldn't have been nil")
|
||||||
@@ -378,7 +335,6 @@ badconfig:
|
|||||||
func TestParseAndValidateConfigBytesWithAlerting(t *testing.T) {
|
func TestParseAndValidateConfigBytesWithAlerting(t *testing.T) {
|
||||||
config, err := parseAndValidateConfigBytes([]byte(`
|
config, err := parseAndValidateConfigBytes([]byte(`
|
||||||
debug: true
|
debug: true
|
||||||
|
|
||||||
alerting:
|
alerting:
|
||||||
slack:
|
slack:
|
||||||
webhook-url: "http://example.com"
|
webhook-url: "http://example.com"
|
||||||
@@ -405,7 +361,7 @@ services:
|
|||||||
- "[STATUS] == 200"
|
- "[STATUS] == 200"
|
||||||
`))
|
`))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("No error should've been returned")
|
t.Error("expected no error, got", err.Error())
|
||||||
}
|
}
|
||||||
if config == nil {
|
if config == nil {
|
||||||
t.Fatal("Config shouldn't have been nil")
|
t.Fatal("Config shouldn't have been nil")
|
||||||
@@ -498,7 +454,7 @@ services:
|
|||||||
- "[STATUS] == 200"
|
- "[STATUS] == 200"
|
||||||
`))
|
`))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("No error should've been returned")
|
t.Error("expected no error, got", err.Error())
|
||||||
}
|
}
|
||||||
if config == nil {
|
if config == nil {
|
||||||
t.Fatal("Config shouldn't have been nil")
|
t.Fatal("Config shouldn't have been nil")
|
||||||
@@ -532,7 +488,7 @@ services:
|
|||||||
- "[STATUS] == 200"
|
- "[STATUS] == 200"
|
||||||
`))
|
`))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("No error should've been returned")
|
t.Error("expected no error, got", err.Error())
|
||||||
}
|
}
|
||||||
if config == nil {
|
if config == nil {
|
||||||
t.Fatal("Config shouldn't have been nil")
|
t.Fatal("Config shouldn't have been nil")
|
||||||
@@ -546,11 +502,106 @@ services:
|
|||||||
if !config.Alerting.Custom.IsValid() {
|
if !config.Alerting.Custom.IsValid() {
|
||||||
t.Fatal("Custom alerting config should've been valid")
|
t.Fatal("Custom alerting config should've been valid")
|
||||||
}
|
}
|
||||||
|
if config.Alerting.Custom.GetAlertStatePlaceholderValue(true) != "RESOLVED" {
|
||||||
|
t.Fatal("ALERT_TRIGGERED_OR_RESOLVED placeholder value for RESOLVED should've been 'RESOLVED', got", config.Alerting.Custom.GetAlertStatePlaceholderValue(true))
|
||||||
|
}
|
||||||
|
if config.Alerting.Custom.GetAlertStatePlaceholderValue(false) != "TRIGGERED" {
|
||||||
|
t.Fatal("ALERT_TRIGGERED_OR_RESOLVED placeholder value for TRIGGERED should've been 'TRIGGERED', got", config.Alerting.Custom.GetAlertStatePlaceholderValue(false))
|
||||||
|
}
|
||||||
if config.Alerting.Custom.Insecure {
|
if config.Alerting.Custom.Insecure {
|
||||||
t.Fatal("config.Alerting.Custom.Insecure shouldn't have been true")
|
t.Fatal("config.Alerting.Custom.Insecure shouldn't have been true")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseAndValidateConfigBytesWithCustomAlertingConfigAndCustomPlaceholderValues(t *testing.T) {
|
||||||
|
config, err := parseAndValidateConfigBytes([]byte(`
|
||||||
|
alerting:
|
||||||
|
custom:
|
||||||
|
placeholders:
|
||||||
|
ALERT_TRIGGERED_OR_RESOLVED:
|
||||||
|
TRIGGERED: "partial_outage"
|
||||||
|
RESOLVED: "operational"
|
||||||
|
url: "https://example.com"
|
||||||
|
insecure: true
|
||||||
|
body: "[ALERT_TRIGGERED_OR_RESOLVED]: [SERVICE_NAME] - [ALERT_DESCRIPTION]"
|
||||||
|
services:
|
||||||
|
- name: twinnation
|
||||||
|
url: https://twinnation.org/health
|
||||||
|
alerts:
|
||||||
|
- type: custom
|
||||||
|
conditions:
|
||||||
|
- "[STATUS] == 200"
|
||||||
|
`))
|
||||||
|
if err != nil {
|
||||||
|
t.Error("expected no error, got", err.Error())
|
||||||
|
}
|
||||||
|
if config == nil {
|
||||||
|
t.Fatal("Config shouldn't have been nil")
|
||||||
|
}
|
||||||
|
if config.Alerting == nil {
|
||||||
|
t.Fatal("config.Alerting shouldn't have been nil")
|
||||||
|
}
|
||||||
|
if config.Alerting.Custom == nil {
|
||||||
|
t.Fatal("PagerDuty alerting config shouldn't have been nil")
|
||||||
|
}
|
||||||
|
if !config.Alerting.Custom.IsValid() {
|
||||||
|
t.Fatal("Custom alerting config should've been valid")
|
||||||
|
}
|
||||||
|
if config.Alerting.Custom.GetAlertStatePlaceholderValue(true) != "operational" {
|
||||||
|
t.Fatal("ALERT_TRIGGERED_OR_RESOLVED placeholder value for RESOLVED should've been 'operational'")
|
||||||
|
}
|
||||||
|
if config.Alerting.Custom.GetAlertStatePlaceholderValue(false) != "partial_outage" {
|
||||||
|
t.Fatal("ALERT_TRIGGERED_OR_RESOLVED placeholder value for TRIGGERED should've been 'partial_outage'")
|
||||||
|
}
|
||||||
|
if !config.Alerting.Custom.Insecure {
|
||||||
|
t.Fatal("config.Alerting.Custom.Insecure shouldn't have been true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseAndValidateConfigBytesWithCustomAlertingConfigAndOneCustomPlaceholderValue(t *testing.T) {
|
||||||
|
config, err := parseAndValidateConfigBytes([]byte(`
|
||||||
|
alerting:
|
||||||
|
custom:
|
||||||
|
placeholders:
|
||||||
|
ALERT_TRIGGERED_OR_RESOLVED:
|
||||||
|
TRIGGERED: "partial_outage"
|
||||||
|
url: "https://example.com"
|
||||||
|
insecure: true
|
||||||
|
body: "[ALERT_TRIGGERED_OR_RESOLVED]: [SERVICE_NAME] - [ALERT_DESCRIPTION]"
|
||||||
|
services:
|
||||||
|
- name: twinnation
|
||||||
|
url: https://twinnation.org/health
|
||||||
|
alerts:
|
||||||
|
- type: custom
|
||||||
|
conditions:
|
||||||
|
- "[STATUS] == 200"
|
||||||
|
`))
|
||||||
|
if err != nil {
|
||||||
|
t.Error("expected no error, got", err.Error())
|
||||||
|
}
|
||||||
|
if config == nil {
|
||||||
|
t.Fatal("Config shouldn't have been nil")
|
||||||
|
}
|
||||||
|
if config.Alerting == nil {
|
||||||
|
t.Fatal("config.Alerting shouldn't have been nil")
|
||||||
|
}
|
||||||
|
if config.Alerting.Custom == nil {
|
||||||
|
t.Fatal("PagerDuty alerting config shouldn't have been nil")
|
||||||
|
}
|
||||||
|
if !config.Alerting.Custom.IsValid() {
|
||||||
|
t.Fatal("Custom alerting config should've been valid")
|
||||||
|
}
|
||||||
|
if config.Alerting.Custom.GetAlertStatePlaceholderValue(true) != "RESOLVED" {
|
||||||
|
t.Fatal("ALERT_TRIGGERED_OR_RESOLVED placeholder value for RESOLVED should've been 'RESOLVED'")
|
||||||
|
}
|
||||||
|
if config.Alerting.Custom.GetAlertStatePlaceholderValue(false) != "partial_outage" {
|
||||||
|
t.Fatal("ALERT_TRIGGERED_OR_RESOLVED placeholder value for TRIGGERED should've been 'partial_outage'")
|
||||||
|
}
|
||||||
|
if !config.Alerting.Custom.Insecure {
|
||||||
|
t.Fatal("config.Alerting.Custom.Insecure shouldn't have been true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestParseAndValidateConfigBytesWithCustomAlertingConfigThatHasInsecureSetToTrue(t *testing.T) {
|
func TestParseAndValidateConfigBytesWithCustomAlertingConfigThatHasInsecureSetToTrue(t *testing.T) {
|
||||||
config, err := parseAndValidateConfigBytes([]byte(`
|
config, err := parseAndValidateConfigBytes([]byte(`
|
||||||
alerting:
|
alerting:
|
||||||
@@ -571,7 +622,7 @@ services:
|
|||||||
- "[STATUS] == 200"
|
- "[STATUS] == 200"
|
||||||
`))
|
`))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("No error should've been returned")
|
t.Error("expected no error, got", err.Error())
|
||||||
}
|
}
|
||||||
if config == nil {
|
if config == nil {
|
||||||
t.Fatal("Config shouldn't have been nil")
|
t.Fatal("Config shouldn't have been nil")
|
||||||
@@ -624,7 +675,7 @@ services:
|
|||||||
- "[STATUS] == 200"
|
- "[STATUS] == 200"
|
||||||
`, expectedUsername, expectedPasswordHash)))
|
`, expectedUsername, expectedPasswordHash)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("No error should've been returned")
|
t.Error("expected no error, got", err.Error())
|
||||||
}
|
}
|
||||||
if config == nil {
|
if config == nil {
|
||||||
t.Fatal("Config shouldn't have been nil")
|
t.Fatal("Config shouldn't have been nil")
|
||||||
@@ -691,7 +742,7 @@ kubernetes:
|
|||||||
target-path: "/health"
|
target-path: "/health"
|
||||||
`))
|
`))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("No error should've been returned")
|
t.Error("expected no error, got", err.Error())
|
||||||
}
|
}
|
||||||
if config == nil {
|
if config == nil {
|
||||||
t.Fatal("Config shouldn't have been nil")
|
t.Fatal("Config shouldn't have been nil")
|
||||||
|
|||||||
+4
-32
@@ -3,25 +3,20 @@ package config
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"net/url"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// webConfig is the structure which supports the configuration of the endpoint
|
// WebConfig is the structure which supports the configuration of the endpoint
|
||||||
// which provides access to the web frontend
|
// which provides access to the web frontend
|
||||||
type webConfig struct {
|
type WebConfig struct {
|
||||||
// Address to listen on (defaults to 0.0.0.0 specified by DefaultAddress)
|
// Address to listen on (defaults to 0.0.0.0 specified by DefaultAddress)
|
||||||
Address string `yaml:"address"`
|
Address string `yaml:"address"`
|
||||||
|
|
||||||
// Port to listen on (default to 8080 specified by DefaultPort)
|
// Port to listen on (default to 8080 specified by DefaultPort)
|
||||||
Port int `yaml:"port"`
|
Port int `yaml:"port"`
|
||||||
|
|
||||||
// ContextRoot set the root context for the web application
|
|
||||||
ContextRoot string `yaml:"context-root"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// validateAndSetDefaults checks and sets the default values for fields that are not set
|
// validateAndSetDefaults checks and sets the default values for fields that are not set
|
||||||
func (web *webConfig) validateAndSetDefaults() {
|
func (web *WebConfig) validateAndSetDefaults() {
|
||||||
// Validate the Address
|
// Validate the Address
|
||||||
if len(web.Address) == 0 {
|
if len(web.Address) == 0 {
|
||||||
web.Address = DefaultAddress
|
web.Address = DefaultAddress
|
||||||
@@ -32,32 +27,9 @@ func (web *webConfig) validateAndSetDefaults() {
|
|||||||
} else if web.Port < 0 || web.Port > math.MaxUint16 {
|
} else if web.Port < 0 || web.Port > math.MaxUint16 {
|
||||||
panic(fmt.Sprintf("invalid port: value should be between %d and %d", 0, math.MaxUint16))
|
panic(fmt.Sprintf("invalid port: value should be between %d and %d", 0, math.MaxUint16))
|
||||||
}
|
}
|
||||||
// Validate the ContextRoot
|
|
||||||
if len(web.ContextRoot) == 0 {
|
|
||||||
web.ContextRoot = DefaultContextRoot
|
|
||||||
} else {
|
|
||||||
trimmedContextRoot := strings.Trim(web.ContextRoot, "/")
|
|
||||||
if len(trimmedContextRoot) == 0 {
|
|
||||||
web.ContextRoot = DefaultContextRoot
|
|
||||||
return
|
|
||||||
}
|
|
||||||
rootContextURL, err := url.Parse(trimmedContextRoot)
|
|
||||||
if err != nil {
|
|
||||||
panic("invalid context root:" + err.Error())
|
|
||||||
}
|
|
||||||
if rootContextURL.Path != trimmedContextRoot {
|
|
||||||
panic("invalid context root: too complex")
|
|
||||||
}
|
|
||||||
web.ContextRoot = "/" + strings.Trim(rootContextURL.Path, "/") + "/"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SocketAddress returns the combination of the Address and the Port
|
// SocketAddress returns the combination of the Address and the Port
|
||||||
func (web *webConfig) SocketAddress() string {
|
func (web *WebConfig) SocketAddress() string {
|
||||||
return fmt.Sprintf("%s:%d", web.Address, web.Port)
|
return fmt.Sprintf("%s:%d", web.Address, web.Port)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrependWithContextRoot appends the given path to the ContextRoot
|
|
||||||
func (web *webConfig) PrependWithContextRoot(path string) string {
|
|
||||||
return web.ContextRoot + strings.Trim(path, "/")
|
|
||||||
}
|
|
||||||
+1
-85
@@ -1,12 +1,11 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWebConfig_SocketAddress(t *testing.T) {
|
func TestWebConfig_SocketAddress(t *testing.T) {
|
||||||
web := &webConfig{
|
web := &WebConfig{
|
||||||
Address: "0.0.0.0",
|
Address: "0.0.0.0",
|
||||||
Port: 8081,
|
Port: 8081,
|
||||||
}
|
}
|
||||||
@@ -14,86 +13,3 @@ func TestWebConfig_SocketAddress(t *testing.T) {
|
|||||||
t.Errorf("expected %s, got %s", "0.0.0.0:8081", web.SocketAddress())
|
t.Errorf("expected %s, got %s", "0.0.0.0:8081", web.SocketAddress())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWebConfig_PrependWithContextRoot(t *testing.T) {
|
|
||||||
web := &webConfig{ContextRoot: "/status/"}
|
|
||||||
if result := web.PrependWithContextRoot("/api/v1/results"); result != "/status/api/v1/results" {
|
|
||||||
t.Errorf("expected %s, got %s", "/status/api/v1/results", result)
|
|
||||||
}
|
|
||||||
if result := web.PrependWithContextRoot("/health"); result != "/status/health" {
|
|
||||||
t.Errorf("expected %s, got %s", "/status/health", result)
|
|
||||||
}
|
|
||||||
if result := web.PrependWithContextRoot("/health/"); result != "/status/health" {
|
|
||||||
t.Errorf("expected %s, got %s", "/status/health", result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// validContextRootTest specifies all test case which should end up in
|
|
||||||
// a valid context root used to bind the web interface to
|
|
||||||
var validContextRootTests = []struct {
|
|
||||||
name string
|
|
||||||
path string
|
|
||||||
expectedPath string
|
|
||||||
}{
|
|
||||||
{"Empty", "", "/"},
|
|
||||||
{"/", "/", "/"},
|
|
||||||
{"///", "///", "/"},
|
|
||||||
{"Single character 'a'", "a", "/a/"},
|
|
||||||
{"Slash at the beginning", "/status", "/status/"},
|
|
||||||
{"Slashes at start and end", "/status/", "/status/"},
|
|
||||||
{"Multiple slashes at start", "//status", "/status/"},
|
|
||||||
{"Multiple slashes at start and end", "///status////", "/status/"},
|
|
||||||
{"Contains '@' in path'", "me@/status/gatus", "/me@/status/gatus/"},
|
|
||||||
{"Nested context with trailing slash", "/status/gatus/", "/status/gatus/"},
|
|
||||||
{"Nested context without trailing slash", "/status/gatus/system", "/status/gatus/system/"},
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWebConfig_ValidContextRoots(t *testing.T) {
|
|
||||||
for idx, test := range validContextRootTests {
|
|
||||||
t.Run(fmt.Sprintf("%d: %s", idx, test.name), func(t *testing.T) {
|
|
||||||
expectValidResultForContextRoot(t, test.path, test.expectedPath)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func expectValidResultForContextRoot(t *testing.T, path string, expected string) {
|
|
||||||
web := &webConfig{
|
|
||||||
ContextRoot: path,
|
|
||||||
}
|
|
||||||
web.validateAndSetDefaults()
|
|
||||||
if web.ContextRoot != expected {
|
|
||||||
t.Errorf("expected %s, got %s", expected, web.ContextRoot)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// invalidContextRootTests contains all tests for context root which are
|
|
||||||
// expected to fail and stop program execution
|
|
||||||
var invalidContextRootTests = []struct {
|
|
||||||
name string
|
|
||||||
path string
|
|
||||||
}{
|
|
||||||
{"Only a fragment identifier", "#"},
|
|
||||||
{"Invalid character in path", "/invalid" + string([]byte{0x7F})},
|
|
||||||
{"Starts with protocol", "http://status/gatus"},
|
|
||||||
{"Path with fragment", "/status/gatus#here"},
|
|
||||||
{"Starts with '://'", "://status"},
|
|
||||||
{"Contains query parameter", "/status/h?ello=world"},
|
|
||||||
{"Contains '?'", "/status?"},
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWebConfig_InvalidContextRoots(t *testing.T) {
|
|
||||||
for idx, test := range invalidContextRootTests {
|
|
||||||
t.Run(fmt.Sprintf("%d: %s", idx, test.name), func(t *testing.T) {
|
|
||||||
expectInvalidResultForContextRoot(t, test.path)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func expectInvalidResultForContextRoot(t *testing.T, path string) {
|
|
||||||
defer func() { recover() }()
|
|
||||||
|
|
||||||
web := &webConfig{ContextRoot: path}
|
|
||||||
web.validateAndSetDefaults()
|
|
||||||
|
|
||||||
t.Fatal(fmt.Sprintf("Should've panicked because the configuration specifies an invalid context root: %s", path))
|
|
||||||
}
|
|
||||||
+12
-15
@@ -7,44 +7,41 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/TwinProduction/gatus/core"
|
"github.com/TwinProduction/gatus/core"
|
||||||
"github.com/TwinProduction/gatus/watchdog"
|
"github.com/TwinProduction/gatus/storage"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
// badgeHandler handles the automatic generation of badge based on the group name and service name passed.
|
// badgeHandler handles the automatic generation of badge based on the group name and service name passed.
|
||||||
//
|
//
|
||||||
// Valid values for {duration}: 7d, 24h, 1h
|
// Valid values for {duration}: 7d, 24h, 1h
|
||||||
// Pattern for {identifier}: group-<GROUP_NAME>-service-<SERVICE_NAME>.svg
|
// Pattern for {identifier}: <KEY>.svg
|
||||||
func badgeHandler(writer http.ResponseWriter, request *http.Request) {
|
func badgeHandler(writer http.ResponseWriter, request *http.Request) {
|
||||||
variables := mux.Vars(request)
|
variables := mux.Vars(request)
|
||||||
duration := variables["duration"]
|
duration := variables["duration"]
|
||||||
// group-<GROUP_NAME>-service-<SERVICE_NAME>.svg
|
|
||||||
identifier := variables["identifier"]
|
|
||||||
if duration != "7d" && duration != "24h" && duration != "1h" {
|
if duration != "7d" && duration != "24h" && duration != "1h" {
|
||||||
writer.WriteHeader(http.StatusBadRequest)
|
writer.WriteHeader(http.StatusBadRequest)
|
||||||
_, _ = writer.Write([]byte("Durations supported: 7d, 24h, 1h"))
|
_, _ = writer.Write([]byte("Durations supported: 7d, 24h, 1h"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
parts := strings.Split(identifier, "-service-")
|
identifier := variables["identifier"]
|
||||||
if len(parts) != 2 || !strings.HasPrefix(identifier, "group-") || !strings.HasSuffix(identifier, ".svg") {
|
key := strings.TrimSuffix(identifier, ".svg")
|
||||||
writer.WriteHeader(http.StatusBadRequest)
|
serviceStatus := storage.Get().GetServiceStatusByKey(key)
|
||||||
_, _ = writer.Write([]byte("Invalid path: Pattern should look like /group-<GROUP_NAME>-service-<SERVICE_NAME>.svg"))
|
if serviceStatus == nil {
|
||||||
return
|
|
||||||
}
|
|
||||||
groupName := strings.TrimPrefix(parts[0], "group-")
|
|
||||||
serviceName := strings.TrimSuffix(parts[1], ".svg")
|
|
||||||
uptime := watchdog.GetUptimeByServiceGroupAndName(groupName, serviceName)
|
|
||||||
if uptime == nil {
|
|
||||||
writer.WriteHeader(http.StatusNotFound)
|
writer.WriteHeader(http.StatusNotFound)
|
||||||
_, _ = writer.Write([]byte("Requested service not found"))
|
_, _ = writer.Write([]byte("Requested service not found"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if serviceStatus.Uptime == nil {
|
||||||
|
writer.WriteHeader(http.StatusInternalServerError)
|
||||||
|
_, _ = writer.Write([]byte("Failed to compute uptime"))
|
||||||
|
return
|
||||||
|
}
|
||||||
formattedDate := time.Now().Format(http.TimeFormat)
|
formattedDate := time.Now().Format(http.TimeFormat)
|
||||||
writer.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
writer.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
||||||
writer.Header().Set("Date", formattedDate)
|
writer.Header().Set("Date", formattedDate)
|
||||||
writer.Header().Set("Expires", formattedDate)
|
writer.Header().Set("Expires", formattedDate)
|
||||||
writer.Header().Set("Content-Type", "image/svg+xml")
|
writer.Header().Set("Content-Type", "image/svg+xml")
|
||||||
_, _ = writer.Write(generateSVG(duration, uptime))
|
_, _ = writer.Write(generateSVG(duration, serviceStatus.Uptime))
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateSVG(duration string, uptime *core.Uptime) []byte {
|
func generateSVG(duration string, uptime *core.Uptime) []byte {
|
||||||
|
|||||||
+85
-28
@@ -3,16 +3,20 @@ package controller
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/TwinProduction/gatus/config"
|
"github.com/TwinProduction/gatus/config"
|
||||||
"github.com/TwinProduction/gatus/security"
|
"github.com/TwinProduction/gatus/security"
|
||||||
"github.com/TwinProduction/gatus/watchdog"
|
"github.com/TwinProduction/gatus/storage"
|
||||||
"github.com/TwinProduction/gocache"
|
"github.com/TwinProduction/gocache"
|
||||||
|
"github.com/TwinProduction/health"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
)
|
)
|
||||||
@@ -23,6 +27,14 @@ const (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
cache = gocache.NewCache().WithMaxSize(100).WithEvictionPolicy(gocache.LeastRecentlyUsed)
|
cache = gocache.NewCache().WithMaxSize(100).WithEvictionPolicy(gocache.LeastRecentlyUsed)
|
||||||
|
|
||||||
|
// staticFolder is the path to the location of the static folder from the root path of the project
|
||||||
|
// The only reason this is exposed is to allow running tests from a different path than the root path of the project
|
||||||
|
staticFolder = "./web/static"
|
||||||
|
|
||||||
|
// server is the http.Server created by Handle.
|
||||||
|
// The only reason it exists is for testing purposes.
|
||||||
|
server *http.Server
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -34,54 +46,79 @@ func init() {
|
|||||||
// Handle creates the router and starts the server
|
// Handle creates the router and starts the server
|
||||||
func Handle() {
|
func Handle() {
|
||||||
cfg := config.Get()
|
cfg := config.Get()
|
||||||
router := CreateRouter(cfg)
|
var router http.Handler = CreateRouter(cfg)
|
||||||
server := &http.Server{
|
if os.Getenv("ENVIRONMENT") == "dev" {
|
||||||
|
router = developmentCorsHandler(router)
|
||||||
|
}
|
||||||
|
server = &http.Server{
|
||||||
Addr: fmt.Sprintf("%s:%d", cfg.Web.Address, cfg.Web.Port),
|
Addr: fmt.Sprintf("%s:%d", cfg.Web.Address, cfg.Web.Port),
|
||||||
Handler: router,
|
Handler: router,
|
||||||
ReadTimeout: 15 * time.Second,
|
ReadTimeout: 15 * time.Second,
|
||||||
WriteTimeout: 15 * time.Second,
|
WriteTimeout: 15 * time.Second,
|
||||||
IdleTimeout: 15 * time.Second,
|
IdleTimeout: 15 * time.Second,
|
||||||
}
|
}
|
||||||
log.Printf("[controller][Handle] Listening on %s%s\n", cfg.Web.SocketAddress(), cfg.Web.ContextRoot)
|
log.Println("[controller][Handle] Listening on " + cfg.Web.SocketAddress())
|
||||||
log.Fatal(server.ListenAndServe())
|
if os.Getenv("ROUTER_TEST") == "true" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Println("[controller][Handle]", server.ListenAndServe())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shutdown stops the server
|
||||||
|
func Shutdown() {
|
||||||
|
if server != nil {
|
||||||
|
_ = server.Shutdown(context.TODO())
|
||||||
|
server = nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateRouter creates the router for the http server
|
// CreateRouter creates the router for the http server
|
||||||
func CreateRouter(cfg *config.Config) *mux.Router {
|
func CreateRouter(cfg *config.Config) *mux.Router {
|
||||||
router := mux.NewRouter()
|
router := mux.NewRouter()
|
||||||
statusesHandler := serviceStatusesHandler
|
|
||||||
if cfg.Security != nil && cfg.Security.IsValid() {
|
|
||||||
statusesHandler = security.Handler(serviceStatusesHandler, cfg.Security)
|
|
||||||
}
|
|
||||||
router.HandleFunc("/favicon.ico", favIconHandler).Methods("GET") // favicon needs to be always served from the root
|
|
||||||
router.HandleFunc(cfg.Web.PrependWithContextRoot("/api/v1/statuses"), statusesHandler).Methods("GET")
|
|
||||||
router.HandleFunc(cfg.Web.PrependWithContextRoot("/api/v1/badges/uptime/{duration}/{identifier}"), badgeHandler).Methods("GET")
|
|
||||||
router.HandleFunc(cfg.Web.PrependWithContextRoot("/health"), healthHandler).Methods("GET")
|
|
||||||
router.PathPrefix(cfg.Web.ContextRoot).Handler(GzipHandler(http.StripPrefix(cfg.Web.ContextRoot, http.FileServer(http.Dir("./static")))))
|
|
||||||
if cfg.Metrics {
|
if cfg.Metrics {
|
||||||
router.Handle(cfg.Web.PrependWithContextRoot("/metrics"), promhttp.Handler()).Methods("GET")
|
router.Handle("/metrics", promhttp.Handler()).Methods("GET")
|
||||||
}
|
}
|
||||||
|
router.Handle("/health", health.Handler().WithJSON(true)).Methods("GET")
|
||||||
|
router.HandleFunc("/favicon.ico", favIconHandler).Methods("GET")
|
||||||
|
router.HandleFunc("/api/v1/statuses", secureIfNecessary(cfg, serviceStatusesHandler)).Methods("GET") // No GzipHandler for this one, because we cache the content
|
||||||
|
router.HandleFunc("/api/v1/statuses/{key}", secureIfNecessary(cfg, GzipHandlerFunc(serviceStatusHandler))).Methods("GET")
|
||||||
|
router.HandleFunc("/api/v1/badges/uptime/{duration}/{identifier}", badgeHandler).Methods("GET")
|
||||||
|
// SPA
|
||||||
|
router.HandleFunc("/services/{service}", spaHandler).Methods("GET")
|
||||||
|
// Everything else falls back on static content
|
||||||
|
router.PathPrefix("/").Handler(GzipHandler(http.FileServer(http.Dir(staticFolder))))
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func secureIfNecessary(cfg *config.Config, handler http.HandlerFunc) http.HandlerFunc {
|
||||||
|
if cfg.Security != nil && cfg.Security.IsValid() {
|
||||||
|
return security.Handler(handler, cfg.Security)
|
||||||
|
}
|
||||||
|
return handler
|
||||||
|
}
|
||||||
|
|
||||||
|
// serviceStatusesHandler handles requests to retrieve all service statuses
|
||||||
|
// Due to the size of the response, this function leverages a cache.
|
||||||
|
// Must not be wrapped by GzipHandler
|
||||||
func serviceStatusesHandler(writer http.ResponseWriter, r *http.Request) {
|
func serviceStatusesHandler(writer http.ResponseWriter, r *http.Request) {
|
||||||
|
page, pageSize := extractPageAndPageSizeFromRequest(r)
|
||||||
gzipped := strings.Contains(r.Header.Get("Accept-Encoding"), "gzip")
|
gzipped := strings.Contains(r.Header.Get("Accept-Encoding"), "gzip")
|
||||||
var exists bool
|
var exists bool
|
||||||
var value interface{}
|
var value interface{}
|
||||||
if gzipped {
|
if gzipped {
|
||||||
writer.Header().Set("Content-Encoding", "gzip")
|
writer.Header().Set("Content-Encoding", "gzip")
|
||||||
value, exists = cache.Get("service-status-gzipped")
|
value, exists = cache.Get(fmt.Sprintf("service-status-%d-%d-gzipped", page, pageSize))
|
||||||
} else {
|
} else {
|
||||||
value, exists = cache.Get("service-status")
|
value, exists = cache.Get(fmt.Sprintf("service-status-%d-%d", page, pageSize))
|
||||||
}
|
}
|
||||||
var data []byte
|
var data []byte
|
||||||
if !exists {
|
if !exists {
|
||||||
var err error
|
var err error
|
||||||
buffer := &bytes.Buffer{}
|
buffer := &bytes.Buffer{}
|
||||||
gzipWriter := gzip.NewWriter(buffer)
|
gzipWriter := gzip.NewWriter(buffer)
|
||||||
data, err = watchdog.GetServiceStatusesAsJSON()
|
data, err = json.Marshal(storage.Get().GetAllServiceStatusesWithResultPagination(page, pageSize))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[main][serviceStatusesHandler] Unable to marshal object to JSON: %s", err.Error())
|
log.Printf("[controller][serviceStatusesHandler] Unable to marshal object to JSON: %s", err.Error())
|
||||||
writer.WriteHeader(http.StatusInternalServerError)
|
writer.WriteHeader(http.StatusInternalServerError)
|
||||||
_, _ = writer.Write([]byte("Unable to marshal object to JSON"))
|
_, _ = writer.Write([]byte("Unable to marshal object to JSON"))
|
||||||
return
|
return
|
||||||
@@ -89,8 +126,8 @@ func serviceStatusesHandler(writer http.ResponseWriter, r *http.Request) {
|
|||||||
_, _ = gzipWriter.Write(data)
|
_, _ = gzipWriter.Write(data)
|
||||||
_ = gzipWriter.Close()
|
_ = gzipWriter.Close()
|
||||||
gzippedData := buffer.Bytes()
|
gzippedData := buffer.Bytes()
|
||||||
cache.SetWithTTL("service-status", data, cacheTTL)
|
cache.SetWithTTL(fmt.Sprintf("service-status-%d-%d", page, pageSize), data, cacheTTL)
|
||||||
cache.SetWithTTL("service-status-gzipped", gzippedData, cacheTTL)
|
cache.SetWithTTL(fmt.Sprintf("service-status-%d-%d-gzipped", page, pageSize), gzippedData, cacheTTL)
|
||||||
if gzipped {
|
if gzipped {
|
||||||
data = gzippedData
|
data = gzippedData
|
||||||
}
|
}
|
||||||
@@ -102,13 +139,33 @@ func serviceStatusesHandler(writer http.ResponseWriter, r *http.Request) {
|
|||||||
_, _ = writer.Write(data)
|
_, _ = writer.Write(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func healthHandler(writer http.ResponseWriter, _ *http.Request) {
|
// serviceStatusHandler retrieves a single ServiceStatus by group name and service name
|
||||||
|
func serviceStatusHandler(writer http.ResponseWriter, r *http.Request) {
|
||||||
|
page, pageSize := extractPageAndPageSizeFromRequest(r)
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
serviceStatus := storage.Get().GetServiceStatusByKey(vars["key"])
|
||||||
|
if serviceStatus == nil {
|
||||||
|
log.Printf("[controller][serviceStatusHandler] Service with key=%s not found", vars["key"])
|
||||||
|
writer.WriteHeader(http.StatusNotFound)
|
||||||
|
_, _ = writer.Write([]byte("not found"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"serviceStatus": serviceStatus.WithResultPagination(page, pageSize),
|
||||||
|
// The following fields, while present on core.ServiceStatus, are annotated to remain hidden so that we can
|
||||||
|
// expose only the necessary data on /api/v1/statuses.
|
||||||
|
// Since the /api/v1/statuses/{key} endpoint does need this data, however, we explicitly expose it here
|
||||||
|
"events": serviceStatus.Events,
|
||||||
|
"uptime": serviceStatus.Uptime,
|
||||||
|
}
|
||||||
|
output, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("[controller][serviceStatusHandler] Unable to marshal object to JSON: %s", err.Error())
|
||||||
|
writer.WriteHeader(http.StatusInternalServerError)
|
||||||
|
_, _ = writer.Write([]byte("unable to marshal object to JSON"))
|
||||||
|
return
|
||||||
|
}
|
||||||
writer.Header().Add("Content-Type", "application/json")
|
writer.Header().Add("Content-Type", "application/json")
|
||||||
writer.WriteHeader(http.StatusOK)
|
writer.WriteHeader(http.StatusOK)
|
||||||
_, _ = writer.Write([]byte("{\"status\":\"UP\"}"))
|
_, _ = writer.Write(output)
|
||||||
}
|
|
||||||
|
|
||||||
// favIconHandler handles requests for /favicon.ico
|
|
||||||
func favIconHandler(writer http.ResponseWriter, request *http.Request) {
|
|
||||||
http.ServeFile(writer, request, "./static/favicon.ico")
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,333 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/TwinProduction/gatus/config"
|
||||||
|
"github.com/TwinProduction/gatus/core"
|
||||||
|
"github.com/TwinProduction/gatus/storage"
|
||||||
|
"github.com/TwinProduction/gatus/watchdog"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
firstCondition = core.Condition("[STATUS] == 200")
|
||||||
|
secondCondition = core.Condition("[RESPONSE_TIME] < 500")
|
||||||
|
thirdCondition = core.Condition("[CERTIFICATE_EXPIRATION] < 72h")
|
||||||
|
|
||||||
|
timestamp = time.Now()
|
||||||
|
|
||||||
|
testService = core.Service{
|
||||||
|
Name: "name",
|
||||||
|
Group: "group",
|
||||||
|
URL: "https://example.org/what/ever",
|
||||||
|
Method: "GET",
|
||||||
|
Body: "body",
|
||||||
|
Interval: 30 * time.Second,
|
||||||
|
Conditions: []*core.Condition{&firstCondition, &secondCondition, &thirdCondition},
|
||||||
|
Alerts: nil,
|
||||||
|
Insecure: false,
|
||||||
|
NumberOfFailuresInARow: 0,
|
||||||
|
NumberOfSuccessesInARow: 0,
|
||||||
|
}
|
||||||
|
testSuccessfulResult = core.Result{
|
||||||
|
Hostname: "example.org",
|
||||||
|
IP: "127.0.0.1",
|
||||||
|
HTTPStatus: 200,
|
||||||
|
Body: []byte("body"),
|
||||||
|
Errors: nil,
|
||||||
|
Connected: true,
|
||||||
|
Success: true,
|
||||||
|
Timestamp: timestamp,
|
||||||
|
Duration: 150 * time.Millisecond,
|
||||||
|
CertificateExpiration: 10 * time.Hour,
|
||||||
|
ConditionResults: []*core.ConditionResult{
|
||||||
|
{
|
||||||
|
Condition: "[STATUS] == 200",
|
||||||
|
Success: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Condition: "[RESPONSE_TIME] < 500",
|
||||||
|
Success: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Condition: "[CERTIFICATE_EXPIRATION] < 72h",
|
||||||
|
Success: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
testUnsuccessfulResult = core.Result{
|
||||||
|
Hostname: "example.org",
|
||||||
|
IP: "127.0.0.1",
|
||||||
|
HTTPStatus: 200,
|
||||||
|
Body: []byte("body"),
|
||||||
|
Errors: []string{"error-1", "error-2"},
|
||||||
|
Connected: true,
|
||||||
|
Success: false,
|
||||||
|
Timestamp: timestamp,
|
||||||
|
Duration: 750 * time.Millisecond,
|
||||||
|
CertificateExpiration: 10 * time.Hour,
|
||||||
|
ConditionResults: []*core.ConditionResult{
|
||||||
|
{
|
||||||
|
Condition: "[STATUS] == 200",
|
||||||
|
Success: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Condition: "[RESPONSE_TIME] < 500",
|
||||||
|
Success: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Condition: "[CERTIFICATE_EXPIRATION] < 72h",
|
||||||
|
Success: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCreateRouter(t *testing.T) {
|
||||||
|
defer storage.Get().Clear()
|
||||||
|
defer cache.Clear()
|
||||||
|
staticFolder = "../web/static"
|
||||||
|
cfg := &config.Config{
|
||||||
|
Metrics: true,
|
||||||
|
Services: []*core.Service{
|
||||||
|
{
|
||||||
|
Name: "frontend",
|
||||||
|
Group: "core",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "backend",
|
||||||
|
Group: "core",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
watchdog.UpdateServiceStatuses(cfg.Services[0], &core.Result{Success: true, Duration: time.Millisecond, Timestamp: time.Now()})
|
||||||
|
watchdog.UpdateServiceStatuses(cfg.Services[1], &core.Result{Success: false, Duration: time.Second, Timestamp: time.Now()})
|
||||||
|
router := CreateRouter(cfg)
|
||||||
|
type Scenario struct {
|
||||||
|
Name string
|
||||||
|
Path string
|
||||||
|
ExpectedCode int
|
||||||
|
Gzip bool
|
||||||
|
}
|
||||||
|
scenarios := []Scenario{
|
||||||
|
{
|
||||||
|
Name: "health",
|
||||||
|
Path: "/health",
|
||||||
|
ExpectedCode: http.StatusOK,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "metrics",
|
||||||
|
Path: "/metrics",
|
||||||
|
ExpectedCode: http.StatusOK,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "badges-1h",
|
||||||
|
Path: "/api/v1/badges/uptime/1h/core_frontend.svg",
|
||||||
|
ExpectedCode: http.StatusOK,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "badges-24h",
|
||||||
|
Path: "/api/v1/badges/uptime/24h/core_backend.svg",
|
||||||
|
ExpectedCode: http.StatusOK,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "badges-7d",
|
||||||
|
Path: "/api/v1/badges/uptime/7d/core_frontend.svg",
|
||||||
|
ExpectedCode: http.StatusOK,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "badges-with-invalid-duration",
|
||||||
|
Path: "/api/v1/badges/uptime/3d/core_backend.svg",
|
||||||
|
ExpectedCode: http.StatusBadRequest,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "badges-for-invalid-key",
|
||||||
|
Path: "/api/v1/badges/uptime/7d/invalid_key.svg",
|
||||||
|
ExpectedCode: http.StatusNotFound,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "service-statuses",
|
||||||
|
Path: "/api/v1/statuses",
|
||||||
|
ExpectedCode: http.StatusOK,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "service-statuses-gzip",
|
||||||
|
Path: "/api/v1/statuses",
|
||||||
|
ExpectedCode: http.StatusOK,
|
||||||
|
Gzip: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "service-statuses-pagination",
|
||||||
|
Path: "/api/v1/statuses?page=1&pageSize=20",
|
||||||
|
ExpectedCode: http.StatusOK,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "service-status",
|
||||||
|
Path: "/api/v1/statuses/core_frontend",
|
||||||
|
ExpectedCode: http.StatusOK,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "service-status-gzip",
|
||||||
|
Path: "/api/v1/statuses/core_frontend",
|
||||||
|
ExpectedCode: http.StatusOK,
|
||||||
|
Gzip: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "service-status-for-invalid-key",
|
||||||
|
Path: "/api/v1/statuses/invalid_key",
|
||||||
|
ExpectedCode: http.StatusNotFound,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "favicon",
|
||||||
|
Path: "/favicon.ico",
|
||||||
|
ExpectedCode: http.StatusOK,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "frontend-home",
|
||||||
|
Path: "/",
|
||||||
|
ExpectedCode: http.StatusOK,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "frontend-assets",
|
||||||
|
Path: "/js/app.js",
|
||||||
|
ExpectedCode: http.StatusOK,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "frontend-service",
|
||||||
|
Path: "/services/core_frontend",
|
||||||
|
ExpectedCode: http.StatusOK,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, scenario := range scenarios {
|
||||||
|
t.Run(scenario.Name, func(t *testing.T) {
|
||||||
|
request, _ := http.NewRequest("GET", scenario.Path, nil)
|
||||||
|
if scenario.Gzip {
|
||||||
|
request.Header.Set("Accept-Encoding", "gzip")
|
||||||
|
}
|
||||||
|
responseRecorder := httptest.NewRecorder()
|
||||||
|
router.ServeHTTP(responseRecorder, request)
|
||||||
|
if responseRecorder.Code != scenario.ExpectedCode {
|
||||||
|
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, scenario.ExpectedCode, responseRecorder.Code)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandle(t *testing.T) {
|
||||||
|
defer storage.Get().Clear()
|
||||||
|
defer cache.Clear()
|
||||||
|
cfg := &config.Config{
|
||||||
|
Web: &config.WebConfig{
|
||||||
|
Address: "0.0.0.0",
|
||||||
|
Port: rand.Intn(65534),
|
||||||
|
},
|
||||||
|
Services: []*core.Service{
|
||||||
|
{
|
||||||
|
Name: "frontend",
|
||||||
|
Group: "core",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "backend",
|
||||||
|
Group: "core",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
config.Set(cfg)
|
||||||
|
defer config.Set(nil)
|
||||||
|
_ = os.Setenv("ROUTER_TEST", "true")
|
||||||
|
_ = os.Setenv("ENVIRONMENT", "dev")
|
||||||
|
defer os.Clearenv()
|
||||||
|
Handle()
|
||||||
|
defer Shutdown()
|
||||||
|
request, _ := http.NewRequest("GET", "/health", nil)
|
||||||
|
responseRecorder := httptest.NewRecorder()
|
||||||
|
server.Handler.ServeHTTP(responseRecorder, request)
|
||||||
|
if responseRecorder.Code != http.StatusOK {
|
||||||
|
t.Error("expected GET /health to return status code 200")
|
||||||
|
}
|
||||||
|
if server == nil {
|
||||||
|
t.Fatal("server should've been set (but because we set ROUTER_TEST, it shouldn't have been started)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShutdown(t *testing.T) {
|
||||||
|
// Pretend that we called controller.Handle(), which initializes the server variable
|
||||||
|
server = &http.Server{}
|
||||||
|
Shutdown()
|
||||||
|
if server != nil {
|
||||||
|
t.Error("server should've been shut down")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServiceStatusesHandler(t *testing.T) {
|
||||||
|
defer storage.Get().Clear()
|
||||||
|
defer cache.Clear()
|
||||||
|
staticFolder = "../web/static"
|
||||||
|
firstResult := &testSuccessfulResult
|
||||||
|
secondResult := &testUnsuccessfulResult
|
||||||
|
storage.Get().Insert(&testService, firstResult)
|
||||||
|
storage.Get().Insert(&testService, secondResult)
|
||||||
|
// Can't be bothered dealing with timezone issues on the worker that runs the automated tests
|
||||||
|
firstResult.Timestamp = time.Time{}
|
||||||
|
secondResult.Timestamp = time.Time{}
|
||||||
|
router := CreateRouter(&config.Config{})
|
||||||
|
|
||||||
|
type Scenario struct {
|
||||||
|
Name string
|
||||||
|
Path string
|
||||||
|
ExpectedCode int
|
||||||
|
ExpectedBody string
|
||||||
|
}
|
||||||
|
scenarios := []Scenario{
|
||||||
|
{
|
||||||
|
Name: "no-pagination",
|
||||||
|
Path: "/api/v1/statuses",
|
||||||
|
ExpectedCode: http.StatusOK,
|
||||||
|
ExpectedBody: `{"group_name":{"name":"name","group":"group","key":"group_name","results":[{"status":200,"hostname":"example.org","duration":150000000,"errors":null,"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":true},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":true}],"success":true,"timestamp":"0001-01-01T00:00:00Z"},{"status":200,"hostname":"example.org","duration":750000000,"errors":["error-1","error-2"],"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":false},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":false}],"success":false,"timestamp":"0001-01-01T00:00:00Z"}]}}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "pagination-first-result",
|
||||||
|
Path: "/api/v1/statuses?page=1&pageSize=1",
|
||||||
|
ExpectedCode: http.StatusOK,
|
||||||
|
ExpectedBody: `{"group_name":{"name":"name","group":"group","key":"group_name","results":[{"status":200,"hostname":"example.org","duration":750000000,"errors":["error-1","error-2"],"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":false},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":false}],"success":false,"timestamp":"0001-01-01T00:00:00Z"}]}}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "pagination-second-result",
|
||||||
|
Path: "/api/v1/statuses?page=2&pageSize=1",
|
||||||
|
ExpectedCode: http.StatusOK,
|
||||||
|
ExpectedBody: `{"group_name":{"name":"name","group":"group","key":"group_name","results":[{"status":200,"hostname":"example.org","duration":150000000,"errors":null,"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":true},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":true}],"success":true,"timestamp":"0001-01-01T00:00:00Z"}]}}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "pagination-no-results",
|
||||||
|
Path: "/api/v1/statuses?page=5&pageSize=20",
|
||||||
|
ExpectedCode: http.StatusOK,
|
||||||
|
ExpectedBody: `{"group_name":{"name":"name","group":"group","key":"group_name","results":[]}}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "invalid-pagination-should-fall-back-to-default",
|
||||||
|
Path: "/api/v1/statuses?page=INVALID&pageSize=INVALID",
|
||||||
|
ExpectedCode: http.StatusOK,
|
||||||
|
ExpectedBody: `{"group_name":{"name":"name","group":"group","key":"group_name","results":[{"status":200,"hostname":"example.org","duration":150000000,"errors":null,"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":true},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":true}],"success":true,"timestamp":"0001-01-01T00:00:00Z"},{"status":200,"hostname":"example.org","duration":750000000,"errors":["error-1","error-2"],"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":false},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":false}],"success":false,"timestamp":"0001-01-01T00:00:00Z"}]}}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, scenario := range scenarios {
|
||||||
|
t.Run(scenario.Name, func(t *testing.T) {
|
||||||
|
request, _ := http.NewRequest("GET", scenario.Path, nil)
|
||||||
|
responseRecorder := httptest.NewRecorder()
|
||||||
|
router.ServeHTTP(responseRecorder, request)
|
||||||
|
if responseRecorder.Code != scenario.ExpectedCode {
|
||||||
|
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, scenario.ExpectedCode, responseRecorder.Code)
|
||||||
|
}
|
||||||
|
output := responseRecorder.Body.String()
|
||||||
|
if output != scenario.ExpectedBody {
|
||||||
|
t.Errorf("expected:\n %s\n\ngot:\n %s", scenario.ExpectedBody, output)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
func developmentCorsHandler(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:8081")
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
// favIconHandler handles requests for /favicon.ico
|
||||||
|
func favIconHandler(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
http.ServeFile(writer, request, staticFolder+"/favicon.ico")
|
||||||
|
}
|
||||||
+11
-3
@@ -32,10 +32,18 @@ func (w *gzipResponseWriter) Write(b []byte) (int, error) {
|
|||||||
return w.Writer.Write(b)
|
return w.Writer.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GzipHandler compresses the response of a given handler if the request's headers specify that the client
|
// GzipHandler compresses the response of a given http.Handler if the request's headers specify that the client
|
||||||
// supports gzip encoding
|
// supports gzip encoding
|
||||||
func GzipHandler(next http.Handler) http.Handler {
|
func GzipHandler(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(writer http.ResponseWriter, r *http.Request) {
|
return GzipHandlerFunc(func(writer http.ResponseWriter, r *http.Request) {
|
||||||
|
next.ServeHTTP(writer, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// GzipHandlerFunc compresses the response of a given http.HandlerFunc if the request's headers specify that the client
|
||||||
|
// supports gzip encoding
|
||||||
|
func GzipHandlerFunc(next http.HandlerFunc) http.HandlerFunc {
|
||||||
|
return func(writer http.ResponseWriter, r *http.Request) {
|
||||||
// If the request doesn't specify that it supports gzip, then don't compress it
|
// If the request doesn't specify that it supports gzip, then don't compress it
|
||||||
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
||||||
next.ServeHTTP(writer, r)
|
next.ServeHTTP(writer, r)
|
||||||
@@ -47,5 +55,5 @@ func GzipHandler(next http.Handler) http.Handler {
|
|||||||
gz.Reset(writer)
|
gz.Reset(writer)
|
||||||
defer gz.Close()
|
defer gz.Close()
|
||||||
next.ServeHTTP(&gzipResponseWriter{ResponseWriter: writer, Writer: gz}, r)
|
next.ServeHTTP(&gzipResponseWriter{ResponseWriter: writer, Writer: gz}, r)
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
// spaHandler handles requests for /
|
||||||
|
func spaHandler(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
http.ServeFile(writer, request, staticFolder+"/index.html")
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// DefaultPage is the default page to use if none is specified or an invalid value is provided
|
||||||
|
DefaultPage = 1
|
||||||
|
|
||||||
|
// DefaultPageSize is the default page siZE to use if none is specified or an invalid value is provided
|
||||||
|
DefaultPageSize = 20
|
||||||
|
|
||||||
|
// MaximumPageSize is the maximum page size allowed
|
||||||
|
MaximumPageSize = 100
|
||||||
|
)
|
||||||
|
|
||||||
|
func extractPageAndPageSizeFromRequest(r *http.Request) (page int, pageSize int) {
|
||||||
|
var err error
|
||||||
|
if pageParameter := r.URL.Query().Get("page"); len(pageParameter) == 0 {
|
||||||
|
page = DefaultPage
|
||||||
|
} else {
|
||||||
|
page, err = strconv.Atoi(pageParameter)
|
||||||
|
if err != nil {
|
||||||
|
page = DefaultPage
|
||||||
|
}
|
||||||
|
if page < 1 {
|
||||||
|
page = DefaultPage
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if pageSizeParameter := r.URL.Query().Get("pageSize"); len(pageSizeParameter) == 0 {
|
||||||
|
pageSize = DefaultPageSize
|
||||||
|
} else {
|
||||||
|
pageSize, err = strconv.Atoi(pageSizeParameter)
|
||||||
|
if err != nil {
|
||||||
|
pageSize = DefaultPageSize
|
||||||
|
}
|
||||||
|
if pageSize > MaximumPageSize {
|
||||||
|
pageSize = MaximumPageSize
|
||||||
|
} else if pageSize < 1 {
|
||||||
|
pageSize = DefaultPageSize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestExtractPageAndPageSizeFromRequest(t *testing.T) {
|
||||||
|
type Scenario struct {
|
||||||
|
Name string
|
||||||
|
Page string
|
||||||
|
PageSize string
|
||||||
|
ExpectedPage int
|
||||||
|
ExpectedPageSize int
|
||||||
|
}
|
||||||
|
scenarios := []Scenario{
|
||||||
|
{
|
||||||
|
Page: "1",
|
||||||
|
PageSize: "20",
|
||||||
|
ExpectedPage: 1,
|
||||||
|
ExpectedPageSize: 20,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Page: "2",
|
||||||
|
PageSize: "10",
|
||||||
|
ExpectedPage: 2,
|
||||||
|
ExpectedPageSize: 10,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Page: "2",
|
||||||
|
PageSize: "10",
|
||||||
|
ExpectedPage: 2,
|
||||||
|
ExpectedPageSize: 10,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Page: "1",
|
||||||
|
PageSize: "999999",
|
||||||
|
ExpectedPage: 1,
|
||||||
|
ExpectedPageSize: MaximumPageSize,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Page: "-1",
|
||||||
|
PageSize: "-1",
|
||||||
|
ExpectedPage: DefaultPage,
|
||||||
|
ExpectedPageSize: DefaultPageSize,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Page: "invalid",
|
||||||
|
PageSize: "invalid",
|
||||||
|
ExpectedPage: DefaultPage,
|
||||||
|
ExpectedPageSize: DefaultPageSize,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, scenario := range scenarios {
|
||||||
|
t.Run("page-"+scenario.Page+"-pageSize-"+scenario.PageSize, func(t *testing.T) {
|
||||||
|
request, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/statuses?page=%s&pageSize=%s", scenario.Page, scenario.PageSize), nil)
|
||||||
|
actualPage, actualPageSize := extractPageAndPageSizeFromRequest(request)
|
||||||
|
if actualPage != scenario.ExpectedPage {
|
||||||
|
t.Errorf("expected %d, got %d", scenario.ExpectedPage, actualPage)
|
||||||
|
}
|
||||||
|
if actualPageSize != scenario.ExpectedPageSize {
|
||||||
|
t.Errorf("expected %d, got %d", scenario.ExpectedPageSize, actualPageSize)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,6 +26,12 @@ type Alert struct {
|
|||||||
|
|
||||||
// Triggered is used to determine whether an alert has been triggered. When an alert is resolved, this value
|
// Triggered is used to determine whether an alert has been triggered. When an alert is resolved, this value
|
||||||
// should be set back to false. It is used to prevent the same alert from going out twice.
|
// should be set back to false. It is used to prevent the same alert from going out twice.
|
||||||
|
//
|
||||||
|
// This value should only be modified if the provider.AlertProvider's Send function does not return an error for an
|
||||||
|
// alert that hasn't been triggered yet. This doubles as a lazy retry. The reason why this behavior isn't also
|
||||||
|
// applied for alerts that are already triggered and has become "healthy" again is to prevent a case where, for
|
||||||
|
// some reason, the alert provider always returns errors when trying to send the resolved notification
|
||||||
|
// (SendOnResolved).
|
||||||
Triggered bool
|
Triggered bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+137
-69
@@ -2,7 +2,6 @@ package core
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -48,11 +47,20 @@ const (
|
|||||||
CertificateExpirationPlaceholder = "[CERTIFICATE_EXPIRATION]"
|
CertificateExpirationPlaceholder = "[CERTIFICATE_EXPIRATION]"
|
||||||
|
|
||||||
// LengthFunctionPrefix is the prefix for the length function
|
// LengthFunctionPrefix is the prefix for the length function
|
||||||
|
//
|
||||||
|
// Usage: len([BODY].articles) == 10, len([BODY].name) > 5
|
||||||
LengthFunctionPrefix = "len("
|
LengthFunctionPrefix = "len("
|
||||||
|
|
||||||
// PatternFunctionPrefix is the prefix for the pattern function
|
// PatternFunctionPrefix is the prefix for the pattern function
|
||||||
|
//
|
||||||
|
// Usage: pat(192.168.*.*)
|
||||||
PatternFunctionPrefix = "pat("
|
PatternFunctionPrefix = "pat("
|
||||||
|
|
||||||
|
// AnyFunctionPrefix is the prefix for the any function
|
||||||
|
//
|
||||||
|
// Usage: any(1.1.1.1, 1.0.0.1)
|
||||||
|
AnyFunctionPrefix = "any("
|
||||||
|
|
||||||
// FunctionSuffix is the suffix for all functions
|
// FunctionSuffix is the suffix for all functions
|
||||||
FunctionSuffix = ")"
|
FunctionSuffix = ")"
|
||||||
|
|
||||||
@@ -64,51 +72,52 @@ const (
|
|||||||
type Condition string
|
type Condition string
|
||||||
|
|
||||||
// evaluate the Condition with the Result of the health check
|
// evaluate the Condition with the Result of the health check
|
||||||
func (c *Condition) evaluate(result *Result) bool {
|
func (c Condition) evaluate(result *Result) bool {
|
||||||
condition := string(*c)
|
condition := string(c)
|
||||||
success := false
|
success := false
|
||||||
var resolvedCondition string
|
conditionToDisplay := condition
|
||||||
if strings.Contains(condition, "==") {
|
if strings.Contains(condition, "==") {
|
||||||
parts := sanitizeAndResolve(strings.Split(condition, "=="), result)
|
parameters, resolvedParameters := sanitizeAndResolve(strings.Split(condition, "=="), result)
|
||||||
success = isEqual(parts[0], parts[1])
|
success = isEqual(resolvedParameters[0], resolvedParameters[1])
|
||||||
resolvedCondition = fmt.Sprintf("%v == %v", parts[0], parts[1])
|
if !success {
|
||||||
|
conditionToDisplay = prettify(parameters, resolvedParameters, "==")
|
||||||
|
}
|
||||||
} else if strings.Contains(condition, "!=") {
|
} else if strings.Contains(condition, "!=") {
|
||||||
parts := sanitizeAndResolve(strings.Split(condition, "!="), result)
|
parameters, resolvedParameters := sanitizeAndResolve(strings.Split(condition, "!="), result)
|
||||||
success = !isEqual(parts[0], parts[1])
|
success = !isEqual(resolvedParameters[0], resolvedParameters[1])
|
||||||
resolvedCondition = fmt.Sprintf("%v != %v", parts[0], parts[1])
|
if !success {
|
||||||
|
conditionToDisplay = prettify(parameters, resolvedParameters, "!=")
|
||||||
|
}
|
||||||
} else if strings.Contains(condition, "<=") {
|
} else if strings.Contains(condition, "<=") {
|
||||||
parts := sanitizeAndResolveNumerical(strings.Split(condition, "<="), result)
|
parameters, resolvedParameters := sanitizeAndResolveNumerical(strings.Split(condition, "<="), result)
|
||||||
success = parts[0] <= parts[1]
|
success = resolvedParameters[0] <= resolvedParameters[1]
|
||||||
resolvedCondition = fmt.Sprintf("%v <= %v", parts[0], parts[1])
|
if !success {
|
||||||
|
conditionToDisplay = prettifyNumericalParameters(parameters, resolvedParameters, "<=")
|
||||||
|
}
|
||||||
} else if strings.Contains(condition, ">=") {
|
} else if strings.Contains(condition, ">=") {
|
||||||
parts := sanitizeAndResolveNumerical(strings.Split(condition, ">="), result)
|
parameters, resolvedParameters := sanitizeAndResolveNumerical(strings.Split(condition, ">="), result)
|
||||||
success = parts[0] >= parts[1]
|
success = resolvedParameters[0] >= resolvedParameters[1]
|
||||||
resolvedCondition = fmt.Sprintf("%v >= %v", parts[0], parts[1])
|
if !success {
|
||||||
|
conditionToDisplay = prettifyNumericalParameters(parameters, resolvedParameters, ">=")
|
||||||
|
}
|
||||||
} else if strings.Contains(condition, ">") {
|
} else if strings.Contains(condition, ">") {
|
||||||
parts := sanitizeAndResolveNumerical(strings.Split(condition, ">"), result)
|
parameters, resolvedParameters := sanitizeAndResolveNumerical(strings.Split(condition, ">"), result)
|
||||||
success = parts[0] > parts[1]
|
success = resolvedParameters[0] > resolvedParameters[1]
|
||||||
resolvedCondition = fmt.Sprintf("%v > %v", parts[0], parts[1])
|
if !success {
|
||||||
|
conditionToDisplay = prettifyNumericalParameters(parameters, resolvedParameters, ">")
|
||||||
|
}
|
||||||
} else if strings.Contains(condition, "<") {
|
} else if strings.Contains(condition, "<") {
|
||||||
parts := sanitizeAndResolveNumerical(strings.Split(condition, "<"), result)
|
parameters, resolvedParameters := sanitizeAndResolveNumerical(strings.Split(condition, "<"), result)
|
||||||
success = parts[0] < parts[1]
|
success = resolvedParameters[0] < resolvedParameters[1]
|
||||||
resolvedCondition = fmt.Sprintf("%v < %v", parts[0], parts[1])
|
if !success {
|
||||||
|
conditionToDisplay = prettifyNumericalParameters(parameters, resolvedParameters, "<")
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
result.Errors = append(result.Errors, fmt.Sprintf("invalid condition '%s' has been provided", condition))
|
result.Errors = append(result.Errors, fmt.Sprintf("invalid condition '%s' has been provided", condition))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
conditionToDisplay := condition
|
|
||||||
// If the condition isn't a success, return what the resolved condition was too
|
|
||||||
if !success {
|
if !success {
|
||||||
log.Printf("[Condition][evaluate] Condition '%s' did not succeed because '%s' is false", condition, resolvedCondition)
|
//log.Printf("[Condition][evaluate] Condition '%s' did not succeed because '%s' is false", condition, condition)
|
||||||
// Check if the resolved condition was an invalid path
|
|
||||||
isResolvedConditionInvalidPath := strings.ReplaceAll(resolvedCondition, fmt.Sprintf("%s ", InvalidConditionElementSuffix), "") == condition
|
|
||||||
if isResolvedConditionInvalidPath {
|
|
||||||
// Since, in the event of an invalid path, the resolvedCondition contains the condition itself,
|
|
||||||
// we'll only display the resolvedCondition
|
|
||||||
conditionToDisplay = resolvedCondition
|
|
||||||
} else {
|
|
||||||
conditionToDisplay = fmt.Sprintf("%s (%s)", condition, resolvedCondition)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
result.ConditionResults = append(result.ConditionResults, &ConditionResult{Condition: conditionToDisplay, Success: success})
|
result.ConditionResults = append(result.ConditionResults, &ConditionResult{Condition: conditionToDisplay, Success: success})
|
||||||
return success
|
return success
|
||||||
@@ -116,33 +125,66 @@ func (c *Condition) evaluate(result *Result) bool {
|
|||||||
|
|
||||||
// isEqual compares two strings.
|
// isEqual compares two strings.
|
||||||
//
|
//
|
||||||
// It also supports the pattern function. That is to say, if one of the strings starts with PatternFunctionPrefix
|
// Supports the pattern and the any functions.
|
||||||
// and ends with FunctionSuffix, it will be treated like a pattern.
|
// i.e. if one of the parameters starts with PatternFunctionPrefix and ends with FunctionSuffix, it will be treated like
|
||||||
|
// a pattern.
|
||||||
func isEqual(first, second string) bool {
|
func isEqual(first, second string) bool {
|
||||||
var isFirstPattern, isSecondPattern bool
|
firstHasFunctionSuffix := strings.HasSuffix(first, FunctionSuffix)
|
||||||
if strings.HasPrefix(first, PatternFunctionPrefix) && strings.HasSuffix(first, FunctionSuffix) {
|
secondHasFunctionSuffix := strings.HasSuffix(second, FunctionSuffix)
|
||||||
isFirstPattern = true
|
if firstHasFunctionSuffix || secondHasFunctionSuffix {
|
||||||
first = strings.TrimSuffix(strings.TrimPrefix(first, PatternFunctionPrefix), FunctionSuffix)
|
var isFirstPattern, isSecondPattern bool
|
||||||
}
|
if strings.HasPrefix(first, PatternFunctionPrefix) && firstHasFunctionSuffix {
|
||||||
if strings.HasPrefix(second, PatternFunctionPrefix) && strings.HasSuffix(second, FunctionSuffix) {
|
isFirstPattern = true
|
||||||
isSecondPattern = true
|
first = strings.TrimSuffix(strings.TrimPrefix(first, PatternFunctionPrefix), FunctionSuffix)
|
||||||
second = strings.TrimSuffix(strings.TrimPrefix(second, PatternFunctionPrefix), FunctionSuffix)
|
}
|
||||||
}
|
if strings.HasPrefix(second, PatternFunctionPrefix) && secondHasFunctionSuffix {
|
||||||
if isFirstPattern && !isSecondPattern {
|
isSecondPattern = true
|
||||||
return pattern.Match(first, second)
|
second = strings.TrimSuffix(strings.TrimPrefix(second, PatternFunctionPrefix), FunctionSuffix)
|
||||||
} else if !isFirstPattern && isSecondPattern {
|
}
|
||||||
return pattern.Match(second, first)
|
if isFirstPattern && !isSecondPattern {
|
||||||
} else {
|
return pattern.Match(first, second)
|
||||||
return first == second
|
} else if !isFirstPattern && isSecondPattern {
|
||||||
|
return pattern.Match(second, first)
|
||||||
|
}
|
||||||
|
var isFirstAny, isSecondAny bool
|
||||||
|
if strings.HasPrefix(first, AnyFunctionPrefix) && firstHasFunctionSuffix {
|
||||||
|
isFirstAny = true
|
||||||
|
first = strings.TrimSuffix(strings.TrimPrefix(first, AnyFunctionPrefix), FunctionSuffix)
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(second, AnyFunctionPrefix) && secondHasFunctionSuffix {
|
||||||
|
isSecondAny = true
|
||||||
|
second = strings.TrimSuffix(strings.TrimPrefix(second, AnyFunctionPrefix), FunctionSuffix)
|
||||||
|
}
|
||||||
|
if isFirstAny && !isSecondAny {
|
||||||
|
options := strings.Split(first, ",")
|
||||||
|
for _, option := range options {
|
||||||
|
if strings.TrimSpace(option) == second {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
} else if !isFirstAny && isSecondAny {
|
||||||
|
options := strings.Split(second, ",")
|
||||||
|
for _, option := range options {
|
||||||
|
if strings.TrimSpace(option) == first {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return first == second
|
||||||
}
|
}
|
||||||
|
|
||||||
// sanitizeAndResolve sanitizes and resolves a list of element and returns the list of resolved elements
|
// sanitizeAndResolve sanitizes and resolves a list of elements and returns the list of parameters as well as a list
|
||||||
func sanitizeAndResolve(list []string, result *Result) []string {
|
// of resolved parameters
|
||||||
var sanitizedList []string
|
func sanitizeAndResolve(elements []string, result *Result) ([]string, []string) {
|
||||||
|
parameters := make([]string, len(elements))
|
||||||
|
resolvedParameters := make([]string, len(elements))
|
||||||
body := strings.TrimSpace(string(result.Body))
|
body := strings.TrimSpace(string(result.Body))
|
||||||
for _, element := range list {
|
for i, element := range elements {
|
||||||
element = strings.TrimSpace(element)
|
element = strings.TrimSpace(element)
|
||||||
|
parameters[i] = element
|
||||||
switch strings.ToUpper(element) {
|
switch strings.ToUpper(element) {
|
||||||
case StatusPlaceholder:
|
case StatusPlaceholder:
|
||||||
element = strconv.Itoa(result.HTTPStatus)
|
element = strconv.Itoa(result.HTTPStatus)
|
||||||
@@ -166,42 +208,68 @@ func sanitizeAndResolve(list []string, result *Result) []string {
|
|||||||
wantLength = true
|
wantLength = true
|
||||||
element = strings.TrimSuffix(strings.TrimPrefix(element, LengthFunctionPrefix), FunctionSuffix)
|
element = strings.TrimSuffix(strings.TrimPrefix(element, LengthFunctionPrefix), FunctionSuffix)
|
||||||
}
|
}
|
||||||
resolvedElement, resolvedElementLength, err := jsonpath.Eval(strings.Replace(element, fmt.Sprintf("%s.", BodyPlaceholder), "", 1), result.Body)
|
resolvedElement, resolvedElementLength, err := jsonpath.Eval(strings.TrimPrefix(element, BodyPlaceholder+"."), result.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err.Error() != "unexpected end of JSON input" {
|
if err.Error() != "unexpected end of JSON input" {
|
||||||
result.Errors = append(result.Errors, err.Error())
|
result.Errors = append(result.Errors, err.Error())
|
||||||
}
|
}
|
||||||
if wantLength {
|
if wantLength {
|
||||||
element = fmt.Sprintf("%s%s%s %s", LengthFunctionPrefix, element, FunctionSuffix, InvalidConditionElementSuffix)
|
element = LengthFunctionPrefix + element + FunctionSuffix + " " + InvalidConditionElementSuffix
|
||||||
} else {
|
} else {
|
||||||
element = fmt.Sprintf("%s %s", element, InvalidConditionElementSuffix)
|
element = element + " " + InvalidConditionElementSuffix
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if wantLength {
|
if wantLength {
|
||||||
element = fmt.Sprintf("%d", resolvedElementLength)
|
element = strconv.Itoa(resolvedElementLength)
|
||||||
} else {
|
} else {
|
||||||
element = resolvedElement
|
element = resolvedElement
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sanitizedList = append(sanitizedList, element)
|
resolvedParameters[i] = element
|
||||||
}
|
}
|
||||||
return sanitizedList
|
return parameters, resolvedParameters
|
||||||
}
|
}
|
||||||
|
|
||||||
func sanitizeAndResolveNumerical(list []string, result *Result) []int64 {
|
func sanitizeAndResolveNumerical(list []string, result *Result) (parameters []string, resolvedNumericalParameters []int64) {
|
||||||
var sanitizedNumbers []int64
|
parameters, resolvedParameters := sanitizeAndResolve(list, result)
|
||||||
sanitizedList := sanitizeAndResolve(list, result)
|
for _, element := range resolvedParameters {
|
||||||
for _, element := range sanitizedList {
|
|
||||||
if duration, err := time.ParseDuration(element); duration != 0 && err == nil {
|
if duration, err := time.ParseDuration(element); duration != 0 && err == nil {
|
||||||
sanitizedNumbers = append(sanitizedNumbers, duration.Milliseconds())
|
resolvedNumericalParameters = append(resolvedNumericalParameters, duration.Milliseconds())
|
||||||
} else if number, err := strconv.ParseInt(element, 10, 64); err != nil {
|
} else if number, err := strconv.ParseInt(element, 10, 64); err != nil {
|
||||||
// Default to 0 if the string couldn't be converted to an integer
|
// Default to 0 if the string couldn't be converted to an integer
|
||||||
sanitizedNumbers = append(sanitizedNumbers, 0)
|
resolvedNumericalParameters = append(resolvedNumericalParameters, 0)
|
||||||
} else {
|
} else {
|
||||||
sanitizedNumbers = append(sanitizedNumbers, number)
|
resolvedNumericalParameters = append(resolvedNumericalParameters, number)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sanitizedNumbers
|
return parameters, resolvedNumericalParameters
|
||||||
|
}
|
||||||
|
|
||||||
|
func prettifyNumericalParameters(parameters []string, resolvedParameters []int64, operator string) string {
|
||||||
|
return prettify(parameters, []string{strconv.Itoa(int(resolvedParameters[0])), strconv.Itoa(int(resolvedParameters[1]))}, operator)
|
||||||
|
}
|
||||||
|
|
||||||
|
// XXX: make this configurable? i.e. show-resolved-conditions-on-failure
|
||||||
|
func prettify(parameters []string, resolvedParameters []string, operator string) string {
|
||||||
|
// Since, in the event of an invalid path, the resolvedParameters also contain the condition itself,
|
||||||
|
// we'll return the resolvedParameters as-is.
|
||||||
|
if strings.HasSuffix(resolvedParameters[0], InvalidConditionElementSuffix) || strings.HasSuffix(resolvedParameters[1], InvalidConditionElementSuffix) {
|
||||||
|
return resolvedParameters[0] + " " + operator + " " + resolvedParameters[1]
|
||||||
|
}
|
||||||
|
// First element is a placeholder
|
||||||
|
if parameters[0] != resolvedParameters[0] && parameters[1] == resolvedParameters[1] {
|
||||||
|
return parameters[0] + " (" + resolvedParameters[0] + ") " + operator + " " + parameters[1]
|
||||||
|
}
|
||||||
|
// Second element is a placeholder
|
||||||
|
if parameters[0] == resolvedParameters[0] && parameters[1] != resolvedParameters[1] {
|
||||||
|
return parameters[0] + " " + operator + " " + parameters[1] + " (" + resolvedParameters[1] + ")"
|
||||||
|
}
|
||||||
|
// Both elements are placeholders...?
|
||||||
|
if parameters[0] != resolvedParameters[0] && parameters[1] != resolvedParameters[1] {
|
||||||
|
return parameters[0] + " (" + resolvedParameters[0] + ") " + operator + " " + parameters[1] + " (" + resolvedParameters[1] + ")"
|
||||||
|
}
|
||||||
|
// Neither elements are placeholders
|
||||||
|
return parameters[0] + " " + operator + " " + parameters[1]
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func BenchmarkCondition_evaluateWithBodyStringAny(b *testing.B) {
|
||||||
|
condition := Condition("[BODY].name == any(john.doe, jane.doe)")
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
result := &Result{Body: []byte("{\"name\": \"john.doe\"}")}
|
||||||
|
condition.evaluate(result)
|
||||||
|
}
|
||||||
|
b.ReportAllocs()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkCondition_evaluateWithBodyStringAnyFailure(b *testing.B) {
|
||||||
|
condition := Condition("[BODY].name == any(john.doe, jane.doe)")
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
result := &Result{Body: []byte("{\"name\": \"bob.doe\"}")}
|
||||||
|
condition.evaluate(result)
|
||||||
|
}
|
||||||
|
b.ReportAllocs()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkCondition_evaluateWithBodyString(b *testing.B) {
|
||||||
|
condition := Condition("[BODY].name == john.doe")
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
result := &Result{Body: []byte("{\"name\": \"john.doe\"}")}
|
||||||
|
condition.evaluate(result)
|
||||||
|
}
|
||||||
|
b.ReportAllocs()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkCondition_evaluateWithBodyStringFailure(b *testing.B) {
|
||||||
|
condition := Condition("[BODY].name == john.doe")
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
result := &Result{Body: []byte("{\"name\": \"bob.doe\"}")}
|
||||||
|
condition.evaluate(result)
|
||||||
|
}
|
||||||
|
b.ReportAllocs()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkCondition_evaluateWithBodyStringLen(b *testing.B) {
|
||||||
|
condition := Condition("len([BODY].name) == 8")
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
result := &Result{Body: []byte("{\"name\": \"john.doe\"}")}
|
||||||
|
condition.evaluate(result)
|
||||||
|
}
|
||||||
|
b.ReportAllocs()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkCondition_evaluateWithBodyStringLenFailure(b *testing.B) {
|
||||||
|
condition := Condition("len([BODY].name) == 8")
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
result := &Result{Body: []byte("{\"name\": \"bob.doe\"}")}
|
||||||
|
condition.evaluate(result)
|
||||||
|
}
|
||||||
|
b.ReportAllocs()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkCondition_evaluateWithStatus(b *testing.B) {
|
||||||
|
condition := Condition("[STATUS] == 200")
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
result := &Result{HTTPStatus: 200}
|
||||||
|
condition.evaluate(result)
|
||||||
|
}
|
||||||
|
b.ReportAllocs()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkCondition_evaluateWithStatusFailure(b *testing.B) {
|
||||||
|
condition := Condition("[STATUS] == 200")
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
result := &Result{HTTPStatus: 400}
|
||||||
|
condition.evaluate(result)
|
||||||
|
}
|
||||||
|
b.ReportAllocs()
|
||||||
|
}
|
||||||
+288
-14
@@ -1,6 +1,7 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -22,6 +23,9 @@ func TestCondition_evaluateWithStatus(t *testing.T) {
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
if result.ConditionResults[0].Condition != string(condition) {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, condition, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithStatusFailure(t *testing.T) {
|
func TestCondition_evaluateWithStatusFailure(t *testing.T) {
|
||||||
@@ -31,6 +35,10 @@ func TestCondition_evaluateWithStatusFailure(t *testing.T) {
|
|||||||
if result.ConditionResults[0].Success {
|
if result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a failure", condition)
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[STATUS] (500) == 200"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithStatusUsingLessThan(t *testing.T) {
|
func TestCondition_evaluateWithStatusUsingLessThan(t *testing.T) {
|
||||||
@@ -40,6 +48,10 @@ func TestCondition_evaluateWithStatusUsingLessThan(t *testing.T) {
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[STATUS] < 300"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithStatusFailureUsingLessThan(t *testing.T) {
|
func TestCondition_evaluateWithStatusFailureUsingLessThan(t *testing.T) {
|
||||||
@@ -49,6 +61,10 @@ func TestCondition_evaluateWithStatusFailureUsingLessThan(t *testing.T) {
|
|||||||
if result.ConditionResults[0].Success {
|
if result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a failure", condition)
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[STATUS] (404) < 300"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithResponseTimeUsingLessThan(t *testing.T) {
|
func TestCondition_evaluateWithResponseTimeUsingLessThan(t *testing.T) {
|
||||||
@@ -58,6 +74,10 @@ func TestCondition_evaluateWithResponseTimeUsingLessThan(t *testing.T) {
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[RESPONSE_TIME] < 500"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithResponseTimeUsingLessThanDuration(t *testing.T) {
|
func TestCondition_evaluateWithResponseTimeUsingLessThanDuration(t *testing.T) {
|
||||||
@@ -67,6 +87,10 @@ func TestCondition_evaluateWithResponseTimeUsingLessThanDuration(t *testing.T) {
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[RESPONSE_TIME] < 1s"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithResponseTimeUsingLessThanInvalid(t *testing.T) {
|
func TestCondition_evaluateWithResponseTimeUsingLessThanInvalid(t *testing.T) {
|
||||||
@@ -76,6 +100,10 @@ func TestCondition_evaluateWithResponseTimeUsingLessThanInvalid(t *testing.T) {
|
|||||||
if result.ConditionResults[0].Success {
|
if result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have failed because the condition has an invalid numerical value that should've automatically resolved to 0", condition)
|
t.Errorf("Condition '%s' should have failed because the condition has an invalid numerical value that should've automatically resolved to 0", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[RESPONSE_TIME] (50) < potato (0)"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithResponseTimeUsingGreaterThan(t *testing.T) {
|
func TestCondition_evaluateWithResponseTimeUsingGreaterThan(t *testing.T) {
|
||||||
@@ -87,6 +115,10 @@ func TestCondition_evaluateWithResponseTimeUsingGreaterThan(t *testing.T) {
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[RESPONSE_TIME] > 500"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithResponseTimeUsingGreaterThanDuration(t *testing.T) {
|
func TestCondition_evaluateWithResponseTimeUsingGreaterThanDuration(t *testing.T) {
|
||||||
@@ -96,6 +128,10 @@ func TestCondition_evaluateWithResponseTimeUsingGreaterThanDuration(t *testing.T
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[RESPONSE_TIME] > 1s"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithResponseTimeUsingGreaterThanOrEqualTo(t *testing.T) {
|
func TestCondition_evaluateWithResponseTimeUsingGreaterThanOrEqualTo(t *testing.T) {
|
||||||
@@ -105,6 +141,10 @@ func TestCondition_evaluateWithResponseTimeUsingGreaterThanOrEqualTo(t *testing.
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[RESPONSE_TIME] >= 500"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithResponseTimeUsingLessThanOrEqualTo(t *testing.T) {
|
func TestCondition_evaluateWithResponseTimeUsingLessThanOrEqualTo(t *testing.T) {
|
||||||
@@ -114,6 +154,10 @@ func TestCondition_evaluateWithResponseTimeUsingLessThanOrEqualTo(t *testing.T)
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[RESPONSE_TIME] <= 500"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithBody(t *testing.T) {
|
func TestCondition_evaluateWithBody(t *testing.T) {
|
||||||
@@ -123,6 +167,10 @@ func TestCondition_evaluateWithBody(t *testing.T) {
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[BODY] == test"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithBodyJSONPath(t *testing.T) {
|
func TestCondition_evaluateWithBodyJSONPath(t *testing.T) {
|
||||||
@@ -132,6 +180,10 @@ func TestCondition_evaluateWithBodyJSONPath(t *testing.T) {
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[BODY].status == UP"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithBodyJSONPathComplex(t *testing.T) {
|
func TestCondition_evaluateWithBodyJSONPathComplex(t *testing.T) {
|
||||||
@@ -141,31 +193,35 @@ func TestCondition_evaluateWithBodyJSONPathComplex(t *testing.T) {
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[BODY].data.name == john"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithInvalidBodyJSONPathComplex(t *testing.T) {
|
func TestCondition_evaluateWithInvalidBodyJSONPathComplex(t *testing.T) {
|
||||||
expectedResolvedCondition := "[BODY].data.name (INVALID) == john"
|
|
||||||
condition := Condition("[BODY].data.name == john")
|
condition := Condition("[BODY].data.name == john")
|
||||||
result := &Result{Body: []byte("{\"data\": {\"id\": 1}}")}
|
result := &Result{Body: []byte("{\"data\": {\"id\": 1}}")}
|
||||||
condition.evaluate(result)
|
condition.evaluate(result)
|
||||||
if result.ConditionResults[0].Success {
|
if result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a failure, because the path was invalid", condition)
|
t.Errorf("Condition '%s' should have been a failure, because the path was invalid", condition)
|
||||||
}
|
}
|
||||||
if result.ConditionResults[0].Condition != expectedResolvedCondition {
|
expectedConditionDisplayed := "[BODY].data.name (INVALID) == john"
|
||||||
t.Errorf("Condition '%s' should have resolved to '%s', but resolved to '%s' instead", condition, expectedResolvedCondition, result.ConditionResults[0].Condition)
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithInvalidBodyJSONPathComplexWithLengthFunction(t *testing.T) {
|
func TestCondition_evaluateWithInvalidBodyJSONPathComplexWithLengthFunction(t *testing.T) {
|
||||||
expectedResolvedCondition := "len([BODY].data.name) (INVALID) == john"
|
|
||||||
condition := Condition("len([BODY].data.name) == john")
|
condition := Condition("len([BODY].data.name) == john")
|
||||||
result := &Result{Body: []byte("{\"data\": {\"id\": 1}}")}
|
result := &Result{Body: []byte("{\"data\": {\"id\": 1}}")}
|
||||||
condition.evaluate(result)
|
condition.evaluate(result)
|
||||||
if result.ConditionResults[0].Success {
|
if result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a failure, because the path was invalid", condition)
|
t.Errorf("Condition '%s' should have been a failure, because the path was invalid", condition)
|
||||||
}
|
}
|
||||||
if result.ConditionResults[0].Condition != expectedResolvedCondition {
|
expectedConditionDisplayed := "len([BODY].data.name) (INVALID) == john"
|
||||||
t.Errorf("Condition '%s' should have resolved to '%s', but resolved to '%s' instead", condition, expectedResolvedCondition, result.ConditionResults[0].Condition)
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,6 +232,10 @@ func TestCondition_evaluateWithBodyJSONPathDoublePlaceholders(t *testing.T) {
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[BODY].user.firstName != [BODY].user.lastName"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithBodyJSONPathDoublePlaceholdersFailure(t *testing.T) {
|
func TestCondition_evaluateWithBodyJSONPathDoublePlaceholdersFailure(t *testing.T) {
|
||||||
@@ -185,6 +245,10 @@ func TestCondition_evaluateWithBodyJSONPathDoublePlaceholdersFailure(t *testing.
|
|||||||
if result.ConditionResults[0].Success {
|
if result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a failure", condition)
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[BODY].user.firstName (john) == [BODY].user.lastName (doe)"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithBodyJSONPathLongInt(t *testing.T) {
|
func TestCondition_evaluateWithBodyJSONPathLongInt(t *testing.T) {
|
||||||
@@ -194,6 +258,10 @@ func TestCondition_evaluateWithBodyJSONPathLongInt(t *testing.T) {
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[BODY].data.id == 1"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithBodyJSONPathComplexInt(t *testing.T) {
|
func TestCondition_evaluateWithBodyJSONPathComplexInt(t *testing.T) {
|
||||||
@@ -203,6 +271,10 @@ func TestCondition_evaluateWithBodyJSONPathComplexInt(t *testing.T) {
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[BODY].data[1].id == 2"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithBodyJSONPathComplexIntUsingGreaterThan(t *testing.T) {
|
func TestCondition_evaluateWithBodyJSONPathComplexIntUsingGreaterThan(t *testing.T) {
|
||||||
@@ -212,6 +284,10 @@ func TestCondition_evaluateWithBodyJSONPathComplexIntUsingGreaterThan(t *testing
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[BODY].data.id > 0"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithBodyJSONPathComplexIntFailureUsingGreaterThan(t *testing.T) {
|
func TestCondition_evaluateWithBodyJSONPathComplexIntFailureUsingGreaterThan(t *testing.T) {
|
||||||
@@ -221,6 +297,10 @@ func TestCondition_evaluateWithBodyJSONPathComplexIntFailureUsingGreaterThan(t *
|
|||||||
if result.ConditionResults[0].Success {
|
if result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a failure", condition)
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[BODY].data.id (1) > 5"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithBodyJSONPathComplexIntUsingLessThan(t *testing.T) {
|
func TestCondition_evaluateWithBodyJSONPathComplexIntUsingLessThan(t *testing.T) {
|
||||||
@@ -230,6 +310,10 @@ func TestCondition_evaluateWithBodyJSONPathComplexIntUsingLessThan(t *testing.T)
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[BODY].data.id < 5"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithBodyJSONPathComplexIntFailureUsingLessThan(t *testing.T) {
|
func TestCondition_evaluateWithBodyJSONPathComplexIntFailureUsingLessThan(t *testing.T) {
|
||||||
@@ -239,6 +323,10 @@ func TestCondition_evaluateWithBodyJSONPathComplexIntFailureUsingLessThan(t *tes
|
|||||||
if result.ConditionResults[0].Success {
|
if result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a failure", condition)
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[BODY].data.id (10) < 5"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithBodySliceLength(t *testing.T) {
|
func TestCondition_evaluateWithBodySliceLength(t *testing.T) {
|
||||||
@@ -248,6 +336,10 @@ func TestCondition_evaluateWithBodySliceLength(t *testing.T) {
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "len([BODY].data) == 3"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithBodyStringLength(t *testing.T) {
|
func TestCondition_evaluateWithBodyStringLength(t *testing.T) {
|
||||||
@@ -257,6 +349,36 @@ func TestCondition_evaluateWithBodyStringLength(t *testing.T) {
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "len([BODY].name) == 8"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithBodyPattern(t *testing.T) {
|
||||||
|
condition := Condition("[BODY] == pat(*john*)")
|
||||||
|
result := &Result{Body: []byte("{\"name\": \"john.doe\"}")}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if !result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
|
}
|
||||||
|
expectedConditionDisplayed := "[BODY] == pat(*john*)"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithReverseBodyPattern(t *testing.T) {
|
||||||
|
condition := Condition("pat(*john*) == [BODY]")
|
||||||
|
result := &Result{Body: []byte("{\"name\": \"john.doe\"}")}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if !result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
|
}
|
||||||
|
expectedConditionDisplayed := "pat(*john*) == [BODY]"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithBodyStringPattern(t *testing.T) {
|
func TestCondition_evaluateWithBodyStringPattern(t *testing.T) {
|
||||||
@@ -266,6 +388,24 @@ func TestCondition_evaluateWithBodyStringPattern(t *testing.T) {
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[BODY].name == pat(*ohn*)"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithBodyHTMLPattern(t *testing.T) {
|
||||||
|
var html = `<!DOCTYPE html><html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body><div id="user">john.doe</div></body></html>`
|
||||||
|
condition := Condition("[BODY] == pat(*<div id=\"user\">john.doe</div>*)")
|
||||||
|
result := &Result{Body: []byte(html)}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if !result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
|
}
|
||||||
|
expectedConditionDisplayed := "[BODY] == pat(*<div id=\"user\">john.doe</div>*)"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithBodyStringPatternFailure(t *testing.T) {
|
func TestCondition_evaluateWithBodyStringPatternFailure(t *testing.T) {
|
||||||
@@ -275,14 +415,9 @@ func TestCondition_evaluateWithBodyStringPatternFailure(t *testing.T) {
|
|||||||
if result.ConditionResults[0].Success {
|
if result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a failure", condition)
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
}
|
}
|
||||||
}
|
expectedConditionDisplayed := "[BODY].name (john.doe) == pat(bob*)"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
func TestCondition_evaluateWithBodyPatternFailure(t *testing.T) {
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
condition := Condition("[BODY] == pat(*john*)")
|
|
||||||
result := &Result{Body: []byte("{\"name\": \"john.doe\"}")}
|
|
||||||
condition.evaluate(result)
|
|
||||||
if !result.ConditionResults[0].Success {
|
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -293,6 +428,10 @@ func TestCondition_evaluateWithIPPattern(t *testing.T) {
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[IP] == pat(10.*)"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithIPPatternFailure(t *testing.T) {
|
func TestCondition_evaluateWithIPPatternFailure(t *testing.T) {
|
||||||
@@ -302,6 +441,10 @@ func TestCondition_evaluateWithIPPatternFailure(t *testing.T) {
|
|||||||
if result.ConditionResults[0].Success {
|
if result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a failure", condition)
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[IP] (255.255.255.255) == pat(10.*)"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithStatusPattern(t *testing.T) {
|
func TestCondition_evaluateWithStatusPattern(t *testing.T) {
|
||||||
@@ -311,6 +454,10 @@ func TestCondition_evaluateWithStatusPattern(t *testing.T) {
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[STATUS] == pat(4*)"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithStatusPatternFailure(t *testing.T) {
|
func TestCondition_evaluateWithStatusPatternFailure(t *testing.T) {
|
||||||
@@ -320,6 +467,105 @@ func TestCondition_evaluateWithStatusPatternFailure(t *testing.T) {
|
|||||||
if result.ConditionResults[0].Success {
|
if result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a failure", condition)
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[STATUS] (404) != pat(4*)"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithBodyStringAny(t *testing.T) {
|
||||||
|
condition := Condition("[BODY].name == any(john.doe, jane.doe)")
|
||||||
|
expectedConditionDisplayed := "[BODY].name == any(john.doe, jane.doe)"
|
||||||
|
results := []*Result{
|
||||||
|
{Body: []byte("{\"name\": \"john.doe\"}")},
|
||||||
|
{Body: []byte("{\"name\": \"jane.doe\"}")},
|
||||||
|
}
|
||||||
|
for _, result := range results {
|
||||||
|
success := condition.evaluate(result)
|
||||||
|
if !success || !result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
|
}
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithBodyStringAnyFailure(t *testing.T) {
|
||||||
|
condition := Condition("[BODY].name == any(john.doe, jane.doe)")
|
||||||
|
result := &Result{Body: []byte("{\"name\": \"bob.doe\"}")}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
|
}
|
||||||
|
expectedConditionDisplayed := "[BODY].name (bob.doe) == any(john.doe, jane.doe)"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithStatusAny(t *testing.T) {
|
||||||
|
condition := Condition("[STATUS] == any(200, 429)")
|
||||||
|
statuses := []int{200, 429}
|
||||||
|
for _, status := range statuses {
|
||||||
|
result := &Result{HTTPStatus: status}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if !result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
|
}
|
||||||
|
expectedConditionDisplayed := "[STATUS] == any(200, 429)"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithReverseStatusAny(t *testing.T) {
|
||||||
|
condition := Condition("any(200, 429) == [STATUS]")
|
||||||
|
statuses := []int{200, 429}
|
||||||
|
for _, status := range statuses {
|
||||||
|
result := &Result{HTTPStatus: status}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if !result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
|
}
|
||||||
|
expectedConditionDisplayed := "any(200, 429) == [STATUS]"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithStatusAnyFailure(t *testing.T) {
|
||||||
|
condition := Condition("[STATUS] == any(200, 429)")
|
||||||
|
statuses := []int{201, 400, 404, 500}
|
||||||
|
for _, status := range statuses {
|
||||||
|
result := &Result{HTTPStatus: status}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
|
}
|
||||||
|
expectedConditionDisplayed := fmt.Sprintf("[STATUS] (%d) == any(200, 429)", status)
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithReverseStatusAnyFailure(t *testing.T) {
|
||||||
|
condition := Condition("any(200, 429) == [STATUS]")
|
||||||
|
statuses := []int{201, 400, 404, 500}
|
||||||
|
for _, status := range statuses {
|
||||||
|
result := &Result{HTTPStatus: status}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
|
}
|
||||||
|
expectedConditionDisplayed := fmt.Sprintf("any(200, 429) == [STATUS] (%d)", status)
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithConnected(t *testing.T) {
|
func TestCondition_evaluateWithConnected(t *testing.T) {
|
||||||
@@ -329,6 +575,10 @@ func TestCondition_evaluateWithConnected(t *testing.T) {
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[CONNECTED] == true"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithConnectedFailure(t *testing.T) {
|
func TestCondition_evaluateWithConnectedFailure(t *testing.T) {
|
||||||
@@ -338,6 +588,10 @@ func TestCondition_evaluateWithConnectedFailure(t *testing.T) {
|
|||||||
if result.ConditionResults[0].Success {
|
if result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a failure", condition)
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[CONNECTED] (false) == true"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithUnsetCertificateExpiration(t *testing.T) {
|
func TestCondition_evaluateWithUnsetCertificateExpiration(t *testing.T) {
|
||||||
@@ -347,6 +601,10 @@ func TestCondition_evaluateWithUnsetCertificateExpiration(t *testing.T) {
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[CERTIFICATE_EXPIRATION] == 0"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithCertificateExpirationGreaterThanNumerical(t *testing.T) {
|
func TestCondition_evaluateWithCertificateExpirationGreaterThanNumerical(t *testing.T) {
|
||||||
@@ -357,6 +615,10 @@ func TestCondition_evaluateWithCertificateExpirationGreaterThanNumerical(t *test
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[CERTIFICATE_EXPIRATION] > 2419200000"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithCertificateExpirationGreaterThanNumericalFailure(t *testing.T) {
|
func TestCondition_evaluateWithCertificateExpirationGreaterThanNumericalFailure(t *testing.T) {
|
||||||
@@ -367,6 +629,10 @@ func TestCondition_evaluateWithCertificateExpirationGreaterThanNumericalFailure(
|
|||||||
if result.ConditionResults[0].Success {
|
if result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a failure", condition)
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[CERTIFICATE_EXPIRATION] (1209600000) > 2419200000"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithCertificateExpirationGreaterThanDuration(t *testing.T) {
|
func TestCondition_evaluateWithCertificateExpirationGreaterThanDuration(t *testing.T) {
|
||||||
@@ -376,6 +642,10 @@ func TestCondition_evaluateWithCertificateExpirationGreaterThanDuration(t *testi
|
|||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[CERTIFICATE_EXPIRATION] > 12h"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithCertificateExpirationGreaterThanDurationFailure(t *testing.T) {
|
func TestCondition_evaluateWithCertificateExpirationGreaterThanDurationFailure(t *testing.T) {
|
||||||
@@ -385,4 +655,8 @@ func TestCondition_evaluateWithCertificateExpirationGreaterThanDurationFailure(t
|
|||||||
if result.ConditionResults[0].Success {
|
if result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a failure", condition)
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
}
|
}
|
||||||
|
expectedConditionDisplayed := "[CERTIFICATE_EXPIRATION] (86400000) > 48h (172800000)"
|
||||||
|
if result.ConditionResults[0].Condition != expectedConditionDisplayed {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', got '%s'", condition, expectedConditionDisplayed, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
// Event is something that happens at a specific time
|
||||||
|
type Event struct {
|
||||||
|
// Type is the kind of event
|
||||||
|
Type EventType `json:"type"`
|
||||||
|
|
||||||
|
// Timestamp is the moment at which the event happened
|
||||||
|
Timestamp time.Time `json:"timestamp"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// EventType is, uh, the types of events?
|
||||||
|
type EventType string
|
||||||
|
|
||||||
|
var (
|
||||||
|
// EventStart is a type of event that represents when a service starts being monitored
|
||||||
|
EventStart EventType = "START"
|
||||||
|
|
||||||
|
// EventHealthy is a type of event that represents a service passing all of its conditions
|
||||||
|
EventHealthy EventType = "HEALTHY"
|
||||||
|
|
||||||
|
// EventUnhealthy is a type of event that represents a service failing one or more of its conditions
|
||||||
|
EventUnhealthy EventType = "UNHEALTHY"
|
||||||
|
)
|
||||||
+1
-1
@@ -31,7 +31,7 @@ type Result struct {
|
|||||||
Errors []string `json:"errors"`
|
Errors []string `json:"errors"`
|
||||||
|
|
||||||
// ConditionResults results of the service's conditions
|
// ConditionResults results of the service's conditions
|
||||||
ConditionResults []*ConditionResult `json:"condition-results"`
|
ConditionResults []*ConditionResult `json:"conditionResults"`
|
||||||
|
|
||||||
// Success whether the result signifies a success or not
|
// Success whether the result signifies a success or not
|
||||||
Success bool `json:"success"`
|
Success bool `json:"success"`
|
||||||
|
|||||||
+94
-4
@@ -1,5 +1,19 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/TwinProduction/gatus/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// MaximumNumberOfResults is the maximum number of results that ServiceStatus.Results can have
|
||||||
|
MaximumNumberOfResults = 100
|
||||||
|
|
||||||
|
// MaximumNumberOfEvents is the maximum number of events that ServiceStatus.Events can have
|
||||||
|
MaximumNumberOfEvents = 50
|
||||||
|
)
|
||||||
|
|
||||||
// ServiceStatus contains the evaluation Results of a Service
|
// ServiceStatus contains the evaluation Results of a Service
|
||||||
type ServiceStatus struct {
|
type ServiceStatus struct {
|
||||||
// Name of the service
|
// Name of the service
|
||||||
@@ -8,11 +22,25 @@ type ServiceStatus struct {
|
|||||||
// Group the service is a part of. Used for grouping multiple services together on the front end.
|
// Group the service is a part of. Used for grouping multiple services together on the front end.
|
||||||
Group string `json:"group,omitempty"`
|
Group string `json:"group,omitempty"`
|
||||||
|
|
||||||
|
// Key is the key representing the ServiceStatus
|
||||||
|
Key string `json:"key"`
|
||||||
|
|
||||||
// Results is the list of service evaluation results
|
// Results is the list of service evaluation results
|
||||||
Results []*Result `json:"results"`
|
Results []*Result `json:"results"`
|
||||||
|
|
||||||
|
// Events is a list of events
|
||||||
|
//
|
||||||
|
// We don't expose this through JSON, because the main dashboard doesn't need to have this data.
|
||||||
|
// However, the detailed service page does leverage this by including it to a map that will be
|
||||||
|
// marshalled alongside the ServiceStatus.
|
||||||
|
Events []*Event `json:"-"`
|
||||||
|
|
||||||
// Uptime information on the service's uptime
|
// Uptime information on the service's uptime
|
||||||
Uptime *Uptime `json:"uptime"`
|
//
|
||||||
|
// We don't expose this through JSON, because the main dashboard doesn't need to have this data.
|
||||||
|
// However, the detailed service page does leverage this by including it to a map that will be
|
||||||
|
// marshalled alongside the ServiceStatus.
|
||||||
|
Uptime *Uptime `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewServiceStatus creates a new ServiceStatus
|
// NewServiceStatus creates a new ServiceStatus
|
||||||
@@ -20,17 +48,79 @@ func NewServiceStatus(service *Service) *ServiceStatus {
|
|||||||
return &ServiceStatus{
|
return &ServiceStatus{
|
||||||
Name: service.Name,
|
Name: service.Name,
|
||||||
Group: service.Group,
|
Group: service.Group,
|
||||||
|
Key: util.ConvertGroupAndServiceToKey(service.Group, service.Name),
|
||||||
Results: make([]*Result, 0),
|
Results: make([]*Result, 0),
|
||||||
Uptime: NewUptime(),
|
Events: []*Event{{
|
||||||
|
Type: EventStart,
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
}},
|
||||||
|
Uptime: NewUptime(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ShallowCopy creates a shallow copy of ServiceStatus
|
||||||
|
func (ss *ServiceStatus) ShallowCopy() *ServiceStatus {
|
||||||
|
return &ServiceStatus{
|
||||||
|
Name: ss.Name,
|
||||||
|
Group: ss.Group,
|
||||||
|
Key: ss.Key,
|
||||||
|
Results: ss.Results,
|
||||||
|
Events: ss.Events,
|
||||||
|
Uptime: ss.Uptime,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithResultPagination makes a shallow copy of the ServiceStatus with only the results
|
||||||
|
// within the range defined by the page and pageSize parameters
|
||||||
|
func (ss *ServiceStatus) WithResultPagination(page, pageSize int) *ServiceStatus {
|
||||||
|
shallowCopy := ss.ShallowCopy()
|
||||||
|
numberOfResults := len(shallowCopy.Results)
|
||||||
|
start := numberOfResults - (page * pageSize)
|
||||||
|
end := numberOfResults - ((page - 1) * pageSize)
|
||||||
|
if start > numberOfResults {
|
||||||
|
start = -1
|
||||||
|
} else if start < 0 {
|
||||||
|
start = 0
|
||||||
|
}
|
||||||
|
if end > numberOfResults {
|
||||||
|
end = numberOfResults
|
||||||
|
}
|
||||||
|
if start < 0 || end < 0 {
|
||||||
|
shallowCopy.Results = []*Result{}
|
||||||
|
} else {
|
||||||
|
shallowCopy.Results = shallowCopy.Results[start:end]
|
||||||
|
}
|
||||||
|
return shallowCopy
|
||||||
|
}
|
||||||
|
|
||||||
// AddResult adds a Result to ServiceStatus.Results and makes sure that there are
|
// AddResult adds a Result to ServiceStatus.Results and makes sure that there are
|
||||||
// no more than 20 results in the Results slice
|
// no more than 20 results in the Results slice
|
||||||
func (ss *ServiceStatus) AddResult(result *Result) {
|
func (ss *ServiceStatus) AddResult(result *Result) {
|
||||||
|
if len(ss.Results) > 0 {
|
||||||
|
// Check if there's any change since the last result
|
||||||
|
// OR there's only 1 event, which only happens when there's a start event
|
||||||
|
if ss.Results[len(ss.Results)-1].Success != result.Success || len(ss.Events) == 1 {
|
||||||
|
event := &Event{Timestamp: result.Timestamp}
|
||||||
|
if result.Success {
|
||||||
|
event.Type = EventHealthy
|
||||||
|
} else {
|
||||||
|
event.Type = EventUnhealthy
|
||||||
|
}
|
||||||
|
ss.Events = append(ss.Events, event)
|
||||||
|
if len(ss.Events) > MaximumNumberOfEvents {
|
||||||
|
// Doing ss.Events[1:] would usually be sufficient, but in the case where for some reason, the slice has
|
||||||
|
// more than one extra element, we can get rid of all of them at once and thus returning the slice to a
|
||||||
|
// length of MaximumNumberOfEvents by using ss.Events[len(ss.Events)-MaximumNumberOfEvents:] instead
|
||||||
|
ss.Events = ss.Events[len(ss.Events)-MaximumNumberOfEvents:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
ss.Results = append(ss.Results, result)
|
ss.Results = append(ss.Results, result)
|
||||||
if len(ss.Results) > 20 {
|
if len(ss.Results) > MaximumNumberOfResults {
|
||||||
ss.Results = ss.Results[1:]
|
// Doing ss.Results[1:] would usually be sufficient, but in the case where for some reason, the slice has more
|
||||||
|
// than one extra element, we can get rid of all of them at once and thus returning the slice to a length of
|
||||||
|
// MaximumNumberOfResults by using ss.Results[len(ss.Results)-MaximumNumberOfResults:] instead
|
||||||
|
ss.Results = ss.Results[len(ss.Results)-MaximumNumberOfResults:]
|
||||||
}
|
}
|
||||||
ss.Uptime.ProcessResult(result)
|
ss.Uptime.ProcessResult(result)
|
||||||
}
|
}
|
||||||
@@ -14,15 +14,53 @@ func TestNewServiceStatus(t *testing.T) {
|
|||||||
if serviceStatus.Group != service.Group {
|
if serviceStatus.Group != service.Group {
|
||||||
t.Errorf("expected %s, got %s", service.Group, serviceStatus.Group)
|
t.Errorf("expected %s, got %s", service.Group, serviceStatus.Group)
|
||||||
}
|
}
|
||||||
|
if serviceStatus.Key != "group_name" {
|
||||||
|
t.Errorf("expected %s, got %s", "group_name", serviceStatus.Key)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServiceStatus_AddResult(t *testing.T) {
|
func TestServiceStatus_AddResult(t *testing.T) {
|
||||||
service := &Service{Name: "name", Group: "group"}
|
service := &Service{Name: "name", Group: "group"}
|
||||||
serviceStatus := NewServiceStatus(service)
|
serviceStatus := NewServiceStatus(service)
|
||||||
for i := 0; i < 50; i++ {
|
for i := 0; i < MaximumNumberOfResults+10; i++ {
|
||||||
serviceStatus.AddResult(&Result{Timestamp: time.Now()})
|
serviceStatus.AddResult(&Result{Timestamp: time.Now()})
|
||||||
}
|
}
|
||||||
if len(serviceStatus.Results) != 20 {
|
if len(serviceStatus.Results) != MaximumNumberOfResults {
|
||||||
t.Errorf("expected serviceStatus.Results to not exceed a length of 20")
|
t.Errorf("expected serviceStatus.Results to not exceed a length of 20")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestServiceStatus_WithResultPagination(t *testing.T) {
|
||||||
|
service := &Service{Name: "name", Group: "group"}
|
||||||
|
serviceStatus := NewServiceStatus(service)
|
||||||
|
for i := 0; i < 25; i++ {
|
||||||
|
serviceStatus.AddResult(&Result{Timestamp: time.Now()})
|
||||||
|
}
|
||||||
|
if len(serviceStatus.WithResultPagination(1, 1).Results) != 1 {
|
||||||
|
t.Errorf("expected to have 1 result")
|
||||||
|
}
|
||||||
|
if len(serviceStatus.WithResultPagination(5, 0).Results) != 0 {
|
||||||
|
t.Errorf("expected to have 0 results")
|
||||||
|
}
|
||||||
|
if len(serviceStatus.WithResultPagination(-1, 20).Results) != 0 {
|
||||||
|
t.Errorf("expected to have 0 result, because the page was invalid")
|
||||||
|
}
|
||||||
|
if len(serviceStatus.WithResultPagination(1, -1).Results) != 0 {
|
||||||
|
t.Errorf("expected to have 0 result, because the page size was invalid")
|
||||||
|
}
|
||||||
|
if len(serviceStatus.WithResultPagination(1, 10).Results) != 10 {
|
||||||
|
t.Errorf("expected to have 10 results, because given a page size of 10, page 1 should have 10 elements")
|
||||||
|
}
|
||||||
|
if len(serviceStatus.WithResultPagination(2, 10).Results) != 10 {
|
||||||
|
t.Errorf("expected to have 10 results, because given a page size of 10, page 2 should have 10 elements")
|
||||||
|
}
|
||||||
|
if len(serviceStatus.WithResultPagination(3, 10).Results) != 5 {
|
||||||
|
t.Errorf("expected to have 5 results, because given a page size of 10, page 3 should have 5 elements")
|
||||||
|
}
|
||||||
|
if len(serviceStatus.WithResultPagination(4, 10).Results) != 0 {
|
||||||
|
t.Errorf("expected to have 0 results, because given a page size of 10, page 4 should have 0 elements")
|
||||||
|
}
|
||||||
|
if len(serviceStatus.WithResultPagination(1, 50).Results) != 25 {
|
||||||
|
t.Errorf("expected to have 25 results, because there's only 25 results")
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-1
@@ -213,9 +213,10 @@ func (service *Service) call(result *Result) {
|
|||||||
result.Errors = append(result.Errors, err.Error())
|
result.Errors = append(result.Errors, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
if response.TLS != nil && len(response.TLS.PeerCertificates) > 0 {
|
if response.TLS != nil && len(response.TLS.PeerCertificates) > 0 {
|
||||||
certificate := response.TLS.PeerCertificates[0]
|
certificate := response.TLS.PeerCertificates[0]
|
||||||
result.CertificateExpiration = certificate.NotAfter.Sub(time.Now())
|
result.CertificateExpiration = time.Until(certificate.NotAfter)
|
||||||
}
|
}
|
||||||
result.HTTPStatus = response.StatusCode
|
result.HTTPStatus = response.StatusCode
|
||||||
result.Connected = response.StatusCode > 0
|
result.Connected = response.StatusCode > 0
|
||||||
|
|||||||
+17
-12
@@ -25,15 +25,20 @@ type Uptime struct {
|
|||||||
// LastHour is the uptime percentage over the past hour
|
// LastHour is the uptime percentage over the past hour
|
||||||
LastHour float64 `json:"1h"`
|
LastHour float64 `json:"1h"`
|
||||||
|
|
||||||
successCountPerHour map[string]uint64
|
// SuccessCountPerHour is a map containing the number of successes per hour, per timestamp following the
|
||||||
totalCountPerHour map[string]uint64
|
// custom RFC3339WithoutMinutesAndSeconds format
|
||||||
|
SuccessCountPerHour map[string]uint64 `json:"-"`
|
||||||
|
|
||||||
|
// TotalCountPerHour is a map containing the total number of checks per hour, per timestamp following the
|
||||||
|
// custom RFC3339WithoutMinutesAndSeconds format
|
||||||
|
TotalCountPerHour map[string]uint64 `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUptime creates a new Uptime
|
// NewUptime creates a new Uptime
|
||||||
func NewUptime() *Uptime {
|
func NewUptime() *Uptime {
|
||||||
return &Uptime{
|
return &Uptime{
|
||||||
successCountPerHour: make(map[string]uint64),
|
SuccessCountPerHour: make(map[string]uint64),
|
||||||
totalCountPerHour: make(map[string]uint64),
|
TotalCountPerHour: make(map[string]uint64),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,16 +47,16 @@ func NewUptime() *Uptime {
|
|||||||
func (uptime *Uptime) ProcessResult(result *Result) {
|
func (uptime *Uptime) ProcessResult(result *Result) {
|
||||||
timestampDateWithHour := result.Timestamp.Format(RFC3339WithoutMinutesAndSeconds)
|
timestampDateWithHour := result.Timestamp.Format(RFC3339WithoutMinutesAndSeconds)
|
||||||
if result.Success {
|
if result.Success {
|
||||||
uptime.successCountPerHour[timestampDateWithHour]++
|
uptime.SuccessCountPerHour[timestampDateWithHour]++
|
||||||
}
|
}
|
||||||
uptime.totalCountPerHour[timestampDateWithHour]++
|
uptime.TotalCountPerHour[timestampDateWithHour]++
|
||||||
// Clean up only when we're starting to have too many useless keys
|
// Clean up only when we're starting to have too many useless keys
|
||||||
// Note that this is only triggered when there are more entries than there should be after
|
// Note that this is only triggered when there are more entries than there should be after
|
||||||
// 10 days, despite the fact that we are deleting everything that's older than 7 days.
|
// 10 days, despite the fact that we are deleting everything that's older than 7 days.
|
||||||
// This is to prevent re-iterating on every `ProcessResult` as soon as the uptime has been logged for 7 days.
|
// This is to prevent re-iterating on every `ProcessResult` as soon as the uptime has been logged for 7 days.
|
||||||
if len(uptime.totalCountPerHour) > numberOfHoursInTenDays {
|
if len(uptime.TotalCountPerHour) > numberOfHoursInTenDays {
|
||||||
sevenDaysAgo := time.Now().Add(-(sevenDays + time.Hour))
|
sevenDaysAgo := time.Now().Add(-(sevenDays + time.Hour))
|
||||||
for k := range uptime.totalCountPerHour {
|
for k := range uptime.TotalCountPerHour {
|
||||||
dateWithHour, err := time.Parse(time.RFC3339, k)
|
dateWithHour, err := time.Parse(time.RFC3339, k)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// This shouldn't happen, but we'll log it in case it does happen
|
// This shouldn't happen, but we'll log it in case it does happen
|
||||||
@@ -59,8 +64,8 @@ func (uptime *Uptime) ProcessResult(result *Result) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if sevenDaysAgo.Unix() > dateWithHour.Unix() {
|
if sevenDaysAgo.Unix() > dateWithHour.Unix() {
|
||||||
delete(uptime.totalCountPerHour, k)
|
delete(uptime.TotalCountPerHour, k)
|
||||||
delete(uptime.successCountPerHour, k)
|
delete(uptime.SuccessCountPerHour, k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -88,8 +93,8 @@ func (uptime *Uptime) recalculate() {
|
|||||||
timestamp := now.Add(-sevenDays)
|
timestamp := now.Add(-sevenDays)
|
||||||
for now.Sub(timestamp) >= 0 {
|
for now.Sub(timestamp) >= 0 {
|
||||||
timestampDateWithHour := timestamp.Format(RFC3339WithoutMinutesAndSeconds)
|
timestampDateWithHour := timestamp.Format(RFC3339WithoutMinutesAndSeconds)
|
||||||
successCountForTimestamp := uptime.successCountPerHour[timestampDateWithHour]
|
successCountForTimestamp := uptime.SuccessCountPerHour[timestampDateWithHour]
|
||||||
totalCountForTimestamp := uptime.totalCountPerHour[timestampDateWithHour]
|
totalCountForTimestamp := uptime.TotalCountPerHour[timestampDateWithHour]
|
||||||
uptimeBrackets["7d_success"] += successCountForTimestamp
|
uptimeBrackets["7d_success"] += successCountForTimestamp
|
||||||
uptimeBrackets["7d_total"] += totalCountForTimestamp
|
uptimeBrackets["7d_total"] += totalCountForTimestamp
|
||||||
if now.Sub(timestamp) <= 24*time.Hour {
|
if now.Sub(timestamp) <= 24*time.Hour {
|
||||||
|
|||||||
+2
-2
@@ -51,8 +51,8 @@ func TestServiceStatus_AddResultUptimeIsCleaningUpAfterItself(t *testing.T) {
|
|||||||
timestamp := now.Add(-12 * 24 * time.Hour)
|
timestamp := now.Add(-12 * 24 * time.Hour)
|
||||||
for timestamp.Unix() <= now.Unix() {
|
for timestamp.Unix() <= now.Unix() {
|
||||||
serviceStatus.AddResult(&Result{Timestamp: timestamp, Success: true})
|
serviceStatus.AddResult(&Result{Timestamp: timestamp, Success: true})
|
||||||
if len(serviceStatus.Uptime.successCountPerHour) > numberOfHoursInTenDays {
|
if len(serviceStatus.Uptime.SuccessCountPerHour) > numberOfHoursInTenDays {
|
||||||
t.Errorf("At no point in time should there be more than %d entries in serviceStatus.successCountPerHour", numberOfHoursInTenDays)
|
t.Errorf("At no point in time should there be more than %d entries in serviceStatus.SuccessCountPerHour", numberOfHoursInTenDays)
|
||||||
}
|
}
|
||||||
//fmt.Printf("timestamp=%s; uptimeDuringLastHour=%f; timeAgo=%s\n", timestamp.Format(time.RFC3339), serviceStatus.UptimeDuringLastHour, time.Since(timestamp))
|
//fmt.Printf("timestamp=%s; uptimeDuringLastHour=%f; timeAgo=%s\n", timestamp.Format(time.RFC3339), serviceStatus.UptimeDuringLastHour, time.Since(timestamp))
|
||||||
if now.Sub(timestamp) > time.Hour && serviceStatus.Uptime.LastHour != 0 {
|
if now.Sub(timestamp) > time.Hour && serviceStatus.Uptime.LastHour != 0 {
|
||||||
|
|||||||
@@ -1,6 +1,16 @@
|
|||||||
|
alerting:
|
||||||
|
mattermost:
|
||||||
|
webhook-url: "http://mattermost:8065/hooks/tokengoeshere"
|
||||||
|
insecure: true
|
||||||
|
|
||||||
services:
|
services:
|
||||||
- name: example
|
- name: example
|
||||||
url: http://example.org
|
url: http://example.org
|
||||||
interval: 30s
|
interval: 1m
|
||||||
|
alerts:
|
||||||
|
- type: mattermost
|
||||||
|
enabled: true
|
||||||
|
description: "healthcheck failed 3 times in a row"
|
||||||
|
send-on-resolved: true
|
||||||
conditions:
|
conditions:
|
||||||
- "[STATUS] == 200"
|
- "[STATUS] == 200"
|
||||||
@@ -1,13 +1,24 @@
|
|||||||
version: "3.8"
|
version: "3.8"
|
||||||
services:
|
|
||||||
gatus:
|
|
||||||
image: twinproduction/gatus:latest
|
|
||||||
ports:
|
|
||||||
- 8080:8080
|
|
||||||
volumes:
|
|
||||||
- ./config.yaml:/config/config.yaml
|
|
||||||
|
|
||||||
mattermost-preview:
|
services:
|
||||||
image: mattermost/mattermost-preview:latest
|
gatus:
|
||||||
ports:
|
container_name: gatus
|
||||||
- 8065:8065
|
image: twinproduction/gatus:latest
|
||||||
|
ports:
|
||||||
|
- 8080:8080
|
||||||
|
volumes:
|
||||||
|
- ./config.yaml:/config/config.yaml
|
||||||
|
networks:
|
||||||
|
- default
|
||||||
|
|
||||||
|
mattermost:
|
||||||
|
container_name: mattermost
|
||||||
|
image: mattermost/mattermost-preview:5.26.0
|
||||||
|
ports:
|
||||||
|
- 8065:8065
|
||||||
|
networks:
|
||||||
|
- default
|
||||||
|
|
||||||
|
networks:
|
||||||
|
default:
|
||||||
|
driver: bridge
|
||||||
@@ -81,11 +81,11 @@ spec:
|
|||||||
protocol: TCP
|
protocol: TCP
|
||||||
resources:
|
resources:
|
||||||
limits:
|
limits:
|
||||||
cpu: 200m
|
cpu: 250m
|
||||||
memory: 50M
|
memory: 100M
|
||||||
requests:
|
requests:
|
||||||
cpu: 50m
|
cpu: 50m
|
||||||
memory: 20M
|
memory: 30M
|
||||||
volumeMounts:
|
volumeMounts:
|
||||||
- mountPath: /config
|
- mountPath: /config
|
||||||
name: gatus-config
|
name: gatus-config
|
||||||
|
|||||||
@@ -59,11 +59,11 @@ spec:
|
|||||||
protocol: TCP
|
protocol: TCP
|
||||||
resources:
|
resources:
|
||||||
limits:
|
limits:
|
||||||
cpu: 200m
|
cpu: 250m
|
||||||
memory: 50M
|
memory: 100M
|
||||||
requests:
|
requests:
|
||||||
cpu: 50m
|
cpu: 50m
|
||||||
memory: 20M
|
memory: 30M
|
||||||
volumeMounts:
|
volumeMounts:
|
||||||
- mountPath: /config
|
- mountPath: /config
|
||||||
name: gatus-config
|
name: gatus-config
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ go 1.15
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
cloud.google.com/go v0.74.0 // indirect
|
cloud.google.com/go v0.74.0 // indirect
|
||||||
github.com/TwinProduction/gocache v1.1.0
|
github.com/TwinProduction/gocache v1.2.1
|
||||||
|
github.com/TwinProduction/health v1.0.0
|
||||||
github.com/go-ping/ping v0.0.0-20201115131931-3300c582a663
|
github.com/go-ping/ping v0.0.0-20201115131931-3300c582a663
|
||||||
github.com/google/gofuzz v1.2.0 // indirect
|
github.com/google/gofuzz v1.2.0 // indirect
|
||||||
github.com/gorilla/mux v1.8.0
|
github.com/gorilla/mux v1.8.0
|
||||||
|
|||||||
@@ -50,8 +50,10 @@ github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbt
|
|||||||
github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
|
github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
|
||||||
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
|
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
|
||||||
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
|
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
|
||||||
github.com/TwinProduction/gocache v1.1.0 h1:mibBUyccd8kGHlm5dXhTMDOvWBK4mjNqGyOOkG8mib8=
|
github.com/TwinProduction/gocache v1.2.1 h1:NAdMwO9SQEZFmX69YWx6fzhwb6fHakkLri0451c+V1w=
|
||||||
github.com/TwinProduction/gocache v1.1.0/go.mod h1:+qH57V/K4oAcX9C7CvgJTwUX4lzfIUXQC/6XaRSOS1Y=
|
github.com/TwinProduction/gocache v1.2.1/go.mod h1:6zkBoLjrFLkIISwkZTgLy67qliCGSon1xpORM4Ri5HM=
|
||||||
|
github.com/TwinProduction/health v1.0.0 h1:TVyYTAORQQZ8LaptX8jCHZRCGCAO6e+oJx19BUIzQYY=
|
||||||
|
github.com/TwinProduction/health v1.0.0/go.mod h1:ys4mYKUeEfYrWmkm60xLtPjTuLIEDQNBZaTZvenLG1c=
|
||||||
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
|
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
|
||||||
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
|
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
|
||||||
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||||
@@ -73,8 +75,6 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce
|
|||||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||||
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
|
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
|
||||||
github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=
|
|
||||||
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
|
|
||||||
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
|
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
|
||||||
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
|
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
|
||||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||||
@@ -432,6 +432,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
|
|||||||
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||||
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
|
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
|
||||||
|
go.etcd.io/bbolt v1.3.5 h1:XAzx9gjCb0Rxj7EoqcClPD1d5ZBxZJk0jbuoPHenBt0=
|
||||||
|
go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
|
||||||
go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mIZCrds/GIG4ncV9HhK5PX7jPg=
|
go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mIZCrds/GIG4ncV9HhK5PX7jPg=
|
||||||
go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
|
go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
|
||||||
go.opencensus.io v0.20.2/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
|
go.opencensus.io v0.20.2/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
|
||||||
|
|||||||
+4
-4
@@ -16,13 +16,13 @@ import (
|
|||||||
"k8s.io/client-go/tools/clientcmd"
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
)
|
)
|
||||||
|
|
||||||
// KubernetesClientApi is a minimal interface for interacting with Kubernetes
|
// KubernetesClientAPI is a minimal interface for interacting with Kubernetes
|
||||||
// Created mostly to make mocking the Kubernetes client easier
|
// Created mostly to make mocking the Kubernetes client easier
|
||||||
type KubernetesClientApi interface {
|
type KubernetesClientAPI interface {
|
||||||
GetServices(namespace string) ([]v1.Service, error)
|
GetServices(namespace string) ([]v1.Service, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// KubernetesClient is a working implementation of KubernetesClientApi
|
// KubernetesClient is a working implementation of KubernetesClientAPI
|
||||||
type KubernetesClient struct {
|
type KubernetesClient struct {
|
||||||
client *kubernetes.Clientset
|
client *kubernetes.Clientset
|
||||||
}
|
}
|
||||||
@@ -44,7 +44,7 @@ func NewKubernetesClient(client *kubernetes.Clientset) *KubernetesClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewClient creates a Kubernetes client for the given ClusterMode
|
// NewClient creates a Kubernetes client for the given ClusterMode
|
||||||
func NewClient(clusterMode ClusterMode) (KubernetesClientApi, error) {
|
func NewClient(clusterMode ClusterMode) (KubernetesClientAPI, error) {
|
||||||
var kubeConfig *rest.Config
|
var kubeConfig *rest.Config
|
||||||
var err error
|
var err error
|
||||||
switch clusterMode {
|
switch clusterMode {
|
||||||
|
|||||||
+1
-1
@@ -5,6 +5,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// GetKubernetesServices return a list of Services from the given namespace
|
// GetKubernetesServices return a list of Services from the given namespace
|
||||||
func GetKubernetesServices(client KubernetesClientApi, namespace string) ([]v1.Service, error) {
|
func GetKubernetesServices(client KubernetesClientAPI, namespace string) ([]v1.Service, error) {
|
||||||
return client.GetServices(namespace)
|
return client.GetServices(namespace)
|
||||||
}
|
}
|
||||||
@@ -1,17 +1,37 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"github.com/TwinProduction/gatus/config"
|
"github.com/TwinProduction/gatus/config"
|
||||||
"github.com/TwinProduction/gatus/controller"
|
"github.com/TwinProduction/gatus/controller"
|
||||||
|
"github.com/TwinProduction/gatus/storage"
|
||||||
"github.com/TwinProduction/gatus/watchdog"
|
"github.com/TwinProduction/gatus/watchdog"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cfg := loadConfiguration()
|
cfg := loadConfiguration()
|
||||||
go watchdog.Monitor(cfg)
|
go watchdog.Monitor(cfg)
|
||||||
controller.Handle()
|
go controller.Handle()
|
||||||
|
// Wait for termination signal
|
||||||
|
sig := make(chan os.Signal, 1)
|
||||||
|
done := make(chan bool, 1)
|
||||||
|
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
|
||||||
|
go func() {
|
||||||
|
<-sig
|
||||||
|
log.Println("Received termination signal, attempting to gracefully shut down")
|
||||||
|
controller.Shutdown()
|
||||||
|
err := storage.Get().Save()
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Failed to save storage provider:", err.Error())
|
||||||
|
}
|
||||||
|
done <- true
|
||||||
|
}()
|
||||||
|
<-done
|
||||||
|
log.Println("Shutting down")
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadConfiguration() *config.Config {
|
func loadConfiguration() *config.Config {
|
||||||
|
|||||||
+5
-4
@@ -10,10 +10,11 @@ func Match(pattern, s string) bool {
|
|||||||
if pattern == "*" {
|
if pattern == "*" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
// Backslashes break filepath.Match, so we'll remove all of them.
|
// Separators found in the string break filepath.Match, so we'll remove all of them.
|
||||||
// This has a pretty significant impact on performance when there
|
// This has a pretty significant impact on performance when there are separators in
|
||||||
// are backslashes, but at least it doesn't break filepath.Match.
|
// the strings, but at least it doesn't break filepath.Match.
|
||||||
s = strings.ReplaceAll(s, "\\", "")
|
s = strings.ReplaceAll(s, string(filepath.Separator), "")
|
||||||
|
pattern = strings.ReplaceAll(pattern, string(filepath.Separator), "")
|
||||||
matched, _ := filepath.Match(pattern, s)
|
matched, _ := filepath.Match(pattern, s)
|
||||||
return matched
|
return matched
|
||||||
}
|
}
|
||||||
Vendored
-6
@@ -1,6 +0,0 @@
|
|||||||
/*!
|
|
||||||
* Bootstrap v4.3.1 (https://getbootstrap.com/)
|
|
||||||
* Copyright 2011-2019 The Bootstrap Authors
|
|
||||||
* Copyright 2011-2019 Twitter, Inc.
|
|
||||||
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
|
|
||||||
*/:root{--blue:#007bff;--indigo:#6610f2;--purple:#6f42c1;--pink:#e83e8c;--red:#dc3545;--orange:#fd7e14;--yellow:#ffc107;--green:#28a745;--teal:#20c997;--cyan:#17a2b8;--white:#fff;--gray:#6c757d;--gray-dark:#343a40;--primary:#007bff;--secondary:#6c757d;--success:#28a745;--info:#17a2b8;--warning:#ffc107;--danger:#dc3545;--light:#f8f9fa;--dark:#343a40;--breakpoint-xs:0;--breakpoint-sm:576px;--breakpoint-md:768px;--breakpoint-lg:992px;--breakpoint-xl:1200px;--font-family-sans-serif:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";--font-family-monospace:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace}*,::after,::before{box-sizing:border-box}html{font-family:sans-serif;line-height:1.15;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}article,aside,figcaption,figure,footer,header,hgroup,main,nav,section{display:block}body{margin:0;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;text-align:left;background-color:#fff}[tabindex="-1"]:focus{outline:0!important}hr{box-sizing:content-box;height:0;overflow:visible}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem}p{margin-top:0;margin-bottom:1rem}abbr[data-original-title],abbr[title]{text-decoration:underline;-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;border-bottom:0;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:80%}sub,sup{position:relative;font-size:75%;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#007bff;text-decoration:none;background-color:transparent}a:hover{color:#0056b3;text-decoration:underline}a:not([href]):not([tabindex]){color:inherit;text-decoration:none}a:not([href]):not([tabindex]):focus,a:not([href]):not([tabindex]):hover{color:inherit;text-decoration:none}a:not([href]):not([tabindex]):focus{outline:0}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em}pre{margin-top:0;margin-bottom:1rem;overflow:auto}figure{margin:0 0 1rem}img{vertical-align:middle;border-style:none}svg{overflow:hidden;vertical-align:middle}table{border-collapse:collapse}caption{padding-top:.75rem;padding-bottom:.75rem;color:#6c757d;text-align:left;caption-side:bottom}th{text-align:inherit}label{display:inline-block;margin-bottom:.5rem}button{border-radius:0}button:focus{outline:1px dotted;outline:5px auto -webkit-focus-ring-color}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,input{overflow:visible}button,select{text-transform:none}select{word-wrap:normal}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}[type=button]::-moz-focus-inner,[type=reset]::-moz-focus-inner,[type=submit]::-moz-focus-inner,button::-moz-focus-inner{padding:0;border-style:none}input[type=checkbox],input[type=radio]{box-sizing:border-box;padding:0}input[type=date],input[type=datetime-local],input[type=month],input[type=time]{-webkit-appearance:listbox}textarea{overflow:auto;resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{display:block;width:100%;max-width:100%;padding:0;margin-bottom:.5rem;font-size:1.5rem;line-height:inherit;color:inherit;white-space:normal}progress{vertical-align:baseline}[type=number]::-webkit-inner-spin-button,[type=number]::-webkit-outer-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:none}[type=search]::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}summary{display:list-item;cursor:pointer}template{display:none}[hidden]{display:none!important}.h1,.h2,.h3,.h4,.h5,.h6,h1,h2,h3,h4,h5,h6{margin-bottom:.5rem;font-weight:500;line-height:1.2}.h1,h1{font-size:2.5rem}.h2,h2{font-size:2rem}.h3,h3{font-size:1.75rem}.h4,h4{font-size:1.5rem}.h5,h5{font-size:1.25rem}.h6,h6{font-size:1rem}.lead{font-size:1.25rem;font-weight:300}.display-1{font-size:6rem;font-weight:300;line-height:1.2}.display-2{font-size:5.5rem;font-weight:300;line-height:1.2}.display-3{font-size:4.5rem;font-weight:300;line-height:1.2}.display-4{font-size:3.5rem;font-weight:300;line-height:1.2}hr{margin-top:1rem;margin-bottom:1rem;border:0;border-top:1px solid rgba(0,0,0,.1)}.small,Line truncated
|
|
||||||
@@ -1,414 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<title>Health Dashboard</title>
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
||||||
<link rel="stylesheet" href="./bootstrap.min.css" />
|
|
||||||
<style>
|
|
||||||
html, body {
|
|
||||||
background-color: #f7f9fb;
|
|
||||||
}
|
|
||||||
html {
|
|
||||||
height: 100%;
|
|
||||||
}
|
|
||||||
#results div.container:first-child {
|
|
||||||
border-top-left-radius: 3px;
|
|
||||||
border-top-right-radius: 3px;
|
|
||||||
}
|
|
||||||
#results div.container:last-child {
|
|
||||||
border-bottom-left-radius: 3px;
|
|
||||||
border-bottom-right-radius: 3px;
|
|
||||||
border-bottom-width: 1px;
|
|
||||||
border-color: #dee2e6;
|
|
||||||
border-style: solid;
|
|
||||||
}
|
|
||||||
.status {
|
|
||||||
cursor: pointer;
|
|
||||||
transition: all 500ms ease-in-out;
|
|
||||||
overflow-x: hidden;
|
|
||||||
padding: .25em 0;
|
|
||||||
color: white;
|
|
||||||
}
|
|
||||||
.title {
|
|
||||||
font-size: 2.5rem;
|
|
||||||
}
|
|
||||||
.status:hover {
|
|
||||||
opacity: 0.7;
|
|
||||||
transition: opacity 100ms ease-in-out;
|
|
||||||
color: black;
|
|
||||||
}
|
|
||||||
.status-over-time {
|
|
||||||
overflow: auto;
|
|
||||||
}
|
|
||||||
.status-over-time>span:not(:last-child) {
|
|
||||||
margin-left: 2px;
|
|
||||||
}
|
|
||||||
.status-time-ago {
|
|
||||||
color: #6a737d;
|
|
||||||
opacity: 0.5;
|
|
||||||
margin-top: 5px;
|
|
||||||
}
|
|
||||||
.status-min-max-ms {
|
|
||||||
overflow-x: hidden;
|
|
||||||
}
|
|
||||||
#tooltip {
|
|
||||||
position: fixed;
|
|
||||||
top: 0;
|
|
||||||
left: 0;
|
|
||||||
background-color: white;
|
|
||||||
border: 1px solid lightgray;
|
|
||||||
border-radius: 4px;
|
|
||||||
padding: 6px;
|
|
||||||
font-size: 13px;
|
|
||||||
}
|
|
||||||
#tooltip code {
|
|
||||||
color: #212529;
|
|
||||||
line-height: 1;
|
|
||||||
}
|
|
||||||
#tooltip .tooltip-title {
|
|
||||||
font-weight: bold;
|
|
||||||
margin-bottom: 0;
|
|
||||||
display: block;
|
|
||||||
}
|
|
||||||
#tooltip .tooltip-title {
|
|
||||||
margin-top: 8px;
|
|
||||||
}
|
|
||||||
#tooltip>.tooltip-title:first-child {
|
|
||||||
margin-top: 0;
|
|
||||||
}
|
|
||||||
#social {
|
|
||||||
position: fixed;
|
|
||||||
right: 5px;
|
|
||||||
bottom: 5px;
|
|
||||||
padding: 5px;
|
|
||||||
margin: 0;
|
|
||||||
z-index: 100;
|
|
||||||
}
|
|
||||||
#social img {
|
|
||||||
opacity: 0.3;
|
|
||||||
}
|
|
||||||
#social img:hover {
|
|
||||||
opacity: 1;
|
|
||||||
}
|
|
||||||
#settings {
|
|
||||||
position: fixed;
|
|
||||||
left: 5px;
|
|
||||||
bottom: 5px;
|
|
||||||
padding: 5px;
|
|
||||||
}
|
|
||||||
#settings select:focus {
|
|
||||||
box-shadow: none;
|
|
||||||
}
|
|
||||||
.service-group {
|
|
||||||
cursor: pointer;
|
|
||||||
user-select: none;
|
|
||||||
}
|
|
||||||
.service-group h5:hover {
|
|
||||||
color: #1b1e21 !important;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div class="container my-3 rounded p-3 border shadow">
|
|
||||||
<div class="mb-2">
|
|
||||||
<div class="row">
|
|
||||||
<div class="col-8 text-left my-auto">
|
|
||||||
<div class="title display-4">Health Status</div>
|
|
||||||
</div>
|
|
||||||
<div class="col-4 text-right">
|
|
||||||
<img src="logo.png" alt="Gatus" style="position: relative; min-width: 50px; max-width: 200px; width: 20%;"/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div id="results"></div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div id="tooltip" style="display: none">
|
|
||||||
<div class="tooltip-title">Timestamp:</div>
|
|
||||||
<code id="tooltip-timestamp">...</code>
|
|
||||||
<div class="tooltip-title">Response time:</div>
|
|
||||||
<code id="tooltip-response-time">...</code>
|
|
||||||
<div class="tooltip-title">Conditions:</div>
|
|
||||||
<code id="tooltip-conditions">...</code>
|
|
||||||
<div id="tooltip-errors-container">
|
|
||||||
<div class="tooltip-title">Errors:</div>
|
|
||||||
<code id="tooltip-errors">...</code>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<script src="./jquery.min.js"></script>
|
|
||||||
|
|
||||||
<div id="social">
|
|
||||||
<a href="https://github.com/TwinProduction/gatus" target="_blank" title="Gatus on GitHub">
|
|
||||||
<img src="./github.png" alt="GitHub" width="32" height="auto" />
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div id="settings">
|
|
||||||
<div class="input-group input-group-sm">
|
|
||||||
<div class="input-group-prepend">
|
|
||||||
<div class="input-group-text">↻</div>
|
|
||||||
</div>
|
|
||||||
<select class="form-control form-control-sm" id="refresh-rate">
|
|
||||||
<option value="10">10s</option>
|
|
||||||
<option value="30" selected>30s</option>
|
|
||||||
<option value="60">1m</option>
|
|
||||||
<option value="120">2m</option>
|
|
||||||
<option value="300">5m</option>
|
|
||||||
<option value="600">10m</option>
|
|
||||||
</select>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
let serviceStatuses = {};
|
|
||||||
let timerHandler = 0;
|
|
||||||
let refreshIntervalHandler = 0;
|
|
||||||
let userClickedStatus = false;
|
|
||||||
|
|
||||||
// TODO: make this variable configurable and persist the choice in localStorage
|
|
||||||
let showStatusOnHover = true;
|
|
||||||
|
|
||||||
function showTooltip(serviceName, index, element) {
|
|
||||||
//userClickedStatus = false;
|
|
||||||
clearTimeout(timerHandler);
|
|
||||||
let serviceResult = serviceStatuses[serviceName].results[index];
|
|
||||||
$("#tooltip-timestamp").text(prettifyTimestamp(serviceResult.timestamp));
|
|
||||||
$("#tooltip-response-time").text(parseInt(serviceResult.duration/1000000) + "ms");
|
|
||||||
// Populate the condition section
|
|
||||||
let conditions = "";
|
|
||||||
for (let i in serviceResult['condition-results']) {
|
|
||||||
let conditionResult = serviceResult['condition-results'][i];
|
|
||||||
conditions += (conditionResult.success ? "✓" : "X") + " ~ " + htmlEntities(conditionResult.condition) + "<br />";
|
|
||||||
}
|
|
||||||
$("#tooltip-conditions").html(conditions);
|
|
||||||
// Populate the error section only if there are errors
|
|
||||||
if (serviceResult.errors && serviceResult.errors.length > 0) {
|
|
||||||
let errors = "";
|
|
||||||
for (let i in serviceResult.errors) {
|
|
||||||
errors += "- " + htmlEntities(serviceResult.errors[i]) + "<br />";
|
|
||||||
}
|
|
||||||
$("#tooltip-errors").html(errors);
|
|
||||||
$("#tooltip-errors-container").show();
|
|
||||||
} else {
|
|
||||||
$("#tooltip-errors-container").hide();
|
|
||||||
}
|
|
||||||
// Position tooltip
|
|
||||||
$("#tooltip").css({top: "0px", left: "0px"}).show();
|
|
||||||
let targetTopPosition = element.getBoundingClientRect().y + 30;
|
|
||||||
let targetLeftPosition = element.getBoundingClientRect().x;
|
|
||||||
// Make adjustments if necessary
|
|
||||||
let tooltipBoundingClientRect = document.querySelector('#tooltip').getBoundingClientRect();
|
|
||||||
if (targetLeftPosition + window.scrollX + tooltipBoundingClientRect.width + 50 > document.body.getBoundingClientRect().width) {
|
|
||||||
targetLeftPosition = element.getBoundingClientRect().x - tooltipBoundingClientRect.width + element.getBoundingClientRect().width;
|
|
||||||
if (targetLeftPosition < 0) {
|
|
||||||
targetLeftPosition += -targetLeftPosition;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (targetTopPosition + window.scrollY + tooltipBoundingClientRect.height + 50 > document.body.getBoundingClientRect().height && targetTopPosition >= 0) {
|
|
||||||
targetTopPosition = element.getBoundingClientRect().y - (tooltipBoundingClientRect.height + 10);
|
|
||||||
if (targetTopPosition < 0) {
|
|
||||||
targetTopPosition = element.getBoundingClientRect().y + 30;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$("#tooltip").css({top: targetTopPosition + "px", left: targetLeftPosition + "px"});
|
|
||||||
}
|
|
||||||
|
|
||||||
function fadeTooltip() {
|
|
||||||
if (!userClickedStatus) {
|
|
||||||
timerHandler = setTimeout(function () {
|
|
||||||
$("#tooltip").hide();
|
|
||||||
}, 500);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function toggleTooltip(serviceName, index, element) {
|
|
||||||
console.log("userClickedStatus="+userClickedStatus);
|
|
||||||
if (!userClickedStatus) {
|
|
||||||
showTooltip(serviceName, index, element);
|
|
||||||
userClickedStatus = true;
|
|
||||||
} else {
|
|
||||||
$("#tooltip").hide();
|
|
||||||
userClickedStatus = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function createStatusBadge(serviceStatusIndex, index, success) {
|
|
||||||
if (success) {
|
|
||||||
if (showStatusOnHover) {
|
|
||||||
return "<span class='status badge badge-success' style='width: 5%' onmouseenter='showTooltip(\"" + serviceStatusIndex + "\", " + index + ", this)' onmouseleave='fadeTooltip()' onclick='userClickedStatus = !userClickedStatus;'>✓</span>";
|
|
||||||
} else {
|
|
||||||
return "<span class='status badge badge-success' style='width: 5%' onclick='toggleTooltip(\"" + serviceStatusIndex + "\", " + index + ", this)'>✓</span>";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (showStatusOnHover) {
|
|
||||||
return "<span class='status badge badge-danger' style='width: 5%' onmouseenter='showTooltip(\"" + serviceStatusIndex + "\", " + index + ", this)' onmouseleave='fadeTooltip()' onclick='userClickedStatus = !userClickedStatus;'>X</span>";
|
|
||||||
} else {
|
|
||||||
return "<span class='status badge badge-danger' style='width: 5%' onclick='toggleTooltip(\"" + serviceStatusIndex + "\", " + index + ", this)'>X</span>";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function refreshStatuses() {
|
|
||||||
$.getJSON("./api/v1/statuses", function (data) {
|
|
||||||
// Update the table only if there's a change
|
|
||||||
if (JSON.stringify(serviceStatuses) !== JSON.stringify(data)) {
|
|
||||||
serviceStatuses = data;
|
|
||||||
buildTable();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildTable() {
|
|
||||||
let outputByGroup = {};
|
|
||||||
for (let serviceStatusIndex in serviceStatuses) {
|
|
||||||
let serviceStatusOverTime = "";
|
|
||||||
let serviceStatus = serviceStatuses[serviceStatusIndex];
|
|
||||||
let hostname = serviceStatus.results[serviceStatus.results.length-1].hostname;
|
|
||||||
let minResponseTime = null;
|
|
||||||
let maxResponseTime = null;
|
|
||||||
let newestTimestamp = null;
|
|
||||||
let oldestTimestamp = null;
|
|
||||||
for (let resultIndex in serviceStatus.results) {
|
|
||||||
let serviceResult = serviceStatus.results[resultIndex];
|
|
||||||
serviceStatusOverTime = createStatusBadge(serviceStatusIndex, resultIndex, serviceResult.success) + serviceStatusOverTime;
|
|
||||||
const responseTime = parseInt(serviceResult.duration/1000000);
|
|
||||||
if (minResponseTime == null || minResponseTime > responseTime) {
|
|
||||||
minResponseTime = responseTime;
|
|
||||||
}
|
|
||||||
if (maxResponseTime == null || maxResponseTime < responseTime) {
|
|
||||||
maxResponseTime = responseTime;
|
|
||||||
}
|
|
||||||
const timestamp = new Date(serviceResult.timestamp);
|
|
||||||
if (newestTimestamp == null || newestTimestamp < timestamp) {
|
|
||||||
newestTimestamp = timestamp;
|
|
||||||
}
|
|
||||||
if (oldestTimestamp == null || oldestTimestamp > timestamp) {
|
|
||||||
oldestTimestamp = timestamp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let output = ""
|
|
||||||
+ "<div class='container py-3 border-left border-right border-top border-black rounded-0'>"
|
|
||||||
+ " <div class='row mb-2'>"
|
|
||||||
+ " <div class='col-md-10'>"
|
|
||||||
+ " <span class='font-weight-bold'>" + serviceStatus.name + "</span> <span class='text-secondary font-weight-lighter'>- " + hostname + "</span>"
|
|
||||||
+ " </div>"
|
|
||||||
+ " <div class='col-md-2 text-right'>"
|
|
||||||
+ " <span class='font-weight-lighter status-min-max-ms'>" + (minResponseTime === maxResponseTime ? minResponseTime : (minResponseTime + "-" + maxResponseTime)) + "ms</span>"
|
|
||||||
+ " </div>"
|
|
||||||
+ " </div>"
|
|
||||||
+ " <div class='row'>"
|
|
||||||
+ " <div class='col-12 d-flex flex-row-reverse status-over-time'>"
|
|
||||||
+ " " + serviceStatusOverTime
|
|
||||||
+ " </div>"
|
|
||||||
+ " </div>"
|
|
||||||
+ " <div class='row status-time-ago'>"
|
|
||||||
+ " <div class='col-6'>"
|
|
||||||
+ " " + generatePrettyTimeAgo(oldestTimestamp)
|
|
||||||
+ " </div>"
|
|
||||||
+ " <div class='col-6 text-right'>"
|
|
||||||
+ " " + generatePrettyTimeAgo(newestTimestamp)
|
|
||||||
+ " </div>"
|
|
||||||
+ " </div>"
|
|
||||||
+ "</div>";
|
|
||||||
// create an empty entry if this group is new
|
|
||||||
if (!outputByGroup[serviceStatus.group]) {
|
|
||||||
outputByGroup[serviceStatus.group] = "";
|
|
||||||
}
|
|
||||||
outputByGroup[serviceStatus.group] += output;
|
|
||||||
}
|
|
||||||
let output = "";
|
|
||||||
for (let group in outputByGroup) {
|
|
||||||
// Services that don't have a group should be skipped and left for last
|
|
||||||
if (group === 'undefined') {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
let key = group.replace(/[^a-zA-Z0-9]/g, '');
|
|
||||||
let existingGroupContentSelector = $("#service-group-" + key + "-content");
|
|
||||||
let isCurrentlyHidden = existingGroupContentSelector.length && existingGroupContentSelector[0].style.display === 'none';
|
|
||||||
let groupStatus = "<span class='text-success'>✓</span>";
|
|
||||||
if (outputByGroup[group].includes("badge badge-danger")) {
|
|
||||||
groupStatus = "<span class='text-warning'>~</span>";
|
|
||||||
}
|
|
||||||
output += ""
|
|
||||||
+ "<div class='mt-" + (output.length ? '4' : '3') + "'>"
|
|
||||||
+ " <div class='container pt-2 border-left border-right border-top border-black border-bottom service-group' id='service-group-" + key + "' data-group='" + key + "' onclick='toggleGroup(this)'>"
|
|
||||||
+ " <h5 class='text-secondary text-monospace pb-0'>"
|
|
||||||
+ " " + groupStatus + " " + group
|
|
||||||
+ " <span class='float-right service-group-arrow' id='service-group-" + key + "-arrow'>" + (isCurrentlyHidden ? "▼" : "▲") + "</span>"
|
|
||||||
+ " </h5>"
|
|
||||||
+ " </div>"
|
|
||||||
+ " <div class='service-group-content' id='service-group-" + key + "-content' style='" + (isCurrentlyHidden ? "display: none;" : "") + "'>"
|
|
||||||
+ " " + outputByGroup[group]
|
|
||||||
+ " </div>"
|
|
||||||
+ "</div>";
|
|
||||||
}
|
|
||||||
// Add all services that don't have a group at the end
|
|
||||||
if (outputByGroup['undefined']) {
|
|
||||||
output += ""
|
|
||||||
+ "<div class='mt-" + (output.length ? '4' : '3') + "'>"
|
|
||||||
+ " " + outputByGroup['undefined']
|
|
||||||
+ "</div>"
|
|
||||||
}
|
|
||||||
$("#results").html(output);
|
|
||||||
}
|
|
||||||
|
|
||||||
function toggleGroup(element) {
|
|
||||||
let selector = $("#service-group-" + element.dataset.group + "-content");
|
|
||||||
selector.toggle("fast", function() {
|
|
||||||
if (selector.length && selector[0].style.display === 'none') {
|
|
||||||
$("#service-group-" + element.dataset.group + "-arrow").html("▼");
|
|
||||||
} else {
|
|
||||||
$("#service-group-" + element.dataset.group + "-arrow").html("▲");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function prettifyTimestamp(timestamp) {
|
|
||||||
let date = new Date(timestamp);
|
|
||||||
let YYYY = date.getFullYear();
|
|
||||||
let MM = ((date.getMonth()+1)<10?"0":"")+""+(date.getMonth()+1);
|
|
||||||
let DD = ((date.getDate())<10?"0":"")+""+(date.getDate());
|
|
||||||
let hh = ((date.getHours())<10?"0":"")+""+(date.getHours());
|
|
||||||
let mm = ((date.getMinutes())<10?"0":"")+""+(date.getMinutes());
|
|
||||||
let ss = ((date.getSeconds())<10?"0":"")+""+(date.getSeconds());
|
|
||||||
return YYYY + "-" + MM + "-" + DD + " " + hh + ":" + mm + ":" + ss;
|
|
||||||
}
|
|
||||||
|
|
||||||
function generatePrettyTimeAgo(t) {
|
|
||||||
let differenceInMs = new Date().getTime() - new Date(t).getTime();
|
|
||||||
if (differenceInMs > 3600000) {
|
|
||||||
let hours = (differenceInMs/3600000).toFixed(0);
|
|
||||||
return hours + " hour" + (hours !== "1" ? "s" : "") + " ago";
|
|
||||||
}
|
|
||||||
if (differenceInMs > 60000) {
|
|
||||||
let minutes = (differenceInMs/60000).toFixed(0);
|
|
||||||
return minutes + " minute" + (minutes !== "1" ? "s" : "") + " ago";
|
|
||||||
}
|
|
||||||
return (differenceInMs/1000).toFixed(0) + " seconds ago";
|
|
||||||
}
|
|
||||||
|
|
||||||
function htmlEntities(s) {
|
|
||||||
return String(s)
|
|
||||||
.replace(/&/g, '&')
|
|
||||||
.replace(/</g, '<')
|
|
||||||
.replace(/>/g, '>')
|
|
||||||
.replace(/"/g, '"')
|
|
||||||
.replace(/'/g, ''');
|
|
||||||
}
|
|
||||||
|
|
||||||
function setRefreshInterval(seconds) {
|
|
||||||
refreshStatuses();
|
|
||||||
refreshIntervalHandler = setInterval(function() {
|
|
||||||
refreshStatuses();
|
|
||||||
}, seconds * 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
$("#refresh-rate").change(function() {
|
|
||||||
clearInterval(refreshIntervalHandler);
|
|
||||||
setRefreshInterval($(this).val());
|
|
||||||
});
|
|
||||||
setRefreshInterval(30);
|
|
||||||
$("#refresh-rate").val(30);
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
Vendored
-2
@@ -1,2 +0,0 @@
|
|||||||
/*! jQuery v3.3.1 | (c) JS Foundation and other contributors | jquery.org/license */
|
|
||||||
!function(e,t){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=e.document?t(e,!0):function(e){if(!e.document)throw new Error("jQuery requires a window with a document");return t(e)}:t(e)}("undefined"!=typeof window?window:this,function(e,t){"use strict";var n=[],r=e.document,i=Object.getPrototypeOf,o=n.slice,a=n.concat,s=n.push,u=n.indexOf,l={},c=l.toString,f=l.hasOwnProperty,p=f.toString,d=p.call(Object),h={},g=function e(t){return"function"==typeof t&&"number"!=typeof t.nodeType},y=function e(t){return null!=t&&t===t.window},v={type:!0,src:!0,noModule:!0};function m(e,t,n){var i,o=(t=t||r).createElement("script");if(o.text=e,n)for(i in v)n[i]&&(o[i]=n[i]);t.head.appendChild(o).parentNode.removeChild(o)}function x(e){return null==e?e+"":"object"==typeof e||"function"==typeof e?l[c.call(e)]||"object":typeof e}var b="3.3.1",w=function(e,t){return new w.fn.init(e,t)},T=/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;w.fn=w.prototype={jquery:"3.3.1",constructor:w,length:0,toArray:function(){return o.call(this)},get:function(e){return null==e?o.call(this):e<0?this[e+this.length]:this[e]},pushStack:function(e){var t=w.merge(this.constructor(),e);return t.prevObject=this,t},each:function(e){return w.each(this,e)},map:function(e){return this.pushStack(w.map(this,function(t,n){return e.call(t,n,t)}))},slice:function(){return this.pushStack(o.apply(this,arguments))},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},eq:function(e){var t=this.length,n=+e+(e<0?t:0);return this.pushStack(n>=0&&n<t?[this[n]]:[])},end:function(){return this.prevObject||this.constructor()},push:s,sort:n.sort,splice:n.splice},w.extend=w.fn.extend=function(){var e,t,n,r,i,o,a=arguments[0]||{},s=1,u=arguments.length,l=!1;for("boolean"==typeof a&&(l=a,a=arguments[s]||{},s++),"object"==typeof a||g(a)||(a={}),s===u&&(a=this,s--);s<u;s++)if(null!=(e=arguments[s]))for(t in e)n=a[t],a!==(r=e[t])&&(l&&r&&(w.isPlainObject(r)||(i=Array.isArray(r)))?(i?(i=!1,o=n&&Array.isArray(n)?n:[]):o=n&&w.isPlainObject(n)?n:{},a[t]=w.extend(l,o,r)):void 0!==r&&(a[t]=r));return a},w.extend({expando:"jQuery"+("3.3.1"+Math.random()).replace(/\D/g,""),isReady:!0,error:function(e){throw new Error(e)},noop:function(){},isPlainObject:function(e){var t,n;return!(!e||"[object Object]"!==c.call(e))&&(!(t=i(e))||"function"==typeof(n=f.call(t,"constructor")&&t.constructor)&&p.call(n)===d)},isEmptyObject:function(e){var t;for(t in e)return!1;return!0},globalEval:function(e){m(e)},each:function(e,t){var n,r=0;if(C(e)){for(n=e.length;r<n;r++)if(!1===t.call(e[r],r,e[r]))break}else for(r in e)if(!1===t.call(e[r],r,e[r]))break;return e},trim:function(e){return null==e?"":(e+"").replace(T,"")},makeArray:function(e,t){var n=t||[];return null!=e&&(C(Object(e))?w.merge(n,"string"==typeof e?[e]:e):s.call(n,e)),n},inArray:function(e,t,n){return null==t?-1:u.call(t,e,n)},merge:function(e,t){for(var n=+t.length,r=0,i=e.length;r<n;r++)e[i++]=t[r];return e.length=i,e},grep:function(e,t,n){for(var r,i=[],o=0,a=e.length,s=!n;o<a;o++)(r=!t(e[o],o))!==s&&i.push(e[o]);return i},map:function(e,t,n){var r,i,o=0,s=[];if(C(e))for(r=e.length;o<r;o++)null!=(i=t(e[o],o,n))&&s.push(i);else for(o in e)null!=(i=t(e[o],o,n))&&s.push(i);return a.apply([],s)},guid:1,support:h}),"function"==typeof Symbol&&(w.fn[Symbol.iterator]=n[Symbol.iterator]),w.each("Boolean Number String Function Array Date RegExp Object Error Symbol".split(" "),function(e,t){l["[object "+t+"]"]=t.toLowerCase()});function C(e){var t=!!e&&"length"in e&&e.length,n=x(e);return!g(e)&&!y(e)&&("array"===n||0===t||"number"==typeof t&&t>0&&t-1 in e)}var E=function(e){var t,n,r,i,o,a,s,u,l,c,f,p,d,h,g,y,v,m,x,b="sizzle"+1*new Date,w=e.document,T=0,C=0,E=ae(),k=ae(),S=ae(),D=function(e,t){return e===t&&(f=!0),0},N={}.hasOwnProperty,A=[],j=A.pop,q=A.push,L=A.push,H=A.slice,O=function(e,t){for(var n=0,r=e.length;n<r;n++)if(e[n]===t)return n;return-1},P="checked|selected|async|autofocus|autoplay|controls|defer|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped",M="[\\x20\\t\\r\\n\\f]",R="(?:\\\\.|[\\w-]|[^\0-\\xa0])+",I="\\["+M+"*("+R+")(?:"+M+"*([*^$|!~]?=)"+M+"*(?:'((?:\\\\.|[^\\\\'])*)'|\"((?:\\\\.|[^\\\\\"])*)\"|("+R+"))|)"+M+"*\\]",W=":("+R+")(?:\\((('((?:\\\\.|[^\\\\'])*)'|\"((?:\\\\.|[^\\\\\"])*)\")|((?:\\\\.|[^\\\\()[\\]]|"+I+")*)|.*)\\)|)",$=new RegExp(M+"+","g"),B=new RegExp("^"+M+"+|((?:^|[^\\\\])(?:\\\\.)*)"+M+"+$","g"),F=new RegExp("^"+M+"*,"+M+"*"),_=new RegExp("^"+M+"*([>+~]|"+M+")"+M+"*"),z=new RegExp("="+M+"*([^\\]'\"]*?)"+M+"*\\]","g"),X=new RegExp(W),U=new RegExp("^"+R+"$"),V={ID:new RegExp("^#("+R+")"),CLASS:new RegExp("^\\.("+R+")"),TAG:new RegExp("^("+R+"|[*])"),ATTR:new RegExp("^"+I),PSEUDO:new RegExp("^"+W),CHILD:new RegExp("^:(only|first|last|nth|nth-last)-(child|of-type)(?:\\("+M+"*(even|odd|(([+-]|)(\\d*)n|)"+M+"*(?:([+-]|)"+M+"*(\\d+)|))"+M+"*\\)|)","i"),bool:new RegExp("^(?:"+P+")$","i"),needsContext:new RegExp("^"+M+"*[>+~]|:(even|odd|eq|gt|lt|nth|first|last)Line truncated
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 20 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 83 KiB |
@@ -0,0 +1,8 @@
|
|||||||
|
package storage
|
||||||
|
|
||||||
|
// Config is the configuration for alerting providers
|
||||||
|
type Config struct {
|
||||||
|
// File is the path of the file to use for persistence
|
||||||
|
// If blank, persistence is disabled.
|
||||||
|
File string `yaml:"file"`
|
||||||
|
}
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
package storage
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/TwinProduction/gatus/core"
|
|
||||||
)
|
|
||||||
|
|
||||||
// InMemoryStore implements an in-memory store
|
|
||||||
type InMemoryStore struct {
|
|
||||||
serviceStatuses map[string]*core.ServiceStatus
|
|
||||||
serviceResultsMutex sync.RWMutex
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewInMemoryStore returns an in-memory store. Note that the store acts as a singleton, so although new-ing
|
|
||||||
// up in-memory stores will give you a unique reference to a struct each time, all structs returned
|
|
||||||
// by this function will act on the same in-memory store.
|
|
||||||
func NewInMemoryStore() *InMemoryStore {
|
|
||||||
return &InMemoryStore{
|
|
||||||
serviceStatuses: make(map[string]*core.ServiceStatus),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetAllAsJSON returns the JSON encoding of all monitored core.ServiceStatus
|
|
||||||
func (ims *InMemoryStore) GetAllAsJSON() ([]byte, error) {
|
|
||||||
ims.serviceResultsMutex.RLock()
|
|
||||||
serviceStatuses, err := json.Marshal(ims.serviceStatuses)
|
|
||||||
ims.serviceResultsMutex.RUnlock()
|
|
||||||
return serviceStatuses, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetServiceStatus returns the service status for a given service name in the given group
|
|
||||||
func (ims *InMemoryStore) GetServiceStatus(group, name string) *core.ServiceStatus {
|
|
||||||
key := fmt.Sprintf("%s_%s", group, name)
|
|
||||||
ims.serviceResultsMutex.RLock()
|
|
||||||
serviceStatus := ims.serviceStatuses[key]
|
|
||||||
ims.serviceResultsMutex.RUnlock()
|
|
||||||
return serviceStatus
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert inserts the observed result for the specified service into the in memory store
|
|
||||||
func (ims *InMemoryStore) Insert(service *core.Service, result *core.Result) {
|
|
||||||
key := fmt.Sprintf("%s_%s", service.Group, service.Name)
|
|
||||||
ims.serviceResultsMutex.Lock()
|
|
||||||
serviceStatus, exists := ims.serviceStatuses[key]
|
|
||||||
if !exists {
|
|
||||||
serviceStatus = core.NewServiceStatus(service)
|
|
||||||
ims.serviceStatuses[key] = serviceStatus
|
|
||||||
}
|
|
||||||
serviceStatus.AddResult(result)
|
|
||||||
ims.serviceResultsMutex.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clear will empty all the results from the in memory store
|
|
||||||
func (ims *InMemoryStore) Clear() {
|
|
||||||
ims.serviceResultsMutex.Lock()
|
|
||||||
ims.serviceStatuses = make(map[string]*core.ServiceStatus)
|
|
||||||
ims.serviceResultsMutex.Unlock()
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/TwinProduction/gatus/storage/store"
|
||||||
|
"github.com/TwinProduction/gatus/storage/store/memory"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
provider store.Store
|
||||||
|
|
||||||
|
// initialized keeps track of whether the storage provider was initialized
|
||||||
|
// Because store.Store is an interface, a nil check wouldn't be sufficient, so instead of doing reflection
|
||||||
|
// every single time Get is called, we'll just lazily keep track of its existence through this variable
|
||||||
|
initialized bool
|
||||||
|
)
|
||||||
|
|
||||||
|
// Get retrieves the storage provider
|
||||||
|
func Get() store.Store {
|
||||||
|
if !initialized {
|
||||||
|
log.Println("[storage][Get] Provider requested before it was initialized, automatically initializing")
|
||||||
|
err := Initialize(nil)
|
||||||
|
if err != nil {
|
||||||
|
panic("failed to automatically initialize store: " + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return provider
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize instantiates the storage provider based on the Config provider
|
||||||
|
func Initialize(cfg *Config) error {
|
||||||
|
initialized = true
|
||||||
|
var err error
|
||||||
|
if cfg == nil || len(cfg.File) == 0 {
|
||||||
|
log.Println("[storage][Initialize] Creating storage provider")
|
||||||
|
provider, err = memory.NewStore("")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Printf("[storage][Initialize] Creating storage provider with file=%s", cfg.File)
|
||||||
|
provider, err = memory.NewStore(cfg.File)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
go autoSave(7 * time.Minute)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// autoSave automatically calls the Save function of the provider at every interval
|
||||||
|
func autoSave(interval time.Duration) {
|
||||||
|
for {
|
||||||
|
time.Sleep(interval)
|
||||||
|
log.Printf("[storage][autoSave] Saving")
|
||||||
|
err := provider.Save()
|
||||||
|
if err != nil {
|
||||||
|
log.Println("[storage][autoSave] Save failed:", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
package memory
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/gob"
|
||||||
|
|
||||||
|
"github.com/TwinProduction/gatus/core"
|
||||||
|
"github.com/TwinProduction/gatus/util"
|
||||||
|
"github.com/TwinProduction/gocache"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
gob.Register(&core.ServiceStatus{})
|
||||||
|
gob.Register(&core.Uptime{})
|
||||||
|
gob.Register(&core.Result{})
|
||||||
|
gob.Register(&core.Event{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store that leverages gocache
|
||||||
|
type Store struct {
|
||||||
|
file string
|
||||||
|
cache *gocache.Cache
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewStore creates a new store
|
||||||
|
func NewStore(file string) (*Store, error) {
|
||||||
|
store := &Store{
|
||||||
|
file: file,
|
||||||
|
cache: gocache.NewCache().WithMaxSize(gocache.NoMaxSize),
|
||||||
|
}
|
||||||
|
if len(file) > 0 {
|
||||||
|
_, err := store.cache.ReadFromFile(file)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return store, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAllServiceStatusesWithResultPagination returns all monitored core.ServiceStatus
|
||||||
|
// with a subset of core.Result defined by the page and pageSize parameters
|
||||||
|
func (s *Store) GetAllServiceStatusesWithResultPagination(page, pageSize int) map[string]*core.ServiceStatus {
|
||||||
|
serviceStatuses := s.cache.GetAll()
|
||||||
|
pagedServiceStatuses := make(map[string]*core.ServiceStatus, len(serviceStatuses))
|
||||||
|
for k, v := range serviceStatuses {
|
||||||
|
pagedServiceStatuses[k] = v.(*core.ServiceStatus).WithResultPagination(page, pageSize)
|
||||||
|
}
|
||||||
|
return pagedServiceStatuses
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetServiceStatus returns the service status for a given service name in the given group
|
||||||
|
func (s *Store) GetServiceStatus(groupName, serviceName string) *core.ServiceStatus {
|
||||||
|
return s.GetServiceStatusByKey(util.ConvertGroupAndServiceToKey(groupName, serviceName))
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetServiceStatusByKey returns the service status for a given key
|
||||||
|
func (s *Store) GetServiceStatusByKey(key string) *core.ServiceStatus {
|
||||||
|
serviceStatus := s.cache.GetValue(key)
|
||||||
|
if serviceStatus == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return serviceStatus.(*core.ServiceStatus).ShallowCopy()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert adds the observed result for the specified service into the store
|
||||||
|
func (s *Store) Insert(service *core.Service, result *core.Result) {
|
||||||
|
key := util.ConvertGroupAndServiceToKey(service.Group, service.Name)
|
||||||
|
serviceStatus, exists := s.cache.Get(key)
|
||||||
|
if !exists {
|
||||||
|
serviceStatus = core.NewServiceStatus(service)
|
||||||
|
}
|
||||||
|
serviceStatus.(*core.ServiceStatus).AddResult(result)
|
||||||
|
s.cache.Set(key, serviceStatus)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteAllServiceStatusesNotInKeys removes all ServiceStatus that are not within the keys provided
|
||||||
|
func (s *Store) DeleteAllServiceStatusesNotInKeys(keys []string) int {
|
||||||
|
var keysToDelete []string
|
||||||
|
for _, existingKey := range s.cache.GetKeysByPattern("*", 0) {
|
||||||
|
shouldDelete := true
|
||||||
|
for _, key := range keys {
|
||||||
|
if existingKey == key {
|
||||||
|
shouldDelete = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if shouldDelete {
|
||||||
|
keysToDelete = append(keysToDelete, existingKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return s.cache.DeleteAll(keysToDelete)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear deletes everything from the store
|
||||||
|
func (s *Store) Clear() {
|
||||||
|
s.cache.Clear()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save persists the cache to the store file
|
||||||
|
func (s *Store) Save() error {
|
||||||
|
if len(s.file) > 0 {
|
||||||
|
return s.cache.SaveToFile(s.file)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package storage
|
package memory
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/TwinProduction/gatus/core"
|
"github.com/TwinProduction/gatus/core"
|
||||||
|
"github.com/TwinProduction/gatus/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -82,17 +83,17 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestInMemoryStore_Insert(t *testing.T) {
|
func TestStore_Insert(t *testing.T) {
|
||||||
store := NewInMemoryStore()
|
store, _ := NewStore("")
|
||||||
store.Insert(&testService, &testSuccessfulResult)
|
store.Insert(&testService, &testSuccessfulResult)
|
||||||
store.Insert(&testService, &testUnsuccessfulResult)
|
store.Insert(&testService, &testUnsuccessfulResult)
|
||||||
|
|
||||||
if len(store.serviceStatuses) != 1 {
|
if store.cache.Count() != 1 {
|
||||||
t.Fatalf("expected 1 ServiceStatus, got %d", len(store.serviceStatuses))
|
t.Fatalf("expected 1 ServiceStatus, got %d", store.cache.Count())
|
||||||
}
|
}
|
||||||
key := fmt.Sprintf("%s_%s", testService.Group, testService.Name)
|
key := fmt.Sprintf("%s_%s", testService.Group, testService.Name)
|
||||||
serviceStatus, exists := store.serviceStatuses[key]
|
serviceStatus := store.GetServiceStatusByKey(key)
|
||||||
if !exists {
|
if serviceStatus == nil {
|
||||||
t.Fatalf("Store should've had key '%s', but didn't", key)
|
t.Fatalf("Store should've had key '%s', but didn't", key)
|
||||||
}
|
}
|
||||||
if len(serviceStatus.Results) != 2 {
|
if len(serviceStatus.Results) != 2 {
|
||||||
@@ -139,8 +140,8 @@ func TestInMemoryStore_Insert(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInMemoryStore_GetServiceStatus(t *testing.T) {
|
func TestStore_GetServiceStatus(t *testing.T) {
|
||||||
store := NewInMemoryStore()
|
store, _ := NewStore("")
|
||||||
store.Insert(&testService, &testSuccessfulResult)
|
store.Insert(&testService, &testSuccessfulResult)
|
||||||
store.Insert(&testService, &testUnsuccessfulResult)
|
store.Insert(&testService, &testUnsuccessfulResult)
|
||||||
|
|
||||||
@@ -160,11 +161,10 @@ func TestInMemoryStore_GetServiceStatus(t *testing.T) {
|
|||||||
if serviceStatus.Uptime.LastSevenDays != 0.5 {
|
if serviceStatus.Uptime.LastSevenDays != 0.5 {
|
||||||
t.Errorf("serviceStatus.Uptime.LastSevenDays should've been 0.5")
|
t.Errorf("serviceStatus.Uptime.LastSevenDays should've been 0.5")
|
||||||
}
|
}
|
||||||
fmt.Println(serviceStatus.Results[0].Timestamp.Format(time.RFC3339))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInMemoryStore_GetServiceStatusForMissingStatusReturnsNil(t *testing.T) {
|
func TestStore_GetServiceStatusForMissingStatusReturnsNil(t *testing.T) {
|
||||||
store := NewInMemoryStore()
|
store, _ := NewStore("")
|
||||||
store.Insert(&testService, &testSuccessfulResult)
|
store.Insert(&testService, &testSuccessfulResult)
|
||||||
|
|
||||||
serviceStatus := store.GetServiceStatus("nonexistantgroup", "nonexistantname")
|
serviceStatus := store.GetServiceStatus("nonexistantgroup", "nonexistantname")
|
||||||
@@ -181,8 +181,31 @@ func TestInMemoryStore_GetServiceStatusForMissingStatusReturnsNil(t *testing.T)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInMemoryStore_GetAllAsJSON(t *testing.T) {
|
func TestStore_GetServiceStatusByKey(t *testing.T) {
|
||||||
store := NewInMemoryStore()
|
store, _ := NewStore("")
|
||||||
|
store.Insert(&testService, &testSuccessfulResult)
|
||||||
|
store.Insert(&testService, &testUnsuccessfulResult)
|
||||||
|
|
||||||
|
serviceStatus := store.GetServiceStatusByKey(util.ConvertGroupAndServiceToKey(testService.Group, testService.Name))
|
||||||
|
if serviceStatus == nil {
|
||||||
|
t.Fatalf("serviceStatus shouldn't have been nil")
|
||||||
|
}
|
||||||
|
if serviceStatus.Uptime == nil {
|
||||||
|
t.Fatalf("serviceStatus.Uptime shouldn't have been nil")
|
||||||
|
}
|
||||||
|
if serviceStatus.Uptime.LastHour != 0.5 {
|
||||||
|
t.Errorf("serviceStatus.Uptime.LastHour should've been 0.5")
|
||||||
|
}
|
||||||
|
if serviceStatus.Uptime.LastTwentyFourHours != 0.5 {
|
||||||
|
t.Errorf("serviceStatus.Uptime.LastTwentyFourHours should've been 0.5")
|
||||||
|
}
|
||||||
|
if serviceStatus.Uptime.LastSevenDays != 0.5 {
|
||||||
|
t.Errorf("serviceStatus.Uptime.LastSevenDays should've been 0.5")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStore_GetAllServiceStatusesWithResultPagination(t *testing.T) {
|
||||||
|
store, _ := NewStore("")
|
||||||
firstResult := &testSuccessfulResult
|
firstResult := &testSuccessfulResult
|
||||||
secondResult := &testUnsuccessfulResult
|
secondResult := &testUnsuccessfulResult
|
||||||
store.Insert(&testService, firstResult)
|
store.Insert(&testService, firstResult)
|
||||||
@@ -190,12 +213,65 @@ func TestInMemoryStore_GetAllAsJSON(t *testing.T) {
|
|||||||
// Can't be bothered dealing with timezone issues on the worker that runs the automated tests
|
// Can't be bothered dealing with timezone issues on the worker that runs the automated tests
|
||||||
firstResult.Timestamp = time.Time{}
|
firstResult.Timestamp = time.Time{}
|
||||||
secondResult.Timestamp = time.Time{}
|
secondResult.Timestamp = time.Time{}
|
||||||
output, err := store.GetAllAsJSON()
|
serviceStatuses := store.GetAllServiceStatusesWithResultPagination(1, 20)
|
||||||
if err != nil {
|
if len(serviceStatuses) != 1 {
|
||||||
t.Fatal("shouldn't have returned an error, got", err.Error())
|
t.Fatal("expected 1 service status")
|
||||||
}
|
}
|
||||||
expectedOutput := `{"group_name":{"name":"name","group":"group","results":[{"status":200,"hostname":"example.org","duration":150000000,"errors":null,"condition-results":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":true},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":true}],"success":true,"timestamp":"0001-01-01T00:00:00Z"},{"status":200,"hostname":"example.org","duration":750000000,"errors":["error-1","error-2"],"condition-results":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":false},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":false}],"success":false,"timestamp":"0001-01-01T00:00:00Z"}],"uptime":{"7d":0.5,"24h":0.5,"1h":0.5}}}`
|
actual, exists := serviceStatuses[util.ConvertGroupAndServiceToKey(testService.Group, testService.Name)]
|
||||||
if string(output) != expectedOutput {
|
if !exists {
|
||||||
t.Errorf("expected:\n %s\n\ngot:\n %s", expectedOutput, string(output))
|
t.Fatal("expected service status to exist")
|
||||||
|
}
|
||||||
|
if len(actual.Results) != 2 {
|
||||||
|
t.Error("expected 2 results, got", len(actual.Results))
|
||||||
|
}
|
||||||
|
if len(actual.Events) != 2 {
|
||||||
|
t.Error("expected 2 events, got", len(actual.Events))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStore_DeleteAllServiceStatusesNotInKeys(t *testing.T) {
|
||||||
|
store, _ := NewStore("")
|
||||||
|
firstService := core.Service{Name: "service-1", Group: "group"}
|
||||||
|
secondService := core.Service{Name: "service-2", Group: "group"}
|
||||||
|
result := &testSuccessfulResult
|
||||||
|
store.Insert(&firstService, result)
|
||||||
|
store.Insert(&secondService, result)
|
||||||
|
if store.cache.Count() != 2 {
|
||||||
|
t.Errorf("expected cache to have 2 keys, got %d", store.cache.Count())
|
||||||
|
}
|
||||||
|
if store.GetServiceStatusByKey(util.ConvertGroupAndServiceToKey(firstService.Group, firstService.Name)) == nil {
|
||||||
|
t.Fatal("firstService should exist")
|
||||||
|
}
|
||||||
|
if store.GetServiceStatusByKey(util.ConvertGroupAndServiceToKey(secondService.Group, secondService.Name)) == nil {
|
||||||
|
t.Fatal("secondService should exist")
|
||||||
|
}
|
||||||
|
store.DeleteAllServiceStatusesNotInKeys([]string{util.ConvertGroupAndServiceToKey(firstService.Group, firstService.Name)})
|
||||||
|
if store.cache.Count() != 1 {
|
||||||
|
t.Fatalf("expected cache to have 1 keys, got %d", store.cache.Count())
|
||||||
|
}
|
||||||
|
if store.GetServiceStatusByKey(util.ConvertGroupAndServiceToKey(firstService.Group, firstService.Name)) == nil {
|
||||||
|
t.Error("secondService should've been deleted")
|
||||||
|
}
|
||||||
|
if store.GetServiceStatusByKey(util.ConvertGroupAndServiceToKey(secondService.Group, secondService.Name)) != nil {
|
||||||
|
t.Error("firstService should still exist")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStore_Save(t *testing.T) {
|
||||||
|
files := []string{
|
||||||
|
"",
|
||||||
|
t.TempDir() + "/test.db",
|
||||||
|
}
|
||||||
|
for _, file := range files {
|
||||||
|
t.Run(file, func(t *testing.T) {
|
||||||
|
store, err := NewStore(file)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("expected no error, got", err.Error())
|
||||||
|
}
|
||||||
|
err = store.Save()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("expected no error, got", err.Error())
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package store
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TwinProduction/gatus/core"
|
||||||
|
"github.com/TwinProduction/gatus/storage/store/memory"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Store is the interface that each stores should implement
|
||||||
|
type Store interface {
|
||||||
|
// GetAllAsJSON returns the JSON encoding of all monitored core.ServiceStatus
|
||||||
|
// with a subset of core.Result defined by the page and pageSize parameters
|
||||||
|
GetAllServiceStatusesWithResultPagination(page, pageSize int) map[string]*core.ServiceStatus
|
||||||
|
|
||||||
|
// GetServiceStatus returns the service status for a given service name in the given group
|
||||||
|
GetServiceStatus(groupName, serviceName string) *core.ServiceStatus
|
||||||
|
|
||||||
|
// GetServiceStatusByKey returns the service status for a given key
|
||||||
|
GetServiceStatusByKey(key string) *core.ServiceStatus
|
||||||
|
|
||||||
|
// Insert adds the observed result for the specified service into the store
|
||||||
|
Insert(service *core.Service, result *core.Result)
|
||||||
|
|
||||||
|
// DeleteAllServiceStatusesNotInKeys removes all ServiceStatus that are not within the keys provided
|
||||||
|
//
|
||||||
|
// Used to delete services that have been persisted but are no longer part of the configured services
|
||||||
|
DeleteAllServiceStatusesNotInKeys(keys []string) int
|
||||||
|
|
||||||
|
// Clear deletes everything from the store
|
||||||
|
Clear()
|
||||||
|
|
||||||
|
// Save persists the data if and where it needs to be persisted
|
||||||
|
Save() error
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Validate interface implementation on compile
|
||||||
|
_ Store = (*memory.Store)(nil)
|
||||||
|
)
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
package store
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/TwinProduction/gatus/core"
|
||||||
|
"github.com/TwinProduction/gatus/storage/store/memory"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
firstCondition = core.Condition("[STATUS] == 200")
|
||||||
|
secondCondition = core.Condition("[RESPONSE_TIME] < 500")
|
||||||
|
thirdCondition = core.Condition("[CERTIFICATE_EXPIRATION] < 72h")
|
||||||
|
|
||||||
|
timestamp = time.Now()
|
||||||
|
|
||||||
|
testService = core.Service{
|
||||||
|
Name: "name",
|
||||||
|
Group: "group",
|
||||||
|
URL: "https://example.org/what/ever",
|
||||||
|
Method: "GET",
|
||||||
|
Body: "body",
|
||||||
|
Interval: 30 * time.Second,
|
||||||
|
Conditions: []*core.Condition{&firstCondition, &secondCondition, &thirdCondition},
|
||||||
|
Alerts: nil,
|
||||||
|
Insecure: false,
|
||||||
|
NumberOfFailuresInARow: 0,
|
||||||
|
NumberOfSuccessesInARow: 0,
|
||||||
|
}
|
||||||
|
testSuccessfulResult = core.Result{
|
||||||
|
Hostname: "example.org",
|
||||||
|
IP: "127.0.0.1",
|
||||||
|
HTTPStatus: 200,
|
||||||
|
Body: []byte("body"),
|
||||||
|
Errors: nil,
|
||||||
|
Connected: true,
|
||||||
|
Success: true,
|
||||||
|
Timestamp: timestamp,
|
||||||
|
Duration: 150 * time.Millisecond,
|
||||||
|
CertificateExpiration: 10 * time.Hour,
|
||||||
|
ConditionResults: []*core.ConditionResult{
|
||||||
|
{
|
||||||
|
Condition: "[STATUS] == 200",
|
||||||
|
Success: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Condition: "[RESPONSE_TIME] < 500",
|
||||||
|
Success: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Condition: "[CERTIFICATE_EXPIRATION] < 72h",
|
||||||
|
Success: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
testUnsuccessfulResult = core.Result{
|
||||||
|
Hostname: "example.org",
|
||||||
|
IP: "127.0.0.1",
|
||||||
|
HTTPStatus: 200,
|
||||||
|
Body: []byte("body"),
|
||||||
|
Errors: []string{"error-1", "error-2"},
|
||||||
|
Connected: true,
|
||||||
|
Success: false,
|
||||||
|
Timestamp: timestamp,
|
||||||
|
Duration: 750 * time.Millisecond,
|
||||||
|
CertificateExpiration: 10 * time.Hour,
|
||||||
|
ConditionResults: []*core.ConditionResult{
|
||||||
|
{
|
||||||
|
Condition: "[STATUS] == 200",
|
||||||
|
Success: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Condition: "[RESPONSE_TIME] < 500",
|
||||||
|
Success: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Condition: "[CERTIFICATE_EXPIRATION] < 72h",
|
||||||
|
Success: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func BenchmarkStore_GetAllAsJSON(b *testing.B) {
|
||||||
|
memoryStore, err := memory.NewStore("")
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal("failed to create store:", err.Error())
|
||||||
|
}
|
||||||
|
type Scenario struct {
|
||||||
|
Name string
|
||||||
|
Store Store
|
||||||
|
}
|
||||||
|
scenarios := []Scenario{
|
||||||
|
{
|
||||||
|
Name: "memory",
|
||||||
|
Store: memoryStore,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, scenario := range scenarios {
|
||||||
|
scenario.Store.Insert(&testService, &testSuccessfulResult)
|
||||||
|
scenario.Store.Insert(&testService, &testUnsuccessfulResult)
|
||||||
|
b.Run(scenario.Name, func(b *testing.B) {
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
scenario.Store.GetAllServiceStatusesWithResultPagination(1, 20)
|
||||||
|
}
|
||||||
|
b.ReportAllocs()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStore_Insert(b *testing.B) {
|
||||||
|
memoryStore, err := memory.NewStore("")
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal("failed to create store:", err.Error())
|
||||||
|
}
|
||||||
|
type Scenario struct {
|
||||||
|
Name string
|
||||||
|
Store Store
|
||||||
|
}
|
||||||
|
scenarios := []Scenario{
|
||||||
|
{
|
||||||
|
Name: "memory",
|
||||||
|
Store: memoryStore,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, scenario := range scenarios {
|
||||||
|
b.Run(scenario.Name, func(b *testing.B) {
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
scenario.Store.Insert(&testService, &testSuccessfulResult)
|
||||||
|
scenario.Store.Insert(&testService, &testUnsuccessfulResult)
|
||||||
|
}
|
||||||
|
b.ReportAllocs()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
+18
@@ -0,0 +1,18 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
// ConvertGroupAndServiceToKey converts a group and a service to a key
|
||||||
|
func ConvertGroupAndServiceToKey(group, service string) string {
|
||||||
|
return sanitize(group) + "_" + sanitize(service)
|
||||||
|
}
|
||||||
|
|
||||||
|
func sanitize(s string) string {
|
||||||
|
s = strings.TrimSpace(strings.ToLower(s))
|
||||||
|
s = strings.ReplaceAll(s, "/", "-")
|
||||||
|
s = strings.ReplaceAll(s, "_", "-")
|
||||||
|
s = strings.ReplaceAll(s, ".", "-")
|
||||||
|
s = strings.ReplaceAll(s, ",", "-")
|
||||||
|
s = strings.ReplaceAll(s, " ", "-")
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestConvertGroupAndServiceToKey(t *testing.T) {
|
||||||
|
type Scenario struct {
|
||||||
|
GroupName string
|
||||||
|
ServiceName string
|
||||||
|
ExpectedOutput string
|
||||||
|
}
|
||||||
|
scenarios := []Scenario{
|
||||||
|
{
|
||||||
|
GroupName: "Core",
|
||||||
|
ServiceName: "Front End",
|
||||||
|
ExpectedOutput: "core_front-end",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
GroupName: "Load balancers",
|
||||||
|
ServiceName: "us-west-2",
|
||||||
|
ExpectedOutput: "load-balancers_us-west-2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
GroupName: "a/b test",
|
||||||
|
ServiceName: "a",
|
||||||
|
ExpectedOutput: "a-b-test_a",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, scenario := range scenarios {
|
||||||
|
t.Run(scenario.ExpectedOutput, func(t *testing.T) {
|
||||||
|
output := ConvertGroupAndServiceToKey(scenario.GroupName, scenario.ServiceName)
|
||||||
|
if output != scenario.ExpectedOutput {
|
||||||
|
t.Errorf("Expected '%s', got '%s'", scenario.ExpectedOutput, output)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
+65
-41
@@ -306,9 +306,9 @@ If you do not start the janitor, there will be no passive deletion of expired ke
|
|||||||
For the sake of convenience, a ready-to-go cache server is available
|
For the sake of convenience, a ready-to-go cache server is available
|
||||||
through the `gocacheserver` package.
|
through the `gocacheserver` package.
|
||||||
|
|
||||||
The reason why the server is in a different package is because `gocache` does not use
|
The reason why the server is in a different package is because `gocache` limit its external dependencies to the strict
|
||||||
any external dependencies, but rather than re-inventing the wheel, the server
|
minimum (e.g. boltdb for persistence), however, rather than re-inventing the wheel, the server implementation uses
|
||||||
implementation uses redcon, which is a Redis server framework for Go.
|
redcon, which is a very good Redis server framework for Go.
|
||||||
|
|
||||||
That way, those who desire to use gocache without the server will not add any extra dependencies
|
That way, those who desire to use gocache without the server will not add any extra dependencies
|
||||||
as long as they don't import the `gocacheserver` package.
|
as long as they don't import the `gocacheserver` package.
|
||||||
@@ -323,7 +323,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cache := gocache.NewCache().WithEvictionPolicy(gocache.LeastRecentlyUsed).WithMaxSize(100000)
|
cache := gocache.NewCache().WithEvictionPolicy(gocache.LeastRecentlyUsed).WithMaxSize(100000)
|
||||||
server := gocacheserver.NewServer(cache)
|
server := gocacheserver.NewServer(cache).WithPort(6379)
|
||||||
server.Start()
|
server.Start()
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -382,43 +382,67 @@ but if you're looking into using a library like gocache, odds are, you want more
|
|||||||
| mem | 32G DDR4 |
|
| mem | 32G DDR4 |
|
||||||
|
|
||||||
```
|
```
|
||||||
BenchmarkMap_Get-8 95936680 26.3 ns/op
|
// Normal map
|
||||||
BenchmarkMap_SetSmallValue-8 7738132 424 ns/op
|
BenchmarkMap_Get
|
||||||
BenchmarkMap_SetMediumValue-8 7766346 424 ns/op
|
BenchmarkMap_Get-8 46087372 26.7 ns/op
|
||||||
BenchmarkMap_SetLargeValue-8 7947063 435 ns/op
|
BenchmarkMap_Set
|
||||||
BenchmarkCache_Get-8 54549049 45.7 ns/op
|
BenchmarkMap_Set/small_value-8 3841911 389 ns/op
|
||||||
BenchmarkCache_SetSmallValue-8 35225013 69.2 ns/op
|
BenchmarkMap_Set/medium_value-8 3887074 391 ns/op
|
||||||
BenchmarkCache_SetMediumValue-8 5952064 412 ns/op
|
BenchmarkMap_Set/large_value-8 3921956 393 ns/op
|
||||||
BenchmarkCache_SetLargeValue-8 5969121 411 ns/op
|
// Gocache
|
||||||
BenchmarkCache_GetUsingLRU-8 54545949 45.6 ns/op
|
BenchmarkCache_Get
|
||||||
BenchmarkCache_SetSmallValueUsingLRU-8 5909504 419 ns/op
|
BenchmarkCache_Get/FirstInFirstOut-8 27273036 46.4 ns/op
|
||||||
BenchmarkCache_SetMediumValueUsingLRU-8 5910885 418 ns/op
|
BenchmarkCache_Get/LeastRecentlyUsed-8 26648248 46.3 ns/op
|
||||||
BenchmarkCache_SetLargeValueUsingLRU-8 5867544 419 ns/op
|
BenchmarkCache_Set
|
||||||
BenchmarkCache_SetSmallValueWhenUsingMaxMemoryUsage-8 5477178 462 ns/op
|
BenchmarkCache_Set/FirstInFirstOut_small_value-8 2919584 405 ns/op
|
||||||
BenchmarkCache_SetMediumValueWhenUsingMaxMemoryUsage-8 5417595 475 ns/op
|
BenchmarkCache_Set/FirstInFirstOut_medium_value-8 2990841 391 ns/op
|
||||||
BenchmarkCache_SetLargeValueWhenUsingMaxMemoryUsage-8 5215263 479 ns/op
|
BenchmarkCache_Set/FirstInFirstOut_large_value-8 2970513 391 ns/op
|
||||||
BenchmarkCache_SetSmallValueWithMaxSize10-8 10115574 236 ns/op
|
BenchmarkCache_Set/LeastRecentlyUsed_small_value-8 2962939 402 ns/op
|
||||||
BenchmarkCache_SetMediumValueWithMaxSize10-8 10242792 241 ns/op
|
BenchmarkCache_Set/LeastRecentlyUsed_medium_value-8 2962963 390 ns/op
|
||||||
BenchmarkCache_SetLargeValueWithMaxSize10-8 10201894 241 ns/op
|
BenchmarkCache_Set/LeastRecentlyUsed_large_value-8 2962928 394 ns/op
|
||||||
BenchmarkCache_SetSmallValueWithMaxSize1000-8 9637113 253 ns/op
|
BenchmarkCache_SetUsingMaxMemoryUsage
|
||||||
BenchmarkCache_SetMediumValueWithMaxSize1000-8 9635175 253 ns/op
|
BenchmarkCache_SetUsingMaxMemoryUsage/small_value-8 2683356 447 ns/op
|
||||||
BenchmarkCache_SetLargeValueWithMaxSize1000-8 9598982 260 ns/op
|
BenchmarkCache_SetUsingMaxMemoryUsage/medium_value-8 2637578 441 ns/op
|
||||||
BenchmarkCache_SetSmallValueWithMaxSize100000-8 7642584 337 ns/op
|
BenchmarkCache_SetUsingMaxMemoryUsage/large_value-8 2672434 443 ns/op
|
||||||
BenchmarkCache_SetMediumValueWithMaxSize100000-8 7407571 344 ns/op
|
BenchmarkCache_SetWithMaxSize
|
||||||
BenchmarkCache_SetLargeValueWithMaxSize100000-8 7071360 345 ns/op
|
BenchmarkCache_SetWithMaxSize/100_small_value-8 4782966 252 ns/op
|
||||||
BenchmarkCache_SetSmallValueWithMaxSize100000AndLRU-8 7544194 332 ns/op
|
BenchmarkCache_SetWithMaxSize/10000_small_value-8 4067967 296 ns/op
|
||||||
BenchmarkCache_SetMediumValueWithMaxSize100000AndLRU-8 7667004 344 ns/op
|
BenchmarkCache_SetWithMaxSize/100000_small_value-8 3762055 328 ns/op
|
||||||
BenchmarkCache_SetLargeValueWithMaxSize100000AndLRU-8 7357642 338 ns/op
|
BenchmarkCache_SetWithMaxSize/100_medium_value-8 4760479 252 ns/op
|
||||||
BenchmarkCache_GetAndSetMultipleConcurrently-8 1442306 1684 ns/op
|
BenchmarkCache_SetWithMaxSize/10000_medium_value-8 4081050 295 ns/op
|
||||||
BenchmarkCache_GetAndSetConcurrentlyWithRandomKeysAndLRU-8 5117271 477 ns/op
|
BenchmarkCache_SetWithMaxSize/100000_medium_value-8 3785050 330 ns/op
|
||||||
BenchmarkCache_GetAndSetConcurrentlyWithRandomKeysAndFIFO-8 5228412 475 ns/op
|
BenchmarkCache_SetWithMaxSize/100_large_value-8 4732909 254 ns/op
|
||||||
BenchmarkCache_GetAndSetConcurrentlyWithRandomKeysAndNoEvictionAndLRU-8 5139195 529 ns/op
|
BenchmarkCache_SetWithMaxSize/10000_large_value-8 4079533 297 ns/op
|
||||||
BenchmarkCache_GetAndSetConcurrentlyWithRandomKeysAndNoEvictionAndFIFO-8 5251639 511 ns/op
|
BenchmarkCache_SetWithMaxSize/100000_large_value-8 3712820 331 ns/op
|
||||||
BenchmarkCache_GetAndSetConcurrentlyWithFrequentEvictionsAndLRU-8 7384626 334 ns/op
|
BenchmarkCache_SetWithMaxSizeAndLRU
|
||||||
BenchmarkCache_GetAndSetConcurrentlyWithFrequentEvictionsAndFIFO-8 7361985 332 ns/op
|
BenchmarkCache_SetWithMaxSizeAndLRU/100_small_value-8 4761732 254 ns/op
|
||||||
BenchmarkCache_GetConcurrentlyWithLRU-8 3370784 726 ns/op
|
BenchmarkCache_SetWithMaxSizeAndLRU/10000_small_value-8 4084474 296 ns/op
|
||||||
BenchmarkCache_GetConcurrentlyWithFIFO-8 3749994 681 ns/op
|
BenchmarkCache_SetWithMaxSizeAndLRU/100000_small_value-8 3761402 329 ns/op
|
||||||
BenchmarkCache_GetKeysThatDoNotExistConcurrently-8 17647344 143 ns/op
|
BenchmarkCache_SetWithMaxSizeAndLRU/100_medium_value-8 4783075 254 ns/op
|
||||||
|
BenchmarkCache_SetWithMaxSizeAndLRU/10000_medium_value-8 4103980 296 ns/op
|
||||||
|
BenchmarkCache_SetWithMaxSizeAndLRU/100000_medium_value-8 3646023 331 ns/op
|
||||||
|
BenchmarkCache_SetWithMaxSizeAndLRU/100_large_value-8 4779025 254 ns/op
|
||||||
|
BenchmarkCache_SetWithMaxSizeAndLRU/10000_large_value-8 4096192 296 ns/op
|
||||||
|
BenchmarkCache_SetWithMaxSizeAndLRU/100000_large_value-8 3726823 331 ns/op
|
||||||
|
BenchmarkCache_GetSetMultipleConcurrent
|
||||||
|
BenchmarkCache_GetSetMultipleConcurrent-8 707142 1698 ns/op
|
||||||
|
BenchmarkCache_GetSetConcurrentWithFrequentEviction
|
||||||
|
BenchmarkCache_GetSetConcurrentWithFrequentEviction/FirstInFirstOut-8 3616256 334 ns/op
|
||||||
|
BenchmarkCache_GetSetConcurrentWithFrequentEviction/LeastRecentlyUsed-8 3636367 331 ns/op
|
||||||
|
BenchmarkCache_GetConcurrentWithLRU
|
||||||
|
BenchmarkCache_GetConcurrentWithLRU/FirstInFirstOut-8 4405557 268 ns/op
|
||||||
|
BenchmarkCache_GetConcurrentWithLRU/LeastRecentlyUsed-8 4445475 269 ns/op
|
||||||
|
BenchmarkCache_WithForceNilInterfaceOnNilPointer
|
||||||
|
BenchmarkCache_WithForceNilInterfaceOnNilPointer/true_with_nil_struct_pointer-8 6184591 191 ns/op
|
||||||
|
BenchmarkCache_WithForceNilInterfaceOnNilPointer/true-8 6090482 191 ns/op
|
||||||
|
BenchmarkCache_WithForceNilInterfaceOnNilPointer/false_with_nil_struct_pointer-8 6184629 187 ns/op
|
||||||
|
BenchmarkCache_WithForceNilInterfaceOnNilPointer/false-8 6281781 186 ns/op
|
||||||
|
(Trimmed "BenchmarkCache_" for readability)
|
||||||
|
WithForceNilInterfaceOnNilPointerWithConcurrency
|
||||||
|
WithForceNilInterfaceOnNilPointerWithConcurrency/true_with_nil_struct_pointer-8 4379564 268 ns/op
|
||||||
|
WithForceNilInterfaceOnNilPointerWithConcurrency/true-8 4379558 265 ns/op
|
||||||
|
WithForceNilInterfaceOnNilPointerWithConcurrency/false_with_nil_struct_pointer-8 4444456 261 ns/op
|
||||||
|
WithForceNilInterfaceOnNilPointerWithConcurrency/false-8 4493896 262 ns/op
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -3,9 +3,9 @@ module github.com/TwinProduction/gocache
|
|||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/boltdb/bolt v1.3.1
|
|
||||||
github.com/go-redis/redis v6.15.9+incompatible
|
github.com/go-redis/redis v6.15.9+incompatible
|
||||||
github.com/onsi/ginkgo v1.14.1 // indirect
|
github.com/onsi/ginkgo v1.14.1 // indirect
|
||||||
github.com/onsi/gomega v1.10.2 // indirect
|
github.com/onsi/gomega v1.10.2 // indirect
|
||||||
github.com/tidwall/redcon v1.3.2
|
github.com/tidwall/redcon v1.3.2
|
||||||
|
go.etcd.io/bbolt v1.3.5
|
||||||
)
|
)
|
||||||
+3
-2
@@ -1,5 +1,3 @@
|
|||||||
github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=
|
|
||||||
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
|
|
||||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||||
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
|
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
|
||||||
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
|
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
|
||||||
@@ -30,6 +28,8 @@ github.com/onsi/gomega v1.10.2 h1:aY/nuoWlKJud2J6U0E3NWsjlg+0GtwXxgEqthRdzlcs=
|
|||||||
github.com/onsi/gomega v1.10.2/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
|
github.com/onsi/gomega v1.10.2/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
|
||||||
github.com/tidwall/redcon v1.3.2 h1:8INx/Nm3VSUbDUT16TH1rMgYQsbXNqy9xcX70edHXbo=
|
github.com/tidwall/redcon v1.3.2 h1:8INx/Nm3VSUbDUT16TH1rMgYQsbXNqy9xcX70edHXbo=
|
||||||
github.com/tidwall/redcon v1.3.2/go.mod h1:bdYBm4rlcWpst2XMwKVzWDF9CoUxEbUmM7CQrKeOZas=
|
github.com/tidwall/redcon v1.3.2/go.mod h1:bdYBm4rlcWpst2XMwKVzWDF9CoUxEbUmM7CQrKeOZas=
|
||||||
|
go.etcd.io/bbolt v1.3.5 h1:XAzx9gjCb0Rxj7EoqcClPD1d5ZBxZJk0jbuoPHenBt0=
|
||||||
|
go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7 h1:AeiKBIuRw3UomYXSbLy0Mc2dDLfdtbT/IVn4keq83P0=
|
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7 h1:AeiKBIuRw3UomYXSbLy0Mc2dDLfdtbT/IVn4keq83P0=
|
||||||
@@ -40,6 +40,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
|
|||||||
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 h1:DYfZAGf2WMFjMxbgTjaC+2HC7NkNAQs+6Q8b9WEB/F4=
|
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 h1:DYfZAGf2WMFjMxbgTjaC+2HC7NkNAQs+6Q8b9WEB/F4=
|
||||||
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
|||||||
+10
-2
@@ -170,7 +170,7 @@ func (cache *Cache) WithEvictionPolicy(policy EvictionPolicy) *Cache {
|
|||||||
// value, _ := cache.Get("key")
|
// value, _ := cache.Get("key")
|
||||||
// // the following returns true, because the interface{} was forcefully set to nil
|
// // the following returns true, because the interface{} was forcefully set to nil
|
||||||
// if value == nil {}
|
// if value == nil {}
|
||||||
// // the following will panic, because the value has been casted to its type
|
// // the following will panic, because the value has been casted to its type (which is nil)
|
||||||
// if value.(*Struct) == nil {}
|
// if value.(*Struct) == nil {}
|
||||||
//
|
//
|
||||||
// If set to false:
|
// If set to false:
|
||||||
@@ -218,7 +218,8 @@ func (cache *Cache) Set(key string, value interface{}) {
|
|||||||
// The TTL provided must be greater than 0, or NoExpiration (-1). If a negative value that isn't -1 (NoExpiration) is
|
// The TTL provided must be greater than 0, or NoExpiration (-1). If a negative value that isn't -1 (NoExpiration) is
|
||||||
// provided, the entry will not be created if the key doesn't exist
|
// provided, the entry will not be created if the key doesn't exist
|
||||||
func (cache *Cache) SetWithTTL(key string, value interface{}, ttl time.Duration) {
|
func (cache *Cache) SetWithTTL(key string, value interface{}, ttl time.Duration) {
|
||||||
// An interface is only nil if both its value and its type are nil, however, passing a pointer
|
// An interface is only nil if both its value and its type are nil, however, passing a nil pointer as an interface{}
|
||||||
|
// means that the interface itself is not nil, because the interface value is nil but not the type.
|
||||||
if cache.forceNilInterfaceOnNilPointer {
|
if cache.forceNilInterfaceOnNilPointer {
|
||||||
if value != nil && (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil()) {
|
if value != nil && (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil()) {
|
||||||
value = nil
|
value = nil
|
||||||
@@ -334,6 +335,13 @@ func (cache *Cache) Get(key string) (interface{}, bool) {
|
|||||||
return entry.Value, true
|
return entry.Value, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetValue retrieves an entry using the key passed as parameter
|
||||||
|
// Unlike Get, this function only returns the value
|
||||||
|
func (cache *Cache) GetValue(key string) interface{} {
|
||||||
|
value, _ := cache.Get(key)
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
// GetByKeys retrieves multiple entries using the keys passed as parameter
|
// GetByKeys retrieves multiple entries using the keys passed as parameter
|
||||||
// All keys are returned in the map, regardless of whether they exist or not, however, entries that do not exist in the
|
// All keys are returned in the map, regardless of whether they exist or not, however, entries that do not exist in the
|
||||||
// cache will return nil, meaning that there is no way of determining whether a key genuinely has the value nil, or
|
// cache will return nil, meaning that there is no way of determining whether a key genuinely has the value nil, or
|
||||||
|
|||||||
+1
-1
@@ -8,7 +8,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/boltdb/bolt"
|
bolt "go.etcd.io/bbolt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SaveToFile stores the content of the cache to a file so that it can be read using
|
// SaveToFile stores the content of the cache to a file so that it can be read using
|
||||||
|
|||||||
+25
-1
@@ -3,6 +3,30 @@ package gocache
|
|||||||
type EvictionPolicy string
|
type EvictionPolicy string
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
// LeastRecentlyUsed is an eviction policy that causes the most recently accessed cache entry to be moved to the
|
||||||
|
// head of the cache. Effectively, this causes the cache entries that have not been accessed for some time to
|
||||||
|
// gradually move closer and closer to the tail, and since the tail is the entry that gets deleted when an eviction
|
||||||
|
// is required, it allows less used cache entries to be evicted while keeping recently accessed entries at or close
|
||||||
|
// to the head.
|
||||||
|
//
|
||||||
|
// For instance, creating a Cache with a Cache.MaxSize of 3 and creating the entries 1, 2 and 3 in that order would
|
||||||
|
// put 3 at the head and 1 at the tail:
|
||||||
|
// 3 (head) -> 2 -> 1 (tail)
|
||||||
|
// If the cache entry 1 was then accessed, 1 would become the head and 2 the tail:
|
||||||
|
// 1 (head) -> 3 -> 2 (tail)
|
||||||
|
// If a cache entry 4 was then created, because the Cache.MaxSize is 3, the tail (2) would then be evicted:
|
||||||
|
// 4 (head) -> 1 -> 3 (tail)
|
||||||
LeastRecentlyUsed EvictionPolicy = "LeastRecentlyUsed"
|
LeastRecentlyUsed EvictionPolicy = "LeastRecentlyUsed"
|
||||||
FirstInFirstOut EvictionPolicy = "FirstInFirstOut"
|
|
||||||
|
// FirstInFirstOut is an eviction policy that causes cache entries to be evicted in the same order that they are
|
||||||
|
// created.
|
||||||
|
//
|
||||||
|
// For instance, creating a Cache with a Cache.MaxSize of 3 and creating the entries 1, 2 and 3 in that order would
|
||||||
|
// put 3 at the head and 1 at the tail:
|
||||||
|
// 3 (head) -> 2 -> 1 (tail)
|
||||||
|
// If the cache entry 1 was then accessed, unlike with LeastRecentlyUsed, nothing would change:
|
||||||
|
// 3 (head) -> 2 -> 1 (tail)
|
||||||
|
// If a cache entry 4 was then created, because the Cache.MaxSize is 3, the tail (1) would then be evicted:
|
||||||
|
// 4 (head) -> 3 -> 2 (tail)
|
||||||
|
FirstInFirstOut EvictionPolicy = "FirstInFirstOut"
|
||||||
)
|
)
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
* text=lf
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
.idea
|
||||||
|
*.iml
|
||||||
+72
@@ -0,0 +1,72 @@
|
|||||||
|
# health
|
||||||
|
|
||||||
|

|
||||||
|
[](https://goreportcard.com/report/github.com/TwinProduction/health)
|
||||||
|
[](https://codecov.io/gh/TwinProduction/health)
|
||||||
|
[](https://github.com/TwinProduction/health)
|
||||||
|
[](https://pkg.go.dev/github.com/TwinProduction/health)
|
||||||
|
|
||||||
|
Health is a library used for creating a very simple health endpoint.
|
||||||
|
|
||||||
|
While implementing a health endpoint is very simple, I've grown tired of implementing
|
||||||
|
it over and over again.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
```
|
||||||
|
go get -u github.com/TwinProduction/health
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
To retrieve the handler, you must use `health.Handler()` and are expected to pass it to the router like so:
|
||||||
|
```go
|
||||||
|
router := http.NewServeMux()
|
||||||
|
router.Handle("/health", health.Handler())
|
||||||
|
server := &http.Server{
|
||||||
|
Addr: ":8080",
|
||||||
|
Handler: router,
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
By default, the handler will return `UP` when the status is down, and `DOWN` when the status is down.
|
||||||
|
If you prefer using JSON, however, you may initialize the health handler like so:
|
||||||
|
```go
|
||||||
|
router.Handle("/health", health.Handler().WithJSON(true))
|
||||||
|
```
|
||||||
|
The above will cause the response body to become `{"status":"UP"}` and `{"status":"DOWN"}` for both status respectively.
|
||||||
|
|
||||||
|
To change the health of the application, you can use `health.SetStatus(<status>)` where `<status>` is one `health.Up`
|
||||||
|
or `health.Down`:
|
||||||
|
|
||||||
|
```go
|
||||||
|
health.SetStatus(health.Up)
|
||||||
|
health.SetStatus(health.Down)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Complete example
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/TwinProduction/health"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
router := http.NewServeMux()
|
||||||
|
router.Handle("/health", health.Handler())
|
||||||
|
server := &http.Server{
|
||||||
|
Addr: "0.0.0.0:8080",
|
||||||
|
Handler: router,
|
||||||
|
ReadTimeout: 15 * time.Second,
|
||||||
|
WriteTimeout: 15 * time.Second,
|
||||||
|
IdleTimeout: 15 * time.Second,
|
||||||
|
}
|
||||||
|
server.ListenAndServe()
|
||||||
|
}
|
||||||
|
```
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
module github.com/TwinProduction/health
|
||||||
|
|
||||||
|
go 1.15
|
||||||
+51
@@ -0,0 +1,51 @@
|
|||||||
|
package health
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
var (
|
||||||
|
handler = &healthHandler{
|
||||||
|
useJSON: false,
|
||||||
|
status: Up,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
type healthHandler struct {
|
||||||
|
useJSON bool
|
||||||
|
status Status
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithJSON configures whether the handler should output a response in JSON or in raw text
|
||||||
|
//
|
||||||
|
// Defaults to false
|
||||||
|
func (h *healthHandler) WithJSON(v bool) *healthHandler {
|
||||||
|
h.useJSON = v
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h healthHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
var status int
|
||||||
|
var body []byte
|
||||||
|
if h.status == Up {
|
||||||
|
status = http.StatusOK
|
||||||
|
} else {
|
||||||
|
status = http.StatusInternalServerError
|
||||||
|
}
|
||||||
|
if h.useJSON {
|
||||||
|
writer.Header().Set("Content-Type", "application/json")
|
||||||
|
body = []byte(`{"status":"` + h.status + `"}`)
|
||||||
|
} else {
|
||||||
|
body = []byte(h.status)
|
||||||
|
}
|
||||||
|
writer.WriteHeader(status)
|
||||||
|
_, _ = writer.Write(body)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handler retrieves the health handler
|
||||||
|
func Handler() *healthHandler {
|
||||||
|
return handler
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetStatus sets the status to be reflected by the health handler
|
||||||
|
func SetStatus(status Status) {
|
||||||
|
handler.status = status
|
||||||
|
}
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
package health
|
||||||
|
|
||||||
|
type Status string
|
||||||
|
|
||||||
|
var (
|
||||||
|
Down Status = "DOWN"
|
||||||
|
Up Status = "UP"
|
||||||
|
)
|
||||||
-18
@@ -1,18 +0,0 @@
|
|||||||
BRANCH=`git rev-parse --abbrev-ref HEAD`
|
|
||||||
COMMIT=`git rev-parse --short HEAD`
|
|
||||||
GOLDFLAGS="-X main.branch $(BRANCH) -X main.commit $(COMMIT)"
|
|
||||||
|
|
||||||
default: build
|
|
||||||
|
|
||||||
race:
|
|
||||||
@go test -v -race -test.run="TestSimulate_(100op|1000op)"
|
|
||||||
|
|
||||||
# go get github.com/kisielk/errcheck
|
|
||||||
errcheck:
|
|
||||||
@errcheck -ignorepkg=bytes -ignore=os:Remove github.com/boltdb/bolt
|
|
||||||
|
|
||||||
test:
|
|
||||||
@go test -v -cover .
|
|
||||||
@go test -v ./cmd/bolt
|
|
||||||
|
|
||||||
.PHONY: fmt test
|
|
||||||
-18
@@ -1,18 +0,0 @@
|
|||||||
version: "{build}"
|
|
||||||
|
|
||||||
os: Windows Server 2012 R2
|
|
||||||
|
|
||||||
clone_folder: c:\gopath\src\github.com\boltdb\bolt
|
|
||||||
|
|
||||||
environment:
|
|
||||||
GOPATH: c:\gopath
|
|
||||||
|
|
||||||
install:
|
|
||||||
- echo %PATH%
|
|
||||||
- echo %GOPATH%
|
|
||||||
- go version
|
|
||||||
- go env
|
|
||||||
- go get -v -t ./...
|
|
||||||
|
|
||||||
build_script:
|
|
||||||
- go test -v ./...
|
|
||||||
-28
@@ -1,28 +0,0 @@
|
|||||||
package bolt
|
|
||||||
|
|
||||||
import "unsafe"
|
|
||||||
|
|
||||||
// maxMapSize represents the largest mmap size supported by Bolt.
|
|
||||||
const maxMapSize = 0x7FFFFFFF // 2GB
|
|
||||||
|
|
||||||
// maxAllocSize is the size used when creating array pointers.
|
|
||||||
const maxAllocSize = 0xFFFFFFF
|
|
||||||
|
|
||||||
// Are unaligned load/stores broken on this arch?
|
|
||||||
var brokenUnaligned bool
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
// Simple check to see whether this arch handles unaligned load/stores
|
|
||||||
// correctly.
|
|
||||||
|
|
||||||
// ARM9 and older devices require load/stores to be from/to aligned
|
|
||||||
// addresses. If not, the lower 2 bits are cleared and that address is
|
|
||||||
// read in a jumbled up order.
|
|
||||||
|
|
||||||
// See http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka15414.html
|
|
||||||
|
|
||||||
raw := [6]byte{0xfe, 0xef, 0x11, 0x22, 0x22, 0x11}
|
|
||||||
val := *(*uint32)(unsafe.Pointer(uintptr(unsafe.Pointer(&raw)) + 2))
|
|
||||||
|
|
||||||
brokenUnaligned = val != 0x11222211
|
|
||||||
}
|
|
||||||
-252
@@ -1,252 +0,0 @@
|
|||||||
package bolt
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"sort"
|
|
||||||
"unsafe"
|
|
||||||
)
|
|
||||||
|
|
||||||
// freelist represents a list of all pages that are available for allocation.
|
|
||||||
// It also tracks pages that have been freed but are still in use by open transactions.
|
|
||||||
type freelist struct {
|
|
||||||
ids []pgid // all free and available free page ids.
|
|
||||||
pending map[txid][]pgid // mapping of soon-to-be free page ids by tx.
|
|
||||||
cache map[pgid]bool // fast lookup of all free and pending page ids.
|
|
||||||
}
|
|
||||||
|
|
||||||
// newFreelist returns an empty, initialized freelist.
|
|
||||||
func newFreelist() *freelist {
|
|
||||||
return &freelist{
|
|
||||||
pending: make(map[txid][]pgid),
|
|
||||||
cache: make(map[pgid]bool),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// size returns the size of the page after serialization.
|
|
||||||
func (f *freelist) size() int {
|
|
||||||
n := f.count()
|
|
||||||
if n >= 0xFFFF {
|
|
||||||
// The first element will be used to store the count. See freelist.write.
|
|
||||||
n++
|
|
||||||
}
|
|
||||||
return pageHeaderSize + (int(unsafe.Sizeof(pgid(0))) * n)
|
|
||||||
}
|
|
||||||
|
|
||||||
// count returns count of pages on the freelist
|
|
||||||
func (f *freelist) count() int {
|
|
||||||
return f.free_count() + f.pending_count()
|
|
||||||
}
|
|
||||||
|
|
||||||
// free_count returns count of free pages
|
|
||||||
func (f *freelist) free_count() int {
|
|
||||||
return len(f.ids)
|
|
||||||
}
|
|
||||||
|
|
||||||
// pending_count returns count of pending pages
|
|
||||||
func (f *freelist) pending_count() int {
|
|
||||||
var count int
|
|
||||||
for _, list := range f.pending {
|
|
||||||
count += len(list)
|
|
||||||
}
|
|
||||||
return count
|
|
||||||
}
|
|
||||||
|
|
||||||
// copyall copies into dst a list of all free ids and all pending ids in one sorted list.
|
|
||||||
// f.count returns the minimum length required for dst.
|
|
||||||
func (f *freelist) copyall(dst []pgid) {
|
|
||||||
m := make(pgids, 0, f.pending_count())
|
|
||||||
for _, list := range f.pending {
|
|
||||||
m = append(m, list...)
|
|
||||||
}
|
|
||||||
sort.Sort(m)
|
|
||||||
mergepgids(dst, f.ids, m)
|
|
||||||
}
|
|
||||||
|
|
||||||
// allocate returns the starting page id of a contiguous list of pages of a given size.
|
|
||||||
// If a contiguous block cannot be found then 0 is returned.
|
|
||||||
func (f *freelist) allocate(n int) pgid {
|
|
||||||
if len(f.ids) == 0 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
var initial, previd pgid
|
|
||||||
for i, id := range f.ids {
|
|
||||||
if id <= 1 {
|
|
||||||
panic(fmt.Sprintf("invalid page allocation: %d", id))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset initial page if this is not contiguous.
|
|
||||||
if previd == 0 || id-previd != 1 {
|
|
||||||
initial = id
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we found a contiguous block then remove it and return it.
|
|
||||||
if (id-initial)+1 == pgid(n) {
|
|
||||||
// If we're allocating off the beginning then take the fast path
|
|
||||||
// and just adjust the existing slice. This will use extra memory
|
|
||||||
// temporarily but the append() in free() will realloc the slice
|
|
||||||
// as is necessary.
|
|
||||||
if (i + 1) == n {
|
|
||||||
f.ids = f.ids[i+1:]
|
|
||||||
} else {
|
|
||||||
copy(f.ids[i-n+1:], f.ids[i+1:])
|
|
||||||
f.ids = f.ids[:len(f.ids)-n]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove from the free cache.
|
|
||||||
for i := pgid(0); i < pgid(n); i++ {
|
|
||||||
delete(f.cache, initial+i)
|
|
||||||
}
|
|
||||||
|
|
||||||
return initial
|
|
||||||
}
|
|
||||||
|
|
||||||
previd = id
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// free releases a page and its overflow for a given transaction id.
|
|
||||||
// If the page is already free then a panic will occur.
|
|
||||||
func (f *freelist) free(txid txid, p *page) {
|
|
||||||
if p.id <= 1 {
|
|
||||||
panic(fmt.Sprintf("cannot free page 0 or 1: %d", p.id))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Free page and all its overflow pages.
|
|
||||||
var ids = f.pending[txid]
|
|
||||||
for id := p.id; id <= p.id+pgid(p.overflow); id++ {
|
|
||||||
// Verify that page is not already free.
|
|
||||||
if f.cache[id] {
|
|
||||||
panic(fmt.Sprintf("page %d already freed", id))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add to the freelist and cache.
|
|
||||||
ids = append(ids, id)
|
|
||||||
f.cache[id] = true
|
|
||||||
}
|
|
||||||
f.pending[txid] = ids
|
|
||||||
}
|
|
||||||
|
|
||||||
// release moves all page ids for a transaction id (or older) to the freelist.
|
|
||||||
func (f *freelist) release(txid txid) {
|
|
||||||
m := make(pgids, 0)
|
|
||||||
for tid, ids := range f.pending {
|
|
||||||
if tid <= txid {
|
|
||||||
// Move transaction's pending pages to the available freelist.
|
|
||||||
// Don't remove from the cache since the page is still free.
|
|
||||||
m = append(m, ids...)
|
|
||||||
delete(f.pending, tid)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sort.Sort(m)
|
|
||||||
f.ids = pgids(f.ids).merge(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
// rollback removes the pages from a given pending tx.
|
|
||||||
func (f *freelist) rollback(txid txid) {
|
|
||||||
// Remove page ids from cache.
|
|
||||||
for _, id := range f.pending[txid] {
|
|
||||||
delete(f.cache, id)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove pages from pending list.
|
|
||||||
delete(f.pending, txid)
|
|
||||||
}
|
|
||||||
|
|
||||||
// freed returns whether a given page is in the free list.
|
|
||||||
func (f *freelist) freed(pgid pgid) bool {
|
|
||||||
return f.cache[pgid]
|
|
||||||
}
|
|
||||||
|
|
||||||
// read initializes the freelist from a freelist page.
|
|
||||||
func (f *freelist) read(p *page) {
|
|
||||||
// If the page.count is at the max uint16 value (64k) then it's considered
|
|
||||||
// an overflow and the size of the freelist is stored as the first element.
|
|
||||||
idx, count := 0, int(p.count)
|
|
||||||
if count == 0xFFFF {
|
|
||||||
idx = 1
|
|
||||||
count = int(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy the list of page ids from the freelist.
|
|
||||||
if count == 0 {
|
|
||||||
f.ids = nil
|
|
||||||
} else {
|
|
||||||
ids := ((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[idx:count]
|
|
||||||
f.ids = make([]pgid, len(ids))
|
|
||||||
copy(f.ids, ids)
|
|
||||||
|
|
||||||
// Make sure they're sorted.
|
|
||||||
sort.Sort(pgids(f.ids))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Rebuild the page cache.
|
|
||||||
f.reindex()
|
|
||||||
}
|
|
||||||
|
|
||||||
// write writes the page ids onto a freelist page. All free and pending ids are
|
|
||||||
// saved to disk since in the event of a program crash, all pending ids will
|
|
||||||
// become free.
|
|
||||||
func (f *freelist) write(p *page) error {
|
|
||||||
// Combine the old free pgids and pgids waiting on an open transaction.
|
|
||||||
|
|
||||||
// Update the header flag.
|
|
||||||
p.flags |= freelistPageFlag
|
|
||||||
|
|
||||||
// The page.count can only hold up to 64k elements so if we overflow that
|
|
||||||
// number then we handle it by putting the size in the first element.
|
|
||||||
lenids := f.count()
|
|
||||||
if lenids == 0 {
|
|
||||||
p.count = uint16(lenids)
|
|
||||||
} else if lenids < 0xFFFF {
|
|
||||||
p.count = uint16(lenids)
|
|
||||||
f.copyall(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[:])
|
|
||||||
} else {
|
|
||||||
p.count = 0xFFFF
|
|
||||||
((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[0] = pgid(lenids)
|
|
||||||
f.copyall(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[1:])
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// reload reads the freelist from a page and filters out pending items.
|
|
||||||
func (f *freelist) reload(p *page) {
|
|
||||||
f.read(p)
|
|
||||||
|
|
||||||
// Build a cache of only pending pages.
|
|
||||||
pcache := make(map[pgid]bool)
|
|
||||||
for _, pendingIDs := range f.pending {
|
|
||||||
for _, pendingID := range pendingIDs {
|
|
||||||
pcache[pendingID] = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check each page in the freelist and build a new available freelist
|
|
||||||
// with any pages not in the pending lists.
|
|
||||||
var a []pgid
|
|
||||||
for _, id := range f.ids {
|
|
||||||
if !pcache[id] {
|
|
||||||
a = append(a, id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
f.ids = a
|
|
||||||
|
|
||||||
// Once the available list is rebuilt then rebuild the free cache so that
|
|
||||||
// it includes the available and pending free pages.
|
|
||||||
f.reindex()
|
|
||||||
}
|
|
||||||
|
|
||||||
// reindex rebuilds the free cache based on available and pending free lists.
|
|
||||||
func (f *freelist) reindex() {
|
|
||||||
f.cache = make(map[pgid]bool, len(f.ids))
|
|
||||||
for _, id := range f.ids {
|
|
||||||
f.cache[id] = true
|
|
||||||
}
|
|
||||||
for _, pendingIDs := range f.pending {
|
|
||||||
for _, pendingID := range pendingIDs {
|
|
||||||
f.cache[pendingID] = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+1
@@ -2,3 +2,4 @@
|
|||||||
*.test
|
*.test
|
||||||
*.swp
|
*.swp
|
||||||
/bin/
|
/bin/
|
||||||
|
cover.out
|
||||||
+17
@@ -0,0 +1,17 @@
|
|||||||
|
language: go
|
||||||
|
go_import_path: go.etcd.io/bbolt
|
||||||
|
|
||||||
|
sudo: false
|
||||||
|
|
||||||
|
go:
|
||||||
|
- 1.12
|
||||||
|
|
||||||
|
before_install:
|
||||||
|
- go get -v honnef.co/go/tools/...
|
||||||
|
- go get -v github.com/kisielk/errcheck
|
||||||
|
|
||||||
|
script:
|
||||||
|
- make fmt
|
||||||
|
- make test
|
||||||
|
- make race
|
||||||
|
# - make errcheck
|
||||||
File renamed without changes.
+38
@@ -0,0 +1,38 @@
|
|||||||
|
BRANCH=`git rev-parse --abbrev-ref HEAD`
|
||||||
|
COMMIT=`git rev-parse --short HEAD`
|
||||||
|
GOLDFLAGS="-X main.branch $(BRANCH) -X main.commit $(COMMIT)"
|
||||||
|
|
||||||
|
default: build
|
||||||
|
|
||||||
|
race:
|
||||||
|
@TEST_FREELIST_TYPE=hashmap go test -v -race -test.run="TestSimulate_(100op|1000op)"
|
||||||
|
@echo "array freelist test"
|
||||||
|
@TEST_FREELIST_TYPE=array go test -v -race -test.run="TestSimulate_(100op|1000op)"
|
||||||
|
|
||||||
|
fmt:
|
||||||
|
!(gofmt -l -s -d $(shell find . -name \*.go) | grep '[a-z]')
|
||||||
|
|
||||||
|
# go get honnef.co/go/tools/simple
|
||||||
|
gosimple:
|
||||||
|
gosimple ./...
|
||||||
|
|
||||||
|
# go get honnef.co/go/tools/unused
|
||||||
|
unused:
|
||||||
|
unused ./...
|
||||||
|
|
||||||
|
# go get github.com/kisielk/errcheck
|
||||||
|
errcheck:
|
||||||
|
@errcheck -ignorepkg=bytes -ignore=os:Remove go.etcd.io/bbolt
|
||||||
|
|
||||||
|
test:
|
||||||
|
TEST_FREELIST_TYPE=hashmap go test -timeout 20m -v -coverprofile cover.out -covermode atomic
|
||||||
|
# Note: gets "program not an importable package" in out of path builds
|
||||||
|
TEST_FREELIST_TYPE=hashmap go test -v ./cmd/bbolt
|
||||||
|
|
||||||
|
@echo "array freelist test"
|
||||||
|
|
||||||
|
@TEST_FREELIST_TYPE=array go test -timeout 20m -v -coverprofile cover.out -covermode atomic
|
||||||
|
# Note: gets "program not an importable package" in out of path builds
|
||||||
|
@TEST_FREELIST_TYPE=array go test -v ./cmd/bbolt
|
||||||
|
|
||||||
|
.PHONY: race fmt errcheck test gosimple unused
|
||||||
+123
-82
@@ -1,5 +1,18 @@
|
|||||||
Bolt [](https://coveralls.io/r/boltdb/bolt?branch=master) [](https://godoc.org/github.com/boltdb/bolt) 
|
bbolt
|
||||||
====
|
=====
|
||||||
|
|
||||||
|
[](https://goreportcard.com/report/github.com/etcd-io/bbolt)
|
||||||
|
[](https://codecov.io/gh/etcd-io/bbolt)
|
||||||
|
[](https://travis-ci.com/etcd-io/bbolt)
|
||||||
|
[](https://godoc.org/github.com/etcd-io/bbolt)
|
||||||
|
[](https://github.com/etcd-io/bbolt/releases)
|
||||||
|
[](https://github.com/etcd-io/bbolt/blob/master/LICENSE)
|
||||||
|
|
||||||
|
bbolt is a fork of [Ben Johnson's][gh_ben] [Bolt][bolt] key/value
|
||||||
|
store. The purpose of this fork is to provide the Go community with an active
|
||||||
|
maintenance and development target for Bolt; the goal is improved reliability
|
||||||
|
and stability. bbolt includes bug fixes, performance enhancements, and features
|
||||||
|
not found in Bolt while preserving backwards compatibility with the Bolt API.
|
||||||
|
|
||||||
Bolt is a pure Go key/value store inspired by [Howard Chu's][hyc_symas]
|
Bolt is a pure Go key/value store inspired by [Howard Chu's][hyc_symas]
|
||||||
[LMDB project][lmdb]. The goal of the project is to provide a simple,
|
[LMDB project][lmdb]. The goal of the project is to provide a simple,
|
||||||
@@ -10,6 +23,8 @@ Since Bolt is meant to be used as such a low-level piece of functionality,
|
|||||||
simplicity is key. The API will be small and only focus on getting values
|
simplicity is key. The API will be small and only focus on getting values
|
||||||
and setting values. That's it.
|
and setting values. That's it.
|
||||||
|
|
||||||
|
[gh_ben]: https://github.com/benbjohnson
|
||||||
|
[bolt]: https://github.com/boltdb/bolt
|
||||||
[hyc_symas]: https://twitter.com/hyc_symas
|
[hyc_symas]: https://twitter.com/hyc_symas
|
||||||
[lmdb]: http://symas.com/mdb/
|
[lmdb]: http://symas.com/mdb/
|
||||||
|
|
||||||
@@ -21,36 +36,42 @@ consistency and thread safety. Bolt is currently used in high-load production
|
|||||||
environments serving databases as large as 1TB. Many companies such as
|
environments serving databases as large as 1TB. Many companies such as
|
||||||
Shopify and Heroku use Bolt-backed services every day.
|
Shopify and Heroku use Bolt-backed services every day.
|
||||||
|
|
||||||
|
## Project versioning
|
||||||
|
|
||||||
|
bbolt uses [semantic versioning](http://semver.org).
|
||||||
|
API should not change between patch and minor releases.
|
||||||
|
New minor versions may add additional features to the API.
|
||||||
|
|
||||||
## Table of Contents
|
## Table of Contents
|
||||||
|
|
||||||
- [Getting Started](#getting-started)
|
- [Getting Started](#getting-started)
|
||||||
- [Installing](#installing)
|
- [Installing](#installing)
|
||||||
- [Opening a database](#opening-a-database)
|
- [Opening a database](#opening-a-database)
|
||||||
- [Transactions](#transactions)
|
- [Transactions](#transactions)
|
||||||
- [Read-write transactions](#read-write-transactions)
|
- [Read-write transactions](#read-write-transactions)
|
||||||
- [Read-only transactions](#read-only-transactions)
|
- [Read-only transactions](#read-only-transactions)
|
||||||
- [Batch read-write transactions](#batch-read-write-transactions)
|
- [Batch read-write transactions](#batch-read-write-transactions)
|
||||||
- [Managing transactions manually](#managing-transactions-manually)
|
- [Managing transactions manually](#managing-transactions-manually)
|
||||||
- [Using buckets](#using-buckets)
|
- [Using buckets](#using-buckets)
|
||||||
- [Using key/value pairs](#using-keyvalue-pairs)
|
- [Using key/value pairs](#using-keyvalue-pairs)
|
||||||
- [Autoincrementing integer for the bucket](#autoincrementing-integer-for-the-bucket)
|
- [Autoincrementing integer for the bucket](#autoincrementing-integer-for-the-bucket)
|
||||||
- [Iterating over keys](#iterating-over-keys)
|
- [Iterating over keys](#iterating-over-keys)
|
||||||
- [Prefix scans](#prefix-scans)
|
- [Prefix scans](#prefix-scans)
|
||||||
- [Range scans](#range-scans)
|
- [Range scans](#range-scans)
|
||||||
- [ForEach()](#foreach)
|
- [ForEach()](#foreach)
|
||||||
- [Nested buckets](#nested-buckets)
|
- [Nested buckets](#nested-buckets)
|
||||||
- [Database backups](#database-backups)
|
- [Database backups](#database-backups)
|
||||||
- [Statistics](#statistics)
|
- [Statistics](#statistics)
|
||||||
- [Read-Only Mode](#read-only-mode)
|
- [Read-Only Mode](#read-only-mode)
|
||||||
- [Mobile Use (iOS/Android)](#mobile-use-iosandroid)
|
- [Mobile Use (iOS/Android)](#mobile-use-iosandroid)
|
||||||
- [Resources](#resources)
|
- [Resources](#resources)
|
||||||
- [Comparison with other databases](#comparison-with-other-databases)
|
- [Comparison with other databases](#comparison-with-other-databases)
|
||||||
- [Postgres, MySQL, & other relational databases](#postgres-mysql--other-relational-databases)
|
- [Postgres, MySQL, & other relational databases](#postgres-mysql--other-relational-databases)
|
||||||
- [LevelDB, RocksDB](#leveldb-rocksdb)
|
- [LevelDB, RocksDB](#leveldb-rocksdb)
|
||||||
- [LMDB](#lmdb)
|
- [LMDB](#lmdb)
|
||||||
- [Caveats & Limitations](#caveats--limitations)
|
- [Caveats & Limitations](#caveats--limitations)
|
||||||
- [Reading the Source](#reading-the-source)
|
- [Reading the Source](#reading-the-source)
|
||||||
- [Other Projects Using Bolt](#other-projects-using-bolt)
|
- [Other Projects Using Bolt](#other-projects-using-bolt)
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
|
|
||||||
@@ -59,13 +80,28 @@ Shopify and Heroku use Bolt-backed services every day.
|
|||||||
To start using Bolt, install Go and run `go get`:
|
To start using Bolt, install Go and run `go get`:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ go get github.com/boltdb/bolt/...
|
$ go get go.etcd.io/bbolt/...
|
||||||
```
|
```
|
||||||
|
|
||||||
This will retrieve the library and install the `bolt` command line utility into
|
This will retrieve the library and install the `bolt` command line utility into
|
||||||
your `$GOBIN` path.
|
your `$GOBIN` path.
|
||||||
|
|
||||||
|
|
||||||
|
### Importing bbolt
|
||||||
|
|
||||||
|
To use bbolt as an embedded key-value store, import as:
|
||||||
|
|
||||||
|
```go
|
||||||
|
import bolt "go.etcd.io/bbolt"
|
||||||
|
|
||||||
|
db, err := bolt.Open(path, 0666, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Opening a database
|
### Opening a database
|
||||||
|
|
||||||
The top-level object in Bolt is a `DB`. It is represented as a single file on
|
The top-level object in Bolt is a `DB`. It is represented as a single file on
|
||||||
@@ -79,7 +115,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/boltdb/bolt"
|
bolt "go.etcd.io/bbolt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -116,11 +152,12 @@ are not thread safe. To work with data in multiple goroutines you must start
|
|||||||
a transaction for each one or use locking to ensure only one goroutine accesses
|
a transaction for each one or use locking to ensure only one goroutine accesses
|
||||||
a transaction at a time. Creating transaction from the `DB` is thread safe.
|
a transaction at a time. Creating transaction from the `DB` is thread safe.
|
||||||
|
|
||||||
Read-only transactions and read-write transactions should not depend on one
|
Transactions should not depend on one another and generally shouldn't be opened
|
||||||
another and generally shouldn't be opened simultaneously in the same goroutine.
|
simultaneously in the same goroutine. This can cause a deadlock as the read-write
|
||||||
This can cause a deadlock as the read-write transaction needs to periodically
|
transaction needs to periodically re-map the data file but it cannot do so while
|
||||||
re-map the data file but it cannot do so while a read-only transaction is open.
|
any read-only transaction is open. Even a nested read-only transaction can cause
|
||||||
|
a deadlock, as the child transaction can block the parent transaction from releasing
|
||||||
|
its resources.
|
||||||
|
|
||||||
#### Read-write transactions
|
#### Read-write transactions
|
||||||
|
|
||||||
@@ -239,7 +276,7 @@ should be writable.
|
|||||||
### Using buckets
|
### Using buckets
|
||||||
|
|
||||||
Buckets are collections of key/value pairs within the database. All keys in a
|
Buckets are collections of key/value pairs within the database. All keys in a
|
||||||
bucket must be unique. You can create a bucket using the `DB.CreateBucket()`
|
bucket must be unique. You can create a bucket using the `Tx.CreateBucket()`
|
||||||
function:
|
function:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -522,7 +559,7 @@ this from a read-only transaction, it will perform a hot backup and not block
|
|||||||
your other database reads and writes.
|
your other database reads and writes.
|
||||||
|
|
||||||
By default, it will use a regular file handle which will utilize the operating
|
By default, it will use a regular file handle which will utilize the operating
|
||||||
system's page cache. See the [`Tx`](https://godoc.org/github.com/boltdb/bolt#Tx)
|
system's page cache. See the [`Tx`](https://godoc.org/go.etcd.io/bbolt#Tx)
|
||||||
documentation for information about optimizing for larger-than-RAM datasets.
|
documentation for information about optimizing for larger-than-RAM datasets.
|
||||||
|
|
||||||
One common use case is to backup over HTTP so you can use tools like `cURL` to
|
One common use case is to backup over HTTP so you can use tools like `cURL` to
|
||||||
@@ -811,7 +848,7 @@ Here are a few things to note when evaluating and using Bolt:
|
|||||||
|
|
||||||
## Reading the Source
|
## Reading the Source
|
||||||
|
|
||||||
Bolt is a relatively small code base (<3KLOC) for an embedded, serializable,
|
Bolt is a relatively small code base (<5KLOC) for an embedded, serializable,
|
||||||
transactional key/value database so it can be a good starting point for people
|
transactional key/value database so it can be a good starting point for people
|
||||||
interested in how databases work.
|
interested in how databases work.
|
||||||
|
|
||||||
@@ -863,54 +900,58 @@ them via pull request.
|
|||||||
|
|
||||||
Below is a list of public, open source projects that use Bolt:
|
Below is a list of public, open source projects that use Bolt:
|
||||||
|
|
||||||
* [BoltDbWeb](https://github.com/evnix/boltdbweb) - A web based GUI for BoltDB files.
|
* [Algernon](https://github.com/xyproto/algernon) - A HTTP/2 web server with built-in support for Lua. Uses BoltDB as the default database backend.
|
||||||
* [Operation Go: A Routine Mission](http://gocode.io) - An online programming game for Golang using Bolt for user accounts and a leaderboard.
|
|
||||||
* [Bazil](https://bazil.org/) - A file system that lets your data reside where it is most convenient for it to reside.
|
* [Bazil](https://bazil.org/) - A file system that lets your data reside where it is most convenient for it to reside.
|
||||||
* [DVID](https://github.com/janelia-flyem/dvid) - Added Bolt as optional storage engine and testing it against Basho-tuned leveldb.
|
* [bolter](https://github.com/hasit/bolter) - Command-line app for viewing BoltDB file in your terminal.
|
||||||
* [Skybox Analytics](https://github.com/skybox/skybox) - A standalone funnel analysis tool for web analytics.
|
* [boltcli](https://github.com/spacewander/boltcli) - the redis-cli for boltdb with Lua script support.
|
||||||
* [Scuttlebutt](https://github.com/benbjohnson/scuttlebutt) - Uses Bolt to store and process all Twitter mentions of GitHub projects.
|
* [BoltHold](https://github.com/timshannon/bolthold) - An embeddable NoSQL store for Go types built on BoltDB
|
||||||
* [Wiki](https://github.com/peterhellberg/wiki) - A tiny wiki using Goji, BoltDB and Blackfriday.
|
|
||||||
* [ChainStore](https://github.com/pressly/chainstore) - Simple key-value interface to a variety of storage engines organized as a chain of operations.
|
|
||||||
* [MetricBase](https://github.com/msiebuhr/MetricBase) - Single-binary version of Graphite.
|
|
||||||
* [Gitchain](https://github.com/gitchain/gitchain) - Decentralized, peer-to-peer Git repositories aka "Git meets Bitcoin".
|
|
||||||
* [event-shuttle](https://github.com/sclasen/event-shuttle) - A Unix system service to collect and reliably deliver messages to Kafka.
|
|
||||||
* [ipxed](https://github.com/kelseyhightower/ipxed) - Web interface and api for ipxed.
|
|
||||||
* [BoltStore](https://github.com/yosssi/boltstore) - Session store using Bolt.
|
* [BoltStore](https://github.com/yosssi/boltstore) - Session store using Bolt.
|
||||||
* [photosite/session](https://godoc.org/bitbucket.org/kardianos/photosite/session) - Sessions for a photo viewing site.
|
* [Boltdb Boilerplate](https://github.com/bobintornado/boltdb-boilerplate) - Boilerplate wrapper around bolt aiming to make simple calls one-liners.
|
||||||
* [LedisDB](https://github.com/siddontang/ledisdb) - A high performance NoSQL, using Bolt as optional storage.
|
* [BoltDbWeb](https://github.com/evnix/boltdbweb) - A web based GUI for BoltDB files.
|
||||||
* [ipLocator](https://github.com/AndreasBriese/ipLocator) - A fast ip-geo-location-server using bolt with bloom filters.
|
|
||||||
* [cayley](https://github.com/google/cayley) - Cayley is an open-source graph database using Bolt as optional backend.
|
|
||||||
* [bleve](http://www.blevesearch.com/) - A pure Go search engine similar to ElasticSearch that uses Bolt as the default storage backend.
|
* [bleve](http://www.blevesearch.com/) - A pure Go search engine similar to ElasticSearch that uses Bolt as the default storage backend.
|
||||||
* [tentacool](https://github.com/optiflows/tentacool) - REST api server to manage system stuff (IP, DNS, Gateway...) on a linux server.
|
* [btcwallet](https://github.com/btcsuite/btcwallet) - A bitcoin wallet.
|
||||||
* [Seaweed File System](https://github.com/chrislusf/seaweedfs) - Highly scalable distributed key~file system with O(1) disk read.
|
|
||||||
* [InfluxDB](https://influxdata.com) - Scalable datastore for metrics, events, and real-time analytics.
|
|
||||||
* [Freehold](http://tshannon.bitbucket.org/freehold/) - An open, secure, and lightweight platform for your files and data.
|
|
||||||
* [Prometheus Annotation Server](https://github.com/oliver006/prom_annotation_server) - Annotation server for PromDash & Prometheus service monitoring system.
|
|
||||||
* [Consul](https://github.com/hashicorp/consul) - Consul is service discovery and configuration made easy. Distributed, highly available, and datacenter-aware.
|
|
||||||
* [Kala](https://github.com/ajvb/kala) - Kala is a modern job scheduler optimized to run on a single node. It is persistent, JSON over HTTP API, ISO 8601 duration notation, and dependent jobs.
|
|
||||||
* [drive](https://github.com/odeke-em/drive) - drive is an unofficial Google Drive command line client for \*NIX operating systems.
|
|
||||||
* [stow](https://github.com/djherbis/stow) - a persistence manager for objects
|
|
||||||
backed by boltdb.
|
|
||||||
* [buckets](https://github.com/joyrexus/buckets) - a bolt wrapper streamlining
|
* [buckets](https://github.com/joyrexus/buckets) - a bolt wrapper streamlining
|
||||||
simple tx and key scans.
|
simple tx and key scans.
|
||||||
* [mbuckets](https://github.com/abhigupta912/mbuckets) - A Bolt wrapper that allows easy operations on multi level (nested) buckets.
|
* [cayley](https://github.com/google/cayley) - Cayley is an open-source graph database using Bolt as optional backend.
|
||||||
* [Request Baskets](https://github.com/darklynx/request-baskets) - A web service to collect arbitrary HTTP requests and inspect them via REST API or simple web UI, similar to [RequestBin](http://requestb.in/) service
|
* [ChainStore](https://github.com/pressly/chainstore) - Simple key-value interface to a variety of storage engines organized as a chain of operations.
|
||||||
* [Go Report Card](https://goreportcard.com/) - Go code quality report cards as a (free and open source) service.
|
* [Consul](https://github.com/hashicorp/consul) - Consul is service discovery and configuration made easy. Distributed, highly available, and datacenter-aware.
|
||||||
* [Boltdb Boilerplate](https://github.com/bobintornado/boltdb-boilerplate) - Boilerplate wrapper around bolt aiming to make simple calls one-liners.
|
* [DVID](https://github.com/janelia-flyem/dvid) - Added Bolt as optional storage engine and testing it against Basho-tuned leveldb.
|
||||||
* [lru](https://github.com/crowdriff/lru) - Easy to use Bolt-backed Least-Recently-Used (LRU) read-through cache with chainable remote stores.
|
|
||||||
* [Storm](https://github.com/asdine/storm) - Simple and powerful ORM for BoltDB.
|
|
||||||
* [GoWebApp](https://github.com/josephspurrier/gowebapp) - A basic MVC web application in Go using BoltDB.
|
|
||||||
* [SimpleBolt](https://github.com/xyproto/simplebolt) - A simple way to use BoltDB. Deals mainly with strings.
|
|
||||||
* [Algernon](https://github.com/xyproto/algernon) - A HTTP/2 web server with built-in support for Lua. Uses BoltDB as the default database backend.
|
|
||||||
* [MuLiFS](https://github.com/dankomiocevic/mulifs) - Music Library Filesystem creates a filesystem to organise your music files.
|
|
||||||
* [GoShort](https://github.com/pankajkhairnar/goShort) - GoShort is a URL shortener written in Golang and BoltDB for persistent key/value storage and for routing it's using high performent HTTPRouter.
|
|
||||||
* [torrent](https://github.com/anacrolix/torrent) - Full-featured BitTorrent client package and utilities in Go. BoltDB is a storage backend in development.
|
|
||||||
* [gopherpit](https://github.com/gopherpit/gopherpit) - A web service to manage Go remote import paths with custom domains
|
|
||||||
* [bolter](https://github.com/hasit/bolter) - Command-line app for viewing BoltDB file in your terminal.
|
|
||||||
* [btcwallet](https://github.com/btcsuite/btcwallet) - A bitcoin wallet.
|
|
||||||
* [dcrwallet](https://github.com/decred/dcrwallet) - A wallet for the Decred cryptocurrency.
|
* [dcrwallet](https://github.com/decred/dcrwallet) - A wallet for the Decred cryptocurrency.
|
||||||
|
* [drive](https://github.com/odeke-em/drive) - drive is an unofficial Google Drive command line client for \*NIX operating systems.
|
||||||
|
* [event-shuttle](https://github.com/sclasen/event-shuttle) - A Unix system service to collect and reliably deliver messages to Kafka.
|
||||||
|
* [Freehold](http://tshannon.bitbucket.org/freehold/) - An open, secure, and lightweight platform for your files and data.
|
||||||
|
* [Go Report Card](https://goreportcard.com/) - Go code quality report cards as a (free and open source) service.
|
||||||
|
* [GoWebApp](https://github.com/josephspurrier/gowebapp) - A basic MVC web application in Go using BoltDB.
|
||||||
|
* [GoShort](https://github.com/pankajkhairnar/goShort) - GoShort is a URL shortener written in Golang and BoltDB for persistent key/value storage and for routing it's using high performent HTTPRouter.
|
||||||
|
* [gopherpit](https://github.com/gopherpit/gopherpit) - A web service to manage Go remote import paths with custom domains
|
||||||
|
* [gokv](https://github.com/philippgille/gokv) - Simple key-value store abstraction and implementations for Go (Redis, Consul, etcd, bbolt, BadgerDB, LevelDB, Memcached, DynamoDB, S3, PostgreSQL, MongoDB, CockroachDB and many more)
|
||||||
|
* [Gitchain](https://github.com/gitchain/gitchain) - Decentralized, peer-to-peer Git repositories aka "Git meets Bitcoin".
|
||||||
|
* [InfluxDB](https://influxdata.com) - Scalable datastore for metrics, events, and real-time analytics.
|
||||||
|
* [ipLocator](https://github.com/AndreasBriese/ipLocator) - A fast ip-geo-location-server using bolt with bloom filters.
|
||||||
|
* [ipxed](https://github.com/kelseyhightower/ipxed) - Web interface and api for ipxed.
|
||||||
* [Ironsmith](https://github.com/timshannon/ironsmith) - A simple, script-driven continuous integration (build - > test -> release) tool, with no external dependencies
|
* [Ironsmith](https://github.com/timshannon/ironsmith) - A simple, script-driven continuous integration (build - > test -> release) tool, with no external dependencies
|
||||||
* [BoltHold](https://github.com/timshannon/bolthold) - An embeddable NoSQL store for Go types built on BoltDB
|
* [Kala](https://github.com/ajvb/kala) - Kala is a modern job scheduler optimized to run on a single node. It is persistent, JSON over HTTP API, ISO 8601 duration notation, and dependent jobs.
|
||||||
* [Ponzu CMS](https://ponzu-cms.org) - Headless CMS + automatic JSON API with auto-HTTPS, HTTP/2 Server Push, and flexible server framework.
|
* [Key Value Access Langusge (KVAL)](https://github.com/kval-access-language) - A proposed grammar for key-value datastores offering a bbolt binding.
|
||||||
|
* [LedisDB](https://github.com/siddontang/ledisdb) - A high performance NoSQL, using Bolt as optional storage.
|
||||||
|
* [lru](https://github.com/crowdriff/lru) - Easy to use Bolt-backed Least-Recently-Used (LRU) read-through cache with chainable remote stores.
|
||||||
|
* [mbuckets](https://github.com/abhigupta912/mbuckets) - A Bolt wrapper that allows easy operations on multi level (nested) buckets.
|
||||||
|
* [MetricBase](https://github.com/msiebuhr/MetricBase) - Single-binary version of Graphite.
|
||||||
|
* [MuLiFS](https://github.com/dankomiocevic/mulifs) - Music Library Filesystem creates a filesystem to organise your music files.
|
||||||
|
* [NATS](https://github.com/nats-io/nats-streaming-server) - NATS Streaming uses bbolt for message and metadata storage.
|
||||||
|
* [Operation Go: A Routine Mission](http://gocode.io) - An online programming game for Golang using Bolt for user accounts and a leaderboard.
|
||||||
|
* [photosite/session](https://godoc.org/bitbucket.org/kardianos/photosite/session) - Sessions for a photo viewing site.
|
||||||
|
* [Prometheus Annotation Server](https://github.com/oliver006/prom_annotation_server) - Annotation server for PromDash & Prometheus service monitoring system.
|
||||||
|
* [reef-pi](https://github.com/reef-pi/reef-pi) - reef-pi is an award winning, modular, DIY reef tank controller using easy to learn electronics based on a Raspberry Pi.
|
||||||
|
* [Request Baskets](https://github.com/darklynx/request-baskets) - A web service to collect arbitrary HTTP requests and inspect them via REST API or simple web UI, similar to [RequestBin](http://requestb.in/) service
|
||||||
|
* [Seaweed File System](https://github.com/chrislusf/seaweedfs) - Highly scalable distributed key~file system with O(1) disk read.
|
||||||
|
* [stow](https://github.com/djherbis/stow) - a persistence manager for objects
|
||||||
|
backed by boltdb.
|
||||||
|
* [Storm](https://github.com/asdine/storm) - Simple and powerful ORM for BoltDB.
|
||||||
|
* [SimpleBolt](https://github.com/xyproto/simplebolt) - A simple way to use BoltDB. Deals mainly with strings.
|
||||||
|
* [Skybox Analytics](https://github.com/skybox/skybox) - A standalone funnel analysis tool for web analytics.
|
||||||
|
* [Scuttlebutt](https://github.com/benbjohnson/scuttlebutt) - Uses Bolt to store and process all Twitter mentions of GitHub projects.
|
||||||
|
* [tentacool](https://github.com/optiflows/tentacool) - REST api server to manage system stuff (IP, DNS, Gateway...) on a linux server.
|
||||||
|
* [torrent](https://github.com/anacrolix/torrent) - Full-featured BitTorrent client package and utilities in Go. BoltDB is a storage backend in development.
|
||||||
|
* [Wiki](https://github.com/peterhellberg/wiki) - A tiny wiki using Goji, BoltDB and Blackfriday.
|
||||||
|
|
||||||
If you are using Bolt in a project please send a pull request to add it to the list.
|
If you are using Bolt in a project please send a pull request to add it to the list.
|
||||||
+1
-4
@@ -1,10 +1,7 @@
|
|||||||
package bolt
|
package bbolt
|
||||||
|
|
||||||
// maxMapSize represents the largest mmap size supported by Bolt.
|
// maxMapSize represents the largest mmap size supported by Bolt.
|
||||||
const maxMapSize = 0x7FFFFFFF // 2GB
|
const maxMapSize = 0x7FFFFFFF // 2GB
|
||||||
|
|
||||||
// maxAllocSize is the size used when creating array pointers.
|
// maxAllocSize is the size used when creating array pointers.
|
||||||
const maxAllocSize = 0xFFFFFFF
|
const maxAllocSize = 0xFFFFFFF
|
||||||
|
|
||||||
// Are unaligned load/stores broken on this arch?
|
|
||||||
var brokenUnaligned = false
|
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
package bbolt
|
||||||
|
|
||||||
|
// maxMapSize represents the largest mmap size supported by Bolt.
|
||||||
|
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
||||||
|
|
||||||
|
// maxAllocSize is the size used when creating array pointers.
|
||||||
|
const maxAllocSize = 0x7FFFFFFF
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
package bbolt
|
||||||
|
|
||||||
|
// maxMapSize represents the largest mmap size supported by Bolt.
|
||||||
|
const maxMapSize = 0x7FFFFFFF // 2GB
|
||||||
|
|
||||||
|
// maxAllocSize is the size used when creating array pointers.
|
||||||
|
const maxAllocSize = 0xFFFFFFF
|
||||||
Generated
Vendored
+1
-4
@@ -1,12 +1,9 @@
|
|||||||
// +build arm64
|
// +build arm64
|
||||||
|
|
||||||
package bolt
|
package bbolt
|
||||||
|
|
||||||
// maxMapSize represents the largest mmap size supported by Bolt.
|
// maxMapSize represents the largest mmap size supported by Bolt.
|
||||||
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
||||||
|
|
||||||
// maxAllocSize is the size used when creating array pointers.
|
// maxAllocSize is the size used when creating array pointers.
|
||||||
const maxAllocSize = 0x7FFFFFFF
|
const maxAllocSize = 0x7FFFFFFF
|
||||||
|
|
||||||
// Are unaligned load/stores broken on this arch?
|
|
||||||
var brokenUnaligned = false
|
|
||||||
Generated
Vendored
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package bolt
|
package bbolt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"syscall"
|
"syscall"
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
// +build mips64 mips64le
|
||||||
|
|
||||||
|
package bbolt
|
||||||
|
|
||||||
|
// maxMapSize represents the largest mmap size supported by Bolt.
|
||||||
|
const maxMapSize = 0x8000000000 // 512GB
|
||||||
|
|
||||||
|
// maxAllocSize is the size used when creating array pointers.
|
||||||
|
const maxAllocSize = 0x7FFFFFFF
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
// +build mips mipsle
|
||||||
|
|
||||||
|
package bbolt
|
||||||
|
|
||||||
|
// maxMapSize represents the largest mmap size supported by Bolt.
|
||||||
|
const maxMapSize = 0x40000000 // 1GB
|
||||||
|
|
||||||
|
// maxAllocSize is the size used when creating array pointers.
|
||||||
|
const maxAllocSize = 0xFFFFFFF
|
||||||
Generated
Vendored
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package bolt
|
package bbolt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"syscall"
|
"syscall"
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
// +build ppc
|
// +build ppc
|
||||||
|
|
||||||
package bolt
|
package bbolt
|
||||||
|
|
||||||
// maxMapSize represents the largest mmap size supported by Bolt.
|
// maxMapSize represents the largest mmap size supported by Bolt.
|
||||||
const maxMapSize = 0x7FFFFFFF // 2GB
|
const maxMapSize = 0x7FFFFFFF // 2GB
|
||||||
Generated
Vendored
+1
-4
@@ -1,12 +1,9 @@
|
|||||||
// +build ppc64
|
// +build ppc64
|
||||||
|
|
||||||
package bolt
|
package bbolt
|
||||||
|
|
||||||
// maxMapSize represents the largest mmap size supported by Bolt.
|
// maxMapSize represents the largest mmap size supported by Bolt.
|
||||||
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
||||||
|
|
||||||
// maxAllocSize is the size used when creating array pointers.
|
// maxAllocSize is the size used when creating array pointers.
|
||||||
const maxAllocSize = 0x7FFFFFFF
|
const maxAllocSize = 0x7FFFFFFF
|
||||||
|
|
||||||
// Are unaligned load/stores broken on this arch?
|
|
||||||
var brokenUnaligned = false
|
|
||||||
Generated
Vendored
+1
-4
@@ -1,12 +1,9 @@
|
|||||||
// +build ppc64le
|
// +build ppc64le
|
||||||
|
|
||||||
package bolt
|
package bbolt
|
||||||
|
|
||||||
// maxMapSize represents the largest mmap size supported by Bolt.
|
// maxMapSize represents the largest mmap size supported by Bolt.
|
||||||
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
||||||
|
|
||||||
// maxAllocSize is the size used when creating array pointers.
|
// maxAllocSize is the size used when creating array pointers.
|
||||||
const maxAllocSize = 0x7FFFFFFF
|
const maxAllocSize = 0x7FFFFFFF
|
||||||
|
|
||||||
// Are unaligned load/stores broken on this arch?
|
|
||||||
var brokenUnaligned = false
|
|
||||||
Generated
Vendored
+3
-4
@@ -1,10 +1,9 @@
|
|||||||
package bolt
|
// +build riscv64
|
||||||
|
|
||||||
|
package bbolt
|
||||||
|
|
||||||
// maxMapSize represents the largest mmap size supported by Bolt.
|
// maxMapSize represents the largest mmap size supported by Bolt.
|
||||||
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
||||||
|
|
||||||
// maxAllocSize is the size used when creating array pointers.
|
// maxAllocSize is the size used when creating array pointers.
|
||||||
const maxAllocSize = 0x7FFFFFFF
|
const maxAllocSize = 0x7FFFFFFF
|
||||||
|
|
||||||
// Are unaligned load/stores broken on this arch?
|
|
||||||
var brokenUnaligned = false
|
|
||||||
Generated
Vendored
+1
-4
@@ -1,12 +1,9 @@
|
|||||||
// +build s390x
|
// +build s390x
|
||||||
|
|
||||||
package bolt
|
package bbolt
|
||||||
|
|
||||||
// maxMapSize represents the largest mmap size supported by Bolt.
|
// maxMapSize represents the largest mmap size supported by Bolt.
|
||||||
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
||||||
|
|
||||||
// maxAllocSize is the size used when creating array pointers.
|
// maxAllocSize is the size used when creating array pointers.
|
||||||
const maxAllocSize = 0x7FFFFFFF
|
const maxAllocSize = 0x7FFFFFFF
|
||||||
|
|
||||||
// Are unaligned load/stores broken on this arch?
|
|
||||||
var brokenUnaligned = false
|
|
||||||
Loaded 100 of 158 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user