Skip to content

Commit

Permalink
Merge pull request ClickHouse#15 from f1yegor/feature/support-credent…
Browse files Browse the repository at this point in the history
…ials

add credentials for authorization
  • Loading branch information
f1yegor committed Sep 26, 2017
2 parents a4ff954 + 26793e0 commit 788cfe6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ Help on flags:
./clickhouse_exporter --help
```

Credentials(if not default):

via environment variables
```
CLICKHOUSE_USER
CLICKHOUSE_PASSWORD
```

## Using Docker

```
Expand Down
32 changes: 24 additions & 8 deletions clickhouse_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ import (

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

const (
namespace = "clickhouse" // For Prometheus metrics.
)

var (
listeningAddress = flag.String("telemetry.address", ":9116", "Address on which to expose metrics.")
metricsEndpoint = flag.String("telemetry.endpoint", "/metrics", "Path under which to expose metrics.")
clickhouseScrapeURI = flag.String("scrape_uri", "http://localhost:8123/", "URI to clickhouse http endpoint")
insecure = flag.Bool("insecure", true, "Ignore server certificate if using https")
listeningAddress = flag.String("telemetry.address", ":9116", "Address on which to expose metrics.")
metricsEndpoint = flag.String("telemetry.endpoint", "/metrics", "Path under which to expose metrics.")
clickhouseScrapeURI = flag.String("scrape_uri", "http://localhost:8123/", "URI to clickhouse http endpoint")
insecure = flag.Bool("insecure", true, "Ignore server certificate if using https")
credentialsPresent, user, password = getCredentials()
)

// Exporter collects clickhouse stats from the given URI and exports them using
Expand Down Expand Up @@ -183,8 +185,22 @@ func (e *Exporter) collect(ch chan<- prometheus.Metric) error {
return nil
}

func (e *Exporter) response(uri string) ([]byte, error) {
resp, err := e.client.Get(uri)
func getCredentials() (bool, string, string) {
user, userPresent := os.LookupEnv("CLICKHOUSE_USER")
password, passwordPresent := os.LookupEnv("CLICKHOUSE_PASSWORD")
return userPresent && passwordPresent, user, password
}

func (e *Exporter) handleResponse(uri string) ([]byte, error) {
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
if credentialsPresent {
req.Header.Set("X-ClickHouse-User", user)
req.Header.Set("X-ClickHouse-Key", password)
}
resp, err := e.client.Do(req)
if err != nil {
return nil, fmt.Errorf("Error scraping clickhouse: %v", err)
}
Expand All @@ -207,7 +223,7 @@ type lineResult struct {
}

func (e *Exporter) parseKeyValueResponse(uri string) ([]lineResult, error) {
data, err := e.response(uri)
data, err := e.handleResponse(uri)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -244,7 +260,7 @@ type partsResult struct {
}

func (e *Exporter) parsePartsResponse(uri string) ([]partsResult, error) {
data, err := e.response(uri)
data, err := e.handleResponse(uri)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 788cfe6

Please sign in to comment.