From e3e66d0bb9be9559b06e6c30ce90bedd6ca3d6c6 Mon Sep 17 00:00:00 2001 From: Bob Shannon Date: Wed, 11 Apr 2018 07:38:10 -0700 Subject: [PATCH] PR comments Signed-off-by: Bob Shannon --- .travis.yml | 5 ++- api/prometheus/v1/api.go | 78 ++++++++--------------------------- api/prometheus/v1/api_test.go | 32 ++++++-------- 3 files changed, 32 insertions(+), 83 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1d18e98..3ff54ea 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,8 +2,9 @@ sudo: false language: go go: - - 1.10.x - - 1.x +- 1.7.x +- 1.8.x +- 1.9.x script: - go test -short ./... diff --git a/api/prometheus/v1/api.go b/api/prometheus/v1/api.go index 5261dc6..3d37437 100644 --- a/api/prometheus/v1/api.go +++ b/api/prometheus/v1/api.go @@ -22,13 +22,11 @@ import ( "encoding/json" "fmt" "net/http" - "net/url" "strconv" "time" "github.com/prometheus/client_golang/api" "github.com/prometheus/common/model" - "github.com/prometheus/prometheus/scrape" ) const ( @@ -52,13 +50,21 @@ const ( // ErrorType models the different API error types. type ErrorType string -// Possible values for ErrorType. +// HealthStatus models the health status of a scrape target. +type HealthStatus string + const ( + // Possible values for ErrorType. ErrBadData ErrorType = "bad_data" ErrTimeout = "timeout" ErrCanceled = "canceled" ErrExec = "execution" ErrBadResponse = "bad_response" + + // Possible values for HealthStatus. + HealthGood HealthStatus = "up" + HealthUnknown HealthStatus = "unknown" + HealthBad HealthStatus = "down" ) // Error is an error returned by the API. @@ -114,7 +120,7 @@ type AlertManagersResult struct { // AlertManager models a configured Alert Manager. type AlertManager struct { - URL *url.URL `json:"url"` + URL string `json:"url"` } // ConfigResult contains the result from querying the config endpoint. @@ -123,9 +129,7 @@ type ConfigResult struct { } // FlagsResult contains the result from querying the flag endpoint. -type FlagsResult struct { - Flags Flags -} +type FlagsResult map[string]string // Flags models a set of flags that Prometheus is configured with. type Flags map[string]string @@ -143,12 +147,12 @@ type TargetsResult struct { // ActiveTarget models an active Prometheus scrape target. type ActiveTarget struct { - DiscoveredLabels model.LabelSet `json:"discoveredLabels"` - Labels model.LabelSet `json:"labels"` - ScrapeURL *url.URL `json:"scrapeUrl"` - LastError string `json:"lastError"` - LastScrape time.Time `json:"lastScrape"` - Health scrape.TargetHealth `json:"health"` + DiscoveredLabels model.LabelSet `json:"discoveredLabels"` + Labels model.LabelSet `json:"labels"` + ScrapeURL string `json:"scrapeUrl"` + LastError string `json:"lastError"` + LastScrape time.Time `json:"lastScrape"` + Health HealthStatus `json:"health"` } // DroppedTarget models a dropped Prometheus scrape target. @@ -165,54 +169,6 @@ type queryResult struct { v model.Value } -func (a *ActiveTarget) UnmarshalJSON(b []byte) error { - v := struct { - DiscoveredLabels model.LabelSet `json:"discoveredLabels"` - Labels model.LabelSet `json:"labels"` - ScrapeURL string `json:"scrapeUrl"` - LastError string `json:"lastError"` - LastScrape time.Time `json:"lastScrape"` - Health scrape.TargetHealth `json:"health"` - }{} - - err := json.Unmarshal(b, &v) - if err != nil { - return err - } - - url, err := url.Parse(v.ScrapeURL) - - a.DiscoveredLabels = v.DiscoveredLabels - a.Labels = v.Labels - a.ScrapeURL = url - a.LastScrape = v.LastScrape - a.Health = v.Health - a.LastError = v.LastError - - return err -} - -func (a *AlertManager) UnmarshalJSON(b []byte) error { - var v map[string]string - - err := json.Unmarshal(b, &v) - if err != nil { - return err - } - - url, err := url.Parse(v["url"]) - a.URL = url - return err -} - -func (f *FlagsResult) UnmarshalJSON(b []byte) error { - var v map[string]string - - err := json.Unmarshal(b, &v) - f.Flags = v - return err -} - func (qr *queryResult) UnmarshalJSON(b []byte) error { v := struct { Type model.ValueType `json:"resultType"` diff --git a/api/prometheus/v1/api_test.go b/api/prometheus/v1/api_test.go index 5e66076..2fe6044 100644 --- a/api/prometheus/v1/api_test.go +++ b/api/prometheus/v1/api_test.go @@ -27,7 +27,6 @@ import ( "time" "github.com/prometheus/common/model" - "github.com/prometheus/prometheus/scrape" ) type apiTest struct { @@ -89,11 +88,6 @@ func TestAPIs(t *testing.T) { testTime := time.Now() - testURL, err := url.Parse("http://127.0.0.1:9090/api/v1/alerts") - if err != nil { - t.Errorf("Failed to initialize test URL.") - } - client := &apiTestClient{T: t} promAPI := &httpAPI{ @@ -372,13 +366,11 @@ func TestAPIs(t *testing.T) { "query.max-concurrency": "20", }, res: FlagsResult{ - Flags{ - "alertmanager.notification-queue-capacity": "10000", - "alertmanager.timeout": "10s", - "log.level": "info", - "query.lookback-delta": "5m", - "query.max-concurrency": "20", - }, + "alertmanager.notification-queue-capacity": "10000", + "alertmanager.timeout": "10s", + "log.level": "info", + "query.lookback-delta": "5m", + "query.max-concurrency": "20", }, }, @@ -397,24 +389,24 @@ func TestAPIs(t *testing.T) { inRes: map[string]interface{}{ "activeAlertManagers": []map[string]string{ { - "url": testURL.String(), + "url": "http://127.0.0.1:9091/api/v1/alerts", }, }, "droppedAlertManagers": []map[string]string{ { - "url": testURL.String(), + "url": "http://127.0.0.1:9092/api/v1/alerts", }, }, }, res: AlertManagersResult{ Active: []AlertManager{ { - URL: testURL, + URL: "http://127.0.0.1:9091/api/v1/alerts", }, }, Dropped: []AlertManager{ { - URL: testURL, + URL: "http://127.0.0.1:9092/api/v1/alerts", }, }, }, @@ -445,7 +437,7 @@ func TestAPIs(t *testing.T) { "instance": "127.0.0.1:9090", "job": "prometheus", }, - "scrapeUrl": testURL.String(), + "scrapeUrl": "http://127.0.0.1:9090", "lastError": "error while scraping target", "lastScrape": testTime.UTC().Format(time.RFC3339Nano), "health": "up", @@ -475,10 +467,10 @@ func TestAPIs(t *testing.T) { "instance": "127.0.0.1:9090", "job": "prometheus", }, - ScrapeURL: testURL, + ScrapeURL: "http://127.0.0.1:9090", LastError: "error while scraping target", LastScrape: testTime.UTC(), - Health: scrape.HealthGood, + Health: HealthGood, }, }, Dropped: []DroppedTarget{