Skip to content

Commit

Permalink
services/horizon: Add Stellar-Core sync metrics (#3418)
Browse files Browse the repository at this point in the history
This commit adds two new metrics connected to Stellar-Core:

* `horizon_ ingest_captive_stellar_core_synced` determines if Captive
Stellar-Core instance started by ingesting server is synced.
* `horizon_stellar_core_synced` determines if Stellar-Core defined by
`--stellar-core-url` is synced.

The reason for adding two separate metrics is that in some deployments
Stellar-Core connected via `--stellar-core-url` and Captive Stellar-Core are
separate instances.
  • Loading branch information
bartekn authored Feb 22, 2021
1 parent 5d821ab commit d5d8527
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions services/horizon/internal/actions/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

type CoreSettings struct {
Synced bool
CurrentProtocolVersion int32
CoreSupportedProtocolVersion int32
CoreVersion string
Expand Down
2 changes: 2 additions & 0 deletions services/horizon/internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type coreSettingsStore struct {
func (c *coreSettingsStore) set(resp *proto.InfoResponse) {
c.Lock()
defer c.Unlock()
c.Synced = resp.IsSynced()
c.CoreVersion = resp.Info.Build
c.CurrentProtocolVersion = int32(resp.Info.Ledger.Version)
c.CoreSupportedProtocolVersion = int32(resp.Info.ProtocolVersion)
Expand Down Expand Up @@ -80,6 +81,7 @@ type App struct {
dbWaitCountCounter prometheus.CounterFunc
dbWaitDurationCounter prometheus.CounterFunc
coreLatestLedgerCounter prometheus.CounterFunc
coreSynced prometheus.GaugeFunc
}

func (a *App) GetCoreSettings() actions.CoreSettings {
Expand Down
37 changes: 37 additions & 0 deletions services/horizon/internal/ingest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package ingest

import (
"context"
"fmt"
"net/http"
"sync"
"time"

Expand Down Expand Up @@ -116,6 +118,10 @@ type Metrics struct {

// ProcessorsRunDuration exposes processors run durations.
ProcessorsRunDuration *prometheus.CounterVec

// CaptiveStellarCoreSynced exposes synced status of Captive Stellar-Core.
// 1 if sync, 0 if not synced, -1 if unable to connect or HTTP server disabled.
CaptiveStellarCoreSynced prometheus.GaugeFunc
}

type System interface {
Expand Down Expand Up @@ -293,6 +299,37 @@ func (s *system) initMetrics() {
},
[]string{"name"},
)

s.metrics.CaptiveStellarCoreSynced = prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: "horizon", Subsystem: "ingest", Name: "captive_stellar_core_synced",
Help: "1 if sync, 0 if not synced, -1 if unable to connect or HTTP server disabled.",
},
func() float64 {
if !s.config.EnableCaptiveCore || s.config.CaptiveCoreHTTPPort == 0 {
return -1
}

client := stellarcore.Client{
HTTP: &http.Client{
Timeout: 2 * time.Second,
},
URL: fmt.Sprintf("http://localhost:%d", s.config.CaptiveCoreHTTPPort),
}

info, err := client.Info(s.ctx)
if err != nil {
log.WithError(err).Error("Cannot connect to Captive Stellar-Core HTTP server")
return -1
}

if info.IsSynced() {
return 1
} else {
return 0
}
},
)
}

func (s *system) Metrics() Metrics {
Expand Down
16 changes: 16 additions & 0 deletions services/horizon/internal/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,21 @@ func initDbMetrics(app *App) {
)
app.prometheusRegistry.MustRegister(app.coreLatestLedgerCounter)

app.coreSynced = prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: "horizon", Subsystem: "stellar_core", Name: "synced",
Help: "determines if Stellar-Core defined by --stellar-core-url is synced with the network",
},
func() float64 {
if app.coreSettings.Synced {
return 1
} else {
return 0
}
},
)
app.prometheusRegistry.MustRegister(app.coreSynced)

app.dbMaxOpenConnectionsGauge = prometheus.NewGaugeFunc(
prometheus.GaugeOpts{Namespace: "horizon", Subsystem: "db", Name: "max_open_connections"},
func() float64 {
Expand Down Expand Up @@ -261,6 +276,7 @@ func initIngestMetrics(app *App) {
app.prometheusRegistry.MustRegister(app.ingester.Metrics().StateInvalidGauge)
app.prometheusRegistry.MustRegister(app.ingester.Metrics().LedgerStatsCounter)
app.prometheusRegistry.MustRegister(app.ingester.Metrics().ProcessorsRunDuration)
app.prometheusRegistry.MustRegister(app.ingester.Metrics().CaptiveStellarCoreSynced)
}

func initTxSubMetrics(app *App) {
Expand Down

0 comments on commit d5d8527

Please sign in to comment.