Skip to content

Commit

Permalink
Add Authorization header
Browse files Browse the repository at this point in the history
This closes #7
  • Loading branch information
discordianfish committed Jan 6, 2018
1 parent 76011fd commit e8e22b3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
16 changes: 13 additions & 3 deletions sensu-prometheus-collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,17 @@ func QueryPrometheus(promURL string, queryString string) (model.Vector, error) {
return nil, errors.New("unexpected response type")
}

func QueryExporter(exporterURL string) (model.Vector, error) {
expResponse, err := http.Get(exporterURL)
func QueryExporter(exporterURL, authorization string) (model.Vector, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", exporterURL, nil)
if err != nil {
return nil, err
}
if authorization != "" {
req.Header.Set("Authorization", authorization)
}

expResponse, err := client.Do(req)
defer expResponse.Body.Close()

if err != nil {
Expand Down Expand Up @@ -174,6 +183,7 @@ func QueryExporter(exporterURL string) (model.Vector, error) {

func main() {
exporterURL := flag.String("exporter-url", "", "Prometheus exporter URL to pull metrics from.")
exporterAuthorizationHeader := flag.String("exporter-authorization", "", "Prometheus exporter Authorization header.")
promURL := flag.String("prom-url", "http://localhost:9090", "Prometheus API URL.")
queryString := flag.String("prom-query", "up", "Prometheus API query string.")
outputFormat := flag.String("output-format", "influx", "The check output format to use for metrics {influx|graphite|json}.")
Expand All @@ -184,7 +194,7 @@ func main() {
var err error

if *exporterURL != "" {
samples, err = QueryExporter(*exporterURL)
samples, err = QueryExporter(*exporterURL, *exporterAuthorizationHeader)
} else {
samples, err = QueryPrometheus(*promURL, *queryString)
}
Expand Down
2 changes: 1 addition & 1 deletion sensu-prometheus-collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestQueryExporter(t *testing.T) {

time.Sleep(2 * time.Second)

samples, err := QueryExporter("http://localhost:7777/metrics")
samples, err := QueryExporter("http://localhost:7777/metrics", "")

assert.NoError(t, err)
assert.NotNil(t, samples)
Expand Down

0 comments on commit e8e22b3

Please sign in to comment.