/
probe.go
174 lines (138 loc) · 3.23 KB
/
probe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package internal
import (
"bufio"
"bytes"
"fmt"
"regexp"
"strings"
"text/template"
"time"
"github.com/Masterminds/sprig/v3"
"github.com/phramz/go-healthcheck/pkg/contract"
"github.com/phramz/go-healthcheck/pkg/http"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
var _ contract.Probe = (*probe)(nil)
var _ assert.TestingT = (*probe)(nil)
func NewProbe(name, tpl string, logger logrus.FieldLogger, config contract.Config) (contract.Probe, error) {
functions := sprig.TxtFuncMap()
functions["regexp"] = regexp.MustCompile
t, err := template.New(name).Funcs(functions).Parse(tpl)
if err != nil {
return nil, err
}
return &probe{
name: name,
tpl: t,
config: config,
logger: logger,
results: make([]contract.Result, 0),
}, nil
}
type probe struct {
config contract.Config
logger logrus.FieldLogger
tpl *template.Template
results []contract.Result
name string
started *time.Time
finished *time.Time
}
func (p *probe) HasFailed() bool {
for _, r := range p.results {
if r.Failed() {
return true
}
}
return false
}
func (p *probe) HasPassed() bool {
return !p.HasFailed()
}
func (p *probe) IsDone() bool {
return p.finished != nil
}
func (p *probe) clone() *probe {
i := &probe{
tpl: p.tpl,
config: p.config,
logger: p.logger,
results: p.results,
}
return i
}
func (p *probe) Run() (string, error) {
if p.started != nil {
panic("already started")
}
ts := time.Now()
p.started = &ts
logger := p.logger.WithField(LogFieldStatus, "STARTED")
logger.Info(`starting probe`)
defer func() {
tf := time.Now()
p.finished = &tf
if p.HasFailed() {
logger.WithField(LogFieldStatus, "FAIL").Warning(`finished probe`)
return
}
logger.WithField(LogFieldStatus, "PASS").Info(`finished probe`)
}()
param := struct {
Assert *assert.Assertions
HTTP contract.HealthCheck
}{
Assert: assert.New(p),
HTTP: http.NewHttpHealthCheck(p).WithLogger(logger.WithField(LogFieldComponent, "http")),
}
var b bytes.Buffer
out := bufio.NewWriter(&b)
err := p.tpl.Execute(out, param)
if err != nil {
p.logger.Error(err)
return "", err
}
rendered := b.String()
return rendered, err
}
func (p *probe) Fail(err error) {
p.logger.Warning(err.Error())
p.results = append(p.results, NewFail(err))
}
func (p *probe) Failf(format string, args ...interface{}) {
p.Errorf(format, args...)
}
func (p *probe) Infof(format string, args ...interface{}) {
p.logger.Infof(format, args...)
}
func (p *probe) Errorf(format string, args ...interface{}) {
// remove stack
if len(args) == 1 {
if str, ok := args[0].(string); ok && strings.HasPrefix(str, "\tError Trace:") {
start := false
args[0] = ""
for _, l := range strings.Split(str, "\n") {
if strings.HasPrefix(strings.TrimSpace(l), "Error:") {
start = true
}
if !start {
continue
}
args[0] = fmt.Sprintf("%s\n%s", args[0], l)
}
}
}
p.logger.Warningf(format, args...)
p.results = append(p.results, NewFailf(format, args...))
}
func (p *probe) WithConfig(config contract.Config) contract.Probe {
i := p.clone()
i.config = config
return i
}
func (p *probe) WithLogger(logger logrus.FieldLogger) contract.Probe {
i := p.clone()
i.logger = logger
return i
}