2018-04-18 19:01:42 -04:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2018 MinIO, Inc.
|
2018-04-18 19:01:42 -04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
2019-10-23 00:01:14 -04:00
|
|
|
"strings"
|
2018-04-18 19:01:42 -04:00
|
|
|
|
2018-05-08 23:10:55 -04:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2018-04-18 19:01:42 -04:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2018-05-08 23:10:55 -04:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2018-04-18 19:01:42 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
httpRequestsDuration = prometheus.NewHistogramVec(
|
|
|
|
prometheus.HistogramOpts{
|
2019-10-23 00:01:14 -04:00
|
|
|
Name: "s3_ttfb_seconds",
|
2019-04-09 14:39:42 -04:00
|
|
|
Help: "Time taken by requests served by current MinIO server instance",
|
2019-10-23 00:01:14 -04:00
|
|
|
Buckets: []float64{.05, .1, .25, .5, 1, 2.5, 5, 10},
|
2018-04-18 19:01:42 -04:00
|
|
|
},
|
2019-10-23 00:01:14 -04:00
|
|
|
[]string{"api"},
|
2018-04-18 19:01:42 -04:00
|
|
|
)
|
2019-06-26 13:36:54 -04:00
|
|
|
minioVersionInfo = prometheus.NewGaugeVec(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: "minio",
|
|
|
|
Name: "version_info",
|
|
|
|
Help: "Version of current MinIO server instance",
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
// current version
|
|
|
|
"version",
|
|
|
|
// commit-id of the current version
|
|
|
|
"commit",
|
|
|
|
},
|
|
|
|
)
|
2018-04-18 19:01:42 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(httpRequestsDuration)
|
2018-05-08 23:10:55 -04:00
|
|
|
prometheus.MustRegister(newMinioCollector())
|
2019-06-26 13:36:54 -04:00
|
|
|
prometheus.MustRegister(minioVersionInfo)
|
2018-04-18 19:01:42 -04:00
|
|
|
}
|
|
|
|
|
2018-05-08 23:10:55 -04:00
|
|
|
// 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{
|
2019-04-09 14:39:42 -04:00
|
|
|
desc: prometheus.NewDesc("minio_stats", "Statistics exposed by MinIO server", nil, nil),
|
2018-05-08 23:10:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2018-04-18 19:01:42 -04:00
|
|
|
|
2019-06-26 13:36:54 -04:00
|
|
|
// Expose MinIO's version information
|
2019-10-23 00:01:14 -04:00
|
|
|
minioVersionInfo.WithLabelValues(Version, CommitID).Set(float64(1.0))
|
2019-06-26 13:36:54 -04:00
|
|
|
|
2019-10-23 00:01:14 -04:00
|
|
|
// Fetch disk space info
|
|
|
|
objLayer := newObjectLayerFn()
|
|
|
|
// Service not initialized yet
|
|
|
|
if objLayer == nil {
|
|
|
|
return
|
|
|
|
}
|
2018-05-08 23:10:55 -04:00
|
|
|
|
2019-10-23 00:01:14 -04:00
|
|
|
storageAPIs := []StorageAPI{}
|
|
|
|
for _, endpoint := range globalEndpoints {
|
|
|
|
if endpoint.IsLocal {
|
|
|
|
// Construct storageAPIs.
|
|
|
|
sAPI, _ := newStorageAPI(endpoint)
|
|
|
|
storageAPIs = append(storageAPIs, sAPI)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disksInfo, onlineDisks, offlineDisks := getDisksInfo(storageAPIs)
|
|
|
|
totalDisks := offlineDisks.Merge(onlineDisks)
|
2018-05-15 15:23:43 -04:00
|
|
|
|
2019-10-23 00:01:14 -04:00
|
|
|
for _, offDisks := range offlineDisks {
|
|
|
|
// MinIO Offline Disks per node
|
2018-05-30 14:30:14 -04:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
2019-10-23 00:01:14 -04:00
|
|
|
prometheus.BuildFQName("minio", "disks", "offline"),
|
|
|
|
"Total number of offline disks in current MinIO server instance",
|
2018-05-30 14:30:14 -04:00
|
|
|
nil, nil),
|
|
|
|
prometheus.GaugeValue,
|
2019-10-23 00:01:14 -04:00
|
|
|
float64(offDisks),
|
2018-05-30 14:30:14 -04:00
|
|
|
)
|
2019-10-23 00:01:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, totDisks := range totalDisks {
|
|
|
|
// MinIO Total Disks per node
|
2018-05-30 14:30:14 -04:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
2019-10-23 00:01:14 -04:00
|
|
|
prometheus.BuildFQName("minio", "disks", "total"),
|
|
|
|
"Total number of disks for current MinIO server instance",
|
2018-05-30 14:30:14 -04:00
|
|
|
nil, nil),
|
|
|
|
prometheus.GaugeValue,
|
2019-10-23 00:01:14 -04:00
|
|
|
float64(totDisks),
|
2018-05-30 14:30:14 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-10-23 00:01:14 -04:00
|
|
|
localPeer := GetLocalPeer(globalEndpoints)
|
|
|
|
for _, di := range disksInfo {
|
|
|
|
// Trim the host
|
|
|
|
absPath := strings.TrimPrefix(di.RelativePath, localPeer)
|
2018-05-30 00:43:46 -04:00
|
|
|
|
2019-10-23 00:01:14 -04:00
|
|
|
// Total disk usage by the disk
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName("disk", "storage", "used"),
|
|
|
|
"Total disk storage used on the disk",
|
|
|
|
[]string{"disk"}, nil),
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(di.Total-di.Free),
|
|
|
|
absPath,
|
|
|
|
)
|
2018-05-30 14:30:14 -04:00
|
|
|
|
2019-10-23 00:01:14 -04:00
|
|
|
// Total available space in the disk
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName("disk", "storage", "available"),
|
|
|
|
"Total available space left on the disk",
|
|
|
|
[]string{"disk"}, nil),
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(di.Free),
|
|
|
|
absPath,
|
|
|
|
)
|
2018-05-30 14:30:14 -04:00
|
|
|
|
2019-10-23 00:01:14 -04:00
|
|
|
// Total storage space of the disk
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName("disk", "storage", "total"),
|
|
|
|
"Total space on the disk",
|
|
|
|
[]string{"disk"}, nil),
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(di.Total),
|
|
|
|
absPath,
|
|
|
|
)
|
2018-05-30 00:43:46 -04:00
|
|
|
}
|
|
|
|
|
2019-10-23 00:01:14 -04:00
|
|
|
connStats := globalConnStats.toServerConnStats()
|
|
|
|
httpStats := globalHTTPStats.toServerHTTPStats()
|
2018-05-30 00:43:46 -04:00
|
|
|
|
2019-10-23 00:01:14 -04:00
|
|
|
// Network Sent/Received Bytes (internode)
|
2018-05-08 23:10:55 -04:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
2019-10-23 00:01:14 -04:00
|
|
|
prometheus.BuildFQName("internode", "tx", "bytes_total"),
|
|
|
|
"Total number of bytes sent to the other peer nodes by current MinIO server instance",
|
2018-05-08 23:10:55 -04:00
|
|
|
nil, nil),
|
2019-10-23 00:01:14 -04:00
|
|
|
prometheus.CounterValue,
|
|
|
|
float64(connStats.TotalOutputBytes),
|
2018-05-08 23:10:55 -04:00
|
|
|
)
|
2018-04-18 19:01:42 -04:00
|
|
|
|
2019-04-05 00:21:50 -04:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
2019-10-23 00:01:14 -04:00
|
|
|
prometheus.BuildFQName("internode", "rx", "bytes_total"),
|
|
|
|
"Total number of internode bytes received by current MinIO server instance",
|
2019-04-05 00:21:50 -04:00
|
|
|
nil, nil),
|
2019-10-23 00:01:14 -04:00
|
|
|
prometheus.CounterValue,
|
|
|
|
float64(connStats.TotalInputBytes),
|
2019-04-05 00:21:50 -04:00
|
|
|
)
|
|
|
|
|
2019-10-23 00:01:14 -04:00
|
|
|
// Network Sent/Received Bytes (Outbound)
|
2019-04-05 00:21:50 -04:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
2019-10-23 00:01:14 -04:00
|
|
|
prometheus.BuildFQName("s3", "tx", "bytes_total"),
|
|
|
|
"Total number of s3 bytes sent by current MinIO server instance",
|
2019-04-05 00:21:50 -04:00
|
|
|
nil, nil),
|
2019-10-23 00:01:14 -04:00
|
|
|
prometheus.CounterValue,
|
|
|
|
float64(connStats.S3OutputBytes),
|
2019-04-05 00:21:50 -04:00
|
|
|
)
|
|
|
|
|
2018-05-08 23:10:55 -04:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
2019-10-23 00:01:14 -04:00
|
|
|
prometheus.BuildFQName("s3", "rx", "bytes_total"),
|
|
|
|
"Total number of s3 bytes received by current MinIO server instance",
|
2018-05-08 23:10:55 -04:00
|
|
|
nil, nil),
|
2019-10-23 00:01:14 -04:00
|
|
|
prometheus.CounterValue,
|
|
|
|
float64(connStats.S3InputBytes),
|
2018-05-08 23:10:55 -04:00
|
|
|
)
|
2019-10-23 00:01:14 -04:00
|
|
|
|
|
|
|
for api, value := range httpStats.CurrentS3Requests.APIStats {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName("s3", "requests", "current"),
|
|
|
|
"Total number of running s3 requests in current MinIO server instance",
|
|
|
|
[]string{"api"}, nil),
|
|
|
|
prometheus.CounterValue,
|
|
|
|
float64(value),
|
|
|
|
api,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
for api, value := range httpStats.TotalS3Requests.APIStats {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName("s3", "requests", "total"),
|
|
|
|
"Total number of s3 requests in current MinIO server instance",
|
|
|
|
[]string{"api"}, nil),
|
|
|
|
prometheus.CounterValue,
|
|
|
|
float64(value),
|
|
|
|
api,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
for api, value := range httpStats.TotalS3Errors.APIStats {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName("s3", "errors", "total"),
|
|
|
|
"Total number of s3 errors in current MinIO server instance",
|
|
|
|
[]string{"api"}, nil),
|
|
|
|
prometheus.CounterValue,
|
|
|
|
float64(value),
|
|
|
|
api,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-04-18 19:01:42 -04:00
|
|
|
}
|
|
|
|
|
2018-05-08 23:10:55 -04:00
|
|
|
func metricsHandler() http.Handler {
|
2019-09-22 10:57:12 -04:00
|
|
|
|
2018-05-08 23:10:55 -04:00
|
|
|
registry := prometheus.NewRegistry()
|
|
|
|
|
2019-06-26 13:36:54 -04:00
|
|
|
err := registry.Register(minioVersionInfo)
|
|
|
|
logger.LogIf(context.Background(), err)
|
|
|
|
|
|
|
|
err = registry.Register(httpRequestsDuration)
|
2018-05-08 23:10:55 -04:00
|
|
|
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,
|
|
|
|
}),
|
|
|
|
)
|
2019-09-22 10:57:12 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthMiddleware checks if the bearer token is valid and authorized.
|
|
|
|
func AuthMiddleware(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
claims, _, authErr := webRequestAuthenticate(r)
|
|
|
|
if authErr != nil || !claims.VerifyIssuer("prometheus", true) {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
2018-04-18 19:01:42 -04:00
|
|
|
}
|