Skip to content

Track metrics ports with prometheus #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func (o *OpGeth) Run(service *service, ctx *ExContext) {
WithImage("us-docker.pkg.dev/oplabs-tools-artifacts/images/op-geth").
WithTag("v1.101503.2-rc.5").
WithEntrypoint("/bin/sh").
WithLabel("metrics_path", "/debug/metrics/prometheus").
WithArgs(
"-c",
"geth init --datadir {{.Dir}}/data_opgeth --state.scheme hash {{.Dir}}/l2-genesis.json && "+
Expand Down Expand Up @@ -262,6 +263,7 @@ func (r *RethEL) Run(svc *service, ctx *ExContext) {
"--authrpc.port", `{{Port "authrpc" 8551}}`,
"--authrpc.addr", "0.0.0.0",
"--authrpc.jwtsecret", "{{.Dir}}/jwtsecret",
"--metrics", `0.0.0.0:{{Port "metrics" 9090}}`,
// For reth version 1.2.0 the "legacy" engine was removed, so we now require these arguments:
"--engine.persistence-threshold", "0", "--engine.memory-block-buffer-target", "0",
logLevelToRethVerbosity(ctx.LogLevel),
Expand Down Expand Up @@ -512,6 +514,7 @@ func (o *OpReth) Run(service *service, ctx *ExContext) {
"--datadir", "{{.Dir}}/data_op_reth",
"--disable-discovery",
"--color", "never",
"--metrics", `0.0.0.0:{{Port "metrics" 9090}}`,
"--port", `{{Port "rpc" 30303}}`)
}

Expand Down
62 changes: 62 additions & 0 deletions internal/local_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,68 @@ func (d *LocalRunner) trackContainerStatusAndLogs() {
}
}

func CreatePrometheusServices(manifest *Manifest, out *output) error {
// Read all the components to be deployed and find all the ports with name 'metrics'
// to create the prometheus scrapper config
var scrapeConfigs []map[string]interface{}

// global scrape config
scrapeConfigs = append(scrapeConfigs, map[string]interface{}{
"job_name": "external",
"metrics_path": "/metrics",
"static_configs": []map[string]interface{}{
{
"targets": []string{"host.docker.internal:5555"},
},
},
})

for _, c := range manifest.services {
for _, port := range c.ports {
if port.Name == "metrics" {
metricsPath := "/metrics"
if overrideMetricsPath, ok := c.labels["metrics_path"]; ok {
metricsPath = overrideMetricsPath
}

scrapeConfig := map[string]interface{}{
"job_name": c.Name,
"metrics_path": metricsPath,
"static_configs": []map[string]interface{}{
{
"targets": []string{fmt.Sprintf("%s:%d", c.Name, port.Port)},
},
},
}
scrapeConfigs = append(scrapeConfigs, scrapeConfig)
}
}
}

promConfig := map[string]interface{}{
"global": map[string]interface{}{
"scrape_interval": "1s",
"evaluation_interval": "1s",
},
"scrape_configs": scrapeConfigs,
}

if err := out.WriteFile("prometheus.yaml", promConfig); err != nil {
return fmt.Errorf("failed to write prometheus.yml: %w", err)
}

// add to the manifest the prometheus service
// This is a bit of a hack.
srv := manifest.NewService("prometheus").
WithImage("prom/prometheus").
WithTag("latest").
WithArgs("--config.file", "{{.Dir}}/prometheus.yaml").
WithPort("metrics", 9090, "tcp")
manifest.services = append(manifest.services, srv)

return nil
}

func (d *LocalRunner) Run() error {
go d.trackContainerStatusAndLogs()

Expand Down
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var interactive bool
var timeout time.Duration
var logLevelFlag string
var bindExternal bool
var withPrometheus bool

var rootCmd = &cobra.Command{
Use: "playground",
Expand Down Expand Up @@ -169,6 +170,7 @@ func main() {
recipeCmd.Flags().DurationVar(&timeout, "timeout", 0, "") // Used for CI
recipeCmd.Flags().StringVar(&logLevelFlag, "log-level", "info", "log level")
recipeCmd.Flags().BoolVar(&bindExternal, "bind-external", false, "bind host ports to external interface")
recipeCmd.Flags().BoolVar(&withPrometheus, "with-prometheus", false, "whether to gather the Prometheus metrics")

cookCmd.AddCommand(recipeCmd)
}
Expand Down Expand Up @@ -225,6 +227,12 @@ func runIt(recipe internal.Recipe) error {
return err
}

if withPrometheus {
if err := internal.CreatePrometheusServices(svcManager, artifacts.Out); err != nil {
return fmt.Errorf("failed to create prometheus services: %w", err)
}
}

if dryRun {
return nil
}
Expand Down