func (serv MetricsService) Query(w http.ResponseWriter, r *http.Request) { setAccessControlHeaders(w) params := http_utils.GetQueryParams(r) expr := params.Get("expr") asText := params.Get("asText") var format ast.OutputFormat // BUG(julius): Use Content-Type negotiation. if asText == "" { format = ast.JSON w.Header().Set("Content-Type", "application/json") } else { format = ast.TEXT w.Header().Set("Content-Type", "text/plain") } exprNode, err := rules.LoadExprFromString(expr) if err != nil { fmt.Fprint(w, ast.ErrorToJSON(err)) return } timestamp := clientmodel.TimestampFromTime(serv.time.Now()) queryStats := stats.NewTimerGroup() result := ast.EvalToString(exprNode, timestamp, format, serv.Storage, queryStats) glog.Infof("Instant query: %s\nQuery stats:\n%s\n", expr, queryStats) fmt.Fprint(w, result) }
func (serv MetricsService) SetTargets(w http.ResponseWriter, r *http.Request) { params := http_utils.GetQueryParams(r) jobName := params.Get("job") decoder := json.NewDecoder(r.Body) var targetGroups []TargetGroup err := decoder.Decode(&targetGroups) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } job := serv.Config.GetJobByName(jobName) if job == nil { http.Error(w, "job not found", http.StatusNotFound) return } newTargets := []retrieval.Target{} for _, targetGroup := range targetGroups { // Do mandatory map type conversion due to Go shortcomings. baseLabels := clientmodel.LabelSet{ clientmodel.JobLabel: clientmodel.LabelValue(job.GetName()), } for label, value := range targetGroup.BaseLabels { baseLabels[clientmodel.LabelName(label)] = clientmodel.LabelValue(value) } for _, endpoint := range targetGroup.Endpoints { newTarget := retrieval.NewTarget(endpoint, job.ScrapeTimeout(), baseLabels) newTargets = append(newTargets, newTarget) } } // BUG(julius): Validate that this ScrapeInterval is in fact the proper one // for the job. serv.TargetManager.ReplaceTargets(*job, newTargets) }
func (serv MetricsService) QueryRange(w http.ResponseWriter, r *http.Request) { setAccessControlHeaders(w) w.Header().Set("Content-Type", "application/json") params := http_utils.GetQueryParams(r) expr := params.Get("expr") end, _ := strconv.ParseInt(params.Get("end"), 0, 64) duration, _ := strconv.ParseInt(params.Get("range"), 0, 64) step, _ := strconv.ParseInt(params.Get("step"), 0, 64) exprNode, err := rules.LoadExprFromString(expr) if err != nil { fmt.Fprint(w, ast.ErrorToJSON(err)) return } if exprNode.Type() != ast.VECTOR { fmt.Fprint(w, ast.ErrorToJSON(errors.New("Expression does not evaluate to vector type"))) return } if end == 0 { end = clientmodel.Now().Unix() } if step < 1 { step = 1 } if end-duration < 0 { duration = end } // Align the start to step "tick" boundary. end -= end % step queryStats := stats.NewTimerGroup() evalTimer := queryStats.GetTimer(stats.TotalEvalTime).Start() matrix, err := ast.EvalVectorRange( exprNode.(ast.VectorNode), clientmodel.TimestampFromUnix(end-duration), clientmodel.TimestampFromUnix(end), time.Duration(step)*time.Second, serv.Storage, queryStats) if err != nil { fmt.Fprint(w, ast.ErrorToJSON(err)) return } evalTimer.Stop() sortTimer := queryStats.GetTimer(stats.ResultSortTime).Start() sort.Sort(matrix) sortTimer.Stop() jsonTimer := queryStats.GetTimer(stats.JsonEncodeTime).Start() result := ast.TypedValueToJSON(matrix, "matrix") jsonTimer.Stop() glog.Infof("Range query: %s\nQuery stats:\n%s\n", expr, queryStats) fmt.Fprint(w, result) }