From 6966e42d5df10db392b01027a8dcc6ab84a90924 Mon Sep 17 00:00:00 2001 From: hvanz Date: Mon, 12 Jun 2023 11:08:27 +0200 Subject: [PATCH 1/4] Generate prometheus yaml on setup --- test/e2e/pkg/testnet.go | 36 ++++++++++++++++++++++++++++++++++++ test/e2e/runner/setup.go | 6 ++++++ 2 files changed, 42 insertions(+) diff --git a/test/e2e/pkg/testnet.go b/test/e2e/pkg/testnet.go index fcf34d64b6b..a39fc8fc2ec 100644 --- a/test/e2e/pkg/testnet.go +++ b/test/e2e/pkg/testnet.go @@ -1,14 +1,17 @@ package e2e import ( + "bytes" "errors" "fmt" "io" "math/rand" "net" + "os" "path/filepath" "strconv" "strings" + "text/template" "time" "github.com/cometbft/cometbft/crypto" @@ -482,6 +485,39 @@ func (t Testnet) HasPerturbations() bool { return false } +func (t Testnet) prometheusConfigBytes() ([]byte, error) { + tmpl, err := template.New("prometheus-yaml").Parse(`global: + scrape_interval: 1s + +scrape_configs: +{{- range .Nodes }} + - job_name: '{{ .Name }}' + static_configs: + - targets: ['localhost:{{ .PrometheusProxyPort }}'] +{{end}}`) + if err != nil { + return nil, err + } + var buf bytes.Buffer + err = tmpl.Execute(&buf, t) + if err != nil { + return nil, err + } + return buf.Bytes(), nil +} + +func (t Testnet) WritePrometheusConfig() error { + bytes, err := t.prometheusConfigBytes() + if err != nil { + return err + } + err = os.WriteFile(filepath.Join(t.Dir, "prometheus.yml"), bytes, 0o644) //nolint:gosec + if err != nil { + return err + } + return nil +} + // Address returns a P2P endpoint address for the node. func (n Node) AddressP2P(withID bool) string { ip := n.InternalIP.String() diff --git a/test/e2e/runner/setup.go b/test/e2e/runner/setup.go index 7075ecbfb74..b2c898fb98e 100644 --- a/test/e2e/runner/setup.go +++ b/test/e2e/runner/setup.go @@ -116,6 +116,12 @@ func Setup(testnet *e2e.Testnet, infp infra.Provider) error { )).Save() } + if testnet.Prometheus { + if err := testnet.WritePrometheusConfig(); err != nil { + return err + } + } + return nil } From 35d4a48a3643b979a3bf5847d2d38547b4d0dfa8 Mon Sep 17 00:00:00 2001 From: hvanz Date: Mon, 12 Jun 2023 19:48:28 +0200 Subject: [PATCH 2/4] change file extension --- test/e2e/pkg/testnet.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/pkg/testnet.go b/test/e2e/pkg/testnet.go index a39fc8fc2ec..6801cf5e865 100644 --- a/test/e2e/pkg/testnet.go +++ b/test/e2e/pkg/testnet.go @@ -511,7 +511,7 @@ func (t Testnet) WritePrometheusConfig() error { if err != nil { return err } - err = os.WriteFile(filepath.Join(t.Dir, "prometheus.yml"), bytes, 0o644) //nolint:gosec + err = os.WriteFile(filepath.Join(t.Dir, "prometheus.yaml"), bytes, 0o644) //nolint:gosec if err != nil { return err } From 9416162b7f7d13cdb1bc17cb43fb7e4b205123a9 Mon Sep 17 00:00:00 2001 From: hvanz Date: Mon, 12 Jun 2023 20:19:35 +0200 Subject: [PATCH 3/4] move template to file --- test/e2e/pkg/prometheus-yaml.tmpl | 9 +++++++++ test/e2e/pkg/testnet.go | 15 ++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) create mode 100644 test/e2e/pkg/prometheus-yaml.tmpl diff --git a/test/e2e/pkg/prometheus-yaml.tmpl b/test/e2e/pkg/prometheus-yaml.tmpl new file mode 100644 index 00000000000..3c7636e0ddc --- /dev/null +++ b/test/e2e/pkg/prometheus-yaml.tmpl @@ -0,0 +1,9 @@ +global: + scrape_interval: 1s + +scrape_configs: +{{- range .Nodes }} + - job_name: '{{ .Name }}' + static_configs: + - targets: ['localhost:{{ .PrometheusProxyPort }}'] +{{end}} \ No newline at end of file diff --git a/test/e2e/pkg/testnet.go b/test/e2e/pkg/testnet.go index 6801cf5e865..640a6852665 100644 --- a/test/e2e/pkg/testnet.go +++ b/test/e2e/pkg/testnet.go @@ -18,8 +18,13 @@ import ( "github.com/cometbft/cometbft/crypto/ed25519" "github.com/cometbft/cometbft/crypto/secp256k1" rpchttp "github.com/cometbft/cometbft/rpc/client/http" + + _ "embed" ) +//go:embed prometheus-yaml.tmpl +var prometheusYamlTemplate string + const ( randomSeed int64 = 2308084734268 proxyPortFirst uint32 = 5701 @@ -486,15 +491,7 @@ func (t Testnet) HasPerturbations() bool { } func (t Testnet) prometheusConfigBytes() ([]byte, error) { - tmpl, err := template.New("prometheus-yaml").Parse(`global: - scrape_interval: 1s - -scrape_configs: -{{- range .Nodes }} - - job_name: '{{ .Name }}' - static_configs: - - targets: ['localhost:{{ .PrometheusProxyPort }}'] -{{end}}`) + tmpl, err := template.New("prometheus-yaml").Parse(prometheusYamlTemplate) if err != nil { return nil, err } From 832dc21b800cfddacad76862777ba896ead81c5d Mon Sep 17 00:00:00 2001 From: hvanz Date: Mon, 12 Jun 2023 21:41:15 +0200 Subject: [PATCH 4/4] add templates dir --- test/e2e/pkg/{ => templates}/prometheus-yaml.tmpl | 0 test/e2e/pkg/testnet.go | 6 +++--- 2 files changed, 3 insertions(+), 3 deletions(-) rename test/e2e/pkg/{ => templates}/prometheus-yaml.tmpl (100%) diff --git a/test/e2e/pkg/prometheus-yaml.tmpl b/test/e2e/pkg/templates/prometheus-yaml.tmpl similarity index 100% rename from test/e2e/pkg/prometheus-yaml.tmpl rename to test/e2e/pkg/templates/prometheus-yaml.tmpl diff --git a/test/e2e/pkg/testnet.go b/test/e2e/pkg/testnet.go index 640a6852665..baf18dc5f3a 100644 --- a/test/e2e/pkg/testnet.go +++ b/test/e2e/pkg/testnet.go @@ -22,9 +22,6 @@ import ( _ "embed" ) -//go:embed prometheus-yaml.tmpl -var prometheusYamlTemplate string - const ( randomSeed int64 = 2308084734268 proxyPortFirst uint32 = 5701 @@ -490,6 +487,9 @@ func (t Testnet) HasPerturbations() bool { return false } +//go:embed templates/prometheus-yaml.tmpl +var prometheusYamlTemplate string + func (t Testnet) prometheusConfigBytes() ([]byte, error) { tmpl, err := template.New("prometheus-yaml").Parse(prometheusYamlTemplate) if err != nil {