Skip to content

IMPROVE: improve balance related metrics #1702

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

Merged
merged 4 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
137 changes: 102 additions & 35 deletions pkg/bbgo/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,94 @@ var (
Help: "bbgo exchange session connection status",
},
[]string{
"exchange", // exchange name
"channel", // channel: user or market
"margin", // margin type: none, margin or isolated
"symbol", // margin symbol of the connection.
"session",
"exchange", // exchange name
"channel", // channel: user or market
"margin_type", // margin type: none, margin or isolated
"symbol", // margin symbol of the connection.
},
)

metricsLockedBalances = prometheus.NewGaugeVec(
metricsBalanceLockedMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_balances_locked",
Help: "bbgo exchange locked balances",
},
[]string{
"exchange", // exchange name
"margin", // margin of connection. 1 or 0
"symbol", // margin symbol of the connection.
"session",
"exchange", // exchange name
"margin_type", // margin of connection. 1 or 0
"symbol", // margin symbol of the connection.
"currency",
},
)

metricsAvailableBalances = prometheus.NewGaugeVec(
metricsBalanceAvailableMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_balances_available",
Help: "bbgo exchange available balances",
},
[]string{
"exchange", // exchange name
"margin", // margin of connection. none, margin or isolated
"symbol", // margin symbol of the connection.
"session",
"exchange", // exchange name
"margin_type", // margin of connection. none, margin or isolated
"symbol", // margin symbol of the connection.
"currency",
},
)

metricsBalanceDebtMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_balances_debt",
Help: "bbgo exchange balance debt",
},
[]string{
"session",
"exchange", // exchange name
"margin_type", // margin of connection. none, margin or isolated
"symbol", // margin symbol of the connection.
"currency",
},
)

metricsBalanceBorrowedMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_balances_borrowed",
Help: "bbgo exchange balance borrowed",
},
[]string{
"session",
"exchange", // exchange name
"margin_type", // margin of connection. none, margin or isolated
"symbol", // margin symbol of the connection.
"currency",
},
)

metricsBalanceInterestMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_balances_interest",
Help: "bbgo exchange balance interest",
},
[]string{
"session",
"exchange", // exchange name
"margin_type", // margin of connection. none, margin or isolated
"symbol", // margin symbol of the connection.
"currency",
},
)

metricsBalanceNetMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_balances_net",
Help: "bbgo exchange session total net balances",
},
[]string{
"session",
"exchange", // exchange name
"margin_type", // margin of connection. none, margin or isolated
"symbol", // margin symbol of the connection.
"currency",
},
)
Expand All @@ -48,9 +107,10 @@ var (
Help: "bbgo exchange session total balances",
},
[]string{
"exchange", // exchange name
"margin", // margin of connection. none, margin or isolated
"symbol", // margin symbol of the connection.
"session",
"exchange", // exchange name
"margin_type", // margin of connection. none, margin or isolated
"symbol", // margin symbol of the connection.
"currency",
},
)
Expand All @@ -61,11 +121,12 @@ var (
Help: "bbgo exchange session trades",
},
[]string{
"exchange", // exchange name
"margin", // margin of connection. none, margin or isolated
"symbol", // margin symbol of the connection.
"side", // side: buy or sell
"liquidity", // maker or taker
"session",
"exchange", // exchange name
"margin_type", // margin of connection. none, margin or isolated
"symbol", // margin symbol of the connection.
"side", // side: buy or sell
"liquidity", // maker or taker
},
)

Expand All @@ -75,26 +136,28 @@ var (
Help: "bbgo trading volume",
},
[]string{
"exchange", // exchange name
"margin", // margin of connection. none, margin or isolated
"symbol", // margin symbol of the connection.
"side", // side: buy or sell
"liquidity", // maker or taker
"session",
"exchange", // exchange name
"margin_type", // margin of connection. none, margin or isolated
"symbol", // margin symbol of the connection.
"side", // side: buy or sell
"liquidity", // maker or taker
},
)

metricsLastUpdateTimeBalance = prometheus.NewGaugeVec(
metricsLastUpdateTimeMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_last_update_time",
Help: "bbgo last update time of different channel",
},
[]string{
"exchange", // exchange name
"margin", // margin of connection. none, margin or isolated
"channel", // channel: user, market
"data_type", // type: balance, ticker, kline, orderbook, trade, order
"symbol", // for market data, trade and order
"currency", // for balance
"session",
"exchange", // exchange name
"margin_type", // margin of connection. none, margin or isolated
"channel", // channel: user, market
"data_type", // type: balance, ticker, kline, orderbook, trade, order
"symbol", // for market data, trade and order
"currency", // for balance
},
)
)
Expand All @@ -103,10 +166,14 @@ func init() {
prometheus.MustRegister(
metricsConnectionStatus,
metricsTotalBalances,
metricsLockedBalances,
metricsAvailableBalances,
metricsBalanceNetMetrics,
metricsBalanceLockedMetrics,
metricsBalanceAvailableMetrics,
metricsBalanceDebtMetrics,
metricsBalanceBorrowedMetrics,
metricsBalanceInterestMetrics,
metricsTradesTotal,
metricsTradingVolume,
metricsLastUpdateTimeBalance,
metricsLastUpdateTimeMetrics,
)
}
135 changes: 76 additions & 59 deletions pkg/bbgo/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,91 +886,106 @@ func (session *ExchangeSession) InitExchange(name string, ex types.Exchange) err
return nil
}

func (session *ExchangeSession) MarginType() string {
margin := "none"
func (session *ExchangeSession) MarginType() types.MarginType {
if session.Margin {
margin = "margin"
if session.IsolatedMargin {
margin = "isolated"
return types.MarginTypeIsolatedMargin
} else {
return types.MarginTypeCrossMargin
}
}
return margin

return types.MarginTypeSpot
}

func (session *ExchangeSession) metricsBalancesUpdater(balances types.BalanceMap) {
for currency, balance := range balances {
labels := prometheus.Labels{
"exchange": session.ExchangeName.String(),
"margin": session.MarginType(),
"symbol": session.IsolatedMarginSymbol,
"currency": currency,
"session": session.Name,
"exchange": session.ExchangeName.String(),
"margin_type": string(session.MarginType()),
"symbol": session.IsolatedMarginSymbol,
"currency": currency,
}

metricsTotalBalances.With(labels).Set(balance.Total().Float64())
metricsLockedBalances.With(labels).Set(balance.Locked.Float64())
metricsAvailableBalances.With(labels).Set(balance.Available.Float64())
metricsLastUpdateTimeBalance.With(prometheus.Labels{
"exchange": session.ExchangeName.String(),
"margin": session.MarginType(),
"channel": "user",
"data_type": "balance",
"symbol": "",
"currency": currency,
metricsBalanceNetMetrics.With(labels).Set(balance.Net().Float64())
metricsBalanceAvailableMetrics.With(labels).Set(balance.Available.Float64())
metricsBalanceLockedMetrics.With(labels).Set(balance.Locked.Float64())

// margin metrics
metricsBalanceDebtMetrics.With(labels).Set(balance.Debt().Float64())
metricsBalanceBorrowedMetrics.With(labels).Set(balance.Borrowed.Float64())
metricsBalanceInterestMetrics.With(labels).Set(balance.Interest.Float64())

metricsLastUpdateTimeMetrics.With(prometheus.Labels{
"session": session.Name,
"exchange": session.ExchangeName.String(),
"margin_type": string(session.MarginType()),
"channel": "user",
"data_type": "balance",
"symbol": "",
"currency": currency,
}).SetToCurrentTime()
}

}

func (session *ExchangeSession) metricsOrderUpdater(order types.Order) {
metricsLastUpdateTimeBalance.With(prometheus.Labels{
"exchange": session.ExchangeName.String(),
"margin": session.MarginType(),
"channel": "user",
"data_type": "order",
"symbol": order.Symbol,
"currency": "",
metricsLastUpdateTimeMetrics.With(prometheus.Labels{
"session": session.Name,
"exchange": session.ExchangeName.String(),
"margin_type": string(session.MarginType()),
"channel": "user",
"data_type": "order",
"symbol": order.Symbol,
"currency": "",
}).SetToCurrentTime()
}

func (session *ExchangeSession) metricsTradeUpdater(trade types.Trade) {
labels := prometheus.Labels{
"exchange": session.ExchangeName.String(),
"margin": session.MarginType(),
"side": trade.Side.String(),
"symbol": trade.Symbol,
"liquidity": trade.Liquidity(),
"session": session.Name,
"exchange": session.ExchangeName.String(),
"margin_type": string(session.MarginType()),
"side": trade.Side.String(),
"symbol": trade.Symbol,
"liquidity": trade.Liquidity(),
}
metricsTradingVolume.With(labels).Add(trade.Quantity.Mul(trade.Price).Float64())
metricsTradesTotal.With(labels).Inc()
metricsLastUpdateTimeBalance.With(prometheus.Labels{
"exchange": session.ExchangeName.String(),
"margin": session.MarginType(),
"channel": "user",
"data_type": "trade",
"symbol": trade.Symbol,
"currency": "",
metricsLastUpdateTimeMetrics.With(prometheus.Labels{
"session": session.Name,
"exchange": session.ExchangeName.String(),
"margin_type": string(session.MarginType()),
"channel": "user",
"data_type": "trade",
"symbol": trade.Symbol,
"currency": "",
}).SetToCurrentTime()
}

func (session *ExchangeSession) bindMarketDataStreamMetrics(stream types.Stream) {
stream.OnBookUpdate(func(book types.SliceOrderBook) {
metricsLastUpdateTimeBalance.With(prometheus.Labels{
"exchange": session.ExchangeName.String(),
"margin": session.MarginType(),
"channel": "market",
"data_type": "book",
"symbol": book.Symbol,
"currency": "",
metricsLastUpdateTimeMetrics.With(prometheus.Labels{
"session": session.Name,
"exchange": session.ExchangeName.String(),
"margin_type": string(session.MarginType()),
"channel": "market",
"data_type": "book",
"symbol": book.Symbol,
"currency": "",
}).SetToCurrentTime()
})
stream.OnKLineClosed(func(kline types.KLine) {
metricsLastUpdateTimeBalance.With(prometheus.Labels{
"exchange": session.ExchangeName.String(),
"margin": session.MarginType(),
"channel": "market",
"data_type": "kline",
"symbol": kline.Symbol,
"currency": "",
metricsLastUpdateTimeMetrics.With(prometheus.Labels{
"session": session.Name,
"exchange": session.ExchangeName.String(),
"margin_type": string(session.MarginType()),
"channel": "market",
"data_type": "kline",
"symbol": kline.Symbol,
"currency": "",
}).SetToCurrentTime()
})
}
Expand All @@ -982,18 +997,20 @@ func (session *ExchangeSession) bindUserDataStreamMetrics(stream types.Stream) {
stream.OnOrderUpdate(session.metricsOrderUpdater)
stream.OnDisconnect(func() {
metricsConnectionStatus.With(prometheus.Labels{
"channel": "user",
"exchange": session.ExchangeName.String(),
"margin": session.MarginType(),
"symbol": session.IsolatedMarginSymbol,
"channel": "user",
"session": session.Name,
"exchange": session.ExchangeName.String(),
"margin_type": string(session.MarginType()),
"symbol": session.IsolatedMarginSymbol,
}).Set(0.0)
})
stream.OnConnect(func() {
metricsConnectionStatus.With(prometheus.Labels{
"channel": "user",
"exchange": session.ExchangeName.String(),
"margin": session.MarginType(),
"symbol": session.IsolatedMarginSymbol,
"channel": "user",
"session": session.Name,
"exchange": session.ExchangeName.String(),
"margin_type": string(session.MarginType()),
"symbol": session.IsolatedMarginSymbol,
}).Set(1.0)
})
}
Expand Down
Loading
Loading