From fa1935a64476a8d1fb419b39b603453cad82ab6a Mon Sep 17 00:00:00 2001 From: beorn7 Date: Wed, 18 Mar 2015 18:53:43 +0100 Subject: [PATCH] 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. --- retrieval/target.go | 19 +++++++-- retrieval/target_test.go | 16 ++++++++ retrieval/targetmanager_test.go | 4 ++ web/api/api.go | 3 -- web/api/targets.go | 72 --------------------------------- web/status.go | 1 + web/templates/status.html | 2 +- 7 files changed, 38 insertions(+), 79 deletions(-) delete mode 100644 web/api/targets.go diff --git a/retrieval/target.go b/retrieval/target.go index 3fc758a004..6cdfedf07f 100644 --- a/retrieval/target.go +++ b/retrieval/target.go @@ -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() { diff --git a/retrieval/target_test.go b/retrieval/target_test.go index 5b96b4cc30..427f630367 100644 --- a/retrieval/target_test.go +++ b/retrieval/target_test.go @@ -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"} diff --git a/retrieval/targetmanager_test.go b/retrieval/targetmanager_test.go index 683386f8ca..981fa3a3b5 100644 --- a/retrieval/targetmanager_test.go +++ b/retrieval/targetmanager_test.go @@ -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 } diff --git a/web/api/api.go b/web/api/api.go index 4599edf07e..90bb06627b 100644 --- a/web/api/api.go +++ b/web/api/api.go @@ -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), - )) } diff --git a/web/api/targets.go b/web/api/targets.go deleted file mode 100644 index d864fe4056..0000000000 --- a/web/api/targets.go +++ /dev/null @@ -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) -} diff --git a/web/status.go b/web/status.go index a0175fa890..af82d52ec7 100644 --- a/web/status.go +++ b/web/status.go @@ -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", diff --git a/web/templates/status.html b/web/templates/status.html index cf3586d7d8..91332c6016 100644 --- a/web/templates/status.html +++ b/web/templates/status.html @@ -56,7 +56,7 @@ - {{.BaseLabels}} + {{.BaseLabelsWithoutJobAndInstance}} {{if .LastScrape.IsZero}}Never{{else}}{{since .LastScrape}} ago{{end}}