2022-04-27 01:00:20 -05:00
|
|
|
//go:build metrics
|
|
|
|
// +build metrics
|
2021-10-15 10:05:00 -05:00
|
|
|
|
|
|
|
package monitoring
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
"zotregistry.io/zot/errors"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/log"
|
2021-10-15 10:05:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const metricsNamespace = "zot"
|
|
|
|
|
|
|
|
var (
|
2022-10-05 05:21:14 -05:00
|
|
|
httpConnRequests = promauto.NewCounterVec( //nolint: gochecknoglobals
|
2021-10-15 10:05:00 -05:00
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
Name: "http_requests_total",
|
|
|
|
Help: "Total number of http request in zot",
|
|
|
|
},
|
|
|
|
[]string{"method", "code"},
|
|
|
|
)
|
2022-10-05 05:21:14 -05:00
|
|
|
httpRepoLatency = promauto.NewSummaryVec( //nolint: gochecknoglobals
|
2021-10-15 10:05:00 -05:00
|
|
|
prometheus.SummaryOpts{
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
Name: "http_repo_latency_seconds",
|
|
|
|
Help: "Latency of serving HTTP requests",
|
|
|
|
},
|
|
|
|
[]string{"repo"},
|
|
|
|
)
|
2022-10-05 05:21:14 -05:00
|
|
|
httpMethodLatency = promauto.NewHistogramVec( //nolint: gochecknoglobals
|
2021-10-15 10:05:00 -05:00
|
|
|
prometheus.HistogramOpts{
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
Name: "http_method_latency_seconds",
|
|
|
|
Help: "Latency of serving HTTP requests",
|
|
|
|
Buckets: GetDefaultBuckets(),
|
|
|
|
},
|
|
|
|
[]string{"method"},
|
|
|
|
)
|
2022-10-05 05:21:14 -05:00
|
|
|
repoStorageBytes = promauto.NewGaugeVec( //nolint: gochecknoglobals
|
2021-10-15 10:05:00 -05:00
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
Name: "repo_storage_bytes",
|
|
|
|
Help: "Storage used per zot repo",
|
|
|
|
},
|
|
|
|
[]string{"repo"},
|
|
|
|
)
|
2022-10-05 05:21:14 -05:00
|
|
|
uploadCounter = promauto.NewCounterVec( //nolint: gochecknoglobals
|
2021-10-15 10:05:00 -05:00
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
Name: "repo_uploads_total",
|
|
|
|
Help: "Total number times an image was uploaded",
|
|
|
|
},
|
|
|
|
[]string{"repo"},
|
|
|
|
)
|
2022-10-05 05:21:14 -05:00
|
|
|
downloadCounter = promauto.NewCounterVec( //nolint: gochecknoglobals
|
2021-10-15 10:05:00 -05:00
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
Name: "repo_downloads_total",
|
|
|
|
Help: "Total number times an image was downloaded",
|
|
|
|
},
|
|
|
|
[]string{"repo"},
|
|
|
|
)
|
2022-10-05 05:21:14 -05:00
|
|
|
serverInfo = promauto.NewGaugeVec( //nolint: gochecknoglobals
|
2021-10-15 10:05:00 -05:00
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
Name: "info",
|
|
|
|
Help: "Server general information",
|
|
|
|
},
|
|
|
|
[]string{"commit", "binaryType", "goVersion", "version"},
|
|
|
|
)
|
2022-10-05 05:21:14 -05:00
|
|
|
storageLockLatency = promauto.NewHistogramVec( //nolint: gochecknoglobals
|
2021-12-21 08:19:40 -05:00
|
|
|
prometheus.HistogramOpts{
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
Name: "storage_lock_latency_seconds",
|
|
|
|
Help: "Latency of serving HTTP requests",
|
|
|
|
Buckets: GetStorageLatencyBuckets(),
|
|
|
|
},
|
|
|
|
[]string{"storageName", "lockType"},
|
|
|
|
)
|
2021-10-15 10:05:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type metricServer struct {
|
|
|
|
enabled bool
|
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
2021-12-21 08:19:40 -05:00
|
|
|
func GetDefaultBuckets() []float64 {
|
|
|
|
return []float64{.05, .5, 1, 5, 30, 60, 600}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetStorageLatencyBuckets() []float64 {
|
|
|
|
return []float64{.001, .01, 0.1, 1, 5, 10, 15, 30, 60}
|
|
|
|
}
|
|
|
|
|
2021-10-15 10:05:00 -05:00
|
|
|
func NewMetricsServer(enabled bool, log log.Logger) MetricServer {
|
|
|
|
return &metricServer{
|
|
|
|
enabled: enabled,
|
|
|
|
log: log,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// implementing the MetricServer interface.
|
|
|
|
func (ms *metricServer) SendMetric(mfunc interface{}) {
|
|
|
|
if ms.enabled {
|
2021-12-13 14:23:31 -05:00
|
|
|
mfn, ok := mfunc.(func())
|
|
|
|
if !ok {
|
|
|
|
ms.log.Error().Err(errors.ErrInvalidMetric).Msg("type conversion")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mfn()
|
2021-10-15 10:05:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *metricServer) ForceSendMetric(mfunc interface{}) {
|
2021-12-13 14:23:31 -05:00
|
|
|
mfn, ok := mfunc.(func())
|
|
|
|
if !ok {
|
|
|
|
ms.log.Error().Err(errors.ErrInvalidMetric).Msg("type conversion")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mfn()
|
2021-10-15 10:05:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *metricServer) ReceiveMetrics() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *metricServer) IsEnabled() bool {
|
|
|
|
return ms.enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
func IncHTTPConnRequests(ms MetricServer, lvalues ...string) {
|
|
|
|
ms.SendMetric(func() {
|
|
|
|
httpConnRequests.WithLabelValues(lvalues...).Inc()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func ObserveHTTPRepoLatency(ms MetricServer, path string, latency time.Duration) {
|
|
|
|
ms.SendMetric(func() {
|
|
|
|
match := re.FindStringSubmatch(path)
|
|
|
|
|
|
|
|
if len(match) > 1 {
|
|
|
|
httpRepoLatency.WithLabelValues(match[1]).Observe(latency.Seconds())
|
|
|
|
} else {
|
|
|
|
httpRepoLatency.WithLabelValues("N/A").Observe(latency.Seconds())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func ObserveHTTPMethodLatency(ms MetricServer, method string, latency time.Duration) {
|
|
|
|
ms.SendMetric(func() {
|
|
|
|
httpMethodLatency.WithLabelValues(method).Observe(latency.Seconds())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func IncDownloadCounter(ms MetricServer, repo string) {
|
|
|
|
ms.SendMetric(func() {
|
|
|
|
downloadCounter.WithLabelValues(repo).Inc()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-21 12:37:23 -05:00
|
|
|
func SetStorageUsage(ms MetricServer, rootDir, repo string) {
|
2021-10-15 10:05:00 -05:00
|
|
|
ms.SendMetric(func() {
|
|
|
|
dir := path.Join(rootDir, repo)
|
|
|
|
repoSize, err := getDirSize(dir)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
repoStorageBytes.WithLabelValues(repo).Set(float64(repoSize))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func IncUploadCounter(ms MetricServer, repo string) {
|
|
|
|
ms.SendMetric(func() {
|
|
|
|
uploadCounter.WithLabelValues(repo).Inc()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetServerInfo(ms MetricServer, lvalues ...string) {
|
|
|
|
ms.ForceSendMetric(func() {
|
|
|
|
serverInfo.WithLabelValues(lvalues...).Set(0)
|
|
|
|
})
|
|
|
|
}
|
2021-12-21 08:19:40 -05:00
|
|
|
|
|
|
|
func ObserveStorageLockLatency(ms MetricServer, latency time.Duration, storageName, lockType string) {
|
|
|
|
ms.SendMetric(func() {
|
|
|
|
storageLockLatency.WithLabelValues(storageName, lockType).Observe(latency.Seconds())
|
|
|
|
})
|
|
|
|
}
|