Live: Telegraf input modifiers (#32982)

This commit is contained in:
Alexander Emelin
2021-04-19 18:48:43 +03:00
committed by GitHub
parent f92b6518c5
commit d807fbc9e9
10 changed files with 187 additions and 57 deletions
+24 -17
View File
@@ -2,14 +2,15 @@ package push
import (
"context"
"errors"
"net/http"
"github.com/grafana/grafana-live-sdk/telemetry/telegraf"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/services/live"
"github.com/grafana/grafana/pkg/services/live/convert"
"github.com/grafana/grafana/pkg/services/live/pushurl"
"github.com/grafana/grafana/pkg/setting"
)
@@ -26,8 +27,7 @@ type Gateway struct {
Cfg *setting.Cfg `inject:""`
GrafanaLive *live.GrafanaLive `inject:""`
telegrafConverterWide *telegraf.Converter
telegrafConverterLabelsColumn *telegraf.Converter
converter *convert.Converter
}
// Init Gateway.
@@ -39,9 +39,7 @@ func (g *Gateway) Init() error {
return nil
}
// For now only Telegraf converter (influx format) is supported.
g.telegrafConverterWide = telegraf.NewConverter()
g.telegrafConverterLabelsColumn = telegraf.NewConverter(telegraf.WithUseLabelsColumn(true))
g.converter = convert.NewConverter()
return nil
}
@@ -70,11 +68,10 @@ func (g *Gateway) Handle(ctx *models.ReqContext) {
return
}
// TODO Grafana 8: decide which format to use or keep both.
converter := g.telegrafConverterWide
if ctx.Req.URL.Query().Get("format") == "labels_column" {
converter = g.telegrafConverterLabelsColumn
}
// TODO Grafana 8: decide which formats to use or keep all.
urlValues := ctx.Req.URL.Query()
frameFormat := pushurl.FrameFormatFromValues(urlValues)
stableSchema := pushurl.StableSchemaFromValues(urlValues)
body, err := ctx.Req.Body().Bytes()
if err != nil {
@@ -82,12 +79,22 @@ func (g *Gateway) Handle(ctx *models.ReqContext) {
ctx.Resp.WriteHeader(http.StatusInternalServerError)
return
}
logger.Debug("Live Push request body", "streamId", streamID, "bodyLength", len(body))
logger.Debug("Live Push request",
"protocol", "http",
"streamId", streamID,
"bodyLength", len(body),
"stableSchema", stableSchema,
"frameFormat", frameFormat,
)
metricFrames, err := converter.Convert(body)
metricFrames, err := g.converter.Convert(body, frameFormat)
if err != nil {
logger.Error("Error converting metrics", "error", err)
ctx.Resp.WriteHeader(http.StatusInternalServerError)
logger.Error("Error converting metrics", "error", err, "frameFormat", frameFormat)
if errors.Is(err, convert.ErrUnsupportedFrameFormat) {
ctx.Resp.WriteHeader(http.StatusBadRequest)
} else {
ctx.Resp.WriteHeader(http.StatusInternalServerError)
}
return
}
@@ -95,7 +102,7 @@ func (g *Gateway) Handle(ctx *models.ReqContext) {
// interval = "1s" vs flush_interval = "5s"
for _, mf := range metricFrames {
err := stream.Push(mf.Key(), mf.Frame())
err := stream.Push(mf.Key(), mf.Frame(), stableSchema)
if err != nil {
ctx.Resp.WriteHeader(http.StatusInternalServerError)
return