2022-04-27 01:00:20 -05:00
|
|
|
//go:build metrics
|
|
|
|
// +build metrics
|
2022-01-26 16:52:59 -05:00
|
|
|
|
|
|
|
package monitoring_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
"gopkg.in/resty.v1"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2022-01-26 16:52:59 -05:00
|
|
|
"zotregistry.io/zot/pkg/api"
|
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
|
|
|
extconf "zotregistry.io/zot/pkg/extensions/config"
|
|
|
|
"zotregistry.io/zot/pkg/extensions/monitoring"
|
2023-09-27 13:34:48 -05:00
|
|
|
test "zotregistry.io/zot/pkg/test/common"
|
2023-09-15 11:53:15 -05:00
|
|
|
. "zotregistry.io/zot/pkg/test/image-utils"
|
2023-09-27 13:34:48 -05:00
|
|
|
ociutils "zotregistry.io/zot/pkg/test/oci-utils"
|
2022-01-26 16:52:59 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestExtensionMetrics(t *testing.T) {
|
|
|
|
Convey("Make a new controller with explicitly enabled metrics", t, func() {
|
2022-01-19 14:54:17 -05:00
|
|
|
port := test.GetFreePort()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
2022-01-26 16:52:59 -05:00
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
|
2022-03-07 03:55:12 -05:00
|
|
|
rootDir := t.TempDir()
|
2022-01-26 16:52:59 -05:00
|
|
|
|
|
|
|
conf.Storage.RootDirectory = rootDir
|
|
|
|
conf.Extensions = &extconf.ExtensionConfig{}
|
2021-12-28 08:29:30 -05:00
|
|
|
enabled := true
|
2022-01-26 16:52:59 -05:00
|
|
|
conf.Extensions.Metrics = &extconf.MetricsConfig{
|
2022-10-21 07:33:54 -05:00
|
|
|
BaseConfig: extconf.BaseConfig{Enable: &enabled},
|
2022-01-26 16:52:59 -05:00
|
|
|
Prometheus: &extconf.PrometheusConfig{Path: "/metrics"},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
So(ctlr, ShouldNotBeNil)
|
|
|
|
|
2023-01-19 11:54:05 -05:00
|
|
|
cm := test.NewControllerManager(ctlr)
|
|
|
|
cm.StartAndWait(port)
|
|
|
|
defer cm.StopServer()
|
2022-01-26 16:52:59 -05:00
|
|
|
|
|
|
|
// improve code coverage
|
|
|
|
ctlr.Metrics.SendMetric(baseURL)
|
|
|
|
ctlr.Metrics.ForceSendMetric(baseURL)
|
|
|
|
|
|
|
|
So(ctlr.Metrics.IsEnabled(), ShouldBeTrue)
|
|
|
|
So(ctlr.Metrics.ReceiveMetrics(), ShouldBeNil)
|
|
|
|
|
|
|
|
monitoring.ObserveHTTPRepoLatency(ctlr.Metrics,
|
|
|
|
"/v2/alpine/blobs/uploads/299148f0-0e32-4830-90d2-a3fa744137d9", time.Millisecond)
|
|
|
|
monitoring.IncDownloadCounter(ctlr.Metrics, "alpine")
|
|
|
|
monitoring.IncUploadCounter(ctlr.Metrics, "alpine")
|
|
|
|
|
2023-09-27 13:34:48 -05:00
|
|
|
srcStorageCtlr := ociutils.GetDefaultStoreController(rootDir, ctlr.Log)
|
|
|
|
err := WriteImageToFileSystem(CreateDefaultImage(), "alpine", "0.0.1", srcStorageCtlr)
|
2023-08-18 03:46:11 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-01-26 16:52:59 -05:00
|
|
|
monitoring.SetStorageUsage(ctlr.Metrics, rootDir, "alpine")
|
|
|
|
|
|
|
|
monitoring.ObserveStorageLockLatency(ctlr.Metrics, time.Millisecond, rootDir, "RWLock")
|
|
|
|
|
|
|
|
resp, err := resty.R().Get(baseURL + "/metrics")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, http.StatusOK)
|
|
|
|
|
|
|
|
respStr := string(resp.Body())
|
|
|
|
So(respStr, ShouldContainSubstring, "zot_repo_downloads_total{repo=\"alpine\"} 1")
|
|
|
|
So(respStr, ShouldContainSubstring, "zot_repo_uploads_total{repo=\"alpine\"} 1")
|
|
|
|
So(respStr, ShouldContainSubstring, "zot_repo_storage_bytes{repo=\"alpine\"}")
|
|
|
|
So(respStr, ShouldContainSubstring, "zot_storage_lock_latency_seconds_bucket")
|
|
|
|
So(respStr, ShouldContainSubstring, "zot_storage_lock_latency_seconds_sum")
|
|
|
|
So(respStr, ShouldContainSubstring, "zot_storage_lock_latency_seconds_bucket")
|
|
|
|
})
|
|
|
|
Convey("Make a new controller with disabled metrics extension", t, func() {
|
2022-01-19 14:54:17 -05:00
|
|
|
port := test.GetFreePort()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
2022-01-26 16:52:59 -05:00
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
|
2022-03-07 03:55:12 -05:00
|
|
|
conf.Storage.RootDirectory = t.TempDir()
|
2022-01-26 16:52:59 -05:00
|
|
|
conf.Extensions = &extconf.ExtensionConfig{}
|
2021-12-28 08:29:30 -05:00
|
|
|
var disabled bool
|
2022-10-21 07:33:54 -05:00
|
|
|
conf.Extensions.Metrics = &extconf.MetricsConfig{BaseConfig: extconf.BaseConfig{Enable: &disabled}}
|
2022-01-26 16:52:59 -05:00
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
So(ctlr, ShouldNotBeNil)
|
|
|
|
|
2023-01-19 11:54:05 -05:00
|
|
|
cm := test.NewControllerManager(ctlr)
|
|
|
|
cm.StartAndWait(port)
|
|
|
|
defer cm.StopServer()
|
2022-01-26 16:52:59 -05:00
|
|
|
|
|
|
|
So(ctlr.Metrics.IsEnabled(), ShouldBeFalse)
|
|
|
|
|
|
|
|
resp, err := resty.R().Get(baseURL + "/metrics")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, http.StatusNotFound)
|
|
|
|
})
|
|
|
|
}
|