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:
Arve Knudsen
2021-01-13 15:20:25 +01:00
committed by GitHub
parent 07aa956667
commit 0e2f1fe3f5
5 changed files with 140 additions and 84 deletions
+14 -76
View File
@@ -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 {
return func(c *models.ReqContext) {
var res Response
@@ -45,46 +29,22 @@ func Wrap(action interface{}) macaron.Handler {
}
}
// 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 response.
func Empty(status int) *NormalResponse {
return Respond(status, nil)
}
// JSON create a JSON response
// JSON creates a JSON response.
func JSON(status int, body interface{}) *NormalResponse {
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
func Success(message string) *NormalResponse {
resp := make(map[string]interface{})
@@ -123,16 +83,16 @@ func Error(status int, message string, err error) *NormalResponse {
return resp
}
// Respond create a response
// Respond creates a response.
func Respond(status int, body interface{}) *NormalResponse {
var b []byte
var err error
switch t := body.(type) {
case []byte:
b = t
case string:
b = []byte(t)
default:
var err error
if b, err = json.Marshal(body); err != nil {
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 {
return &RedirectResponse{location: location}
}