-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric.go
54 lines (47 loc) · 1.22 KB
/
metric.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
package mackerunn
import (
"fmt"
"regexp"
"strings"
"github.com/Arthur1/mackerunn/internal/scenariotest"
"github.com/mackerelio/mackerel-client-go"
"github.com/mattn/go-runewidth"
)
func (r *Runner) exportResultAsMetric(result *scenariotest.Result) error {
vs := r.resultToMetricValues(result)
return r.mackerelClient.PostServiceMetricValues(r.mackerelServiceName, vs)
}
func (r *Runner) resultToMetricValues(result *scenariotest.Result) []*mackerel.MetricValue {
key := descToMetricKey(result.Description)
t := result.Timestamp.Unix()
vs := []*mackerel.MetricValue{
{
Name: fmt.Sprintf("mackerunn.execute_result.%s", key),
Time: t,
Value: btoi(result.Err == nil),
},
}
if result.Err == nil {
vs = append(vs, &mackerel.MetricValue{
Name: fmt.Sprintf("mackerunn.elapsed_time.%s", key),
Time: t,
Value: result.ElapsedTime.Seconds(),
})
}
return vs
}
func btoi(b bool) int64 {
if b {
return 1
}
return 0
}
var metricKeyRegex = regexp.MustCompile("[a-zA-Z0-9._-]+")
func descToMetricKey(desc string) string {
tokens := metricKeyRegex.FindAllString(desc, -1)
if len(tokens) == 0 {
tokens = []string{"noname"}
}
key := strings.Join(tokens, "_")
return runewidth.Truncate(key, 200, "")
}