mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Metrics API: Use jsoniter for JSON encoding (#30250)
* QueryMetricsV2: Stream response Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com> * API: Use jsoniter instead of standard JSON package Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
parent
07aa956667
commit
0e2f1fe3f5
1
go.mod
1
go.mod
@ -53,6 +53,7 @@ require (
|
|||||||
github.com/influxdata/influxdb-client-go/v2 v2.2.0
|
github.com/influxdata/influxdb-client-go/v2 v2.2.0
|
||||||
github.com/jmespath/go-jmespath v0.4.0
|
github.com/jmespath/go-jmespath v0.4.0
|
||||||
github.com/jonboulle/clockwork v0.2.2 // indirect
|
github.com/jonboulle/clockwork v0.2.2 // indirect
|
||||||
|
github.com/json-iterator/go v1.1.10
|
||||||
github.com/jung-kurt/gofpdf v1.10.1
|
github.com/jung-kurt/gofpdf v1.10.1
|
||||||
github.com/lib/pq v1.9.0
|
github.com/lib/pq v1.9.0
|
||||||
github.com/linkedin/goavro/v2 v2.9.7
|
github.com/linkedin/goavro/v2 v2.9.7
|
||||||
|
@ -15,22 +15,6 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
type Response interface {
|
|
||||||
WriteTo(ctx *models.ReqContext)
|
|
||||||
// Status gets the response's status.
|
|
||||||
Status() int
|
|
||||||
// Body gets the response's body.
|
|
||||||
Body() []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
type NormalResponse struct {
|
|
||||||
status int
|
|
||||||
body []byte
|
|
||||||
header http.Header
|
|
||||||
errMessage string
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
func Wrap(action interface{}) macaron.Handler {
|
func Wrap(action interface{}) macaron.Handler {
|
||||||
return func(c *models.ReqContext) {
|
return func(c *models.ReqContext) {
|
||||||
var res Response
|
var res Response
|
||||||
@ -45,46 +29,22 @@ func Wrap(action interface{}) macaron.Handler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Status gets the response's status.
|
// JSON creates a JSON response.
|
||||||
func (r *NormalResponse) Status() int {
|
|
||||||
return r.status
|
|
||||||
}
|
|
||||||
|
|
||||||
// Body gets the response's body.
|
|
||||||
func (r *NormalResponse) Body() []byte {
|
|
||||||
return r.body
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *NormalResponse) WriteTo(ctx *models.ReqContext) {
|
|
||||||
if r.err != nil {
|
|
||||||
ctx.Logger.Error(r.errMessage, "error", r.err, "remote_addr", ctx.RemoteAddr())
|
|
||||||
}
|
|
||||||
|
|
||||||
header := ctx.Resp.Header()
|
|
||||||
for k, v := range r.header {
|
|
||||||
header[k] = v
|
|
||||||
}
|
|
||||||
ctx.Resp.WriteHeader(r.status)
|
|
||||||
if _, err := ctx.Resp.Write(r.body); err != nil {
|
|
||||||
ctx.Logger.Error("Error writing to response", "err", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *NormalResponse) Header(key, value string) *NormalResponse {
|
|
||||||
r.header.Set(key, value)
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
// Empty creates an empty response.
|
|
||||||
func Empty(status int) *NormalResponse {
|
|
||||||
return Respond(status, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
// JSON create a JSON response
|
|
||||||
func JSON(status int, body interface{}) *NormalResponse {
|
func JSON(status int, body interface{}) *NormalResponse {
|
||||||
return Respond(status, body).Header("Content-Type", "application/json")
|
return Respond(status, body).Header("Content-Type", "application/json")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// jsonStreaming creates a streaming JSON response.
|
||||||
|
func jsonStreaming(status int, body interface{}) streamingResponse {
|
||||||
|
header := make(http.Header)
|
||||||
|
header.Set("Content-Type", "application/json")
|
||||||
|
return streamingResponse{
|
||||||
|
status: status,
|
||||||
|
body: body,
|
||||||
|
header: header,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Success create a successful response
|
// Success create a successful response
|
||||||
func Success(message string) *NormalResponse {
|
func Success(message string) *NormalResponse {
|
||||||
resp := make(map[string]interface{})
|
resp := make(map[string]interface{})
|
||||||
@ -123,16 +83,16 @@ func Error(status int, message string, err error) *NormalResponse {
|
|||||||
return resp
|
return resp
|
||||||
}
|
}
|
||||||
|
|
||||||
// Respond create a response
|
// Respond creates a response.
|
||||||
func Respond(status int, body interface{}) *NormalResponse {
|
func Respond(status int, body interface{}) *NormalResponse {
|
||||||
var b []byte
|
var b []byte
|
||||||
var err error
|
|
||||||
switch t := body.(type) {
|
switch t := body.(type) {
|
||||||
case []byte:
|
case []byte:
|
||||||
b = t
|
b = t
|
||||||
case string:
|
case string:
|
||||||
b = []byte(t)
|
b = []byte(t)
|
||||||
default:
|
default:
|
||||||
|
var err error
|
||||||
if b, err = json.Marshal(body); err != nil {
|
if b, err = json.Marshal(body); err != nil {
|
||||||
return Error(500, "body json marshal", err)
|
return Error(500, "body json marshal", err)
|
||||||
}
|
}
|
||||||
@ -144,28 +104,6 @@ func Respond(status int, body interface{}) *NormalResponse {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// RedirectResponse represents a redirect response.
|
|
||||||
type RedirectResponse struct {
|
|
||||||
location string
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteTo writes to a response.
|
|
||||||
func (r *RedirectResponse) WriteTo(ctx *models.ReqContext) {
|
|
||||||
ctx.Redirect(r.location)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Status gets the response's status.
|
|
||||||
// Required to implement api.Response.
|
|
||||||
func (*RedirectResponse) Status() int {
|
|
||||||
return http.StatusFound
|
|
||||||
}
|
|
||||||
|
|
||||||
// Body gets the response's body.
|
|
||||||
// Required to implement api.Response.
|
|
||||||
func (r *RedirectResponse) Body() []byte {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func Redirect(location string) *RedirectResponse {
|
func Redirect(location string) *RedirectResponse {
|
||||||
return &RedirectResponse{location: location}
|
return &RedirectResponse{location: location}
|
||||||
}
|
}
|
||||||
|
@ -16,22 +16,22 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/util"
|
"github.com/grafana/grafana/pkg/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// QueryMetricsV2 returns query metrics
|
// QueryMetricsV2 returns query metrics.
|
||||||
// POST /api/ds/query DataSource query w/ expressions
|
// POST /api/ds/query DataSource query w/ expressions
|
||||||
func (hs *HTTPServer) QueryMetricsV2(c *models.ReqContext, reqDto dtos.MetricRequest) Response {
|
func (hs *HTTPServer) QueryMetricsV2(c *models.ReqContext, reqDTO dtos.MetricRequest) Response {
|
||||||
if len(reqDto.Queries) == 0 {
|
if len(reqDTO.Queries) == 0 {
|
||||||
return Error(400, "No queries found in query", nil)
|
return Error(400, "No queries found in query", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
request := &tsdb.TsdbQuery{
|
request := &tsdb.TsdbQuery{
|
||||||
TimeRange: tsdb.NewTimeRange(reqDto.From, reqDto.To),
|
TimeRange: tsdb.NewTimeRange(reqDTO.From, reqDTO.To),
|
||||||
Debug: reqDto.Debug,
|
Debug: reqDTO.Debug,
|
||||||
User: c.SignedInUser,
|
User: c.SignedInUser,
|
||||||
}
|
}
|
||||||
|
|
||||||
hasExpr := false
|
hasExpr := false
|
||||||
var ds *models.DataSource
|
var ds *models.DataSource
|
||||||
for i, query := range reqDto.Queries {
|
for i, query := range reqDTO.Queries {
|
||||||
hs.log.Debug("Processing metrics query", "query", query)
|
hs.log.Debug("Processing metrics query", "query", query)
|
||||||
name := query.Get("datasource").MustString("")
|
name := query.Get("datasource").MustString("")
|
||||||
if name == expr.DatasourceName {
|
if name == expr.DatasourceName {
|
||||||
@ -95,7 +95,7 @@ func (hs *HTTPServer) QueryMetricsV2(c *models.ReqContext, reqDto dtos.MetricReq
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return JSON(statusCode, &resp)
|
return jsonStreaming(statusCode, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryMetrics returns query metrics
|
// QueryMetrics returns query metrics
|
||||||
|
116
pkg/api/response.go
Normal file
116
pkg/api/response.go
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/models"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Response is an HTTP response interface.
|
||||||
|
type Response interface {
|
||||||
|
// WriteTo writes to a context.
|
||||||
|
WriteTo(ctx *models.ReqContext)
|
||||||
|
// Body gets the response's body.
|
||||||
|
Body() []byte
|
||||||
|
// Status gets the response's status.
|
||||||
|
Status() int
|
||||||
|
}
|
||||||
|
|
||||||
|
type NormalResponse struct {
|
||||||
|
status int
|
||||||
|
body []byte
|
||||||
|
header http.Header
|
||||||
|
errMessage string
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Status gets the response's status.
|
||||||
|
func (r *NormalResponse) Status() int {
|
||||||
|
return r.status
|
||||||
|
}
|
||||||
|
|
||||||
|
// Body gets the response's body.
|
||||||
|
func (r *NormalResponse) Body() []byte {
|
||||||
|
return r.body
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *NormalResponse) WriteTo(ctx *models.ReqContext) {
|
||||||
|
if r.err != nil {
|
||||||
|
ctx.Logger.Error(r.errMessage, "error", r.err, "remote_addr", ctx.RemoteAddr())
|
||||||
|
}
|
||||||
|
|
||||||
|
header := ctx.Resp.Header()
|
||||||
|
for k, v := range r.header {
|
||||||
|
header[k] = v
|
||||||
|
}
|
||||||
|
ctx.Resp.WriteHeader(r.status)
|
||||||
|
if _, err := ctx.Resp.Write(r.body); err != nil {
|
||||||
|
ctx.Logger.Error("Error writing to response", "err", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *NormalResponse) Header(key, value string) *NormalResponse {
|
||||||
|
r.header.Set(key, value)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// Empty creates an empty NormalResponse.
|
||||||
|
func Empty(status int) *NormalResponse {
|
||||||
|
return Respond(status, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// streamingResponse is a response that streams itself back to the client.
|
||||||
|
type streamingResponse struct {
|
||||||
|
body interface{}
|
||||||
|
status int
|
||||||
|
header http.Header
|
||||||
|
}
|
||||||
|
|
||||||
|
// Status gets the response's status.
|
||||||
|
// Required to implement api.Response.
|
||||||
|
func (r streamingResponse) Status() int {
|
||||||
|
return r.status
|
||||||
|
}
|
||||||
|
|
||||||
|
// Body gets the response's body.
|
||||||
|
// Required to implement api.Response.
|
||||||
|
func (r streamingResponse) Body() []byte {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteTo writes the response to the provided context.
|
||||||
|
// Required to implement api.Response.
|
||||||
|
func (r streamingResponse) WriteTo(ctx *models.ReqContext) {
|
||||||
|
header := ctx.Resp.Header()
|
||||||
|
for k, v := range r.header {
|
||||||
|
header[k] = v
|
||||||
|
}
|
||||||
|
ctx.Resp.WriteHeader(r.status)
|
||||||
|
enc := jsoniter.NewEncoder(ctx.Resp)
|
||||||
|
if err := enc.Encode(r.body); err != nil {
|
||||||
|
ctx.Logger.Error("Error writing to response", "err", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RedirectResponse represents a redirect response.
|
||||||
|
type RedirectResponse struct {
|
||||||
|
location string
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteTo writes to a response.
|
||||||
|
func (r *RedirectResponse) WriteTo(ctx *models.ReqContext) {
|
||||||
|
ctx.Redirect(r.location)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Status gets the response's status.
|
||||||
|
// Required to implement api.Response.
|
||||||
|
func (*RedirectResponse) Status() int {
|
||||||
|
return http.StatusFound
|
||||||
|
}
|
||||||
|
|
||||||
|
// Body gets the response's body.
|
||||||
|
// Required to implement api.Response.
|
||||||
|
func (r *RedirectResponse) Body() []byte {
|
||||||
|
return nil
|
||||||
|
}
|
@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/components/null"
|
"github.com/grafana/grafana/pkg/components/null"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TsdbQuery contains all information about a query request.
|
// TsdbQuery contains all information about a query request.
|
||||||
@ -263,5 +264,5 @@ func (df *dataFrames) MarshalJSON() ([]byte, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return json.Marshal(encoded)
|
return jsoniter.Marshal(encoded)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user