@@ -8,12 +8,16 @@ import (
8
8
_ "net/http/pprof"
9
9
"time"
10
10
11
+ "github.com/go-chi/chi"
12
+ chimiddleware "github.com/go-chi/chi/middleware"
13
+ "github.com/prometheus/client_golang/prometheus"
11
14
"github.com/spf13/cobra"
12
15
"github.com/spf13/viper"
13
16
14
17
"github.com/stellar/go/historyarchive"
15
18
horizon "github.com/stellar/go/services/horizon/internal"
16
19
"github.com/stellar/go/services/horizon/internal/db2/history"
20
+ "github.com/stellar/go/services/horizon/internal/httpx"
17
21
"github.com/stellar/go/services/horizon/internal/ingest"
18
22
"github.com/stellar/go/support/config"
19
23
support "github.com/stellar/go/support/config"
@@ -23,7 +27,7 @@ import (
23
27
24
28
var ingestBuildStateSequence uint32
25
29
var ingestBuildStateSkipChecks bool
26
- var ingestVerifyFrom , ingestVerifyTo , ingestVerifyDebugServerPort uint32
30
+ var ingestVerifyFrom , ingestVerifyTo uint32
27
31
var ingestVerifyState bool
28
32
var ingestVerifyStorageBackendConfigPath string
29
33
var ingestVerifyLedgerBackendType ingest.LedgerBackendType
@@ -73,14 +77,6 @@ var ingestVerifyRangeCmdOpts = support.ConfigOptions{
73
77
FlagDefault : false ,
74
78
Usage : "[optional] verifies state at the last ledger of the range when true" ,
75
79
},
76
- {
77
- Name : "debug-server-port" ,
78
- ConfigKey : & ingestVerifyDebugServerPort ,
79
- OptType : types .Uint32 ,
80
- Required : false ,
81
- FlagDefault : uint32 (0 ),
82
- Usage : "[optional] opens a net/http/pprof server at given port" ,
83
- },
84
80
generateLedgerBackendOpt (& ingestVerifyLedgerBackendType ),
85
81
generateDatastoreConfigOpt (& ingestVerifyStorageBackendConfigPath ),
86
82
}
@@ -159,19 +155,6 @@ func DefineIngestCommands(rootCmd *cobra.Command, horizonConfig *horizon.Config,
159
155
return err
160
156
}
161
157
162
- if ingestVerifyDebugServerPort != 0 {
163
- go func () {
164
- log .Infof ("Starting debug server at: %d" , ingestVerifyDebugServerPort )
165
- err := http .ListenAndServe (
166
- fmt .Sprintf ("localhost:%d" , ingestVerifyDebugServerPort ),
167
- nil ,
168
- )
169
- if err != nil {
170
- log .Error (err )
171
- }
172
- }()
173
- }
174
-
175
158
mngr := historyarchive .NewCheckpointManager (horizonConfig .CheckpointFrequency )
176
159
if ! mngr .IsCheckpoint (ingestVerifyFrom ) && ingestVerifyFrom != 1 {
177
160
return fmt .Errorf ("`--from` must be a checkpoint ledger" )
@@ -249,17 +232,12 @@ func DefineIngestCommands(rootCmd *cobra.Command, horizonConfig *horizon.Config,
249
232
if err != nil {
250
233
return err
251
234
}
252
-
253
- err = system .StressTest (
254
- stressTestNumTransactions ,
255
- stressTestChangesPerTransaction ,
256
- )
257
- if err != nil {
258
- return err
259
- }
260
-
261
- log .Info ("Stress test completed successfully!" )
262
- return nil
235
+ return runWithMetrics (horizonConfig .AdminPort , system , func () error {
236
+ return system .StressTest (
237
+ stressTestNumTransactions ,
238
+ stressTestChangesPerTransaction ,
239
+ )
240
+ })
263
241
},
264
242
}
265
243
@@ -365,16 +343,12 @@ func DefineIngestCommands(rootCmd *cobra.Command, horizonConfig *horizon.Config,
365
343
return err
366
344
}
367
345
368
- err = system .BuildState (
369
- ingestBuildStateSequence ,
370
- ingestBuildStateSkipChecks ,
371
- )
372
- if err != nil {
373
- return err
374
- }
375
-
376
- log .Info ("State built successfully!" )
377
- return nil
346
+ return runWithMetrics (horizonConfig .AdminPort , system , func () error {
347
+ return system .BuildState (
348
+ ingestBuildStateSequence ,
349
+ ingestBuildStateSkipChecks ,
350
+ )
351
+ })
378
352
},
379
353
}
380
354
@@ -444,11 +418,13 @@ func DefineIngestCommands(rootCmd *cobra.Command, horizonConfig *horizon.Config,
444
418
return err
445
419
}
446
420
447
- return system .LoadTest (
448
- ingestionLoadTestLedgersPath ,
449
- ingestionLoadTestCloseDuration ,
450
- ingestionLoadTestFixturesPath ,
451
- )
421
+ return runWithMetrics (horizonConfig .AdminPort , system , func () error {
422
+ return system .LoadTest (
423
+ ingestionLoadTestLedgersPath ,
424
+ ingestionLoadTestCloseDuration ,
425
+ ingestionLoadTestFixturesPath ,
426
+ )
427
+ })
452
428
},
453
429
}
454
430
@@ -496,6 +472,46 @@ func DefineIngestCommands(rootCmd *cobra.Command, horizonConfig *horizon.Config,
496
472
)
497
473
}
498
474
475
+ func runWithMetrics (metricsPort uint , system ingest.System , f func () error ) error {
476
+ if metricsPort != 0 {
477
+ log .Infof ("Starting metrics server at: %d" , metricsPort )
478
+ mux := chi .NewMux ()
479
+ mux .Use (chimiddleware .StripSlashes )
480
+ mux .Use (chimiddleware .RequestID )
481
+ mux .Use (chimiddleware .RequestLogger (& chimiddleware.DefaultLogFormatter {
482
+ Logger : log .DefaultLogger ,
483
+ NoColor : true ,
484
+ }))
485
+ registry := prometheus .NewRegistry ()
486
+ system .RegisterMetrics (registry )
487
+ httpx .AddMetricRoutes (mux , registry )
488
+ metricsServer := & http.Server {
489
+ Addr : fmt .Sprintf (":%d" , metricsPort ),
490
+ Handler : mux ,
491
+ ReadTimeout : 5 * time .Second ,
492
+ }
493
+ go func () {
494
+ if err := metricsServer .ListenAndServe (); err != nil && err != http .ErrServerClosed {
495
+ log .Fatalf ("error running metrics server: %v" , err )
496
+ }
497
+ }()
498
+ defer func () {
499
+ log .Info ("Waiting for metrics to be flushed" )
500
+ // by default, the scrape_interval for prometheus is 1 minute
501
+ // so if we sleep for 1.5 minutes we ensure that all remaining metrics
502
+ // will be picked up by the prometheus scraper
503
+ time .Sleep (time .Minute + time .Second * 30 )
504
+ log .Info ("Shutting down metrics server..." )
505
+ if err := metricsServer .Shutdown (context .Background ()); err != nil {
506
+ log .Warnf ("error shutting down metrics server: %v" , err )
507
+ }
508
+ }()
509
+ } else {
510
+ log .Info ("Metrics server disabled" )
511
+ }
512
+ return f ()
513
+ }
514
+
499
515
func init () {
500
516
DefineIngestCommands (RootCmd , globalConfig , globalFlags )
501
517
}
@@ -525,11 +541,13 @@ func processVerifyRange(horizonConfig *horizon.Config, horizonFlags config.Confi
525
541
return err
526
542
}
527
543
528
- return system .VerifyRange (
529
- ingestVerifyFrom ,
530
- ingestVerifyTo ,
531
- ingestVerifyState ,
532
- )
544
+ return runWithMetrics (horizonConfig .AdminPort , system , func () error {
545
+ return system .VerifyRange (
546
+ ingestVerifyFrom ,
547
+ ingestVerifyTo ,
548
+ ingestVerifyState ,
549
+ )
550
+ })
533
551
}
534
552
535
553
// generateDatastoreConfigOpt returns a *support.ConfigOption for the datastore-config flag
0 commit comments