Enhancements in Minio Prometheus exporter (#5848)

Standardized Minio collectors based on Prometheus 
recommendations.
This commit is contained in:
Ashish Kumar Sinha
2018-05-09 08:40:55 +05:30
committed by Harshavardhana
parent bb34bd91f1
commit deb685c5b5
19 changed files with 1769 additions and 268 deletions

View File

@@ -17,8 +17,7 @@
package cmd
import (
router "github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/gorilla/mux"
)
const (
@@ -26,8 +25,8 @@ const (
)
// registerMetricsRouter - add handler functions for metrics.
func registerMetricsRouter(mux *router.Router) {
func registerMetricsRouter(router *mux.Router) {
// metrics router
metricsRouter := mux.NewRoute().PathPrefix(minioReservedBucketPath).Subrouter()
metricsRouter.Handle(prometheusMetricsPath, metricsHandler(prometheus.Handler()))
metricsRouter := router.NewRoute().PathPrefix(minioReservedBucketPath).Subrouter()
metricsRouter.Handle(prometheusMetricsPath, metricsHandler())
}

View File

@@ -20,7 +20,9 @@ import (
"context"
"net/http"
"github.com/minio/minio/cmd/logger"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
@@ -39,65 +41,36 @@ var (
},
[]string{"request_type"},
)
networkSentBytes = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "minio_network_sent_bytes",
Help: "Total number of bytes sent by current Minio server instance",
},
)
networkReceivedBytes = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "minio_network_received_bytes",
Help: "Total number of bytes received by current Minio server instance",
},
)
onlineMinioDisks = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "minio_online_disks_total",
Help: "Total number of online disks for current Minio server instance",
},
)
offlineMinioDisks = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "minio_offline_disks_total",
Help: "Total number of offline disks for current Minio server instance",
},
)
serverUptime = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "minio_server_uptime_seconds",
Help: "Time elapsed since current Minio server instance started",
},
)
minioStorageTotal = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "minio_disk_storage_bytes",
Help: "Total disk storage available to current Minio server instance",
},
)
minioStorageFree = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "minio_disk_storage_free_bytes",
Help: "Total free disk storage available to current Minio server instance",
},
)
)
func init() {
prometheus.MustRegister(httpRequests)
prometheus.MustRegister(httpRequestsDuration)
prometheus.MustRegister(networkSentBytes)
prometheus.MustRegister(networkReceivedBytes)
prometheus.MustRegister(onlineMinioDisks)
prometheus.MustRegister(offlineMinioDisks)
prometheus.MustRegister(serverUptime)
prometheus.MustRegister(minioStorageTotal)
prometheus.MustRegister(minioStorageFree)
prometheus.MustRegister(newMinioCollector())
}
func updateGeneralMetrics() {
// Increment server uptime
serverUptime.Set(UTCNow().Sub(globalBootTime).Seconds())
// newMinioCollector describes the collector
// and returns reference of minioCollector
// It creates the Prometheus Description which is used
// to define metric and help string
func newMinioCollector() *minioCollector {
return &minioCollector{
desc: prometheus.NewDesc("minio_stats", "Statistics exposed by Minio server", nil, nil),
}
}
// minioCollector is the Custom Collector
type minioCollector struct {
desc *prometheus.Desc
}
// Describe sends the super-set of all possible descriptors of metrics
func (c *minioCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.desc
}
// Collect is called by the Prometheus registry when collecting metrics.
func (c *minioCollector) Collect(ch chan<- prometheus.Metric) {
// Fetch disk space info
objLayer := newObjectLayerFn()
@@ -105,25 +78,103 @@ func updateGeneralMetrics() {
if objLayer == nil {
return
}
// Update total/free disk space
s := objLayer.StorageInfo(context.Background())
minioStorageTotal.Set(float64(s.Total))
minioStorageFree.Set(float64(s.Free))
// Update online/offline disks
onlineMinioDisks.Set(float64(s.Backend.OnlineDisks))
offlineMinioDisks.Set(float64(s.Backend.OfflineDisks))
var totalDisks, offlineDisks int
// Setting totalDisks to 1 and offlineDisks to 0 in FS mode
if s.Backend.Type == FS {
totalDisks = 1
offlineDisks = 0
} else {
offlineDisks = s.Backend.OfflineDisks
totalDisks = s.Backend.OfflineDisks + s.Backend.OnlineDisks
}
// Update prometheus metric
networkSentBytes.Set(float64(globalConnStats.getTotalOutputBytes()))
networkReceivedBytes.Set(float64(globalConnStats.getTotalInputBytes()))
// Network Sent/Received Bytes
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName("minio", "network", "sent_bytes_total"),
"Total number of bytes sent by current Minio server instance",
nil, nil),
prometheus.CounterValue,
float64(globalConnStats.getTotalOutputBytes()),
)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName("minio", "network", "received_bytes_total"),
"Total number of bytes received by current Minio server instance",
nil, nil),
prometheus.CounterValue,
float64(globalConnStats.getTotalInputBytes()),
)
// Total/Free Storage Bytes
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName("minio", "disk", "storage_bytes"),
"Total disk storage available to current Minio server instance",
nil, nil),
prometheus.GaugeValue,
float64(s.Total),
)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName("minio", "disk", "storage_free_bytes"),
"Total free disk storage available to current Minio server instance",
nil, nil),
prometheus.GaugeValue,
float64(s.Free),
)
// Set Server Start Time
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName("minio", "server", "start_time_seconds"),
"Time when current Minio server instance started",
nil, nil),
prometheus.GaugeValue,
float64(globalBootTime.Unix()),
)
// Minio Total Disk/Offline Disk
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName("minio", "total", "disks"),
"Total number of disks for current Minio server instance",
nil, nil),
prometheus.GaugeValue,
float64(totalDisks),
)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName("minio", "offline", "disks"),
"Total number of offline disks for current Minio server instance",
nil, nil),
prometheus.GaugeValue,
float64(offlineDisks),
)
}
func metricsHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Update generic metrics before handling the prometheus scrape request
updateGeneralMetrics()
prometheus.Handler().ServeHTTP(w, r)
})
func metricsHandler() http.Handler {
registry := prometheus.NewRegistry()
err := registry.Register(httpRequests)
logger.LogIf(context.Background(), err)
err = registry.Register(httpRequestsDuration)
logger.LogIf(context.Background(), err)
err = registry.Register(newMinioCollector())
logger.LogIf(context.Background(), err)
gatherers := prometheus.Gatherers{
prometheus.DefaultGatherer,
registry,
}
// Delegate http serving to Prometheus client library, which will call collector.Collect.
return promhttp.InstrumentMetricHandler(
registry,
promhttp.HandlerFor(gatherers,
promhttp.HandlerOpts{
ErrorHandling: promhttp.ContinueOnError,
}),
)
}