-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkreport.go
46 lines (40 loc) · 1.28 KB
/
checkreport.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
package mackerunn
import (
"fmt"
"github.com/Arthur1/mackerunn/internal/scenariotest"
"github.com/mackerelio/mackerel-client-go"
"github.com/mattn/go-runewidth"
)
func (r *Runner) exportResultAsCheckReport(result *scenariotest.Result, err error) error {
report := r.resultToCheckReport(result, err)
reports := &mackerel.CheckReports{
Reports: []*mackerel.CheckReport{report},
}
return r.mackerelClient.PostCheckReports(reports)
}
func (r *Runner) resultToCheckReport(result *scenariotest.Result, err error) *mackerel.CheckReport {
report := &mackerel.CheckReport{
Name: descToReportName(result.Description),
OccurredAt: result.Timestamp.Unix(),
Source: mackerel.NewCheckSourceHost(r.mackerelHostID),
}
if err != nil {
report.Status = mackerel.CheckStatusUnknown
report.Message = truncateReportMessage(err.Error())
return report
}
if result.Err != nil {
report.Status = mackerel.CheckStatusCritical
report.Message = truncateReportMessage(result.Err.Error())
return report
}
report.Status = mackerel.CheckStatusOK
return report
}
func truncateReportMessage(str string) string {
return runewidth.Truncate(str, 1024, "...")
}
func descToReportName(desc string) string {
name := fmt.Sprintf("mackerunn-%s", desc)
return runewidth.Truncate(name, 254, "...")
}