diff --git a/README.md b/README.md index da118cfa..80cf72ba 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ By default, the configuration file is expected to be at `config/config.yaml`. You can specify a custom path by setting the `GATUS_CONFIG_FILE` environment variable. +Here's a simple example: + ```yaml metrics: true # Whether to expose metrics at /metrics services: @@ -23,13 +25,9 @@ services: interval: 15s # Duration to wait between every status check (default: 10s) conditions: - "[STATUS] == 200" # Status must be 200 + - "[BODY].status == UP" # The json path "$.status" must be equal to UP - "[RESPONSE_TIME] < 300" # Response time must be under 300ms - - name: github - url: https://api.github.com/healthz - interval: 2m - conditions: - - "[STATUS] == 200" - - name: Example + - name: example url: https://example.org/ interval: 30s conditions: @@ -39,6 +37,20 @@ services: Note that you can also add environment variables in the your configuration file (i.e. `$DOMAIN`, `${DOMAIN}`) +### Configuration + +| Parameter | Description | Default | +| ----------------------- | ------------------------------------------------------ | -------------- | +| `metrics` | Whether to expose metrics at /metrics | `false` +| `services[].name` | Name of the service. Can be anything. | Required `""` +| `services[].url` | URL to send the request to | Required `""` +| `services[].conditions` | Conditions used to determine the health of the service | `[]` +| `services[].interval` | Duration to wait between every status check | `10s` +| `services[].method` | Request method | `GET` +| `services[].body` | Request body | `""` +| `services[].headers` | Request headers | `{}` + + ### Conditions Here are some examples of conditions you can use: @@ -52,6 +64,7 @@ Here are some examples of conditions you can use: | `[RESPONSE_TIME] < 500` | Response time must be below 500ms | 100ms, 200ms, 300ms | 500ms, 1500ms | | `[BODY] == 1` | The body must be equal to 1 | 1 | literally anything else | | `[BODY].data.id == 1` | The jsonpath `$.data.id` is equal to 1 | `{"data":{"id":1}}` | literally anything else | +| `[BODY].data[0].id == 1` | The jsonpath `$.data[0].id` is equal to 1 | `{"data":[{"id":1}]}` | literally anything else | **NOTE**: `[BODY]` with JSON path (i.e. `[BODY].id == 1`) is currently in BETA. For the most part, the only thing that doesn't work is arrays. diff --git a/config.yaml b/config.yaml index 796209cc..b577ef2c 100644 --- a/config.yaml +++ b/config.yaml @@ -1,21 +1,21 @@ metrics: true services: -# - name: twinnation -# interval: 10s -# url: https://twinnation.org/health -# conditions: -# - "[STATUS] == 200" -# - "[RESPONSE_TIME] < 1000" -# - "[BODY].status == UP" - - name: twinnation-articles-api + - name: twinnation interval: 10s - url: https://twinnation.org/api/v1/articles + url: https://twinnation.org/health + conditions: + - "[STATUS] == 200" + - "[BODY].status == UP" + - "[RESPONSE_TIME] < 1000" + - name: twinnation-articles-api + interval: 10s + url: https://twinnation.org/api/v1/articles/24 + conditions: + - "[STATUS] == 200" + - "[BODY].id == 24" + - "[BODY].tags[0] == spring" + - name: example + url: https://example.org/ + interval: 30s conditions: - "[STATUS] == 200" - - "[BODY].[0].id == 42" - -# - name: example -# url: https://example.org/ -# interval: 30s -# conditions: -# - "[STATUS] == 200" diff --git a/core/service.go b/core/service.go index 89b657ad..b28055cc 100644 --- a/core/service.go +++ b/core/service.go @@ -2,6 +2,7 @@ package core import ( "bytes" + "errors" "github.com/TwinProduction/gatus/client" "io/ioutil" "net" @@ -10,13 +11,18 @@ import ( "time" ) +var ( + ErrNoCondition = errors.New("you must specify at least one condition per service") + ErrNoUrl = errors.New("you must specify an url for each service") +) + type Service struct { Name string `yaml:"name"` - Interval time.Duration `yaml:"interval,omitempty"` Url string `yaml:"url"` Method string `yaml:"method,omitempty"` Body string `yaml:"body,omitempty"` - Headers map[string]string `yaml:"headers"` + Headers map[string]string `yaml:"headers,omitempty"` + Interval time.Duration `yaml:"interval,omitempty"` Conditions []*Condition `yaml:"conditions"` } @@ -28,6 +34,15 @@ func (service *Service) Validate() { if len(service.Method) == 0 { service.Method = http.MethodGet } + if len(service.Headers) == 0 { + service.Headers = make(map[string]string) + } + if len(service.Url) == 0 { + panic(ErrNoUrl) + } + if len(service.Conditions) == 0 { + panic(ErrNoCondition) + } // Make sure that the request can be created _, err := http.NewRequest(service.Method, service.Url, bytes.NewBuffer([]byte(service.Body)))