Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

services/horizon: Export ledger stats in /metrics, add claimable_balance to change stats #3148

Merged
merged 6 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions ingest/io/stats_change_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ type StatsChangeProcessorResults struct {
AccountsUpdated int64
AccountsRemoved int64

ClaimableBalancesCreated int64
ClaimableBalancesUpdated int64
ClaimableBalancesRemoved int64

DataCreated int64
DataUpdated int64
DataRemoved int64
Expand All @@ -40,6 +44,15 @@ func (p *StatsChangeProcessor) ProcessChange(change Change) error {
case xdr.LedgerEntryChangeTypeLedgerEntryRemoved:
p.results.AccountsRemoved++
}
case xdr.LedgerEntryTypeClaimableBalance:
switch change.LedgerEntryChangeType() {
case xdr.LedgerEntryChangeTypeLedgerEntryCreated:
p.results.ClaimableBalancesCreated++
case xdr.LedgerEntryChangeTypeLedgerEntryUpdated:
p.results.ClaimableBalancesUpdated++
case xdr.LedgerEntryChangeTypeLedgerEntryRemoved:
p.results.ClaimableBalancesRemoved++
}
case xdr.LedgerEntryTypeData:
switch change.LedgerEntryChangeType() {
case xdr.LedgerEntryChangeTypeLedgerEntryCreated:
Expand Down Expand Up @@ -82,6 +95,10 @@ func (stats *StatsChangeProcessorResults) Map() map[string]interface{} {
"stats_accounts_updated": stats.AccountsUpdated,
"stats_accounts_removed": stats.AccountsRemoved,

"stats_claimable_balances_created": stats.ClaimableBalancesCreated,
"stats_claimable_balances_updated": stats.ClaimableBalancesUpdated,
"stats_claimable_balances_removed": stats.ClaimableBalancesRemoved,
Copy link
Contributor

@Shaptic Shaptic Oct 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to extend the CB integration tests to check these new fields, or do you think unit tests are sufficient?

Copy link
Contributor Author

@bartekn bartekn Oct 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we have any tests for this so far. If we want to add some integration tests, I'd vote for adding this in another PR.


"stats_data_created": stats.DataCreated,
"stats_data_updated": stats.DataUpdated,
"stats_data_removed": stats.DataRemoved,
Expand Down
21 changes: 21 additions & 0 deletions ingest/io/stats_change_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ func TestStatsChangeProcessor(t *testing.T) {
Post: &xdr.LedgerEntry{},
}))

assert.NoError(t, processor.ProcessChange(Change{
Type: xdr.LedgerEntryTypeClaimableBalance,
Pre: nil,
Post: &xdr.LedgerEntry{},
}))

assert.NoError(t, processor.ProcessChange(Change{
Type: xdr.LedgerEntryTypeData,
Pre: nil,
Expand All @@ -42,6 +48,12 @@ func TestStatsChangeProcessor(t *testing.T) {
Post: &xdr.LedgerEntry{},
}))

assert.NoError(t, processor.ProcessChange(Change{
Type: xdr.LedgerEntryTypeClaimableBalance,
Pre: &xdr.LedgerEntry{},
Post: &xdr.LedgerEntry{},
}))

assert.NoError(t, processor.ProcessChange(Change{
Type: xdr.LedgerEntryTypeData,
Pre: &xdr.LedgerEntry{},
Expand All @@ -67,6 +79,12 @@ func TestStatsChangeProcessor(t *testing.T) {
Post: nil,
}))

assert.NoError(t, processor.ProcessChange(Change{
Type: xdr.LedgerEntryTypeClaimableBalance,
Pre: &xdr.LedgerEntry{},
Post: nil,
}))

assert.NoError(t, processor.ProcessChange(Change{
Type: xdr.LedgerEntryTypeData,
Pre: &xdr.LedgerEntry{},
Expand All @@ -88,16 +106,19 @@ func TestStatsChangeProcessor(t *testing.T) {
results := processor.GetResults()

assert.Equal(t, int64(1), results.AccountsCreated)
assert.Equal(t, int64(1), results.ClaimableBalancesCreated)
assert.Equal(t, int64(1), results.DataCreated)
assert.Equal(t, int64(1), results.OffersCreated)
assert.Equal(t, int64(1), results.TrustLinesCreated)

assert.Equal(t, int64(1), results.AccountsUpdated)
assert.Equal(t, int64(1), results.ClaimableBalancesUpdated)
assert.Equal(t, int64(1), results.DataUpdated)
assert.Equal(t, int64(1), results.OffersUpdated)
assert.Equal(t, int64(1), results.TrustLinesUpdated)

assert.Equal(t, int64(1), results.AccountsRemoved)
assert.Equal(t, int64(1), results.ClaimableBalancesRemoved)
assert.Equal(t, int64(1), results.DataRemoved)
assert.Equal(t, int64(1), results.OffersRemoved)
assert.Equal(t, int64(1), results.TrustLinesRemoved)
Expand Down
1 change: 1 addition & 0 deletions services/horizon/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ file. This project adheres to [Semantic Versioning](http://semver.org/).x
## Unreleased

* The `service` field emitted in ingestion logs has been changed from `expingest` to `ingest`.
* Ledger stats are now exported in `/metrics` in `horizon_ingest_ledger_stats_total` metric.

## v1.10.1

Expand Down
22 changes: 20 additions & 2 deletions services/horizon/internal/ingest/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package ingest

import (
"fmt"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/stellar/go/ingest/ledgerbackend"

"github.com/stellar/go/ingest/io"
Expand Down Expand Up @@ -471,9 +473,25 @@ func (r resumeState) run(s *system) (transition, error) {

duration := time.Since(startTime).Seconds()
s.Metrics().LedgerIngestionDuration.Observe(float64(duration))

// Update stats metrics
changeStatsMap := changeStats.Map()
for stat, value := range changeStatsMap {
stat = strings.Replace(stat, "stats_", "change_", 1)
s.Metrics().LedgerStatsCounter.
With(prometheus.Labels{"type": stat}).Add(float64(value.(int64)))
}

ledgerTransactionStatsMap := ledgerTransactionStats.Map()
for stat, value := range ledgerTransactionStatsMap {
stat = strings.Replace(stat, "stats_", "ledger_", 1)
s.Metrics().LedgerStatsCounter.
With(prometheus.Labels{"type": stat}).Add(float64(value.(int64)))
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be worth to factor out the metrics-updating code? (the method is already pretty large)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@2opremio do you have something like d6f557b in mind?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's better, yeah.

log.
WithFields(changeStats.Map()).
WithFields(ledgerTransactionStats.Map()).
WithFields(changeStatsMap).
WithFields(ledgerTransactionStatsMap).
WithFields(logpkg.F{
"sequence": ingestLedger,
"duration": duration,
Expand Down
11 changes: 11 additions & 0 deletions services/horizon/internal/ingest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ type Metrics struct {
// StateInvalidGauge exposes state invalid metric. 1 if state is invalid,
// 0 otherwise.
StateInvalidGauge prometheus.GaugeFunc

// LedgerStatsCounter exposes ledger stats counters (like number of ops/changes).
LedgerStatsCounter *prometheus.CounterVec
}

type System interface {
Expand Down Expand Up @@ -250,6 +253,14 @@ func (s *system) initMetrics() {
return invalidFloat
},
)

s.metrics.LedgerStatsCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "horizon", Subsystem: "ingest", Name: "ledger_stats_total",
Help: "counters of different ledger stats",
},
[]string{"type"},
)
}

func (s *system) Metrics() Metrics {
Expand Down
1 change: 1 addition & 0 deletions services/horizon/internal/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ func initIngestMetrics(app *App) {
app.prometheusRegistry.MustRegister(app.ingester.Metrics().LedgerIngestionDuration)
app.prometheusRegistry.MustRegister(app.ingester.Metrics().StateVerifyDuration)
app.prometheusRegistry.MustRegister(app.ingester.Metrics().StateInvalidGauge)
app.prometheusRegistry.MustRegister(app.ingester.Metrics().LedgerStatsCounter)
}

func initTxSubMetrics(app *App) {
Expand Down