Skip to content

Commit

Permalink
Make Promtheus namespace customizable from the binary
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmm committed Dec 24, 2016
1 parent 4636ba3 commit 7a75c78
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 21 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ COPY bin/nginx-vts-exporter /app/
ENV NGIX_HOST http://localhost
ENV METRICS_ENDPOINT "/metrics"
ENV METRICS_ADDR ":9913"
ENV DEFAULT_METRICS_NS "nginx"

EXPOSE 9113

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Variable name | Default | Description
NGINX_STATUS | http://localhost/status/format/json | Nginx JSON format status page
METRICS_ENDPOINT | /metrics | Metrics endpoint exportation URI
METRICS_ADDR | :9913 | Metrics exportation address:port
METRICS_NS | nginx | Prometheus metrics Namespaces


##Build
Expand All @@ -42,5 +43,6 @@ $ docker build -t vts-export .

##Run
```
docker run --rm --env NGIX_HOST="http://localhost/status/format/json" -ti vts-export
docker run -ti --rm --env NGIX_HOST="http://localhost/status/format/json" --env METRICS_NS="nginx_prod1" vts-export
```
Binary file modified bin/nginx-vts-exporter
Binary file not shown.
2 changes: 1 addition & 1 deletion build-binary.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ bin_diretory=`pwd`/bin

echo "[Step 1] - Building binary inside image"
docker build --tag=$tag_name \
--file=Dockerfile-build . >/dev/null
--file=Dockerfile-build .

echo "[Step 2] - Copying the binary from docker image"
docker run --rm --volume=$bin_diretory:/output $tag_name cp /build/nginx-vts-exporter /output/nginx-vts-exporter >/dev/null 2>&1
Expand Down
12 changes: 7 additions & 5 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ set -eo pipefail
binary="/app/nginx-vts-exporter"
default_status="$NGIX_HOST/status/format/json"
NGINX_STATUS=${NGINX_STATUS:-$default_status}
METRICS_NS=${METRICS_NS:-$DEFAULT_METRICS_NS}

# If there are any arguments then we want to run those instead
#if [[ "$1" == "$binary" || -z $1 ]]; then
# exec "$@"
#else
# echo "Running the default"
echo "[$0] - Nginx scrape host --> [$NGINX_STATUS]"
echo "[$0] - Metrics Address --> [$METRICS_ADDR]"
echo "[$0] - Metrics Endpoint --> [$METRICS_ENDPOINT]"
echo "[$0] - Running metrics nginx-vts-exporter"
exec "$binary" -nginx.scrape_uri=$NGINX_STATUS -telemetry.address $METRICS_ADDR -telemetry.endpoint $METRICS_ENDPOINT
#echo "[$0] - Nginx scrape host --> [$NGINX_STATUS]"
#echo "[$0] - Metrics Address --> [$METRICS_ADDR]"
#echo "[$0] - Metrics Endpoint --> [$METRICS_ENDPOINT]"
#echo "[$0] - Metrics Namespace --> [$METRICS_NS]"
#echo "[$0] - Running metrics nginx-vts-exporter"
exec "$binary" -nginx.scrape_uri=$NGINX_STATUS -telemetry.address $METRICS_ADDR -telemetry.endpoint $METRICS_ENDPOINT -metrics.namespace $METRICS_NS
#fi
32 changes: 18 additions & 14 deletions nginx_vts_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ type Exporter struct {
serverMetrics, upstreamMetrics, cacheMetrics map[string]*prometheus.GaugeVec
}

func newServerMetric(metricName string, docString string, labels []string) *prometheus.GaugeVec {
func newServerMetric(metricName string, docString string, labels []string, namespace string) *prometheus.GaugeVec {
return prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Expand All @@ -155,7 +155,7 @@ func newServerMetric(metricName string, docString string, labels []string) *prom
)
}

func newUpstreamMetric(metricName string, docString string, labels []string) *prometheus.GaugeVec {
func newUpstreamMetric(metricName string, docString string, labels []string, namespace string) *prometheus.GaugeVec {
return prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Expand All @@ -166,7 +166,7 @@ func newUpstreamMetric(metricName string, docString string, labels []string) *pr
)
}

func newCacheMetric(metricName string, docString string, labels []string) *prometheus.GaugeVec {
func newCacheMetric(metricName string, docString string, labels []string, namespace string) *prometheus.GaugeVec {
return prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Expand All @@ -177,23 +177,23 @@ func newCacheMetric(metricName string, docString string, labels []string) *prome
)
}

func NewExporter(uri string) *Exporter {
func NewExporter(uri string, namespace string) *Exporter {

return &Exporter{
URI: uri,
serverMetrics: map[string]*prometheus.GaugeVec{
"connections": newServerMetric("connections", "nginx connections", []string{"status"}),
"requests": newServerMetric("requests", "requests counter", []string{"host", "code"}),
"bytes": newServerMetric("bytes", "request/response bytes", []string{"host", "direction"}),
"cache": newServerMetric("cache", "cache counter", []string{"host", "status"}),
"connections": newServerMetric("connections", "nginx connections", []string{"status"}, namespace),
"requests": newServerMetric("requests", "requests counter", []string{"host", "code"}, namespace),
"bytes": newServerMetric("bytes", "request/response bytes", []string{"host", "direction"}, namespace),
"cache": newServerMetric("cache", "cache counter", []string{"host", "status"}, namespace),
},
upstreamMetrics: map[string]*prometheus.GaugeVec{
"requests": newUpstreamMetric("requests", "requests counter", []string{"upstream", "code"}),
"bytes": newUpstreamMetric("bytes", "request/response bytes", []string{"upstream", "direction"}),
"requests": newUpstreamMetric("requests", "requests counter", []string{"upstream", "code"}, namespace),
"bytes": newUpstreamMetric("bytes", "request/response bytes", []string{"upstream", "direction"}, namespace),
},
cacheMetrics: map[string]*prometheus.GaugeVec{
"requests": newCacheMetric("requests", "cache requests counter", []string{"zone", "status"}),
"bytes": newCacheMetric("bytes", "cache request/response bytes", []string{"zone", "direction"}),
"requests": newCacheMetric("requests", "cache requests counter", []string{"zone", "status"}, namespace),
"bytes": newCacheMetric("bytes", "cache request/response bytes", []string{"zone", "direction"}, namespace),
},
}
}
Expand Down Expand Up @@ -342,14 +342,15 @@ func fetchHTTP(uri string, timeout time.Duration) func() (io.ReadCloser, error)
var (
listenAddress = flag.String("telemetry.address", ":9113", "Address on which to expose metrics.")
metricsEndpoint = flag.String("telemetry.endpoint", "/metrics", "Path under which to expose metrics.")
metricsNamespace = flag.String("metrics.namespace", "nginx", "Prometheus metrics namespace.")
nginxScrapeURI = flag.String("nginx.scrape_uri", "http://localhost/status", "URI to nginx stub status page")
insecure = flag.Bool("insecure", true, "Ignore server certificate if using https")
)

func main() {
flag.Parse()

exporter := NewExporter(*nginxScrapeURI)
exporter := NewExporter(*nginxScrapeURI, *metricsNamespace)
prometheus.MustRegister(exporter)
prometheus.Unregister(prometheus.NewProcessCollector(os.Getpid(), ""))
prometheus.Unregister(prometheus.NewGoCollector())
Expand All @@ -365,6 +366,9 @@ func main() {
</html>`))
})

log.Printf("Starting Server: %s", *listenAddress)
log.Printf("Starting Server at : %s", *listenAddress)
log.Printf("Metrics endpoint: %s", *metricsEndpoint)
log.Printf("Metrics namespace: %s", *metricsNamespace)
log.Printf("Scraping information from : %s", *nginxScrapeURI)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}

0 comments on commit 7a75c78

Please sign in to comment.