Skip to content

Commit 86985f7

Browse files
committed
Track metrics ports with prometheus
1 parent 4dc2150 commit 86985f7

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

internal/components.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ func (o *OpGeth) Run(service *service, ctx *ExContext) {
137137
WithImage("us-docker.pkg.dev/oplabs-tools-artifacts/images/op-geth").
138138
WithTag("v1.101503.2-rc.5").
139139
WithEntrypoint("/bin/sh").
140+
WithLabel("metrics_path", "/debug/metrics/prometheus").
140141
WithArgs(
141142
"-c",
142143
"geth init --datadir {{.Dir}}/data_opgeth --state.scheme hash {{.Dir}}/l2-genesis.json && "+
@@ -262,6 +263,7 @@ func (r *RethEL) Run(svc *service, ctx *ExContext) {
262263
"--authrpc.port", `{{Port "authrpc" 8551}}`,
263264
"--authrpc.addr", "0.0.0.0",
264265
"--authrpc.jwtsecret", "{{.Dir}}/jwtsecret",
266+
"--metrics", `0.0.0.0:{{Port "metrics" 9090}}`,
265267
// For reth version 1.2.0 the "legacy" engine was removed, so we now require these arguments:
266268
"--engine.persistence-threshold", "0", "--engine.memory-block-buffer-target", "0",
267269
logLevelToRethVerbosity(ctx.LogLevel),
@@ -512,6 +514,7 @@ func (o *OpReth) Run(service *service, ctx *ExContext) {
512514
"--datadir", "{{.Dir}}/data_op_reth",
513515
"--disable-discovery",
514516
"--color", "never",
517+
"--metrics", `0.0.0.0:{{Port "metrics" 9090}}`,
515518
"--port", `{{Port "rpc" 30303}}`)
516519
}
517520

internal/local_runner.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,56 @@ func (d *LocalRunner) trackContainerStatusAndLogs() {
755755
}
756756
}
757757

758+
func CreatePrometheusServices(manifest *Manifest, out *output) error {
759+
// Read all the components to be deployed and find all the ports with name 'metrics'
760+
// to create the prometheus scrapper config
761+
var scrapeConfigs []map[string]interface{}
762+
for _, c := range manifest.services {
763+
for _, port := range c.ports {
764+
if port.Name == "metrics" {
765+
metricsPath := "/metrics"
766+
if overrideMetricsPath, ok := c.labels["metrics_path"]; ok {
767+
metricsPath = overrideMetricsPath
768+
}
769+
770+
scrapeConfig := map[string]interface{}{
771+
"job_name": c.Name,
772+
"metrics_path": metricsPath,
773+
"static_configs": []map[string]interface{}{
774+
{
775+
"targets": []string{fmt.Sprintf("%s:%d", c.Name, port.Port)},
776+
},
777+
},
778+
}
779+
scrapeConfigs = append(scrapeConfigs, scrapeConfig)
780+
}
781+
}
782+
}
783+
784+
promConfig := map[string]interface{}{
785+
"global": map[string]interface{}{
786+
"scrape_interval": "1s",
787+
"evaluation_interval": "1s",
788+
},
789+
"scrape_configs": scrapeConfigs,
790+
}
791+
792+
if err := out.WriteFile("prometheus.yaml", promConfig); err != nil {
793+
return fmt.Errorf("failed to write prometheus.yml: %w", err)
794+
}
795+
796+
// add to the manifest the prometheus service
797+
// This is a bit of a hack.
798+
srv := manifest.NewService("prometheus").
799+
WithImage("prom/prometheus").
800+
WithTag("latest").
801+
WithArgs("--config.file", "{{.Dir}}/prometheus.yaml").
802+
WithPort("metrics", 9090, "tcp")
803+
manifest.services = append(manifest.services, srv)
804+
805+
return nil
806+
}
807+
758808
func (d *LocalRunner) Run() error {
759809
go d.trackContainerStatusAndLogs()
760810

main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var interactive bool
2525
var timeout time.Duration
2626
var logLevelFlag string
2727
var bindExternal bool
28+
var withPrometheus bool
2829

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

173175
cookCmd.AddCommand(recipeCmd)
174176
}
@@ -225,6 +227,12 @@ func runIt(recipe internal.Recipe) error {
225227
return err
226228
}
227229

230+
if withPrometheus {
231+
if err := internal.CreatePrometheusServices(svcManager, artifacts.Out); err != nil {
232+
return fmt.Errorf("failed to create prometheus services: %w", err)
233+
}
234+
}
235+
228236
if dryRun {
229237
return nil
230238
}

0 commit comments

Comments
 (0)