github.com/influxdata/line-protocol -> github.com/influxdata/line-protocol/v2/lineprotocol

This commit is contained in:
Holger Obermaier 2023-09-15 15:59:03 +02:00
parent baa45b833b
commit e34b0166f9

View File

@ -5,15 +5,15 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io"
"net/http" "net/http"
"strings" "strings"
"sync" "sync"
"time"
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger" cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
lp "github.com/ClusterCockpit/cc-metric-collector/pkg/ccMetric" lp "github.com/ClusterCockpit/cc-metric-collector/pkg/ccMetric"
"github.com/gorilla/mux" "github.com/gorilla/mux"
influx "github.com/influxdata/line-protocol" influx "github.com/influxdata/line-protocol/v2/lineprotocol"
) )
const HTTP_RECEIVER_PORT = "8080" const HTTP_RECEIVER_PORT = "8080"
@ -27,8 +27,6 @@ type HttpReceiverConfig struct {
type HttpReceiver struct { type HttpReceiver struct {
receiver receiver
handler *influx.MetricHandler
parser *influx.Parser
meta map[string]string meta map[string]string
config HttpReceiverConfig config HttpReceiverConfig
router *mux.Router router *mux.Router
@ -57,9 +55,6 @@ func (r *HttpReceiver) Init(name string, config json.RawMessage) error {
addr := fmt.Sprintf("%s:%s", r.config.Addr, r.config.Port) addr := fmt.Sprintf("%s:%s", r.config.Addr, r.config.Port)
uri := addr + p uri := addr + p
cclog.ComponentDebug(r.name, "INIT", "listen on:", uri) cclog.ComponentDebug(r.name, "INIT", "listen on:", uri)
r.handler = influx.NewMetricHandler()
r.parser = influx.NewParser(r.handler)
r.parser.SetTimeFunc(DefaultTime)
// Create new router and register p as path // Create new router and register p as path
r.router = mux.NewRouter() r.router = mux.NewRouter()
@ -91,26 +86,80 @@ func (r *HttpReceiver) ServerHttp(w http.ResponseWriter, req *http.Request) {
return return
} }
body, err := io.ReadAll(req.Body) d := influx.NewDecoder(req.Body)
for d.Next() {
// Decode measurement name
measurement, err := d.Measurement()
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) msg := "ServerHttp: Failed to decode measurement: " + err.Error()
cclog.ComponentError(r.name, msg)
http.Error(w, msg, http.StatusInternalServerError)
return return
} }
metrics, err := r.parser.Parse(body)
// Decode tags
tags := make(map[string]string)
for {
key, value, err := d.NextTag()
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) msg := "ServerHttp: Failed to decode tag: " + err.Error()
cclog.ComponentError(r.name, msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
if key == nil {
break
}
tags[string(key)] = string(value)
} }
for _, m := range metrics { // Decode fields
y := lp.FromInfluxMetric(m) fields := make(map[string]interface{})
for k, v := range r.meta { for {
y.AddMeta(k, v) key, value, err := d.NextField()
if err != nil {
msg := "ServerHttp: Failed to decode field: " + err.Error()
cclog.ComponentError(r.name, msg)
http.Error(w, msg, http.StatusInternalServerError)
return
} }
if key == nil {
break
}
fields[string(key)] = value.Interface()
}
t, err := d.Time(influx.Nanosecond, time.Time{})
if err != nil {
msg := "ServerHttp: Failed to decode time: " + err.Error()
cclog.ComponentError(r.name, msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
y, _ := lp.New(
string(measurement),
tags,
r.meta,
fields,
t,
)
if r.sink != nil { if r.sink != nil {
r.sink <- y r.sink <- y
} }
} }
// Check for IO errors
err := d.Err()
if err != nil {
msg := "ServerHttp: Failed to decode: " + err.Error()
cclog.ComponentError(r.name, msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }