From fbd85fc55b5138733d1bb1b175bbf74d6ce2957f Mon Sep 17 00:00:00 2001 From: Saheem Granados Date: Thu, 12 Jul 2018 08:48:03 -0400 Subject: [PATCH 1/5] add show plan endpoint. --- api/graphite.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ api/routes.go | 2 ++ 2 files changed, 55 insertions(+) diff --git a/api/graphite.go b/api/graphite.go index 463dd0e7ec..57fb1a1d40 100644 --- a/api/graphite.go +++ b/api/graphite.go @@ -1200,3 +1200,56 @@ 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 succesful 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 + } + + reqRenderTargetCount.Value(len(request.Targets)) + + stable := request.Process == "stable" + mdp := request.MaxDataPoints + if request.NoProxy { + // if this request is coming from graphite, we should not do runtime consolidation + // as graphite needs high-res data to perform its processing. + mdp = 0 + } + plan, err := expr.NewPlan(exprs, fromUnix, toUnix, mdp, stable, nil) + if err != nil { + response.Write(ctx, response.NewError(http.StatusBadRequest, err.Error())) + return + } + response.Write(ctx, response.NewJson(200, plan, "")) + plan.Clean() +} diff --git a/api/routes.go b/api/routes.go index 1864d22e89..6d90ba8971 100644 --- a/api/routes.go +++ b/api/routes.go @@ -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) From ad1eb52c8f643c255e509d94c6f229806b358e5d Mon Sep 17 00:00:00 2001 From: Saheem Granados Date: Thu, 12 Jul 2018 10:56:43 -0400 Subject: [PATCH 2/5] review comment. --- api/graphite.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/api/graphite.go b/api/graphite.go index 57fb1a1d40..6a56f3ec67 100644 --- a/api/graphite.go +++ b/api/graphite.go @@ -1240,11 +1240,7 @@ func (s *Server) showPlan(ctx *middleware.Context, request models.GraphiteRender stable := request.Process == "stable" mdp := request.MaxDataPoints - if request.NoProxy { - // if this request is coming from graphite, we should not do runtime consolidation - // as graphite needs high-res data to perform its processing. - mdp = 0 - } + plan, err := expr.NewPlan(exprs, fromUnix, toUnix, mdp, stable, nil) if err != nil { response.Write(ctx, response.NewError(http.StatusBadRequest, err.Error())) From 37552d3e50bc65c9cb9f7c7ea425ccf52a348a03 Mon Sep 17 00:00:00 2001 From: Saheem Granados Date: Thu, 12 Jul 2018 19:34:57 -0400 Subject: [PATCH 3/5] gofmt --- api/graphite.go | 84 ++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/api/graphite.go b/api/graphite.go index 527bccba97..fe046800d2 100644 --- a/api/graphite.go +++ b/api/graphite.go @@ -1207,46 +1207,46 @@ func (s *Server) graphiteTagDelSeries(ctx *middleware.Context, request models.Gr // 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 - } - - reqRenderTargetCount.Value(len(request.Targets)) - - 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 - } - response.Write(ctx, response.NewJson(200, plan, "")) - plan.Clean() + // 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 + } + + reqRenderTargetCount.Value(len(request.Targets)) + + 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 + } + response.Write(ctx, response.NewJson(200, plan, "")) + plan.Clean() } From 2ac1ab6ac06f1fe656df1c6e4914ed7e4ec7a0e7 Mon Sep 17 00:00:00 2001 From: Saheem Granados Date: Thu, 12 Jul 2018 19:44:03 -0400 Subject: [PATCH 4/5] spelling --- api/graphite.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/graphite.go b/api/graphite.go index fe046800d2..1624c89b02 100644 --- a/api/graphite.go +++ b/api/graphite.go @@ -1203,7 +1203,7 @@ func (s *Server) graphiteTagDelSeries(ctx *middleware.Context, request models.Gr } // showPlan attempts to create a Plan given a /render target query. -// If the Plan creation is succesful it returns 200, JSON marshaling of Plan. +// 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) { From 33bffa938ef80c17f1ab1b21471da53aa3c3f3c6 Mon Sep 17 00:00:00 2001 From: Saheem Granados Date: Tue, 24 Jul 2018 15:22:29 -0400 Subject: [PATCH 5/5] review comments. --- api/graphite.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/api/graphite.go b/api/graphite.go index 1624c89b02..5eaa8cf7b3 100644 --- a/api/graphite.go +++ b/api/graphite.go @@ -1237,8 +1237,6 @@ func (s *Server) showPlan(ctx *middleware.Context, request models.GraphiteRender return } - reqRenderTargetCount.Value(len(request.Targets)) - stable := request.Process == "stable" mdp := request.MaxDataPoints @@ -1247,6 +1245,11 @@ func (s *Server) showPlan(ctx *middleware.Context, request models.GraphiteRender response.Write(ctx, response.NewError(http.StatusBadRequest, err.Error())) return } - response.Write(ctx, response.NewJson(200, plan, "")) + 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() }