-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecks.go
62 lines (50 loc) · 1.31 KB
/
checks.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
package fasthttp
import (
"errors"
"strconv"
"time"
"github.com/grafana/sobek"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modules/k6"
"go.k6.io/k6/metrics"
)
func (mi *ModuleInstance) CheckStatus(wantStatus int, r *sobek.Object, extras ...sobek.Value) (bool, error) {
state := mi.vu.State()
if state == nil {
return false, k6.ErrCheckInInitContext
}
if r == nil {
return false, errors.New("nil response")
}
resp, ok := r.Export().(*Response)
if !ok {
return false, errors.New("response object not given to CheckStatus")
}
ctx := mi.vu.Context()
rt := mi.vu.Runtime()
t := time.Now()
// Prepare the metric tags
commonTagsAndMeta := state.Tags.GetCurrentValues()
if len(extras) > 0 {
if err := common.ApplyCustomUserTags(rt, &commonTagsAndMeta, extras[0]); err != nil {
return false, err
}
}
checkName := "check status is " + strconv.FormatInt(int64(wantStatus), 10)
tags := commonTagsAndMeta.Tags
if state.Options.SystemTags.Has(metrics.TagCheck) {
tags = tags.With("check", checkName)
}
pass := resp.Status == wantStatus
sample := metrics.Sample{
TimeSeries: metrics.TimeSeries{
Metric: state.BuiltinMetrics.Checks,
Tags: tags,
},
Time: t,
Metadata: commonTagsAndMeta.Metadata,
Value: 1,
}
metrics.PushIfNotDone(ctx, state.Samples, sample)
return pass, nil
}