From 1c12c655f13838867ab0d466d3e8d1b7daed92f0 Mon Sep 17 00:00:00 2001 From: Lou Knauer Date: Tue, 19 Oct 2021 12:17:16 +0200 Subject: [PATCH] api: return errors via JSON, not status codes --- api.go | 32 +++++++++++++++++++------------- config.json | 29 ++++++++++++++++------------- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/api.go b/api.go index a397d0b..899c52d 100644 --- a/api.go +++ b/api.go @@ -30,18 +30,20 @@ type ApiRequestBody struct { } type ApiMetricData struct { - From int64 `json:"from"` - To int64 `json:"to"` - Data []Float `json:"data"` + Error *string `json:"error"` + From int64 `json:"from"` + To int64 `json:"to"` + Data []Float `json:"data"` } type ApiStatsData struct { - From int64 `json:"from"` - To int64 `json:"to"` - Samples int `json:"samples"` - Avg Float `json:"avg"` - Min Float `json:"min"` - Max Float `json:"max"` + Error *string `json:"error"` + From int64 `json:"from"` + To int64 `json:"to"` + Samples int `json:"samples"` + Avg Float `json:"avg"` + Min Float `json:"min"` + Max Float `json:"max"` } func handleTimeseries(rw http.ResponseWriter, r *http.Request) { @@ -76,8 +78,10 @@ func handleTimeseries(rw http.ResponseWriter, r *http.Request) { for _, metric := range reqBody.Metrics { data, f, t, err := memoryStore.Read(selector, metric, from, to) if err != nil { - http.Error(rw, err.Error(), http.StatusInternalServerError) - return + // http.Error(rw, err.Error(), http.StatusInternalServerError) + msg := err.Error() + metrics[metric] = ApiMetricData{ Error: &msg } + continue } metrics[metric] = ApiMetricData{ @@ -128,8 +132,10 @@ func handleStats(rw http.ResponseWriter, r *http.Request) { for _, metric := range reqBody.Metrics { stats, f, t, err := memoryStore.Stats(selector, metric, from, to) if err != nil { - http.Error(rw, err.Error(), http.StatusInternalServerError) - return + // http.Error(rw, err.Error(), http.StatusInternalServerError) + msg := err.Error() + metrics[metric] = ApiStatsData{ Error: &msg } + continue } metrics[metric] = ApiStatsData{ diff --git a/config.json b/config.json index d9cab72..f3d7ad4 100644 --- a/config.json +++ b/config.json @@ -1,17 +1,19 @@ { "metrics": { - "load_one": { "frequency": 3, "aggregation": null, "scope": "node" }, - "load_five": { "frequency": 3, "aggregation": null, "scope": "node" }, - "load_fifteen": { "frequency": 3, "aggregation": null, "scope": "node" }, - "proc_run": { "frequency": 3, "aggregation": null, "scope": "node" }, - "proc_total": { "frequency": 3, "aggregation": null, "scope": "node" }, - "power": { "frequency": 3, "aggregation": "sum", "scope": "socket" }, - "mem_bw": { "frequency": 3, "aggregation": "sum", "scope": "socket" }, - "flops_sp": { "frequency": 3, "aggregation": "sum", "scope": "cpu" }, - "flops_dp": { "frequency": 3, "aggregation": "sum", "scope": "cpu" }, - "flops_any": { "frequency": 3, "aggregation": "sum", "scope": "cpu" }, - "clock": { "frequency": 3, "aggregation": "avg", "scope": "cpu" }, - "cpi": { "frequency": 3, "aggregation": "avg", "scope": "cpu" } + "load_one": { "frequency": 10, "aggregation": null, "scope": "node" }, + "load_five": { "frequency": 10, "aggregation": null, "scope": "node" }, + "load_fifteen": { "frequency": 10, "aggregation": null, "scope": "node" }, + "proc_run": { "frequency": 10, "aggregation": null, "scope": "node" }, + "proc_total": { "frequency": 10, "aggregation": null, "scope": "node" }, + "mem_free": { "frequency": 10, "aggregation": null, "scope": "node" }, + "mem_used": { "frequency": 10, "aggregation": null, "scope": "node" }, + "power": { "frequency": 10, "aggregation": "sum", "scope": "socket" }, + "mem_bw": { "frequency": 10, "aggregation": "sum", "scope": "socket" }, + "flops_sp": { "frequency": 10, "aggregation": "sum", "scope": "cpu" }, + "flops_dp": { "frequency": 10, "aggregation": "sum", "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": { "interval": 21600, @@ -23,6 +25,7 @@ "directory": "./var/archive" }, "retention-in-memory": 86400, - "nats": "nats://localhost:4222", + "http-api-address": "0.0.0.0:8081", + "nats": null, "jwt-public-key": "kzfYrYy+TzpanWZHJ5qSdMj5uKUWgq74BWhQG6copP0=" }