/
diagnostics.go
140 lines (114 loc) · 4.37 KB
/
diagnostics.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package backend
import (
"context"
"net/http"
"strconv"
)
// CheckHealthHandler enables users to send health check
// requests to a backend plugin
type CheckHealthHandler interface {
CheckHealth(ctx context.Context, req *CheckHealthRequest) (*CheckHealthResult, error)
}
// CheckHealthHandlerFunc is an adapter to allow the use of
// ordinary functions as backend.CheckHealthHandler. If f is a function
// with the appropriate signature, CheckHealthHandlerFunc(f) is a
// Handler that calls f.
type CheckHealthHandlerFunc func(ctx context.Context, req *CheckHealthRequest) (*CheckHealthResult, error)
// CheckHealth calls fn(ctx, req).
func (fn CheckHealthHandlerFunc) CheckHealth(ctx context.Context, req *CheckHealthRequest) (*CheckHealthResult, error) {
return fn(ctx, req)
}
// HealthStatus is the status of the plugin.
type HealthStatus int
const (
// HealthStatusUnknown means the status of the plugin is unknown.
HealthStatusUnknown HealthStatus = iota
// HealthStatusOk means the status of the plugin is good.
HealthStatusOk
// HealthStatusError means the plugin is in an error state.
HealthStatusError
)
var healthStatusNames = map[int]string{
0: "UNKNOWN",
1: "OK",
2: "ERROR",
}
// String textual represntation of the status.
func (hs HealthStatus) String() string {
s, exists := healthStatusNames[int(hs)]
if exists {
return s
}
return strconv.Itoa(int(hs))
}
// CheckHealthRequest contains the healthcheck request
type CheckHealthRequest struct {
// PluginContext the contextual information for the request.
PluginContext PluginContext
// Headers the environment/metadata information for the request.
//
// To access forwarded HTTP headers please use
// GetHTTPHeaders or GetHTTPHeader.
Headers map[string]string
}
// SetHTTPHeader sets the header entries associated with key to the
// single element value. It replaces any existing values
// associated with key. The key is case insensitive; it is
// canonicalized by textproto.CanonicalMIMEHeaderKey.
func (req *CheckHealthRequest) SetHTTPHeader(key, value string) {
if req.Headers == nil {
req.Headers = map[string]string{}
}
setHTTPHeaderInStringMap(req.Headers, key, value)
}
// DeleteHTTPHeader deletes the values associated with key.
// The key is case insensitive; it is canonicalized by
// CanonicalHeaderKey.
func (req *CheckHealthRequest) DeleteHTTPHeader(key string) {
deleteHTTPHeaderInStringMap(req.Headers, key)
}
// GetHTTPHeader gets the first value associated with the given key. If
// there are no values associated with the key, Get returns "".
// It is case insensitive; textproto.CanonicalMIMEHeaderKey is
// used to canonicalize the provided key. Get assumes that all
// keys are stored in canonical form.
func (req *CheckHealthRequest) GetHTTPHeader(key string) string {
return req.GetHTTPHeaders().Get(key)
}
// GetHTTPHeaders returns HTTP headers.
func (req *CheckHealthRequest) GetHTTPHeaders() http.Header {
return getHTTPHeadersFromStringMap(req.Headers)
}
// CheckHealthResult contains the healthcheck response
type CheckHealthResult struct {
// Status the HealthStatus of the healthcheck.
Status HealthStatus
// Message the message of the healthcheck, if any.
Message string
// JSONDetails the details of the healthcheck, if any, encoded as JSON bytes.
JSONDetails []byte
}
// CollectMetricsHandler handles metric collection.
type CollectMetricsHandler interface {
CollectMetrics(ctx context.Context, req *CollectMetricsRequest) (*CollectMetricsResult, error)
}
// CollectMetricsHandlerFunc is an adapter to allow the use of
// ordinary functions as backend.CollectMetricsHandler. If f is a function
// with the appropriate signature, CollectMetricsHandlerFunc(f) is a
// Handler that calls f.
type CollectMetricsHandlerFunc func(ctx context.Context, req *CollectMetricsRequest) (*CollectMetricsResult, error)
// CollectMetrics calls fn(ctx, req).
func (fn CollectMetricsHandlerFunc) CollectMetrics(ctx context.Context, req *CollectMetricsRequest) (*CollectMetricsResult, error) {
return fn(ctx, req)
}
// CollectMetricsRequest contains the metrics request
type CollectMetricsRequest struct {
// PluginContext the contextual information for the request.
PluginContext PluginContext
}
// CollectMetricsResult collect metrics result.
type CollectMetricsResult struct {
// PrometheusMetrics the Prometheus metrics encoded as bytes.
PrometheusMetrics []byte
}
var _ ForwardHTTPHeaders = (*CheckHealthRequest)(nil)