api: return errors via JSON, not status codes

This commit is contained in:
Lou Knauer 2021-10-19 12:17:16 +02:00
parent 024f66f49c
commit 1c12c655f1
2 changed files with 35 additions and 26 deletions

14
api.go
View File

@ -30,12 +30,14 @@ type ApiRequestBody struct {
} }
type ApiMetricData struct { type ApiMetricData struct {
Error *string `json:"error"`
From int64 `json:"from"` From int64 `json:"from"`
To int64 `json:"to"` To int64 `json:"to"`
Data []Float `json:"data"` Data []Float `json:"data"`
} }
type ApiStatsData struct { type ApiStatsData struct {
Error *string `json:"error"`
From int64 `json:"from"` From int64 `json:"from"`
To int64 `json:"to"` To int64 `json:"to"`
Samples int `json:"samples"` Samples int `json:"samples"`
@ -76,8 +78,10 @@ func handleTimeseries(rw http.ResponseWriter, r *http.Request) {
for _, metric := range reqBody.Metrics { for _, metric := range reqBody.Metrics {
data, f, t, err := memoryStore.Read(selector, metric, from, to) data, f, t, err := memoryStore.Read(selector, metric, from, to)
if err != nil { if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) // http.Error(rw, err.Error(), http.StatusInternalServerError)
return msg := err.Error()
metrics[metric] = ApiMetricData{ Error: &msg }
continue
} }
metrics[metric] = ApiMetricData{ metrics[metric] = ApiMetricData{
@ -128,8 +132,10 @@ func handleStats(rw http.ResponseWriter, r *http.Request) {
for _, metric := range reqBody.Metrics { for _, metric := range reqBody.Metrics {
stats, f, t, err := memoryStore.Stats(selector, metric, from, to) stats, f, t, err := memoryStore.Stats(selector, metric, from, to)
if err != nil { if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) // http.Error(rw, err.Error(), http.StatusInternalServerError)
return msg := err.Error()
metrics[metric] = ApiStatsData{ Error: &msg }
continue
} }
metrics[metric] = ApiStatsData{ metrics[metric] = ApiStatsData{

View File

@ -1,17 +1,19 @@
{ {
"metrics": { "metrics": {
"load_one": { "frequency": 3, "aggregation": null, "scope": "node" }, "load_one": { "frequency": 10, "aggregation": null, "scope": "node" },
"load_five": { "frequency": 3, "aggregation": null, "scope": "node" }, "load_five": { "frequency": 10, "aggregation": null, "scope": "node" },
"load_fifteen": { "frequency": 3, "aggregation": null, "scope": "node" }, "load_fifteen": { "frequency": 10, "aggregation": null, "scope": "node" },
"proc_run": { "frequency": 3, "aggregation": null, "scope": "node" }, "proc_run": { "frequency": 10, "aggregation": null, "scope": "node" },
"proc_total": { "frequency": 3, "aggregation": null, "scope": "node" }, "proc_total": { "frequency": 10, "aggregation": null, "scope": "node" },
"power": { "frequency": 3, "aggregation": "sum", "scope": "socket" }, "mem_free": { "frequency": 10, "aggregation": null, "scope": "node" },
"mem_bw": { "frequency": 3, "aggregation": "sum", "scope": "socket" }, "mem_used": { "frequency": 10, "aggregation": null, "scope": "node" },
"flops_sp": { "frequency": 3, "aggregation": "sum", "scope": "cpu" }, "power": { "frequency": 10, "aggregation": "sum", "scope": "socket" },
"flops_dp": { "frequency": 3, "aggregation": "sum", "scope": "cpu" }, "mem_bw": { "frequency": 10, "aggregation": "sum", "scope": "socket" },
"flops_any": { "frequency": 3, "aggregation": "sum", "scope": "cpu" }, "flops_sp": { "frequency": 10, "aggregation": "sum", "scope": "cpu" },
"clock": { "frequency": 3, "aggregation": "avg", "scope": "cpu" }, "flops_dp": { "frequency": 10, "aggregation": "sum", "scope": "cpu" },
"cpi": { "frequency": 3, "aggregation": "avg", "scope": "cpu" } "flops_any": { "frequency": 10, "aggregation": "sum", "scope": "cpu" },
"clock": { "frequency": 10, "aggregation": "avg", "scope": "cpu" },
"cpi": { "frequency": 10, "aggregation": "avg", "scope": "cpu" }
}, },
"checkpoints": { "checkpoints": {
"interval": 21600, "interval": 21600,
@ -23,6 +25,7 @@
"directory": "./var/archive" "directory": "./var/archive"
}, },
"retention-in-memory": 86400, "retention-in-memory": 86400,
"nats": "nats://localhost:4222", "http-api-address": "0.0.0.0:8081",
"nats": null,
"jwt-public-key": "kzfYrYy+TzpanWZHJ5qSdMj5uKUWgq74BWhQG6copP0=" "jwt-public-key": "kzfYrYy+TzpanWZHJ5qSdMj5uKUWgq74BWhQG6copP0="
} }