Skip to content

Commit 26259ff

Browse files
authored
Track metrics ports with prometheus (#111)
* Track metrics ports with prometheus * Add an external scrape endpoint
1 parent 9b64868 commit 26259ff

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-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),
@@ -513,6 +515,7 @@ func (o *OpReth) Run(service *service, ctx *ExContext) {
513515
"--datadir", "{{.Dir}}/data_op_reth",
514516
"--disable-discovery",
515517
"--color", "never",
518+
"--metrics", `0.0.0.0:{{Port "metrics" 9090}}`,
516519
"--port", `{{Port "rpc" 30303}}`)
517520
}
518521

internal/local_runner.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,68 @@ 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+
763+
// global scrape config
764+
scrapeConfigs = append(scrapeConfigs, map[string]interface{}{
765+
"job_name": "external",
766+
"metrics_path": "/metrics",
767+
"static_configs": []map[string]interface{}{
768+
{
769+
"targets": []string{"host.docker.internal:5555"},
770+
},
771+
},
772+
})
773+
774+
for _, c := range manifest.services {
775+
for _, port := range c.ports {
776+
if port.Name == "metrics" {
777+
metricsPath := "/metrics"
778+
if overrideMetricsPath, ok := c.labels["metrics_path"]; ok {
779+
metricsPath = overrideMetricsPath
780+
}
781+
782+
scrapeConfig := map[string]interface{}{
783+
"job_name": c.Name,
784+
"metrics_path": metricsPath,
785+
"static_configs": []map[string]interface{}{
786+
{
787+
"targets": []string{fmt.Sprintf("%s:%d", c.Name, port.Port)},
788+
},
789+
},
790+
}
791+
scrapeConfigs = append(scrapeConfigs, scrapeConfig)
792+
}
793+
}
794+
}
795+
796+
promConfig := map[string]interface{}{
797+
"global": map[string]interface{}{
798+
"scrape_interval": "1s",
799+
"evaluation_interval": "1s",
800+
},
801+
"scrape_configs": scrapeConfigs,
802+
}
803+
804+
if err := out.WriteFile("prometheus.yaml", promConfig); err != nil {
805+
return fmt.Errorf("failed to write prometheus.yml: %w", err)
806+
}
807+
808+
// add to the manifest the prometheus service
809+
// This is a bit of a hack.
810+
srv := manifest.NewService("prometheus").
811+
WithImage("prom/prometheus").
812+
WithTag("latest").
813+
WithArgs("--config.file", "{{.Dir}}/prometheus.yaml").
814+
WithPort("metrics", 9090, "tcp")
815+
manifest.services = append(manifest.services, srv)
816+
817+
return nil
818+
}
819+
758820
func (d *LocalRunner) Run() error {
759821
go d.trackContainerStatusAndLogs()
760822

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)