78596a6756
Fixes #30144 Co-authored-by: dsotirakis <sotirakis.dim@gmail.com> Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com> Co-authored-by: Ida Furjesova <ida.furjesova@grafana.com> Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com> Co-authored-by: Will Browne <wbrowne@users.noreply.github.com> Co-authored-by: Leon Sorokin <leeoniya@gmail.com> Co-authored-by: Andrej Ocenas <mr.ocenas@gmail.com> Co-authored-by: spinillos <selenepinillos@gmail.com> Co-authored-by: Karl Persson <kalle.persson@grafana.com> Co-authored-by: Leonard Gram <leo@xlson.com>
95 lines
2.4 KiB
Go
95 lines
2.4 KiB
Go
package pushhttp
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
"github.com/grafana/grafana/pkg/models"
|
|
"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"
|
|
)
|
|
|
|
var (
|
|
logger = log.New("live.push_http")
|
|
)
|
|
|
|
func ProvideService(cfg *setting.Cfg, live *live.GrafanaLive) *Gateway {
|
|
logger.Info("Live Push Gateway initialization")
|
|
g := &Gateway{
|
|
Cfg: cfg,
|
|
GrafanaLive: live,
|
|
converter: convert.NewConverter(),
|
|
}
|
|
return g
|
|
}
|
|
|
|
// Gateway receives data and translates it to Grafana Live publications.
|
|
type Gateway struct {
|
|
Cfg *setting.Cfg
|
|
GrafanaLive *live.GrafanaLive
|
|
|
|
converter *convert.Converter
|
|
}
|
|
|
|
// Run Gateway.
|
|
func (g *Gateway) Run(ctx context.Context) error {
|
|
<-ctx.Done()
|
|
return ctx.Err()
|
|
}
|
|
|
|
func (g *Gateway) Handle(ctx *models.ReqContext) {
|
|
streamID := ctx.Params(":streamId")
|
|
|
|
stream, err := g.GrafanaLive.ManagedStreamRunner.GetOrCreateStream(ctx.SignedInUser.OrgId, streamID)
|
|
if err != nil {
|
|
logger.Error("Error getting stream", "error", err)
|
|
ctx.Resp.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// TODO Grafana 8: decide which formats to use or keep all.
|
|
urlValues := ctx.Req.URL.Query()
|
|
frameFormat := pushurl.FrameFormatFromValues(urlValues)
|
|
|
|
body, err := io.ReadAll(ctx.Req.Request.Body)
|
|
if err != nil {
|
|
logger.Error("Error reading body", "error", err)
|
|
ctx.Resp.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
logger.Debug("Live Push request",
|
|
"protocol", "http",
|
|
"streamId", streamID,
|
|
"bodyLength", len(body),
|
|
"frameFormat", frameFormat,
|
|
)
|
|
|
|
metricFrames, err := g.converter.Convert(body, frameFormat)
|
|
if err != nil {
|
|
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
|
|
}
|
|
|
|
// TODO -- make sure all packets are combined together!
|
|
// interval = "1s" vs flush_interval = "5s"
|
|
|
|
for _, mf := range metricFrames {
|
|
err := stream.Push(mf.Key(), mf.Frame())
|
|
if err != nil {
|
|
logger.Error("Error pushing frame", "error", err, "data", string(body))
|
|
ctx.Resp.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
}
|