Skip to content

Commit

Permalink
e2e: Generate prometheus.yaml on setup (#954) (#957)
Browse files Browse the repository at this point in the history
* Generate prometheus yaml on setup

* change file extension

* move template to file

* add templates dir

(cherry picked from commit 3701c9f3c1a12a0ce35cef98788bdebb382bd74e)

Co-authored-by: Hernán Vanzetto <[email protected]>
  • Loading branch information
mergify[bot] and hvanz authored Jun 13, 2023
1 parent 587522f commit 9cbdef8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
9 changes: 9 additions & 0 deletions test/e2e/pkg/templates/prometheus-yaml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
global:
scrape_interval: 1s

scrape_configs:
{{- range .Nodes }}
- job_name: '{{ .Name }}'
static_configs:
- targets: ['localhost:{{ .PrometheusProxyPort }}']
{{end}}
33 changes: 33 additions & 0 deletions test/e2e/pkg/testnet.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package e2e

import (
"bytes"
"errors"
"fmt"
"io"
"math/rand"
"net"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"text/template"
"time"

"github.com/cometbft/cometbft/crypto"
"github.com/cometbft/cometbft/crypto/ed25519"
"github.com/cometbft/cometbft/crypto/secp256k1"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"

_ "embed"
)

const (
Expand Down Expand Up @@ -470,6 +475,34 @@ 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 {
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.yaml"), 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.IP.String()
Expand Down
6 changes: 6 additions & 0 deletions test/e2e/runner/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 9cbdef8

Please sign in to comment.