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

Add show plan endpoint #961

Merged
merged 6 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions api/graphite.go
Original file line number Diff line number Diff line change
Expand Up @@ -1201,3 +1201,55 @@ func (s *Server) graphiteTagDelSeries(ctx *middleware.Context, request models.Gr

response.Write(ctx, response.NewJson(200, res, ""))
}

// showPlan attempts to create a Plan given a /render target query.
// If the Plan creation is successful it returns 200, JSON marshaling of Plan.
// Otherwise, it returns 400, error details.
// This is needed to determine if a query cannot be resolved in localOnly mode.
func (s *Server) showPlan(ctx *middleware.Context, request models.GraphiteRender) {
// note: the model is already validated to assure at least one of them has len >0
if len(request.Targets) == 0 {
request.Targets = request.TargetsRails
}

now := time.Now()
defaultFrom := uint32(now.Add(-time.Duration(24) * time.Hour).Unix())
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()))
return
}
if fromUnix >= toUnix {
response.Write(ctx, response.NewError(http.StatusBadRequest, InvalidTimeRangeErr.Error()))
return
}

// render API is modeled after graphite, so from exclusive, to inclusive.
// in MT, from is inclusive, to is exclusive (which is akin to slice syntax)
// so we must adjust
fromUnix += 1
toUnix += 1

exprs, err := expr.ParseMany(request.Targets)
if err != nil {
response.Write(ctx, response.NewError(http.StatusBadRequest, err.Error()))
return
}

stable := request.Process == "stable"
mdp := request.MaxDataPoints

plan, err := expr.NewPlan(exprs, fromUnix, toUnix, mdp, stable, nil)
if err != nil {
response.Write(ctx, response.NewError(http.StatusBadRequest, err.Error()))
return
}
switch request.Format {
case "json":
response.Write(ctx, response.NewJson(200, plan, ""))
default:
response.Write(ctx, response.NewError(http.StatusBadRequest, "Unsupported response format requested: "+request.Format))
}
plan.Clean()
}
2 changes: 2 additions & 0 deletions api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func (s *Server) RegisterRoutes() {
ctx.Write(nil)
})

r.Combo("/showplan", cBody, withOrg, ready, bind(models.GraphiteRender{})).Get(s.showPlan).Post(s.showPlan)

// Graphite endpoints
r.Combo("/render", cBody, withOrg, ready, bind(models.GraphiteRender{})).Get(s.renderMetrics).Post(s.renderMetrics)
r.Combo("/metrics/find", withOrg, ready, bind(models.GraphiteFind{})).Get(s.metricsFind).Post(s.metricsFind)
Expand Down