Skip to content

Commit f50589e

Browse files
committed
Add support for simple GraphQL requests
1 parent 9822162 commit f50589e

File tree

3 files changed

+66
-12
lines changed

3 files changed

+66
-12
lines changed

README.md

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,17 @@ Note that you can also add environment variables in the your configuration file
4040

4141
### Configuration
4242

43-
| Parameter | Description | Default |
44-
| ----------------------- | ------------------------------------------------------ | -------------- |
45-
| `metrics` | Whether to expose metrics at /metrics | `false` |
46-
| `services[].name` | Name of the service. Can be anything. | Required `""` |
47-
| `services[].url` | URL to send the request to | Required `""` |
48-
| `services[].conditions` | Conditions used to determine the health of the service | `[]` |
49-
| `services[].interval` | Duration to wait between every status check | `10s` |
50-
| `services[].method` | Request method | `GET` |
51-
| `services[].body` | Request body | `""` |
52-
| `services[].headers` | Request headers | `{}` |
43+
| Parameter | Description | Default |
44+
| ----------------------- | --------------------------------------------------------------- | -------------- |
45+
| `metrics` | Whether to expose metrics at /metrics | `false` |
46+
| `services[].name` | Name of the service. Can be anything. | Required `""` |
47+
| `services[].url` | URL to send the request to | Required `""` |
48+
| `services[].conditions` | Conditions used to determine the health of the service | `[]` |
49+
| `services[].interval` | Duration to wait between every status check | `10s` |
50+
| `services[].method` | Request method | `GET` |
51+
| `services[].graphql` | Whether to wrap the body in a query param (`{"query":"$body"}`) | `false` |
52+
| `services[].body` | Request body | `""` |
53+
| `services[].headers` | Request headers | `{}` |
5354

5455

5556
### Conditions
@@ -95,3 +96,38 @@ go test ./... -mod vendor
9596
## Using in Production
9697
9798
See the [example](example) folder.
99+
100+
101+
## FAQ
102+
103+
### Sending a GraphQL request
104+
105+
By setting `services[].graphql` to true, the body will automatically be wrapped by the standard GraphQL `query` parameter.
106+
107+
For instance, the following configuration:
108+
```
109+
services:
110+
- name: properties
111+
url: http://localhost:8080/playground
112+
method: POST
113+
graphql: true
114+
body: |
115+
{
116+
user(gender: "female") {
117+
id
118+
name
119+
gender
120+
avatar
121+
}
122+
}
123+
headers:
124+
Content-Type: application/json
125+
conditions:
126+
- "[STATUS] == 200"
127+
- "[BODY].data.user[0].gender == female"
128+
```
129+
130+
will send a `POST` request to `http://localhost:8080/playground` with the following body:
131+
```json
132+
{"query":" {\n user(gender: \"female\") {\n id\n name\n gender\n avatar\n }\n }"}
133+
```

core/service.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package core
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"errors"
67
"github.com/TwinProduction/gatus/client"
78
"io/ioutil"
@@ -21,6 +22,7 @@ type Service struct {
2122
Url string `yaml:"url"`
2223
Method string `yaml:"method,omitempty"`
2324
Body string `yaml:"body,omitempty"`
25+
GraphQL bool `yaml:"graphql,omitempty"`
2426
Headers map[string]string `yaml:"headers,omitempty"`
2527
Interval time.Duration `yaml:"interval,omitempty"`
2628
Conditions []*Condition `yaml:"conditions"`
@@ -102,7 +104,17 @@ func (service *Service) call(result *Result) {
102104
}
103105

104106
func (service *Service) buildRequest() *http.Request {
105-
request, _ := http.NewRequest(service.Method, service.Url, bytes.NewBuffer([]byte(service.Body)))
107+
var bodyBuffer *bytes.Buffer
108+
if service.GraphQL {
109+
graphQlBody := map[string]string{
110+
"query": service.Body,
111+
}
112+
body, _ := json.Marshal(graphQlBody)
113+
bodyBuffer = bytes.NewBuffer(body)
114+
} else {
115+
bodyBuffer = bytes.NewBuffer([]byte(service.Body))
116+
}
117+
request, _ := http.NewRequest(service.Method, service.Url, bodyBuffer)
106118
for k, v := range service.Headers {
107119
request.Header.Set(k, v)
108120
}

watchdog/watchdog.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package watchdog
22

33
import (
4+
"fmt"
45
"github.com/TwinProduction/gatus/config"
56
"github.com/TwinProduction/gatus/core"
67
"github.com/TwinProduction/gatus/metric"
@@ -39,11 +40,16 @@ func monitor(service *core.Service) {
3940
serviceResults[service.Name] = serviceResults[service.Name][1:]
4041
}
4142
rwLock.Unlock()
43+
var extra string
44+
if !result.Success {
45+
extra = fmt.Sprintf("responseBody=%s", result.Body)
46+
}
4247
log.Printf(
43-
"[watchdog][Monitor] Finished monitoring serviceName=%s; errors=%d; requestDuration=%s",
48+
"[watchdog][Monitor] Finished monitoring serviceName=%s; errors=%d; requestDuration=%s; %s",
4449
service.Name,
4550
len(result.Errors),
4651
result.Duration.Round(time.Millisecond),
52+
extra,
4753
)
4854
log.Printf("[watchdog][Monitor] Waiting interval=%s before monitoring serviceName=%s", service.Interval, service.Name)
4955
time.Sleep(service.Interval)

0 commit comments

Comments
 (0)