From daa9993dede216c2276e46513de9f45abd7a3c73 Mon Sep 17 00:00:00 2001 From: Karsten Weiss Date: Fri, 13 Apr 2018 22:11:50 +0200 Subject: [PATCH 01/11] Use direct API calls Fixes: prometheus/http_test.go:117:5:warning: should use resp.Body.String() instead of string(resp.Body.Bytes()) (S1030) (megacheck) prometheus/http_test.go:118:56:warning: should use resp.Body.String() instead of string(resp.Body.Bytes()) (S1030) (megacheck) Signed-off-by: Karsten Weiss --- prometheus/http_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prometheus/http_test.go b/prometheus/http_test.go index 0c7fa23..5c0ae30 100644 --- a/prometheus/http_test.go +++ b/prometheus/http_test.go @@ -114,8 +114,8 @@ func TestInstrumentHandler(t *testing.T) { if resp.Code != http.StatusTeapot { t.Fatalf("expected status %d, got %d", http.StatusTeapot, resp.Code) } - if string(resp.Body.Bytes()) != "Howdy there!" { - t.Fatalf("expected body %s, got %s", "Howdy there!", string(resp.Body.Bytes())) + if resp.Body.String() != "Howdy there!" { + t.Fatalf("expected body %s, got %s", "Howdy there!", resp.Body.String()) } out := &dto.Metric{} From 66b5a26cec40dd699091293fa638267abcec78ba Mon Sep 17 00:00:00 2001 From: Karsten Weiss Date: Fri, 13 Apr 2018 22:15:28 +0200 Subject: [PATCH 02/11] Remove unnecessary for loop in makeLabelPairs() Fixes: value.go:155:2: should replace loop with labelPairs = append(labelPairs, desc.constLabelPairs...) (S1011) Signed-off-by: Karsten Weiss --- prometheus/value.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/prometheus/value.go b/prometheus/value.go index 543b57c..9fb7eab 100644 --- a/prometheus/value.go +++ b/prometheus/value.go @@ -152,9 +152,7 @@ func makeLabelPairs(desc *Desc, labelValues []string) []*dto.LabelPair { Value: proto.String(labelValues[i]), }) } - for _, lp := range desc.constLabelPairs { - labelPairs = append(labelPairs, lp) - } + labelPairs = append(labelPairs, desc.constLabelPairs...) sort.Sort(LabelPairSorter(labelPairs)) return labelPairs } From e01f7ec4fb4fc473c628424e7de8c84dd480df98 Mon Sep 17 00:00:00 2001 From: Karsten Weiss Date: Fri, 13 Apr 2018 22:59:56 +0200 Subject: [PATCH 03/11] Remove unnecessary conversion Fixes: go_collector.go:268:66:warning: unnecessary conversion (unconvert) Signed-off-by: Karsten Weiss --- prometheus/go_collector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prometheus/go_collector.go b/prometheus/go_collector.go index 096454a..122b169 100644 --- a/prometheus/go_collector.go +++ b/prometheus/go_collector.go @@ -265,7 +265,7 @@ func (c *goCollector) Collect(ch chan<- Metric) { quantiles[float64(idx+1)/float64(len(stats.PauseQuantiles)-1)] = pq.Seconds() } quantiles[0.0] = stats.PauseQuantiles[0].Seconds() - ch <- MustNewConstSummary(c.gcDesc, uint64(stats.NumGC), float64(stats.PauseTotal.Seconds()), quantiles) + ch <- MustNewConstSummary(c.gcDesc, uint64(stats.NumGC), stats.PauseTotal.Seconds(), quantiles) ch <- MustNewConstMetric(c.goInfoDesc, GaugeValue, 1) From 587d5265f8b17553255e1b0b72bd81e783c1cf83 Mon Sep 17 00:00:00 2001 From: Karsten Weiss Date: Fri, 13 Apr 2018 22:18:53 +0200 Subject: [PATCH 04/11] metric.go: Remove unused type hashSorter Fixes: metric.go:130:6: type hashSorter is unused (U1000) Signed-off-by: Karsten Weiss --- prometheus/metric.go | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/prometheus/metric.go b/prometheus/metric.go index 6213ee8..76035bc 100644 --- a/prometheus/metric.go +++ b/prometheus/metric.go @@ -127,20 +127,6 @@ func (s LabelPairSorter) Less(i, j int) bool { return s[i].GetName() < s[j].GetName() } -type hashSorter []uint64 - -func (s hashSorter) Len() int { - return len(s) -} - -func (s hashSorter) Swap(i, j int) { - s[i], s[j] = s[j], s[i] -} - -func (s hashSorter) Less(i, j int) bool { - return s[i] < s[j] -} - type invalidMetric struct { desc *Desc err error From 245fde70cb036507be7ffed576c77baeea20d6b3 Mon Sep 17 00:00:00 2001 From: Karsten Weiss Date: Fri, 13 Apr 2018 22:28:16 +0200 Subject: [PATCH 05/11] Simplify if expr in ExampleNewExpvarCollector() Signed-off-by: Karsten Weiss --- prometheus/expvar_collector_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prometheus/expvar_collector_test.go b/prometheus/expvar_collector_test.go index 910dac3..6bcd9b6 100644 --- a/prometheus/expvar_collector_test.go +++ b/prometheus/expvar_collector_test.go @@ -78,7 +78,7 @@ func ExampleNewExpvarCollector() { close(metricChan) }() for m := range metricChan { - if strings.Index(m.Desc().String(), "expvar_memstats") == -1 { + if !strings.Contains(m.Desc().String(), "expvar_memstats") { metric.Reset() m.Write(&metric) metricStrings = append(metricStrings, metric.String()) From 958ea82988bac53670e9b9bc53f483fddc2eb609 Mon Sep 17 00:00:00 2001 From: Karsten Weiss Date: Fri, 13 Apr 2018 22:07:27 +0200 Subject: [PATCH 06/11] Fix typos Signed-off-by: Karsten Weiss --- api/client_test.go | 2 +- prometheus/promhttp/http_test.go | 2 +- prometheus/push/push.go | 2 +- prometheus/registry.go | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/api/client_test.go b/api/client_test.go index 53226d7..b1bcfc9 100644 --- a/api/client_test.go +++ b/api/client_test.go @@ -89,7 +89,7 @@ func TestClientURL(t *testing.T) { address: "http://localhost:9090", endpoint: "/test/:param", args: map[string]string{ - "nonexistant": "content", + "nonexistent": "content", }, expected: "http://localhost:9090/test/:param", }, diff --git a/prometheus/promhttp/http_test.go b/prometheus/promhttp/http_test.go index aeaa0b4..24d2f8c 100644 --- a/prometheus/promhttp/http_test.go +++ b/prometheus/promhttp/http_test.go @@ -121,7 +121,7 @@ the_count 0 t.Errorf("got HTTP status code %d, want %d", got, want) } if got := logBuf.String(); got != wantMsg { - t.Errorf("got log message:\n%s\nwant log mesage:\n%s\n", got, wantMsg) + t.Errorf("got log message:\n%s\nwant log message:\n%s\n", got, wantMsg) } if got := writer.Body.String(); got != wantErrorBody { t.Errorf("got body:\n%s\nwant body:\n%s\n", got, wantErrorBody) diff --git a/prometheus/push/push.go b/prometheus/push/push.go index 02be52c..3721ff1 100644 --- a/prometheus/push/push.go +++ b/prometheus/push/push.go @@ -66,7 +66,7 @@ type Pusher struct { username, password string } -// New creates a new Pusher to push to the provided URL withe the provided job +// New creates a new Pusher to push to the provided URL with the provided job // name. You can use just host:port or ip:port as url, in which case “http://” // is added automatically. Alternatively, include the schema in the // URL. However, do not include the “/metrics/jobs/…” part. diff --git a/prometheus/registry.go b/prometheus/registry.go index bee3703..f8efd1f 100644 --- a/prometheus/registry.go +++ b/prometheus/registry.go @@ -437,7 +437,7 @@ collectLoop: )) default: if goroutineBudget <= 0 || len(collectors) == 0 { - // All collectors are aleady being worked on or + // All collectors are already being worked on or // we have already as many goroutines started as // there are collectors. Just process metrics // from now on. From ea0d27e8cf123ce70732c21a581039bbe07530fd Mon Sep 17 00:00:00 2001 From: Karsten Weiss Date: Fri, 13 Apr 2018 22:32:41 +0200 Subject: [PATCH 07/11] Type responseWriterDelegator: Remove unused fields Fixes: http.go:355:2: field handler is unused (U1000) http.go:355:11: field method is unused (U1000) Signed-off-by: Karsten Weiss --- prometheus/http.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/prometheus/http.go b/prometheus/http.go index dd0f819..74dd349 100644 --- a/prometheus/http.go +++ b/prometheus/http.go @@ -352,10 +352,9 @@ func computeApproximateRequestSize(r *http.Request) <-chan int { type responseWriterDelegator struct { http.ResponseWriter - handler, method string - status int - written int64 - wroteHeader bool + status int + written int64 + wroteHeader bool } func (r *responseWriterDelegator) WriteHeader(code int) { From 7a495a15b3806ee2cc6674b360a2b6c8d70fbed2 Mon Sep 17 00:00:00 2001 From: Karsten Weiss Date: Fri, 13 Apr 2018 22:44:44 +0200 Subject: [PATCH 08/11] Type processCollector: Remove unused field 'pid' Fixes: process_collector.go:19:2: field pid is unused (U1000) Signed-off-by: Karsten Weiss --- prometheus/process_collector.go | 1 - 1 file changed, 1 deletion(-) diff --git a/prometheus/process_collector.go b/prometheus/process_collector.go index 32ac74a..b80adc6 100644 --- a/prometheus/process_collector.go +++ b/prometheus/process_collector.go @@ -16,7 +16,6 @@ package prometheus import "github.com/prometheus/procfs" type processCollector struct { - pid int collectFn func(chan<- Metric) pidFn func() (int, error) cpuTotal *Desc From 0a453dce849be8cd570e7dad1596bade2a3e7f90 Mon Sep 17 00:00:00 2001 From: Karsten Weiss Date: Fri, 13 Apr 2018 22:38:21 +0200 Subject: [PATCH 09/11] Build process_collector_test.go only on Linux The test case requires the /proc filesystem. The change prevents this skip message during "go test -v" on platforms other than Linux: === RUN TestProcessCollector --- SKIP: TestProcessCollector (0.00s) process_collector_test.go:15: skipping TestProcessCollector, procfs not available: could not read /proc: stat /proc: no such file or directory Signed-off-by: Karsten Weiss --- prometheus/process_collector_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/prometheus/process_collector_test.go b/prometheus/process_collector_test.go index c7acb47..d59ad77 100644 --- a/prometheus/process_collector_test.go +++ b/prometheus/process_collector_test.go @@ -1,3 +1,5 @@ +// +build linux + package prometheus import ( From f3a13af35cd349ab6b15935b4237b4a202951f50 Mon Sep 17 00:00:00 2001 From: Karsten Weiss Date: Fri, 13 Apr 2018 22:49:34 +0200 Subject: [PATCH 10/11] http.go: Move helper function nowSeries() to test case Fixes: http.go:142:1:warning: nowSeries is unused (deadcode) Signed-off-by: Karsten Weiss --- prometheus/http.go | 10 ---------- prometheus/http_test.go | 10 ++++++++++ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/prometheus/http.go b/prometheus/http.go index 74dd349..3d47a81 100644 --- a/prometheus/http.go +++ b/prometheus/http.go @@ -139,16 +139,6 @@ var now nower = nowFunc(func() time.Time { return time.Now() }) -func nowSeries(t ...time.Time) nower { - return nowFunc(func() time.Time { - defer func() { - t = t[1:] - }() - - return t[0] - }) -} - // InstrumentHandler wraps the given HTTP handler for instrumentation. It // registers four metric collectors (if not already done) and reports HTTP // metrics to the (newly or already) registered collectors: http_requests_total diff --git a/prometheus/http_test.go b/prometheus/http_test.go index 5c0ae30..edd47ad 100644 --- a/prometheus/http_test.go +++ b/prometheus/http_test.go @@ -29,6 +29,16 @@ func (b respBody) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Write([]byte(b)) } +func nowSeries(t ...time.Time) nower { + return nowFunc(func() time.Time { + defer func() { + t = t[1:] + }() + + return t[0] + }) +} + func TestInstrumentHandler(t *testing.T) { defer func(n nower) { now = n.(nower) From d7590aab3c040059dac7628c7037ffd9db368c01 Mon Sep 17 00:00:00 2001 From: Karsten Weiss Date: Fri, 13 Apr 2018 22:58:07 +0200 Subject: [PATCH 11/11] Fix three shadow variable warnings (govet -shadow) Fixes: http.go:118: declaration of "part" shadows declaration at http.go:117 http_test.go:50: declaration of "respBody" shadows declaration at http_test.go:25 promhttp/http.go:305: declaration of "part" shadows declaration at promhttp/http.go:304 Signed-off-by: Karsten Weiss --- prometheus/http.go | 2 +- prometheus/http_test.go | 4 ++-- prometheus/promhttp/http.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/prometheus/http.go b/prometheus/http.go index 3d47a81..4d08154 100644 --- a/prometheus/http.go +++ b/prometheus/http.go @@ -115,7 +115,7 @@ func decorateWriter(request *http.Request, writer io.Writer) (io.Writer, string) header := request.Header.Get(acceptEncodingHeader) parts := strings.Split(header, ",") for _, part := range parts { - part := strings.TrimSpace(part) + part = strings.TrimSpace(part) if part == "gzip" || strings.HasPrefix(part, "gzip;") { return gzip.NewWriter(writer), "gzip" } diff --git a/prometheus/http_test.go b/prometheus/http_test.go index edd47ad..dd89ccf 100644 --- a/prometheus/http_test.go +++ b/prometheus/http_test.go @@ -47,9 +47,9 @@ func TestInstrumentHandler(t *testing.T) { instant := time.Now() end := instant.Add(30 * time.Second) now = nowSeries(instant, end) - respBody := respBody("Howdy there!") + body := respBody("Howdy there!") - hndlr := InstrumentHandler("test-handler", respBody) + hndlr := InstrumentHandler("test-handler", body) opts := SummaryOpts{ Subsystem: "http", diff --git a/prometheus/promhttp/http.go b/prometheus/promhttp/http.go index 8dc2603..0135737 100644 --- a/prometheus/promhttp/http.go +++ b/prometheus/promhttp/http.go @@ -302,7 +302,7 @@ func decorateWriter(request *http.Request, writer io.Writer, compressionDisabled header := request.Header.Get(acceptEncodingHeader) parts := strings.Split(header, ",") for _, part := range parts { - part := strings.TrimSpace(part) + part = strings.TrimSpace(part) if part == "gzip" || strings.HasPrefix(part, "gzip;") { return gzip.NewWriter(writer), "gzip" }