-
Notifications
You must be signed in to change notification settings - Fork 51
/
types.go
158 lines (125 loc) · 3.42 KB
/
types.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
package blackbox
import (
"context"
"fmt"
"io"
"net/http"
"os"
"sort"
"strings"
"time"
"github.com/microsoft/ApplicationInsights-Go/appinsights"
)
// Blackbox is interface for blackbox monitoring
type Blackbox interface {
// Start blackbox monitoring instance
Start(ctx context.Context)
// Stop blackbox monitoring instance
Stop(w io.Writer) error
}
// Config defines blackbox instance configuration
type Config struct {
Cli *http.Client
ICli appinsights.TelemetryClient
Req *http.Request
Interval time.Duration
LogInitialErrors bool
// samples for individual monitor
samples []*sample
done chan struct{}
collected chan struct{}
logfile string
}
type sample struct {
time time.Time
latency time.Duration
err error
}
// Start runs black box monitor and samples data into buffer until stopped
func (c *Config) Start(ctx context.Context) {
c.done = make(chan struct{})
c.collected = make(chan struct{})
t := time.NewTicker(c.Interval)
go func(ctx context.Context) {
var seenGoodSample bool
for {
select {
case <-t.C:
case <-c.done:
t.Stop()
close(c.collected)
return
}
start := time.Now()
resp, err := c.Cli.Do(c.Req)
end := time.Now()
if c.ICli != nil {
if resp != nil {
request := appinsights.NewRequestTelemetry(c.Req.Method, c.Req.URL.String(), time.Second, resp.Status)
request.Id = os.Getenv("RESOURCEGROUP")
request.MarkTime(start, end)
c.ICli.Track(request)
} else {
request := appinsights.NewRequestTelemetry(c.Req.Method, c.Req.URL.String(), time.Second, "error")
request.Id = os.Getenv("RESOURCEGROUP")
request.MarkTime(start, end)
c.ICli.Track(request)
}
}
if err == nil && resp.StatusCode != http.StatusOK {
err = fmt.Errorf("invalid status %d", resp.StatusCode)
}
if !c.LogInitialErrors && !seenGoodSample && err != nil {
continue
}
if !seenGoodSample && err == nil {
seenGoodSample = true
}
c.samples = append(c.samples, &sample{
time: start,
latency: end.Sub(start),
err: err,
})
}
}(ctx)
}
// Stop stops blackbox instance and persists data into writer
func (c *Config) Stop(w io.Writer) {
close(c.done)
<-c.collected
if len(c.samples) == 0 {
return
}
var sum time.Duration
var errors int
for _, sample := range c.samples {
sum += sample.latency
fmt.Fprintf(w, "%s", sample.time.UTC().Format("2006-01-02T15:04:05.000"))
ct := int(sample.latency / (100 * time.Millisecond))
fmt.Fprintf(w, " %-10s", strings.Repeat("*", min(ct, 10)))
if ct > 10 {
fmt.Fprintf(w, "+")
} else {
fmt.Fprintf(w, " ")
}
fmt.Fprintf(w, " %-4dms", sample.latency/time.Millisecond)
if sample.err != nil {
fmt.Fprintf(w, " %s", sample.err)
errors++
}
fmt.Fprintln(w)
}
fmt.Fprintln(w)
fmt.Fprintf(w, "errors: %d\n", errors)
fmt.Fprintf(w, "mean latency: %-4dms\n", sum/time.Duration(len(c.samples))/time.Millisecond)
sort.Slice(c.samples, func(i, j int) bool { return c.samples[i].latency < c.samples[j].latency })
fmt.Fprintf(w, "median latency: %-4dms\n", c.samples[len(c.samples)/2].latency/time.Millisecond)
fmt.Fprintf(w, "95%%ile latency: %-4dms\n", c.samples[len(c.samples)*95/100].latency/time.Millisecond)
fmt.Fprintf(w, "99%%ile latency: %-4dms\n", c.samples[len(c.samples)*99/100].latency/time.Millisecond)
}
func min(a, b int) int {
if a < b {
return a
}
return b
}