Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for environment variables #3

Merged
merged 1 commit into from
Aug 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ To start the exporter we use the [docker run](https://docs.docker.com/engine/ref
```
Usage of ./nginx-prometheus-exporter:
-nginx.plus
Start the exporter for NGINX Plus. By default, the exporter is started for NGINX.
Start the exporter for NGINX Plus. By default, the exporter is started for NGINX. The default value can be overwritten by NGINX_PLUS environment variable.
-nginx.scrape-uri string
A URI for scraping NGINX or NGINX Plus metrics.
For NGINX, the stub_status page must be available through the URI. For NGINX Plus -- the API. (default "http://127.0.0.1:8080/stub_status")
For NGINX, the stub_status page must be available through the URI. For NGINX Plus -- the API. The default value can be overwritten by SCRAPE_URI environment variable. (default "http://127.0.0.1:8080/stub_status")
-web.listen-address string
An address to listen on for web interface and telemetry. (default ":9113")
An address to listen on for web interface and telemetry. The default value can be overwritten by LISTEN_ADDRESS environment variable. (default ":9113")
-web.telemetry-path string
A path under which to expose metrics. (default "/metrics")
A path under which to expose metrics. The default value can be overwritten by TELEMETRY_PATH environment variable. (default "/metrics")
```

### Exported Metrics
Expand Down
41 changes: 36 additions & 5 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,56 @@ import (
"flag"
"log"
"net/http"
"os"
"strconv"

"github.com/nginxinc/nginx-prometheus-exporter/client"
"github.com/nginxinc/nginx-prometheus-exporter/collector"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func getEnv(key, defaultValue string) string {
value, ok := os.LookupEnv(key)
if !ok {
return defaultValue
}
return value
}

func getEnvBool(key string, defaultValue bool) bool {
value, ok := os.LookupEnv(key)
if !ok {
return defaultValue
}
b, err := strconv.ParseBool(value)
if err != nil {
log.Fatalf("Environment Variable value for %s must be a boolean", key)
}
return b
}

var (
// Set during go build
version string
gitCommit string

// Defaults values
defaultListenAddress = getEnv("LISTEN_ADDRESS", ":9113")
defaultMetricsPath = getEnv("TELEMETRY_PATH", "/metrics")
defaultNginxPlus = getEnvBool("NGINX_PLUS", false)
defaultScrapeURI = getEnv("SCRAPE_URI", "http://127.0.0.1:8080/stub_status")

// Command-line flags
listenAddr = flag.String("web.listen-address", ":9113", "An address to listen on for web interface and telemetry.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "A path under which to expose metrics.")
nginxPlus = flag.Bool("nginx.plus", false, "Start the exporter for NGINX Plus. By default, the exporter is started for NGINX.")
scrapeURI = flag.String("nginx.scrape-uri", "http://127.0.0.1:8080/stub_status",
listenAddr = flag.String("web.listen-address", defaultListenAddress,
"An address to listen on for web interface and telemetry. The default value can be overwritten by LISTEN_ADDRESS environment variable.")
metricsPath = flag.String("web.telemetry-path", defaultMetricsPath,
"A path under which to expose metrics. The default value can be overwritten by TELEMETRY_PATH environment variable.")
nginxPlus = flag.Bool("nginx.plus", defaultNginxPlus,
"Start the exporter for NGINX Plus. By default, the exporter is started for NGINX. The default value can be overwritten by NGINX_PLUS environment variable.")
scrapeURI = flag.String("nginx.scrape-uri", defaultScrapeURI,
`A URI for scraping NGINX or NGINX Plus metrics.
For NGINX, the stub_status page must be available through the URI. For NGINX Plus -- the API.`)
For NGINX, the stub_status page must be available through the URI. For NGINX Plus -- the API. The default value can be overwritten by SCRAPE_URI environment variable.`)
)

func main() {
Expand Down