From 84bb33e062c4c7b707ba72444e67d4490e2b3de4 Mon Sep 17 00:00:00 2001 From: Douglas Camata <159076+douglascamata@users.noreply.github.com> Date: Tue, 30 May 2023 09:52:26 +0200 Subject: [PATCH] Add a static metrics server (#67) --- monitoring/monitoring.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/monitoring/monitoring.go b/monitoring/monitoring.go index 373cb7f..571903f 100644 --- a/monitoring/monitoring.go +++ b/monitoring/monitoring.go @@ -362,3 +362,27 @@ func newCadvisor(env e2e.Environment, name string, cgroupPrefixes ...string) *In Privileged: true, }), "http") } + +const nginxImage = "docker.io/nginx:1.21.1-alpine" + +// NewStaticMetricsServer creates a new nginx server that serves the content of metrics as /metrics endpoint. +// This is useful for testing different metrics scrapers. +func NewStaticMetricsServer(e e2e.Environment, name string, metrics []byte) *InstrumentedRunnable { + f := e.Runnable(name).WithPorts(map[string]int{"http": 80}).Future() + if err := os.MkdirAll(f.Dir(), 0750); err != nil { + return &InstrumentedRunnable{Runnable: e2e.NewFailedRunnable(name, errors.Wrap(err, "create static metrics dir"))} + } + metricsFilePath := filepath.Join(f.Dir(), "metrics.txt") + if err := os.WriteFile(metricsFilePath, metrics, 0644); err != nil { + return &InstrumentedRunnable{Runnable: e2e.NewFailedRunnable(name, errors.Wrap(err, "creating static metrics file"))} + } + probe := e2e.NewHTTPReadinessProbe("http", "/metrics", 200, 200) + return AsInstrumented( + f.Init(e2e.StartOptions{ + Image: nginxImage, + Volumes: []string{metricsFilePath + ":/usr/share/nginx/html/metrics:ro"}, + Readiness: probe, + }), + "http", + ) +}