PR comments

Signed-off-by: Bob Shannon <bshannon@palantir.com>
This commit is contained in:
Bob Shannon 2018-04-11 07:38:10 -07:00
parent fc258df5b5
commit e3e66d0bb9
3 changed files with 32 additions and 83 deletions

View File

@ -2,8 +2,9 @@ sudo: false
language: go language: go
go: go:
- 1.10.x - 1.7.x
- 1.x - 1.8.x
- 1.9.x
script: script:
- go test -short ./... - go test -short ./...

View File

@ -22,13 +22,11 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"net/url"
"strconv" "strconv"
"time" "time"
"github.com/prometheus/client_golang/api" "github.com/prometheus/client_golang/api"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/prometheus/prometheus/scrape"
) )
const ( const (
@ -52,13 +50,21 @@ const (
// ErrorType models the different API error types. // ErrorType models the different API error types.
type ErrorType string type ErrorType string
// Possible values for ErrorType. // HealthStatus models the health status of a scrape target.
type HealthStatus string
const ( const (
// Possible values for ErrorType.
ErrBadData ErrorType = "bad_data" ErrBadData ErrorType = "bad_data"
ErrTimeout = "timeout" ErrTimeout = "timeout"
ErrCanceled = "canceled" ErrCanceled = "canceled"
ErrExec = "execution" ErrExec = "execution"
ErrBadResponse = "bad_response" ErrBadResponse = "bad_response"
// Possible values for HealthStatus.
HealthGood HealthStatus = "up"
HealthUnknown HealthStatus = "unknown"
HealthBad HealthStatus = "down"
) )
// Error is an error returned by the API. // Error is an error returned by the API.
@ -114,7 +120,7 @@ type AlertManagersResult struct {
// AlertManager models a configured Alert Manager. // AlertManager models a configured Alert Manager.
type AlertManager struct { type AlertManager struct {
URL *url.URL `json:"url"` URL string `json:"url"`
} }
// ConfigResult contains the result from querying the config endpoint. // 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. // FlagsResult contains the result from querying the flag endpoint.
type FlagsResult struct { type FlagsResult map[string]string
Flags Flags
}
// Flags models a set of flags that Prometheus is configured with. // Flags models a set of flags that Prometheus is configured with.
type Flags map[string]string type Flags map[string]string
@ -143,12 +147,12 @@ type TargetsResult struct {
// ActiveTarget models an active Prometheus scrape target. // ActiveTarget models an active Prometheus scrape target.
type ActiveTarget struct { type ActiveTarget struct {
DiscoveredLabels model.LabelSet `json:"discoveredLabels"` DiscoveredLabels model.LabelSet `json:"discoveredLabels"`
Labels model.LabelSet `json:"labels"` Labels model.LabelSet `json:"labels"`
ScrapeURL *url.URL `json:"scrapeUrl"` ScrapeURL string `json:"scrapeUrl"`
LastError string `json:"lastError"` LastError string `json:"lastError"`
LastScrape time.Time `json:"lastScrape"` LastScrape time.Time `json:"lastScrape"`
Health scrape.TargetHealth `json:"health"` Health HealthStatus `json:"health"`
} }
// DroppedTarget models a dropped Prometheus scrape target. // DroppedTarget models a dropped Prometheus scrape target.
@ -165,54 +169,6 @@ type queryResult struct {
v model.Value 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 { func (qr *queryResult) UnmarshalJSON(b []byte) error {
v := struct { v := struct {
Type model.ValueType `json:"resultType"` Type model.ValueType `json:"resultType"`

View File

@ -27,7 +27,6 @@ import (
"time" "time"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/prometheus/prometheus/scrape"
) )
type apiTest struct { type apiTest struct {
@ -89,11 +88,6 @@ func TestAPIs(t *testing.T) {
testTime := time.Now() 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} client := &apiTestClient{T: t}
promAPI := &httpAPI{ promAPI := &httpAPI{
@ -372,13 +366,11 @@ func TestAPIs(t *testing.T) {
"query.max-concurrency": "20", "query.max-concurrency": "20",
}, },
res: FlagsResult{ res: FlagsResult{
Flags{ "alertmanager.notification-queue-capacity": "10000",
"alertmanager.notification-queue-capacity": "10000", "alertmanager.timeout": "10s",
"alertmanager.timeout": "10s", "log.level": "info",
"log.level": "info", "query.lookback-delta": "5m",
"query.lookback-delta": "5m", "query.max-concurrency": "20",
"query.max-concurrency": "20",
},
}, },
}, },
@ -397,24 +389,24 @@ func TestAPIs(t *testing.T) {
inRes: map[string]interface{}{ inRes: map[string]interface{}{
"activeAlertManagers": []map[string]string{ "activeAlertManagers": []map[string]string{
{ {
"url": testURL.String(), "url": "http://127.0.0.1:9091/api/v1/alerts",
}, },
}, },
"droppedAlertManagers": []map[string]string{ "droppedAlertManagers": []map[string]string{
{ {
"url": testURL.String(), "url": "http://127.0.0.1:9092/api/v1/alerts",
}, },
}, },
}, },
res: AlertManagersResult{ res: AlertManagersResult{
Active: []AlertManager{ Active: []AlertManager{
{ {
URL: testURL, URL: "http://127.0.0.1:9091/api/v1/alerts",
}, },
}, },
Dropped: []AlertManager{ 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", "instance": "127.0.0.1:9090",
"job": "prometheus", "job": "prometheus",
}, },
"scrapeUrl": testURL.String(), "scrapeUrl": "http://127.0.0.1:9090",
"lastError": "error while scraping target", "lastError": "error while scraping target",
"lastScrape": testTime.UTC().Format(time.RFC3339Nano), "lastScrape": testTime.UTC().Format(time.RFC3339Nano),
"health": "up", "health": "up",
@ -475,10 +467,10 @@ func TestAPIs(t *testing.T) {
"instance": "127.0.0.1:9090", "instance": "127.0.0.1:9090",
"job": "prometheus", "job": "prometheus",
}, },
ScrapeURL: testURL, ScrapeURL: "http://127.0.0.1:9090",
LastError: "error while scraping target", LastError: "error while scraping target",
LastScrape: testTime.UTC(), LastScrape: testTime.UTC(),
Health: scrape.HealthGood, Health: HealthGood,
}, },
}, },
Dropped: []DroppedTarget{ Dropped: []DroppedTarget{