Skip to content

Commit d7eaa40

Browse files
authored
Merge pull request #110 from go-bridget/master
Refactoring for better separation of concerns
2 parents 8ffb973 + b17f4c1 commit d7eaa40

56 files changed

Lines changed: 1160 additions & 744 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Makefile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
.PHONY: all build test docker
1+
.PHONY: all build build-sql test docker
22

33
all: build test
44

55
DOCKER_IMAGE := checkup
66

77
build:
8+
go fmt ./...
9+
go mod tidy
810
mkdir -p builds/
911
go build -o builds/ ./cmd/...
1012

13+
build-sql:
14+
go fmt ./...
15+
go mod tidy
16+
go build -o builds/ -tags sql ./cmd/...
17+
1118
test:
12-
go test -race -count=1 -v ./...
19+
go test -race -count=1 ./...
1320

1421
docker:
1522
docker build --no-cache . -t $(DOCKER_IMAGE)

README.md

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,20 @@ Checkup was created by Matt Holt, author of the [Caddy web server](https://caddy
1111

1212
This tool is a work-in-progress. Please use liberally (with discretion) and report any bugs!
1313

14+
## Recent changes
1415

16+
Due to recent development, some breaking changes have been introduced:
17+
18+
- providers: the json config field `provider` was renamed to `type` for consistency,
19+
- notifiers: the json config field `name` was renamed to `type` for consistency,
20+
- sql: by default the sqlite storage engine is disabled (needs build with `-tags sql` to enable),
21+
22+
If you want to build the latest version, it's best to run:
23+
24+
- `make build` - builds checkup without sql support,
25+
- `make build-sql` - builds checkup with pgsql/sqlite;
26+
27+
The resulting binary will be placed into `builds/checkup`.
1528

1629
## Intro
1730

@@ -76,9 +89,9 @@ You can configure Checkup entirely with a simple JSON document. You should confi
7689
// storage configuration goes here
7790
},
7891

79-
"notifier": {
92+
"notifiers": [
8093
// notifier configuration goes here
81-
}
94+
]
8295
}
8396
```
8497

@@ -90,7 +103,7 @@ Here are the configuration structures you can use, which are explained fully [in
90103

91104
#### HTTP Checkers
92105

93-
**[godoc: HTTPChecker](https://godoc.org/github.com/sourcegraph/checkup#HTTPChecker)**
106+
**[godoc: HTTPChecker](https://godoc.org/github.com/sourcegraph/checkup/check/http)**
94107

95108
```js
96109
{
@@ -104,7 +117,7 @@ Here are the configuration structures you can use, which are explained fully [in
104117

105118
#### TCP Checkers
106119

107-
**[godoc: TCPChecker](https://godoc.org/github.com/sourcegraph/checkup#TCPChecker)**
120+
**[godoc: TCPChecker](https://godoc.org/github.com/sourcegraph/checkup/check/tcp)**
108121

109122
```js
110123
{
@@ -116,7 +129,7 @@ Here are the configuration structures you can use, which are explained fully [in
116129

117130
#### DNS Checkers
118131

119-
**[godoc: DNSChecker](https://godoc.org/github.com/sourcegraph/checkup#DNSChecker)**
132+
**[godoc: DNSChecker](https://godoc.org/github.com/sourcegraph/checkup/check/dns)**
120133

121134
```js
122135
{
@@ -129,7 +142,7 @@ Here are the configuration structures you can use, which are explained fully [in
129142

130143
#### TLS Checkers
131144

132-
**[godoc: TLSChecker](https://godoc.org/github.com/sourcegraph/checkup#TLSChecker)**
145+
**[godoc: TLSChecker](https://godoc.org/github.com/sourcegraph/checkup/check/tls)**
133146

134147
```js
135148
{
@@ -142,11 +155,11 @@ Here are the configuration structures you can use, which are explained fully [in
142155

143156
#### Amazon S3 Storage
144157

145-
**[godoc: S3](https://godoc.org/github.com/sourcegraph/checkup#S3)**
158+
**[godoc: S3](https://godoc.org/github.com/sourcegraph/checkup/check/s3)**
146159

147160
```js
148161
{
149-
"provider": "s3",
162+
"type": "s3",
150163
"access_key_id": "<yours>",
151164
"secret_access_key": "<yours>",
152165
"bucket": "<yours>",
@@ -159,11 +172,11 @@ S3 is the default storage provider assumed by the status page, so the only chang
159172

160173
#### File System Storage
161174

162-
**[godoc: FS](https://godoc.org/github.com/sourcegraph/checkup#FS)**
175+
**[godoc: FS](https://godoc.org/github.com/sourcegraph/checkup/storage/fs)**
163176

164177
```js
165178
{
166-
"provider": "fs",
179+
"type": "fs",
167180
"dir": "/path/to/your/check_files",
168181
"url": "http://127.0.0.1:2015/check_files"
169182
}
@@ -180,11 +193,11 @@ Then fill out [config.js](https://github.com/sourcegraph/checkup/blob/master/sta
180193

181194
#### GitHub Storage
182195

183-
**[godoc: GitHub](https://godoc.org/github.com/sourcegraph/checkup#GitHub)**
196+
**[godoc: GitHub](https://godoc.org/github.com/sourcegraph/checkup/storage/github)**
184197

185198
```js
186199
{
187-
"provider": "github",
200+
"type": "github",
188201
"access_token": "some_api_access_token_with_repo_scope",
189202
"repository_owner": "owner",
190203
"repository_name": "repo",
@@ -209,22 +222,22 @@ Where "dir" is a subdirectory within the repo to push all the check files. Setup
209222

210223
#### SQL Storage (sqlite3/PostgreSQL)
211224

212-
**[godoc: SQL](https://godoc.org/github.com/sourcegraph/checkup#SQL)**
225+
**[godoc: SQL](https://godoc.org/github.com/sourcegraph/checkup/storage/sql)**
213226

214227
Postgres or sqlite3 databases can be used as storage backends.
215228

216229
sqlite database file configuration:
217230
```js
218231
{
219-
"provider": "sql",
232+
"type": "sql",
220233
"sqlite_db_file": "/path/to/your/sqlite.db"
221234
}
222235
```
223236

224237
postgresql database file configuration:
225238
```js
226239
{
227-
"provider": "sql",
240+
"type": "sql",
228241
"postgresql": {
229242
"user": "postgres",
230243
"dbname": "dbname",
@@ -251,7 +264,7 @@ Currently the status page does not support SQL storage.
251264
Enable notifications in Slack with this Notifier configuration:
252265
```js
253266
{
254-
"name": "slack",
267+
"type": "slack",
255268
"username": "username",
256269
"channel": "#channel-name",
257270
"webhook": "webhook-url"
@@ -260,6 +273,26 @@ Enable notifications in Slack with this Notifier configuration:
260273

261274
Follow these instructions to [create a webhook](https://get.slack.help/hc/en-us/articles/115005265063-Incoming-WebHooks-for-Slack).
262275

276+
#### Mail notifier
277+
278+
Enable E-mail notifications with this Notifier configuration:
279+
```js
280+
{
281+
"type": "mail",
282+
"from": "from@example.com",
283+
"to": [ "support1@examiple.com", "support2@example.com" ],
284+
"subject": "Custom subject line",
285+
"smtp": {
286+
"server": "smtp.example.com",
287+
"port": 25,
288+
"username": "username",
289+
"password": "password"
290+
}
291+
}
292+
```
293+
294+
The settings for `subject`, `smtp.port` (default to 25), `smtp.username` and `smtp.password` are optional.
295+
263296
## Setting up storage on S3
264297

265298
The easiest way to do this is to give an IAM user these two privileges (keep the credentials secret):
@@ -453,34 +486,18 @@ You can implement your own Checker and Storage types. If it's general enough, fe
453486

454487
### Building Locally
455488

456-
Requires Go v1.10 or newer.
489+
Requires Go v1.13 or newer.
457490

458491
```bash
459492
git clone git@github.com:sourcegraph/checkup.git
460-
cd checkup/cmd/checkup/
461-
462-
# Install dependencies
463-
go get -v -d
464-
465-
# Build binary
466-
go build -v -ldflags '-s' -o ../../checkup
467-
468-
# Run tests
469-
go test -race ../../
493+
cd checkup
494+
make
470495
```
471496

472-
### Building with Docker
497+
Building the SQL enabled version is done with `make build-sql`.
473498

474-
Linux binary:
475-
476-
```bash
477-
git clone git@github.com:sourcegraph/checkup.git
478-
cd checkup
479-
docker pull golang:latest
480-
docker run --net=host --rm \
481-
-v `pwd`:/project \
482-
-w /project golang bash \
483-
-c "cd cmd/checkup; go get -v -d; go build -v -ldflags '-s' -o ../../checkup"
484-
```
499+
### Building a Docker image
485500

486-
This will create a checkup binary in the root project folder.
501+
If you would like to run checkup in a docker container, building it is done by running `make docker`.
502+
It will build the version without sql support. An SQL supported docker image is currently not provided,
503+
but there's a plan to do that in the future.

check.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package checkup
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/sourcegraph/checkup/check/dns"
8+
"github.com/sourcegraph/checkup/check/exec"
9+
"github.com/sourcegraph/checkup/check/http"
10+
"github.com/sourcegraph/checkup/check/tcp"
11+
"github.com/sourcegraph/checkup/check/tls"
12+
)
13+
14+
func checkerDecode(typeName string, config json.RawMessage) (Checker, error) {
15+
switch typeName {
16+
case dns.Type:
17+
return dns.New(config)
18+
case exec.Type:
19+
return exec.New(config)
20+
case http.Type:
21+
return http.New(config)
22+
case tcp.Type:
23+
return tcp.New(config)
24+
case tls.Type:
25+
return tls.New(config)
26+
default:
27+
return nil, fmt.Errorf(errUnknownCheckerType, typeName)
28+
}
29+
}

dnschecker.go renamed to check/dns/dns.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
package checkup
1+
package dns
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"net"
67
"time"
78

89
"github.com/miekg/dns"
10+
11+
"github.com/sourcegraph/checkup/types"
912
)
1013

11-
// DNSChecker implements a Checker for TCP endpoints.
12-
type DNSChecker struct {
14+
// Type should match the package name
15+
const Type = "dns"
16+
17+
// Checker implements a Checker for TCP endpoints.
18+
type Checker struct {
1319
// Name is the name of the endpoint.
1420
Name string `json:"endpoint_name"`
1521
// This is the name of the DNS server you are testing.
@@ -31,29 +37,43 @@ type DNSChecker struct {
3137
Attempts int `json:"attempts,omitempty"`
3238
}
3339

40+
// New creates a new Checker instance based on json config
41+
func New(config json.RawMessage) (Checker, error) {
42+
var checker Checker
43+
err := json.Unmarshal(config, &checker)
44+
return checker, err
45+
}
46+
47+
// Type returns the checker package name
48+
func (Checker) Type() string {
49+
return Type
50+
}
51+
3452
// Check performs checks using c according to its configuration.
3553
// An error is only returned if there is a configuration error.
36-
func (c DNSChecker) Check() (Result, error) {
54+
func (c Checker) Check() (types.Result, error) {
3755
if c.Attempts < 1 {
3856
c.Attempts = 1
3957
}
4058

41-
result := Result{Title: c.Name, Endpoint: c.URL, Timestamp: Timestamp()}
59+
result := types.NewResult()
60+
result.Title = c.Name
61+
result.Endpoint = c.URL
4262
result.Times = c.doChecks()
4363

4464
return c.conclude(result), nil
4565
}
4666

4767
// doChecks executes and returns each attempt.
48-
func (c DNSChecker) doChecks() Attempts {
68+
func (c Checker) doChecks() types.Attempts {
4969
var conn net.Conn
5070

5171
timeout := c.Timeout
5272
if timeout == 0 {
5373
timeout = 1 * time.Second
5474
}
5575

56-
checks := make(Attempts, c.Attempts)
76+
checks := make(types.Attempts, c.Attempts)
5777
for i := 0; i < c.Attempts; i++ {
5878
var err error
5979
start := time.Now()
@@ -86,7 +106,7 @@ func (c DNSChecker) doChecks() Attempts {
86106
// computes remaining values needed to fill out the result.
87107
// It detects degraded (high-latency) responses and makes
88108
// the conclusion about the result's status.
89-
func (c DNSChecker) conclude(result Result) Result {
109+
func (c Checker) conclude(result types.Result) types.Result {
90110
result.ThresholdRTT = c.ThresholdRTT
91111

92112
// Check errors (down)
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package checkup
1+
package dns
22

33
import (
44
"net"
55
"testing"
66
"time"
77
)
88

9-
func TestDNSChecker(t *testing.T) {
9+
func TestChecker(t *testing.T) {
1010
// Listen on localhost, random port
1111
srv, err := net.Listen("tcp", "localhost:8382")
1212
if err != nil {
@@ -28,7 +28,7 @@ func TestDNSChecker(t *testing.T) {
2828
// Should know the host:port by now
2929
endpt := srv.Addr().String()
3030
testName := "TestDNS"
31-
hc := DNSChecker{Name: testName, URL: endpt, Attempts: 2}
31+
hc := Checker{Name: testName, URL: endpt, Attempts: 2}
3232

3333
// Try an up server
3434
result, err := hc.Check()
@@ -102,7 +102,7 @@ func TestDNSChecker(t *testing.T) {
102102
}
103103
}
104104

105-
func TestDNSCheckerWithAgressiveTimeout(t *testing.T) {
105+
func TestCheckerWithAgressiveTimeout(t *testing.T) {
106106
// Listen on localhost, random port
107107
srv, err := net.Listen("tcp", "localhost:0")
108108
if err != nil {
@@ -124,7 +124,7 @@ func TestDNSCheckerWithAgressiveTimeout(t *testing.T) {
124124
// Should know the host:port by now
125125
endpt := srv.Addr().String()
126126
testName := "TestTCP"
127-
hc := DNSChecker{Name: testName, URL: endpt, Attempts: 2, Timeout: 1 * time.Nanosecond}
127+
hc := Checker{Name: testName, URL: endpt, Attempts: 2, Timeout: 1 * time.Nanosecond}
128128

129129
result, err := hc.Check()
130130
if err != nil {

0 commit comments

Comments
 (0)