Skip to content

Commit

Permalink
Expose captive core version details (#5332)
Browse files Browse the repository at this point in the history
* Expose captive core version details

* Log error instead of terminating the process

* Fetch core-version using stellar-core version

* fix formatting

* Fix review comments

* Use log from config
  • Loading branch information
psheth9 authored Jun 14, 2024
1 parent d62fa1d commit 3a31ed7
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions ingest/ledgerbackend/captive_core_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"net/http"
"os"
"os/exec"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -110,8 +112,9 @@ type CaptiveStellarCore struct {
lastLedger *uint32 // end of current segment if offline, nil if online
previousLedgerHash *string

config CaptiveCoreConfig
stellarCoreClient *stellarcore.Client
config CaptiveCoreConfig
stellarCoreClient *stellarcore.Client
captiveCoreVersion string // Updates when captive-core restarts
}

// CaptiveCoreConfig contains all the parameters required to create a CaptiveStellarCore instance
Expand Down Expand Up @@ -200,6 +203,7 @@ func NewCaptive(config CaptiveCoreConfig) (*CaptiveStellarCore, error) {
}

c.stellarCoreRunnerFactory = func() stellarCoreRunnerInterface {
c.setCoreVersion()
return newStellarCoreRunner(config)
}

Expand All @@ -211,7 +215,7 @@ func NewCaptive(config CaptiveCoreConfig) (*CaptiveStellarCore, error) {
URL: fmt.Sprintf("http://localhost:%d", config.Toml.HTTPPort),
}
}

c.setCoreVersion()
return c, nil
}

Expand Down Expand Up @@ -245,6 +249,35 @@ func (c *CaptiveStellarCore) coreVersionMetric() float64 {
return float64(info.Info.ProtocolVersion)
}

// By default, it points to exec.Command, overridden for testing purpose
var execCommand = exec.Command

// Executes the "stellar-core version" command and parses its output to extract
// the core version
// The output of the "version" command is expected to be a multi-line string where the
// first line is the core version in format "vX.Y.Z-*".
func (c *CaptiveStellarCore) setCoreVersion() {
versionCmd := execCommand(c.config.BinaryPath, "version")
versionOutput, err := versionCmd.Output()
if err != nil {
c.config.Log.Errorf("failed to execute stellar-core version command: %s", err)
}

// Split the output into lines
rows := strings.Split(string(versionOutput), "\n")
if len(rows) == 0 || len(rows[0]) == 0 {
c.config.Log.Error("stellar-core version not found")
return
}

c.captiveCoreVersion = rows[0]
c.config.Log.Infof("stellar-core version: %s", c.captiveCoreVersion)
}

func (c *CaptiveStellarCore) GetCoreVersion() string {
return c.captiveCoreVersion
}

func (c *CaptiveStellarCore) registerMetrics(registry *prometheus.Registry, namespace string) {
coreSynced := prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Expand Down

0 comments on commit 3a31ed7

Please sign in to comment.