Skip to content

Commit

Permalink
prometheus exporter http endpoint support
Browse files Browse the repository at this point in the history
  • Loading branch information
portertech committed Oct 2, 2017
1 parent 8d67f82 commit 55287c5
Show file tree
Hide file tree
Showing 98 changed files with 27,170 additions and 9 deletions.
63 changes: 54 additions & 9 deletions sensu-prometheus-collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import (
"errors"
"flag"
"fmt"
"net/http"
"os"
"time"

"github.com/prometheus/client_golang/api/prometheus"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/common/model"
)

type Tag struct {
Name string
Value string
Name model.LabelName
Value model.LabelValue
}

type Metric struct {
Expand All @@ -31,8 +33,8 @@ func CreateJSONMetrics(samples model.Vector) string {

for name, value := range sample.Metric {
tag := Tag{
Name: string(name),
Value: string(value),
Name: name,
Value: value,
}

metric.Tags = append(metric.Tags, tag)
Expand Down Expand Up @@ -117,7 +119,6 @@ func QueryPrometheus(promURL string, queryString string) (model.Vector, error) {
promClient, err := prometheus.New(promConfig)

if err != nil {
fmt.Errorf("%v", err)
return nil, err
}

Expand All @@ -126,7 +127,6 @@ func QueryPrometheus(promURL string, queryString string) (model.Vector, error) {
promResponse, err := promQueryClient.Query(ctx, queryString, time.Now())

if err != nil {
fmt.Errorf("%v", err)
return nil, err
}

Expand All @@ -137,13 +137,58 @@ 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)
defer expResponse.Body.Close()

if err != nil {
return nil, err
}

if expResponse.StatusCode != http.StatusOK {
return nil, errors.New("non OK HTTP response status")
}

var parser expfmt.TextParser

metricFamilies, err := parser.TextToMetricFamilies(expResponse.Body)

if err != nil {
return nil, err
}

samples := model.Vector{}

decodeOptions := &expfmt.DecodeOptions{
Timestamp: model.Time(time.Now().Unix()),
}

for _, family := range metricFamilies {
familySamples, _ := expfmt.ExtractSamples(decodeOptions, family)

for _, sample := range familySamples {
samples = append(samples, sample)
}
}

return samples, nil
}

func main() {
promURL := flag.String("url", "http://localhost:9090", "Prometheus API URL")
queryString := flag.String("query", "up", "Prometheus API query string")
exporterURL := flag.String("exporter-url", "", "Prometheus exporter URL to pull metrics from")
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}")
flag.Parse()

samples, err := QueryPrometheus(*promURL, *queryString)
samples := model.Vector{}
var err error

if *exporterURL != "" {
samples, err = QueryExporter(*exporterURL)
} else {
samples, err = QueryPrometheus(*promURL, *queryString)
}

if err != nil {
fmt.Errorf("%v", err)
Expand Down
24 changes: 24 additions & 0 deletions sensu-prometheus-collector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"net/http"
"testing"
"time"

"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/stretchr/testify/assert"
)

func TestQueryExporter(t *testing.T) {
go func() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":7777", nil)
}()

time.Sleep(2 * time.Second)

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

assert.NoError(t, err)
assert.NotNil(t, samples)
}
20 changes: 20 additions & 0 deletions vendor/github.com/beorn7/perks/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 55287c5

Please sign in to comment.