cc-metric-store/api.go

108 lines
2.2 KiB
Go
Raw Normal View History

2021-08-20 12:54:11 +02:00
package main
import (
"context"
"encoding/json"
"log"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
)
// Example:
// [
// { "selector": ["emmy", "host123"], "metrics": ["load_one"] }
// ]
type ApiRequestBody []struct {
Selector []string `json:"selector"`
Metrics []string `json:"metrics"`
2021-08-20 12:54:11 +02:00
}
type ApiMetricData struct {
From int64 `json:"from"`
To int64 `json:"to"`
Data []Float `json:"data"`
2021-08-20 12:54:11 +02:00
}
2021-08-24 10:41:30 +02:00
func handleTimeseries(rw http.ResponseWriter, r *http.Request) {
2021-08-20 12:54:11 +02:00
vars := mux.Vars(r)
from, err := strconv.ParseInt(vars["from"], 10, 64)
if err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
to, err := strconv.ParseInt(vars["to"], 10, 64)
if err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
if r.Method != http.MethodPost {
http.Error(rw, "Method Not Allowed", http.StatusMethodNotAllowed)
2021-08-20 12:54:11 +02:00
return
}
bodyDec := json.NewDecoder(r.Body)
var reqBody ApiRequestBody
err = bodyDec.Decode(&reqBody)
2021-08-24 10:41:30 +02:00
if err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
res := make([]map[string]ApiMetricData, 0, len(reqBody))
for _, req := range reqBody {
metrics := make(map[string]ApiMetricData)
for _, metric := range req.Metrics {
data, f, t, err := memoryStore.Read(req.Selector, metric, from, to)
2021-08-24 10:41:30 +02:00
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
metrics[metric] = ApiMetricData{
From: f,
To: t,
Data: data,
}
2021-08-24 10:41:30 +02:00
}
res = append(res, metrics)
2021-08-24 10:41:30 +02:00
}
rw.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(rw).Encode(res)
2021-08-24 10:41:30 +02:00
if err != nil {
log.Println(err.Error())
}
}
2021-08-20 12:54:11 +02:00
func StartApiServer(address string, done chan bool) error {
r := mux.NewRouter()
r.HandleFunc("/api/{from:[0-9]+}/{to:[0-9]+}/timeseries", handleTimeseries)
2021-08-20 12:54:11 +02:00
server := &http.Server{
Handler: r,
Addr: address,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
go func() {
log.Printf("API http endpoint listening on '%s'\n", address)
err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Println(err)
}
}()
for {
_ = <-done
err := server.Shutdown(context.Background())
log.Println("API server shut down")
return err
}
}