mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-25 13:44:05 -08:00
Remove /api/targets call and do not show job and instance labels on status.
/api/targets was undocumented and never used and also broken. Showing instance and job labels on the status page (next to targets) does not make sense as those labels are set in an obvious way. Also add a doc comment to TargetStateToClass.
This commit is contained in:
parent
be11cb2b07
commit
fa1935a644
|
@ -126,6 +126,9 @@ type Target interface {
|
|||
GlobalURL() string
|
||||
// Return the target's base labels.
|
||||
BaseLabels() clientmodel.LabelSet
|
||||
// Return the target's base labels without job and instance label. That's
|
||||
// useful for display purposes.
|
||||
BaseLabelsWithoutJobAndInstance() clientmodel.LabelSet
|
||||
// SetBaseLabelsFrom queues a replacement of the current base labels by
|
||||
// the labels of the given target. The method returns immediately after
|
||||
// queuing. The actual replacement of the base labels happens
|
||||
|
@ -183,11 +186,10 @@ func NewTarget(url string, deadline time.Duration, baseLabels clientmodel.LabelS
|
|||
scraperStopped: make(chan struct{}),
|
||||
newBaseLabels: make(chan clientmodel.LabelSet, 1),
|
||||
}
|
||||
labels := clientmodel.LabelSet{InstanceLabel: clientmodel.LabelValue(t.InstanceIdentifier())}
|
||||
t.baseLabels = clientmodel.LabelSet{InstanceLabel: clientmodel.LabelValue(t.InstanceIdentifier())}
|
||||
for baseLabel, baseValue := range baseLabels {
|
||||
labels[baseLabel] = baseValue
|
||||
t.baseLabels[baseLabel] = baseValue
|
||||
}
|
||||
t.baseLabels = labels
|
||||
return t
|
||||
}
|
||||
|
||||
|
@ -409,6 +411,17 @@ func (t *target) BaseLabels() clientmodel.LabelSet {
|
|||
return t.baseLabels
|
||||
}
|
||||
|
||||
// BaseLabelsWithoutJobAndInstance implements Target.
|
||||
func (t *target) BaseLabelsWithoutJobAndInstance() clientmodel.LabelSet {
|
||||
ls := clientmodel.LabelSet{}
|
||||
for ln, lv := range t.BaseLabels() {
|
||||
if ln != clientmodel.JobLabel && ln != InstanceLabel {
|
||||
ls[ln] = lv
|
||||
}
|
||||
}
|
||||
return ls
|
||||
}
|
||||
|
||||
// SetBaseLabelsFrom implements Target.
|
||||
func (t *target) SetBaseLabelsFrom(newTarget Target) {
|
||||
if t.URL() != newTarget.URL() {
|
||||
|
|
|
@ -18,6 +18,7 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -26,6 +27,21 @@ import (
|
|||
"github.com/prometheus/prometheus/utility"
|
||||
)
|
||||
|
||||
func TestBaseLabels(t *testing.T) {
|
||||
target := NewTarget("http://example.com/metrics", 0, clientmodel.LabelSet{"job": "some_job", "foo": "bar"})
|
||||
want := clientmodel.LabelSet{"job": "some_job", "foo": "bar", "instance": "example.com:80"}
|
||||
got := target.BaseLabels()
|
||||
if !reflect.DeepEqual(want, got) {
|
||||
t.Errorf("want base labels %v, got %v", want, got)
|
||||
}
|
||||
delete(want, "job")
|
||||
delete(want, "instance")
|
||||
got = target.BaseLabelsWithoutJobAndInstance()
|
||||
if !reflect.DeepEqual(want, got) {
|
||||
t.Errorf("want base labels %v, got %v", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTargetHidesURLAuth(t *testing.T) {
|
||||
testVectors := []string{"http://secret:data@host.com/query?args#fragment", "https://example.net/foo", "http://foo.com:31337/bar"}
|
||||
testResults := []string{"host.com:80", "example.net:443", "foo.com:31337"}
|
||||
|
|
|
@ -53,6 +53,10 @@ func (t fakeTarget) BaseLabels() clientmodel.LabelSet {
|
|||
return clientmodel.LabelSet{}
|
||||
}
|
||||
|
||||
func (t fakeTarget) BaseLabelsWithoutJobAndInstance() clientmodel.LabelSet {
|
||||
return clientmodel.LabelSet{}
|
||||
}
|
||||
|
||||
func (t fakeTarget) Interval() time.Duration {
|
||||
return t.interval
|
||||
}
|
||||
|
|
|
@ -49,7 +49,4 @@ func (msrv *MetricsService) RegisterHandler() {
|
|||
http.Handle("/api/metrics", prometheus.InstrumentHandler(
|
||||
"/api/metrics", handler(msrv.Metrics),
|
||||
))
|
||||
http.Handle("/api/targets", prometheus.InstrumentHandler(
|
||||
"/api/targets", handler(msrv.SetTargets),
|
||||
))
|
||||
}
|
||||
|
|
|
@ -1,72 +0,0 @@
|
|||
// Copyright 2013 The Prometheus Authors
|
||||
// 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 (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
clientmodel "github.com/prometheus/client_golang/model"
|
||||
|
||||
"github.com/prometheus/prometheus/retrieval"
|
||||
"github.com/prometheus/prometheus/web/httputils"
|
||||
)
|
||||
|
||||
// TargetGroup bundles endpoints and base labels with appropriate JSON
|
||||
// annotations.
|
||||
type TargetGroup struct {
|
||||
Endpoints []string `json:"endpoints"`
|
||||
BaseLabels map[string]string `json:"baseLabels"`
|
||||
}
|
||||
|
||||
// SetTargets handles the /api/targets endpoint.
|
||||
func (serv MetricsService) SetTargets(w http.ResponseWriter, r *http.Request) {
|
||||
params := httputils.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)
|
||||
}
|
|
@ -35,6 +35,7 @@ type PrometheusStatusHandler struct {
|
|||
Birth time.Time
|
||||
}
|
||||
|
||||
// TargetStateToClass returns a map of TargetState to the name of a Bootstrap CSS class.
|
||||
func (h *PrometheusStatusHandler) TargetStateToClass() map[retrieval.TargetState]string {
|
||||
return map[retrieval.TargetState]string{
|
||||
retrieval.Unknown: "warning",
|
||||
|
|
|
@ -56,7 +56,7 @@
|
|||
</span>
|
||||
</td>
|
||||
<td>
|
||||
{{.BaseLabels}}
|
||||
{{.BaseLabelsWithoutJobAndInstance}}
|
||||
</td>
|
||||
<td>
|
||||
{{if .LastScrape.IsZero}}Never{{else}}{{since .LastScrape}} ago{{end}}
|
||||
|
|
Loading…
Reference in a new issue