-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflaki.go
77 lines (64 loc) · 1.72 KB
/
flaki.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
package common
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/pkg/errors"
)
// NewFlakiModule returns the Flaki health module.
func NewFlakiModule(client FlakiClient, enabled bool) *FlakiModule {
return &FlakiModule{
flakiClient: client,
enabled: enabled,
}
}
// FlakiModule is the health check module for Flaki.
type FlakiModule struct {
flakiClient FlakiClient
enabled bool
}
// FlakiClient is the interface of the Flaki client.
type FlakiClient interface {
NextID(context.Context) (string, error)
}
type flakiReport struct {
Name string `json:"name"`
Status string `json:"status"`
Duration string `json:"duration,omitempty"`
Error string `json:"error,omitempty"`
}
// HealthCheck executes the desired influx health check.
func (m *FlakiModule) HealthCheck(_ context.Context, name string) (json.RawMessage, error) {
if !m.enabled {
return json.MarshalIndent([]influxReport{{Name: "flaki", Status: Deactivated.String()}}, "", " ")
}
var reports []flakiReport
switch name {
case "":
reports = append(reports, m.nextID())
case "ping":
reports = append(reports, m.nextID())
default:
// Should not happen: there is a middleware validating the inputs name.
panic(fmt.Sprintf("Unknown influx health check name: %v", name))
}
return json.MarshalIndent(reports, "", " ")
}
func (m *FlakiModule) nextID() flakiReport {
var name = "nextid"
var status = OK
var now = time.Now()
var _, err = m.flakiClient.NextID(context.Background())
var duration = time.Since(now)
if err != nil {
status = KO
err = errors.Wrap(err, "could not get ID from flaki")
}
return flakiReport{
Name: name,
Duration: duration.String(),
Status: status.String(),
Error: str(err),
}
}