Skip to content

Commit

Permalink
Merge pull request ClickHouse#3 from marcelcorso/master
Browse files Browse the repository at this point in the history
url.Parse scrape_uri to respect potential query params
  • Loading branch information
f1yegor committed Apr 8, 2017
2 parents 96c4dec + 37958ea commit de5c52d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
30 changes: 24 additions & 6 deletions clickhouse_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
"strings"
"sync"

"unicode"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/log"
"unicode"
)

const (
Expand Down Expand Up @@ -43,11 +44,24 @@ type Exporter struct {
}

// NewExporter returns an initialized Exporter.
func NewExporter(uri string) *Exporter {
func NewExporter(uri url.URL) *Exporter {
q := uri.Query()
metricsURI := uri
q.Set("query", "select * from system.metrics")
metricsURI.RawQuery = q.Encode()

asyncMetricsURI := uri
q.Set("query", "select * from system.asynchronous_metrics")
asyncMetricsURI.RawQuery = q.Encode()

eventsURI := uri
q.Set("query", "select * from system.events")
eventsURI.RawQuery = q.Encode()

return &Exporter{
metricsURI: uri + "?query=" + url.QueryEscape("select * from system.metrics"),
asyncMetricsURI: uri + "?query=" + url.QueryEscape("select * from system.asynchronous_metrics"),
eventsURI: uri + "?query=" + url.QueryEscape("select * from system.events"),
metricsURI: metricsURI.String(),
asyncMetricsURI: asyncMetricsURI.String(),
eventsURI: eventsURI.String(),
scrapeFailures: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "exporter_scrape_failures_total",
Expand Down Expand Up @@ -233,7 +247,11 @@ func toSnake(in string) string {
func main() {
flag.Parse()

exporter := NewExporter(*clickhouseScrapeURI)
uri, err := url.Parse(*clickhouseScrapeURI)
if err != nil {
log.Fatal(err)
}
exporter := NewExporter(*uri)
prometheus.MustRegister(exporter)

log.Printf("Starting Server: %s", *listeningAddress)
Expand Down
4 changes: 3 additions & 1 deletion clickhouse_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -39,7 +40,8 @@ func TestClickhouseStatus(t *testing.T) {
})
server := httptest.NewServer(handler)

e := NewExporter(server.URL)
url, _ := url.Parse(server.URL)
e := NewExporter(*url)
ch := make(chan prometheus.Metric)

go func() {
Expand Down

0 comments on commit de5c52d

Please sign in to comment.