mirror of
https://github.com/ClusterCockpit/cc-metric-store.git
synced 2025-03-01 04:27:06 +01:00
Add (untested) HTTP write API
This commit is contained in:
parent
a15238a7a9
commit
807c613cae
19
api.go
19
api.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"crypto/ed25519"
|
"crypto/ed25519"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
@ -15,6 +16,7 @@ import (
|
|||||||
|
|
||||||
"github.com/golang-jwt/jwt/v4"
|
"github.com/golang-jwt/jwt/v4"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/influxdata/line-protocol/v2/lineprotocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Example:
|
// Example:
|
||||||
@ -208,6 +210,22 @@ func handlePeek(rw http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleWrite(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(rw, "Method Not Allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
reader := bufio.NewReader(r.Body)
|
||||||
|
dec := lineprotocol.NewDecoder(reader)
|
||||||
|
// Unlike the name suggests, handleLine can handle multiple lines
|
||||||
|
if err := handleLine(dec); err != nil {
|
||||||
|
http.Error(rw, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
rw.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
func authentication(next http.Handler, publicKey ed25519.PublicKey) http.Handler {
|
func authentication(next http.Handler, publicKey ed25519.PublicKey) http.Handler {
|
||||||
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||||
authheader := r.Header.Get("Authorization")
|
authheader := r.Header.Get("Authorization")
|
||||||
@ -244,6 +262,7 @@ func StartApiServer(address string, ctx context.Context) error {
|
|||||||
r.HandleFunc("/api/{from:[0-9]+}/{to:[0-9]+}/stats", handleStats)
|
r.HandleFunc("/api/{from:[0-9]+}/{to:[0-9]+}/stats", handleStats)
|
||||||
r.HandleFunc("/api/{to:[0-9]+}/free", handleFree)
|
r.HandleFunc("/api/{to:[0-9]+}/free", handleFree)
|
||||||
r.HandleFunc("/api/{cluster}/peek", handlePeek)
|
r.HandleFunc("/api/{cluster}/peek", handlePeek)
|
||||||
|
r.HandleFunc("/api/write", handleWrite)
|
||||||
|
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
Handler: r,
|
Handler: r,
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
"net"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@ -53,48 +51,10 @@ type Metric struct {
|
|||||||
Value Float
|
Value Float
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen for connections sending metric data in the line protocol format.
|
|
||||||
//
|
|
||||||
// This is a blocking function, send `true` through the channel argument to shut down the server.
|
|
||||||
// `handleLine` will be called from different go routines for different connections.
|
|
||||||
//
|
|
||||||
func ReceiveTCP(address string, handleLine func(dec *lineprotocol.Decoder), done chan bool) error {
|
|
||||||
ln, err := net.Listen("tcp", address)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
stop := <-done
|
|
||||||
if stop {
|
|
||||||
err := ln.Close()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("closing listener failed: %s\n", err.Error())
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
for {
|
|
||||||
conn, err := ln.Accept()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
reader := bufio.NewReader(conn)
|
|
||||||
dec := lineprotocol.NewDecoder(reader)
|
|
||||||
handleLine(dec)
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Connect to a nats server and subscribe to "updates". This is a blocking
|
// Connect to a nats server and subscribe to "updates". This is a blocking
|
||||||
// function. handleLine will be called for each line recieved via nats.
|
// function. handleLine will be called for each line recieved via nats.
|
||||||
// Send `true` through the done channel for gracefull termination.
|
// Send `true` through the done channel for gracefull termination.
|
||||||
func ReceiveNats(address string, handleLine func(dec *lineprotocol.Decoder), workers int, ctx context.Context) error {
|
func ReceiveNats(address string, handleLine func(dec *lineprotocol.Decoder) error, workers int, ctx context.Context) error {
|
||||||
nc, err := nats.Connect(nats.DefaultURL)
|
nc, err := nats.Connect(nats.DefaultURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -113,7 +73,9 @@ func ReceiveNats(address string, handleLine func(dec *lineprotocol.Decoder), wor
|
|||||||
go func() {
|
go func() {
|
||||||
for m := range msgs {
|
for m := range msgs {
|
||||||
dec := lineprotocol.NewDecoderWithBytes(m.Data)
|
dec := lineprotocol.NewDecoderWithBytes(m.Data)
|
||||||
handleLine(dec)
|
if err := handleLine(dec); err != nil {
|
||||||
|
log.Printf("error: %s\n", err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Done()
|
wg.Done()
|
||||||
@ -126,7 +88,9 @@ func ReceiveNats(address string, handleLine func(dec *lineprotocol.Decoder), wor
|
|||||||
} else {
|
} else {
|
||||||
sub, err = nc.Subscribe("updates", func(m *nats.Msg) {
|
sub, err = nc.Subscribe("updates", func(m *nats.Msg) {
|
||||||
dec := lineprotocol.NewDecoderWithBytes(m.Data)
|
dec := lineprotocol.NewDecoderWithBytes(m.Data)
|
||||||
handleLine(dec)
|
if err := handleLine(dec); err != nil {
|
||||||
|
log.Printf("error: %s\n", err.Error())
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,18 +52,18 @@ func loadConfiguration(file string) Config {
|
|||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleLine(dec *lineprotocol.Decoder) {
|
func handleLine(dec *lineprotocol.Decoder) error {
|
||||||
for dec.Next() {
|
for dec.Next() {
|
||||||
measurement, err := dec.Measurement()
|
measurement, err := dec.Measurement()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var cluster, host, typeName, typeId string
|
var cluster, host, typeName, typeId string
|
||||||
for {
|
for {
|
||||||
key, val, err := dec.NextTag()
|
key, val, err := dec.NextTag()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return err
|
||||||
}
|
}
|
||||||
if key == nil {
|
if key == nil {
|
||||||
break
|
break
|
||||||
@ -79,7 +79,7 @@ func handleLine(dec *lineprotocol.Decoder) {
|
|||||||
case "type-id":
|
case "type-id":
|
||||||
typeId = string(val)
|
typeId = string(val)
|
||||||
default:
|
default:
|
||||||
log.Fatalf("Unkown tag: '%s' (value: '%s')", string(key), string(val))
|
return fmt.Errorf("unkown tag: '%s' (value: '%s')", string(key), string(val))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,7 +94,7 @@ func handleLine(dec *lineprotocol.Decoder) {
|
|||||||
for {
|
for {
|
||||||
key, val, err := dec.NextField()
|
key, val, err := dec.NextField()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if key == nil {
|
if key == nil {
|
||||||
@ -102,7 +102,7 @@ func handleLine(dec *lineprotocol.Decoder) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if string(key) != "value" {
|
if string(key) != "value" {
|
||||||
log.Fatalf("Unkown field: '%s' (value: %#v)", string(key), val)
|
return fmt.Errorf("unkown field: '%s' (value: %#v)", string(key), val)
|
||||||
}
|
}
|
||||||
|
|
||||||
if val.Kind() == lineprotocol.Float {
|
if val.Kind() == lineprotocol.Float {
|
||||||
@ -110,21 +110,22 @@ func handleLine(dec *lineprotocol.Decoder) {
|
|||||||
} else if val.Kind() == lineprotocol.Int {
|
} else if val.Kind() == lineprotocol.Int {
|
||||||
value = Float(val.IntV())
|
value = Float(val.IntV())
|
||||||
} else {
|
} else {
|
||||||
log.Fatalf("Unsupported value type in message: %s", val.Kind().String())
|
return fmt.Errorf("unsupported value type in message: %s", val.Kind().String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t, err := dec.Time(lineprotocol.Second, time.Now())
|
t, err := dec.Time(lineprotocol.Second, time.Now())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := memoryStore.Write(selector, t.Unix(), []Metric{
|
if err := memoryStore.Write(selector, t.Unix(), []Metric{
|
||||||
{Name: string(measurement), Value: value},
|
{Name: string(measurement), Value: value},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Fatal(err)
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func intervals(wg *sync.WaitGroup, ctx context.Context) {
|
func intervals(wg *sync.WaitGroup, ctx context.Context) {
|
||||||
|
Loading…
Reference in New Issue
Block a user