2013-02-22 12:07:35 -08:00
|
|
|
// Copyright 2013 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2013-10-22 11:31:52 -07:00
|
|
|
"encoding/json"
|
2013-02-22 12:07:35 -08:00
|
|
|
"net/http"
|
2013-06-25 05:02:27 -07:00
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/retrieval"
|
2014-12-10 07:16:49 -08:00
|
|
|
"github.com/prometheus/prometheus/web/httputils"
|
2013-02-22 12:07:35 -08:00
|
|
|
)
|
|
|
|
|
2014-12-10 08:46:56 -08:00
|
|
|
// TargetGroup bundles endpoints and base labels with appropriate JSON
|
2014-12-10 07:16:49 -08:00
|
|
|
// annotations.
|
2013-02-22 12:07:35 -08:00
|
|
|
type TargetGroup struct {
|
|
|
|
Endpoints []string `json:"endpoints"`
|
|
|
|
BaseLabels map[string]string `json:"baseLabels"`
|
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// SetTargets handles the /api/targets endpoint.
|
2013-10-22 11:31:52 -07:00
|
|
|
func (serv MetricsService) SetTargets(w http.ResponseWriter, r *http.Request) {
|
2014-12-10 07:16:49 -08:00
|
|
|
params := httputils.GetQueryParams(r)
|
2013-10-22 11:31:52 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-05-05 10:32:04 -07:00
|
|
|
job := serv.Config.GetJobByName(jobName)
|
|
|
|
if job == nil {
|
2013-10-22 11:31:52 -07:00
|
|
|
http.Error(w, "job not found", http.StatusNotFound)
|
2013-05-05 10:32:04 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newTargets := []retrieval.Target{}
|
|
|
|
|
|
|
|
for _, targetGroup := range targetGroups {
|
|
|
|
// Do mandatory map type conversion due to Go shortcomings.
|
2013-06-25 05:02:27 -07:00
|
|
|
baseLabels := clientmodel.LabelSet{
|
|
|
|
clientmodel.JobLabel: clientmodel.LabelValue(job.GetName()),
|
2013-05-05 10:32:04 -07:00
|
|
|
}
|
|
|
|
for label, value := range targetGroup.BaseLabels {
|
2013-06-25 05:02:27 -07:00
|
|
|
baseLabels[clientmodel.LabelName(label)] = clientmodel.LabelValue(value)
|
2013-05-05 10:32:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, endpoint := range targetGroup.Endpoints {
|
2013-08-16 09:17:48 -07:00
|
|
|
newTarget := retrieval.NewTarget(endpoint, job.ScrapeTimeout(), baseLabels)
|
2013-05-05 10:32:04 -07:00
|
|
|
newTargets = append(newTargets, newTarget)
|
2013-02-22 12:07:35 -08:00
|
|
|
}
|
|
|
|
}
|
2013-05-05 10:32:04 -07:00
|
|
|
|
|
|
|
// BUG(julius): Validate that this ScrapeInterval is in fact the proper one
|
|
|
|
// for the job.
|
2013-06-11 08:54:58 -07:00
|
|
|
serv.TargetManager.ReplaceTargets(*job, newTargets)
|
2013-02-22 12:07:35 -08:00
|
|
|
}
|