-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjaeger.go
82 lines (69 loc) · 2.1 KB
/
jaeger.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
78
79
80
81
82
package common
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/pkg/errors"
)
// NewJaegerModule returns the jaeger health module.
func NewJaegerModule(httpClient HTTPClient, collectorHealthHostPort string, enabled bool) *JaegerModule {
return &JaegerModule{
httpClient: httpClient,
collectorHealthHostPort: collectorHealthHostPort,
enabled: enabled,
}
}
// JaegerModule is the health check module for jaeger.
type JaegerModule struct {
collectorHealthHostPort string
httpClient HTTPClient
enabled bool
}
type jaegerReport struct {
Name string `json:"name"`
Status string `json:"status"`
Duration string `json:"duration,omitempty"`
Error string `json:"error,omitempty"`
}
// HealthCheck executes the desired jaeger health check.
func (m *JaegerModule) HealthCheck(_ context.Context, name string) (json.RawMessage, error) {
if !m.enabled {
return json.MarshalIndent([]jaegerReport{{Name: "jaeger", Status: Deactivated.String()}}, "", " ")
}
var reports []jaegerReport
switch name {
case "":
reports = append(reports, m.jaegerCollectorPing())
case "collector":
reports = append(reports, m.jaegerCollectorPing())
default:
// Should not happen: there is a middleware validating the inputs name.
panic(fmt.Sprintf("Unknown jaeger health check name: %v", name))
}
return json.MarshalIndent(reports, "", " ")
}
func (m *JaegerModule) jaegerCollectorPing() jaegerReport {
var name = "ping collector"
var status = OK
// Query jaeger collector health check URL
var now = time.Now()
var res, err = m.httpClient.Get(fmt.Sprintf("http://%s", m.collectorHealthHostPort))
var duration = time.Since(now)
switch {
case err != nil:
err = errors.Wrap(err, "could not query jaeger collector health check service")
status = KO
case res.StatusCode != 204:
err = errors.Wrapf(err, "jaeger health check service returned invalid status code: %v", res.StatusCode)
status = KO
default:
status = OK
}
return jaegerReport{
Name: name,
Duration: duration.String(),
Status: status.String(),
Error: str(err),
}
}