/
health.go
199 lines (177 loc) · 4.8 KB
/
health.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package plugin
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/build"
)
var openAIModels = []string{"gpt-3.5-turbo", "gpt-4"}
type healthCheckClient interface {
Do(req *http.Request) (*http.Response, error)
}
type openAIModelHealth struct {
OK bool `json:"ok"`
Error string `json:"error,omitempty"`
}
type openAIHealthDetails struct {
Configured bool `json:"configured"`
OK bool `json:"ok"`
Error string `json:"error,omitempty"`
Models map[string]openAIModelHealth `json:"models"`
}
type vectorHealthDetails struct {
Enabled bool `json:"enabled"`
OK bool `json:"ok"`
Error string `json:"error,omitempty"`
}
type healthCheckDetails struct {
OpenAI openAIHealthDetails `json:"openAI"`
Vector vectorHealthDetails `json:"vector"`
Version string `json:"version"`
}
func getVersion() string {
buildInfo, err := build.GetBuildInfo()
if err != nil {
return "unknown"
}
return buildInfo.Version
}
func (a *App) testOpenAIModel(ctx context.Context, model string) error {
body := map[string]interface{}{
"model": model,
"messages": []map[string]interface{}{
{
"role": "user",
"content": "Hello",
},
},
"max_tokens": 1,
}
req, err := a.newOpenAIChatCompletionsRequest(ctx, body)
if err != nil {
return fmt.Errorf("create request: %w", err)
}
resp, err := a.healthCheckClient.Do(req)
if err != nil {
return fmt.Errorf("make request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return fmt.Errorf("unexpected status code: %d: %s", resp.StatusCode, respBody)
}
return nil
}
// openAIHealth checks the health of the OpenAI configuration and caches the
// result if successful. The caller must lock a.healthCheckMutex.
func (a *App) openAIHealth(ctx context.Context, req *backend.CheckHealthRequest) (openAIHealthDetails, error) {
if a.healthOpenAI != nil {
return *a.healthOpenAI, nil
}
d := openAIHealthDetails{
OK: true,
Configured: ((a.settings.OpenAI.Provider == openAIProviderAzure || a.settings.OpenAI.Provider == openAIProviderOpenAI) && a.settings.OpenAI.apiKey != "") || a.settings.OpenAI.Provider == openAIProviderGrafana,
Models: map[string]openAIModelHealth{},
}
for _, model := range openAIModels {
health := openAIModelHealth{OK: false, Error: "OpenAI not configured"}
if d.Configured {
health.OK = true
health.Error = ""
err := a.testOpenAIModel(ctx, model)
if err != nil {
health.OK = false
health.Error = err.Error()
}
}
d.Models[model] = health
}
anyOK := false
for _, v := range d.Models {
if v.OK {
anyOK = true
break
}
}
if !anyOK {
d.OK = false
d.Error = "No functioning models are available"
}
// Only cache result if openAI is ok to use.
if d.OK {
a.healthOpenAI = &d
}
return d, nil
}
// testVectorService checks the health of VectorAPI and caches the result if
// successful. The caller must lock a.healthCheckMutex.
func (a *App) testVectorService(ctx context.Context) error {
if a.vectorService == nil {
return fmt.Errorf("vector service not configured")
}
err := a.vectorService.Health(ctx)
if err != nil {
return fmt.Errorf("vector service health check failed: %w", err)
}
return nil
}
func (a *App) vectorHealth(ctx context.Context) vectorHealthDetails {
if a.healthVector != nil {
return *a.healthVector
}
d := vectorHealthDetails{
Enabled: a.settings.Vector.Enabled,
OK: true,
}
if !d.Enabled {
d.OK = false
return d
}
err := a.testVectorService(ctx)
if err != nil {
d.OK = false
d.Error = err.Error()
}
// Only cache if the health check succeeded.
if d.OK {
a.healthVector = &d
}
return d
}
// CheckHealth handles health checks sent from Grafana to the plugin.
// It returns whether each feature is working based on the plugin settings.
func (a *App) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
a.healthCheckMutex.Lock()
defer a.healthCheckMutex.Unlock()
openAI, err := a.openAIHealth(ctx, req)
if err != nil {
openAI.OK = false
openAI.Error = err.Error()
}
vector := a.vectorHealth(ctx)
if vector.Error == "" {
a.healthVector = &vector
}
details := healthCheckDetails{
OpenAI: openAI,
Vector: vector,
Version: getVersion(),
}
body, err := json.Marshal(details)
if err != nil {
return &backend.CheckHealthResult{
Status: backend.HealthStatusError,
Message: "failed to marshal details",
}, nil
}
return &backend.CheckHealthResult{
Status: backend.HealthStatusOk,
JSONDetails: body,
}, nil
}