-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinflux.go
77 lines (64 loc) · 1.73 KB
/
influx.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"
)
// NewInfluxModule returns the influx health module.
func NewInfluxModule(influx InfluxClient, enabled bool) *InfluxModule {
return &InfluxModule{
influx: influx,
enabled: enabled,
}
}
// InfluxModule is the health check module for influx.
type InfluxModule struct {
influx InfluxClient
enabled bool
}
// InfluxClient is the interface of the influx client.
type InfluxClient interface {
Ping(timeout time.Duration) (time.Duration, string, error)
}
type influxReport 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 *InfluxModule) HealthCheck(_ context.Context, name string) (json.RawMessage, error) {
if !m.enabled {
return json.MarshalIndent([]influxReport{{Name: "influx", Status: Deactivated.String()}}, "", " ")
}
var reports []influxReport
switch name {
case "":
reports = append(reports, m.influxPing())
case "ping":
reports = append(reports, m.influxPing())
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 *InfluxModule) influxPing() influxReport {
var name = "ping"
var status = OK
var now = time.Now()
var _, _, err = m.influx.Ping(5 * time.Second)
var duration = time.Since(now)
if err != nil {
status = KO
err = errors.Wrap(err, "could not ping influx")
}
return influxReport{
Name: name,
Duration: duration.String(),
Status: status.String(),
Error: str(err),
}
}