From b783e8bebf42de29ec9ae3b91db977f08efb1d63 Mon Sep 17 00:00:00 2001 From: Alexey Remizov Date: Wed, 10 Jun 2020 16:11:14 +0300 Subject: [PATCH] Add IMGPROXY_PROMETHEUS_NAMESPACE config (#405) * Add IMGPROXY_PROMETHEUS_NAMESPACE config * Add documentations about IMGPROXY_PROMETHEUS_NAMESPACE Co-authored-by: Alexey Remizov --- config.go | 4 +++- docs/prometheus.md | 4 +++- prometheus.go | 55 +++++++++++++++++++++++++++------------------- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/config.go b/config.go index 9bc86218..baf6f882 100644 --- a/config.go +++ b/config.go @@ -231,7 +231,8 @@ type config struct { NewRelicAppName string NewRelicKey string - PrometheusBind string + PrometheusBind string + PrometheusNamespace string BugsnagKey string BugsnagStage string @@ -389,6 +390,7 @@ func configure() error { strEnvConfig(&conf.NewRelicKey, "IMGPROXY_NEW_RELIC_KEY") strEnvConfig(&conf.PrometheusBind, "IMGPROXY_PROMETHEUS_BIND") + strEnvConfig(&conf.PrometheusNamespace, "IMGPROXY_PROMETHEUS_NAMESPACE") strEnvConfig(&conf.BugsnagKey, "IMGPROXY_BUGSNAG_KEY") strEnvConfig(&conf.BugsnagStage, "IMGPROXY_BUGSNAG_STAGE") diff --git a/docs/prometheus.md b/docs/prometheus.md index 432f8951..7a36f5aa 100644 --- a/docs/prometheus.md +++ b/docs/prometheus.md @@ -3,7 +3,9 @@ imgproxy can collect its metrics for Prometheus. To use this feature, do the following: 1. Set `IMGPROXY_PROMETHEUS_BIND` environment variable. Note that you can't bind the main server and Prometheus to the same port; -2. Collect the metrics from any path on the specified binding. +2. Set `IMGPROXY_PROMETHEUS_NAMESPACE` to prepend prefix to the names of metrics. + I.e. with `IMGPROXY_PROMETHEUS_NAMESPACE=imgproxy` names will look like `imgproxy_requests_total`. +3. Collect the metrics from any path on the specified binding. imgproxy will collect the following metrics: diff --git a/prometheus.go b/prometheus.go index dc08656a..5026bb8b 100644 --- a/prometheus.go +++ b/prometheus.go @@ -32,58 +32,69 @@ func initPrometheus() { } prometheusRequestsTotal = prometheus.NewCounter(prometheus.CounterOpts{ - Name: "requests_total", - Help: "A counter of the total number of HTTP requests imgproxy processed.", + Namespace: conf.PrometheusNamespace, + Name: "requests_total", + Help: "A counter of the total number of HTTP requests imgproxy processed.", }) prometheusErrorsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ - Name: "errors_total", - Help: "A counter of the occurred errors separated by type.", + Namespace: conf.PrometheusNamespace, + Name: "errors_total", + Help: "A counter of the occurred errors separated by type.", }, []string{"type"}) prometheusRequestDuration = prometheus.NewHistogram(prometheus.HistogramOpts{ - Name: "request_duration_seconds", - Help: "A histogram of the response latency.", + Namespace: conf.PrometheusNamespace, + Name: "request_duration_seconds", + Help: "A histogram of the response latency.", }) prometheusDownloadDuration = prometheus.NewHistogram(prometheus.HistogramOpts{ - Name: "download_duration_seconds", - Help: "A histogram of the source image downloading latency.", + Namespace: conf.PrometheusNamespace, + Name: "download_duration_seconds", + Help: "A histogram of the source image downloading latency.", }) prometheusProcessingDuration = prometheus.NewHistogram(prometheus.HistogramOpts{ - Name: "processing_duration_seconds", - Help: "A histogram of the image processing latency.", + Namespace: conf.PrometheusNamespace, + Name: "processing_duration_seconds", + Help: "A histogram of the image processing latency.", }) prometheusBufferSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{ - Name: "buffer_size_bytes", - Help: "A histogram of the buffer size in bytes.", + Namespace: conf.PrometheusNamespace, + Name: "buffer_size_bytes", + Help: "A histogram of the buffer size in bytes.", }, []string{"type"}) prometheusBufferDefaultSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{ - Name: "buffer_default_size_bytes", - Help: "A gauge of the buffer default size in bytes.", + Namespace: conf.PrometheusNamespace, + Name: "buffer_default_size_bytes", + Help: "A gauge of the buffer default size in bytes.", }, []string{"type"}) prometheusBufferMaxSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{ - Name: "buffer_max_size_bytes", - Help: "A gauge of the buffer max size in bytes.", + Namespace: conf.PrometheusNamespace, + Name: "buffer_max_size_bytes", + Help: "A gauge of the buffer max size in bytes.", }, []string{"type"}) prometheusVipsMemory = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "vips_memory_bytes", - Help: "A gauge of the vips tracked memory usage in bytes.", + Namespace: conf.PrometheusNamespace, + Name: "vips_memory_bytes", + Help: "A gauge of the vips tracked memory usage in bytes.", }) prometheusVipsMaxMemory = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "vips_max_memory_bytes", - Help: "A gauge of the max vips tracked memory usage in bytes.", + Namespace: conf.PrometheusNamespace, + Name: "vips_max_memory_bytes", + Help: "A gauge of the max vips tracked memory usage in bytes.", }) prometheusVipsAllocs = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "vips_allocs", - Help: "A gauge of the number of active vips allocations.", + Namespace: conf.PrometheusNamespace, + Name: "vips_allocs", + Help: "A gauge of the number of active vips allocations.", }) prometheus.MustRegister(