Compare commits

...
7 Commits
8 changed files with 82 additions and 23 deletions

No files matched your search

+1 -1
View File
@@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier same "printed page" as the copyright notice for easier
identification within third-party archives. identification within third-party archives.
Copyright [yyyy] [name of copyright owner] Copyright 2020 TwinProduction
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
+46 -10
View File
@@ -40,16 +40,17 @@ Note that you can also add environment variables in the your configuration file
### Configuration ### Configuration
| Parameter | Description | Default | | Parameter | Description | Default |
| ----------------------- | ------------------------------------------------------ | -------------- | | ----------------------- | --------------------------------------------------------------- | -------------- |
| `metrics` | Whether to expose metrics at /metrics | `false` | | `metrics` | Whether to expose metrics at /metrics | `false` |
| `services[].name` | Name of the service. Can be anything. | Required `""` | | `services[].name` | Name of the service. Can be anything. | Required `""` |
| `services[].url` | URL to send the request to | Required `""` | | `services[].url` | URL to send the request to | Required `""` |
| `services[].conditions` | Conditions used to determine the health of the service | `[]` | | `services[].conditions` | Conditions used to determine the health of the service | `[]` |
| `services[].interval` | Duration to wait between every status check | `10s` | | `services[].interval` | Duration to wait between every status check | `10s` |
| `services[].method` | Request method | `GET` | | `services[].method` | Request method | `GET` |
| `services[].body` | Request body | `""` | | `services[].graphql` | Whether to wrap the body in a query param (`{"query":"$body"}`) | `false` |
| `services[].headers` | Request headers | `{}` | | `services[].body` | Request body | `""` |
| `services[].headers` | Request headers | `{}` |
### Conditions ### Conditions
@@ -95,3 +96,38 @@ go test ./... -mod vendor
## Using in Production ## Using in Production
See the [example](example) folder. See the [example](example) folder.
## FAQ
### Sending a GraphQL request
By setting `services[].graphql` to true, the body will automatically be wrapped by the standard GraphQL `query` parameter.
For instance, the following configuration:
```
services:
- name: properties
url: http://localhost:8080/playground
method: POST
graphql: true
body: |
{
user(gender: "female") {
id
name
gender
avatar
}
}
headers:
Content-Type: application/json
conditions:
- "[STATUS] == 200"
- "[BODY].data.user[0].gender == female"
```
will send a `POST` request to `http://localhost:8080/playground` with the following body:
```json
{"query":" {\n user(gender: \"female\") {\n id\n name\n gender\n avatar\n }\n }"}
```
+9 -5
View File
@@ -9,10 +9,9 @@ import (
"os" "os"
) )
type Config struct { const (
Metrics bool `yaml:"metrics"` DefaultConfigurationFilePath = "config/config.yaml"
Services []*core.Service `yaml:"services"` )
}
var ( var (
ErrNoServiceInConfig = errors.New("configuration file should contain at least 1 service") ErrNoServiceInConfig = errors.New("configuration file should contain at least 1 service")
@@ -21,6 +20,11 @@ var (
config *Config config *Config
) )
type Config struct {
Metrics bool `yaml:"metrics"`
Services []*core.Service `yaml:"services"`
}
func Get() *Config { func Get() *Config {
if config == nil { if config == nil {
panic(ErrConfigNotLoaded) panic(ErrConfigNotLoaded)
@@ -43,7 +47,7 @@ func Load(configFile string) error {
} }
func LoadDefaultConfiguration() error { func LoadDefaultConfiguration() error {
err := Load("config/config.yaml") err := Load(DefaultConfigurationFilePath)
if err != nil { if err != nil {
if err == ErrConfigFileNotFound { if err == ErrConfigFileNotFound {
return Load("config/config.yml") return Load("config/config.yml")
+13 -1
View File
@@ -2,6 +2,7 @@ package core
import ( import (
"bytes" "bytes"
"encoding/json"
"errors" "errors"
"github.com/TwinProduction/gatus/client" "github.com/TwinProduction/gatus/client"
"io/ioutil" "io/ioutil"
@@ -21,6 +22,7 @@ type Service struct {
Url string `yaml:"url"` Url string `yaml:"url"`
Method string `yaml:"method,omitempty"` Method string `yaml:"method,omitempty"`
Body string `yaml:"body,omitempty"` Body string `yaml:"body,omitempty"`
GraphQL bool `yaml:"graphql,omitempty"`
Headers map[string]string `yaml:"headers,omitempty"` Headers map[string]string `yaml:"headers,omitempty"`
Interval time.Duration `yaml:"interval,omitempty"` Interval time.Duration `yaml:"interval,omitempty"`
Conditions []*Condition `yaml:"conditions"` Conditions []*Condition `yaml:"conditions"`
@@ -102,7 +104,17 @@ func (service *Service) call(result *Result) {
} }
func (service *Service) buildRequest() *http.Request { func (service *Service) buildRequest() *http.Request {
request, _ := http.NewRequest(service.Method, service.Url, bytes.NewBuffer([]byte(service.Body))) var bodyBuffer *bytes.Buffer
if service.GraphQL {
graphQlBody := map[string]string{
"query": service.Body,
}
body, _ := json.Marshal(graphQlBody)
bodyBuffer = bytes.NewBuffer(body)
} else {
bodyBuffer = bytes.NewBuffer([]byte(service.Body))
}
request, _ := http.NewRequest(service.Method, service.Url, bodyBuffer)
for k, v := range service.Headers { for k, v := range service.Headers {
request.Header.Set(k, v) request.Header.Set(k, v)
} }
+2 -1
View File
@@ -18,6 +18,7 @@ const (
func sanitizeAndResolve(list []string, result *Result) []string { func sanitizeAndResolve(list []string, result *Result) []string {
var sanitizedList []string var sanitizedList []string
body := strings.TrimSpace(string(result.Body))
for _, element := range list { for _, element := range list {
element = strings.TrimSpace(element) element = strings.TrimSpace(element)
switch strings.ToUpper(element) { switch strings.ToUpper(element) {
@@ -28,7 +29,7 @@ func sanitizeAndResolve(list []string, result *Result) []string {
case ResponseTimePlaceHolder: case ResponseTimePlaceHolder:
element = strconv.Itoa(int(result.Duration.Milliseconds())) element = strconv.Itoa(int(result.Duration.Milliseconds()))
case BodyPlaceHolder: case BodyPlaceHolder:
element = string(result.Body) element = body
default: default:
// if starts with BodyPlaceHolder, then evaluate json path // if starts with BodyPlaceHolder, then evaluate json path
if strings.HasPrefix(element, BodyPlaceHolder) { if strings.HasPrefix(element, BodyPlaceHolder) {
+3
View File
@@ -52,6 +52,9 @@ func extractValue(currentKey string, value interface{}) interface{} {
} }
return nil return nil
} }
if value == nil || value.(map[string]interface{})[currentKey] == nil {
return nil
}
// if currentKey contains both a key and an index (i.e. data[0]) // if currentKey contains both a key and an index (i.e. data[0])
array := value.(map[string]interface{})[currentKey].([]interface{}) array := value.(map[string]interface{})[currentKey].([]interface{})
if len(array) > arrayIndex { if len(array) > arrayIndex {
+1 -4
View File
@@ -11,9 +11,6 @@
html { html {
height: 100%; height: 100%;
} }
body {
min-height: 100%;
}
#results div.container:first-child { #results div.container:first-child {
border-top-left-radius: 3px; border-top-left-radius: 3px;
border-top-right-radius: 3px; border-top-right-radius: 3px;
@@ -150,7 +147,7 @@
if (!userClickedStatus) { if (!userClickedStatus) {
timerHandler = setTimeout(function () { timerHandler = setTimeout(function () {
$("#tooltip").hide(); $("#tooltip").hide();
}, 2000); }, 500);
} }
} }
+7 -1
View File
@@ -1,6 +1,7 @@
package watchdog package watchdog
import ( import (
"fmt"
"github.com/TwinProduction/gatus/config" "github.com/TwinProduction/gatus/config"
"github.com/TwinProduction/gatus/core" "github.com/TwinProduction/gatus/core"
"github.com/TwinProduction/gatus/metric" "github.com/TwinProduction/gatus/metric"
@@ -39,11 +40,16 @@ func monitor(service *core.Service) {
serviceResults[service.Name] = serviceResults[service.Name][1:] serviceResults[service.Name] = serviceResults[service.Name][1:]
} }
rwLock.Unlock() rwLock.Unlock()
var extra string
if !result.Success {
extra = fmt.Sprintf("responseBody=%s", result.Body)
}
log.Printf( log.Printf(
"[watchdog][Monitor] Finished monitoring serviceName=%s; errors=%d; requestDuration=%s", "[watchdog][Monitor] Finished monitoring serviceName=%s; errors=%d; requestDuration=%s; %s",
service.Name, service.Name,
len(result.Errors), len(result.Errors),
result.Duration.Round(time.Millisecond), result.Duration.Round(time.Millisecond),
extra,
) )
log.Printf("[watchdog][Monitor] Waiting interval=%s before monitoring serviceName=%s", service.Interval, service.Name) log.Printf("[watchdog][Monitor] Waiting interval=%s before monitoring serviceName=%s", service.Interval, service.Name)
time.Sleep(service.Interval) time.Sleep(service.Interval)