Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
log http erroreous responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Dieterbe committed Aug 10, 2017
1 parent e63cc7c commit c88cd8c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
27 changes: 12 additions & 15 deletions api/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ func (s *Server) getNodeStatus(ctx *middleware.Context) {
func (s *Server) setNodeStatus(ctx *middleware.Context, status models.NodeStatus) {
primary, err := strconv.ParseBool(status.Primary)
if err != nil {
response.Write(ctx, response.NewError(http.StatusBadRequest, fmt.Sprintf(
"could not parse status to bool. %s",
err.Error())),
response.WriteErr(ctx.Req.Context(), ctx, response.NewError(http.StatusBadRequest,
fmt.Sprintf("could not parse status to bool. %s", err.Error())),
)
return
}
Expand All @@ -38,8 +37,7 @@ func (s *Server) appStatus(ctx *middleware.Context) {
ctx.PlainText(200, []byte("OK"))
return
}

response.Write(ctx, response.NewError(http.StatusServiceUnavailable, "node not ready"))
response.WriteErr(ctx.Req.Context(), ctx, response.NewError(http.StatusServiceUnavailable, "node not ready"))
}

func (s *Server) getClusterStatus(ctx *middleware.Context) {
Expand Down Expand Up @@ -77,13 +75,12 @@ func (s *Server) postClusterMembers(ctx *middleware.Context, req models.ClusterM

n, err := cluster.Manager.Join(toJoin)
if err != nil {
response.Write(ctx, response.NewError(http.StatusBadRequest, fmt.Sprintf(
"error when joining cluster members: %s", err.Error())),
)
} else {
resp.MembersAdded = n
response.Write(ctx, response.NewJson(200, resp, ""))
response.WriteErr(ctx.Req.Context(), ctx, response.NewError(http.StatusBadRequest,
fmt.Sprintf("error when joining cluster members: %s", err.Error())))
return
}
resp.MembersAdded = n
response.Write(ctx, response.NewJson(200, resp, ""))
}

// IndexFind returns a sequence of msgp encoded idx.Node's
Expand All @@ -98,7 +95,7 @@ func (s *Server) indexFind(ctx *middleware.Context, req models.IndexFind) {
for _, pattern := range req.Patterns {
nodes, err := s.MetricIndex.Find(req.OrgId, pattern, req.From)
if err != nil {
response.Write(ctx, response.NewError(http.StatusBadRequest, err.Error()))
response.WriteErr(ctx.Req.Context(), ctx, response.NewError(http.StatusBadRequest, err.Error()))
return
}
resp.Nodes[pattern] = nodes
Expand All @@ -110,7 +107,7 @@ func (s *Server) indexFind(ctx *middleware.Context, req models.IndexFind) {
func (s *Server) indexGet(ctx *middleware.Context, req models.IndexGet) {
def, ok := s.MetricIndex.Get(req.Id)
if !ok {
response.Write(ctx, response.NewError(http.StatusNotFound, "Not Found"))
response.WriteErr(ctx.Req.Context(), ctx, response.NewError(http.StatusNotFound, "Not Found"))
return
}

Expand All @@ -134,7 +131,7 @@ func (s *Server) getData(ctx *middleware.Context, request models.GetData) {
// the only errors returned are from us catching panics, so we should treat them
// all as internalServerErrors
log.Error(3, "HTTP getData() %s", err.Error())
response.Write(ctx, response.WrapError(err))
response.WriteErr(ctx.Req.Context(), ctx, response.WrapError(err))
return
}
response.Write(ctx, response.NewMsgp(200, &models.GetDataResp{Series: series}))
Expand All @@ -144,7 +141,7 @@ func (s *Server) indexDelete(ctx *middleware.Context, req models.IndexDelete) {
defs, err := s.MetricIndex.Delete(req.OrgId, req.Query)
if err != nil {
// errors can only be caused by bad request.
response.Write(ctx, response.NewError(http.StatusBadRequest, err.Error()))
response.WriteErr(ctx.Req.Context(), ctx, response.NewError(http.StatusBadRequest, err.Error()))
return
}

Expand Down
14 changes: 7 additions & 7 deletions api/graphite.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ func (s *Server) renderMetrics(ctx *middleware.Context, request models.GraphiteR
defaultTo := uint32(now.Unix())
fromUnix, toUnix, err := getFromTo(request.FromTo, now, defaultFrom, defaultTo)
if err != nil {
response.Write(ctx, response.NewError(http.StatusBadRequest, err.Error()))
response.WriteErr(ctx.Req.Context(), ctx, response.NewError(http.StatusBadRequest, err.Error()))
return
}
if fromUnix >= toUnix {
response.Write(ctx, response.NewError(http.StatusBadRequest, InvalidTimeRangeErr.Error()))
response.WriteErr(ctx.Req.Context(), ctx, response.NewError(http.StatusBadRequest, InvalidTimeRangeErr.Error()))
return
}

Expand Down Expand Up @@ -270,13 +270,13 @@ func (s *Server) metricsFind(ctx *middleware.Context, request models.GraphiteFin
var defaultFrom, defaultTo uint32
fromUnix, toUnix, err := getFromTo(request.FromTo, now, defaultFrom, defaultTo)
if err != nil {
response.Write(ctx, response.NewError(http.StatusBadRequest, err.Error()))
response.WriteErr(ctx.Req.Context(), ctx, response.NewError(http.StatusBadRequest, err.Error()))
return
}
nodes := make([]idx.Node, 0)
series, err := s.findSeries(ctx.Req.Context(), ctx.OrgId, []string{request.Query}, int64(fromUnix))
if err != nil {
response.Write(ctx, response.WrapError(err))
response.WriteErr(ctx.Req.Context(), ctx, response.WrapError(err))
return
}
seenPaths := make(map[string]struct{})
Expand Down Expand Up @@ -331,7 +331,7 @@ func (s *Server) listRemote(ctx context.Context, orgId int, peer cluster.Node) (
func (s *Server) metricsIndex(ctx *middleware.Context) {
peers, err := cluster.MembersForQuery()
if err != nil {
response.Write(ctx, response.WrapError(err))
response.WriteErr(ctx.Req.Context(), ctx, response.WrapError(err))
return
}
errors := make([]error, 0)
Expand Down Expand Up @@ -379,7 +379,7 @@ func (s *Server) metricsIndex(ctx *middleware.Context) {

if err != nil {
log.Error(3, "HTTP IndexJson() %s", err.Error())
response.Write(ctx, response.WrapError(err))
response.WriteErr(ctx.Req.Context(), ctx, response.WrapError(err))
return
}

Expand Down Expand Up @@ -510,7 +510,7 @@ func (s *Server) metricsDelete(ctx *middleware.Context, req models.MetricsDelete
wg.Wait()
var err error
if len(errors) > 0 {
response.Write(ctx, response.WrapError(err))
response.WriteErr(ctx.Req.Context(), ctx, response.WrapError(err))
}

resp := models.MetricsDeleteResp{
Expand Down
12 changes: 12 additions & 0 deletions api/response/response.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package response

import (
"context"
"errors"
"net/http"

opentracing "github.com/opentracing/opentracing-go"
tags "github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/log"
"github.com/raintank/metrictank/util"
)

Expand All @@ -27,6 +31,14 @@ func Write(w http.ResponseWriter, resp Response) {
return
}

func WriteErr(ctx context.Context, w http.ResponseWriter, resp Response) {
Write(w, resp)
span := opentracing.SpanFromContext(ctx)
body, _ := resp.Body()
span.LogFields(log.String("error.kind", string(body)))
tags.Error.Set(span, true)
}

type Response interface {
Code() int
Body() ([]byte, error)
Expand Down

0 comments on commit c88cd8c

Please sign in to comment.