add more balance metrics

This commit is contained in:
c9s 2024-08-21 15:33:27 +08:00
parent 40d3a40277
commit fead99aaa6
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 149 additions and 86 deletions

View File

@ -9,35 +9,87 @@ var (
Help: "bbgo exchange session connection status", Help: "bbgo exchange session connection status",
}, },
[]string{ []string{
"exchange", // exchange name "exchange", // exchange name
"channel", // channel: user or market "channel", // channel: user or market
"margin", // margin type: none, margin or isolated "margin_type", // margin type: none, margin or isolated
"symbol", // margin symbol of the connection. "symbol", // margin symbol of the connection.
}, },
) )
metricsLockedBalances = prometheus.NewGaugeVec( metricsBalanceLockedMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{ prometheus.GaugeOpts{
Name: "bbgo_balances_locked", Name: "bbgo_balances_locked",
Help: "bbgo exchange locked balances", Help: "bbgo exchange locked balances",
}, },
[]string{ []string{
"exchange", // exchange name "exchange", // exchange name
"margin", // margin of connection. 1 or 0 "margin_type", // margin of connection. 1 or 0
"symbol", // margin symbol of the connection. "symbol", // margin symbol of the connection.
"currency", "currency",
}, },
) )
metricsAvailableBalances = prometheus.NewGaugeVec( metricsBalanceAvailableMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{ prometheus.GaugeOpts{
Name: "bbgo_balances_available", Name: "bbgo_balances_available",
Help: "bbgo exchange available balances", Help: "bbgo exchange available balances",
}, },
[]string{ []string{
"exchange", // exchange name "exchange", // exchange name
"margin", // margin of connection. none, margin or isolated "margin_type", // margin of connection. none, margin or isolated
"symbol", // margin symbol of the connection. "symbol", // margin symbol of the connection.
"currency",
},
)
metricsBalanceDebtMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_balances_debt",
Help: "bbgo exchange balance debt",
},
[]string{
"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{
"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{
"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{
"exchange", // exchange name
"margin_type", // margin of connection. none, margin or isolated
"symbol", // margin symbol of the connection.
"currency", "currency",
}, },
) )
@ -48,9 +100,9 @@ var (
Help: "bbgo exchange session total balances", Help: "bbgo exchange session total balances",
}, },
[]string{ []string{
"exchange", // exchange name "exchange", // exchange name
"margin", // margin of connection. none, margin or isolated "margin_type", // margin of connection. none, margin or isolated
"symbol", // margin symbol of the connection. "symbol", // margin symbol of the connection.
"currency", "currency",
}, },
) )
@ -61,11 +113,11 @@ var (
Help: "bbgo exchange session trades", Help: "bbgo exchange session trades",
}, },
[]string{ []string{
"exchange", // exchange name "exchange", // exchange name
"margin", // margin of connection. none, margin or isolated "margin_type", // margin of connection. none, margin or isolated
"symbol", // margin symbol of the connection. "symbol", // margin symbol of the connection.
"side", // side: buy or sell "side", // side: buy or sell
"liquidity", // maker or taker "liquidity", // maker or taker
}, },
) )
@ -75,26 +127,26 @@ var (
Help: "bbgo trading volume", Help: "bbgo trading volume",
}, },
[]string{ []string{
"exchange", // exchange name "exchange", // exchange name
"margin", // margin of connection. none, margin or isolated "margin_type", // margin of connection. none, margin or isolated
"symbol", // margin symbol of the connection. "symbol", // margin symbol of the connection.
"side", // side: buy or sell "side", // side: buy or sell
"liquidity", // maker or taker "liquidity", // maker or taker
}, },
) )
metricsLastUpdateTimeBalance = prometheus.NewGaugeVec( metricsLastUpdateTimeMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{ prometheus.GaugeOpts{
Name: "bbgo_last_update_time", Name: "bbgo_last_update_time",
Help: "bbgo last update time of different channel", Help: "bbgo last update time of different channel",
}, },
[]string{ []string{
"exchange", // exchange name "exchange", // exchange name
"margin", // margin of connection. none, margin or isolated "margin_type", // margin of connection. none, margin or isolated
"channel", // channel: user, market "channel", // channel: user, market
"data_type", // type: balance, ticker, kline, orderbook, trade, order "data_type", // type: balance, ticker, kline, orderbook, trade, order
"symbol", // for market data, trade and order "symbol", // for market data, trade and order
"currency", // for balance "currency", // for balance
}, },
) )
) )
@ -103,10 +155,14 @@ func init() {
prometheus.MustRegister( prometheus.MustRegister(
metricsConnectionStatus, metricsConnectionStatus,
metricsTotalBalances, metricsTotalBalances,
metricsLockedBalances, metricsBalanceNetMetrics,
metricsAvailableBalances, metricsBalanceLockedMetrics,
metricsBalanceAvailableMetrics,
metricsBalanceDebtMetrics,
metricsBalanceBorrowedMetrics,
metricsBalanceInterestMetrics,
metricsTradesTotal, metricsTradesTotal,
metricsTradingVolume, metricsTradingVolume,
metricsLastUpdateTimeBalance, metricsLastUpdateTimeMetrics,
) )
} }

View File

@ -902,76 +902,83 @@ func (session *ExchangeSession) metricsBalancesUpdater(balances types.BalanceMap
for currency, balance := range balances { for currency, balance := range balances {
labels := prometheus.Labels{ labels := prometheus.Labels{
"exchange": session.ExchangeName.String(), "exchange": session.ExchangeName.String(),
"margin": session.MarginType(), "margin": string(session.MarginType()),
"symbol": session.IsolatedMarginSymbol, "symbol": session.IsolatedMarginSymbol,
"currency": currency, "currency": currency,
} }
metricsTotalBalances.With(labels).Set(balance.Total().Float64()) metricsTotalBalances.With(labels).Set(balance.Total().Float64())
metricsLockedBalances.With(labels).Set(balance.Locked.Float64()) metricsBalanceNetMetrics.With(labels).Set(balance.Net().Float64())
metricsAvailableBalances.With(labels).Set(balance.Available.Float64()) metricsBalanceAvailableMetrics.With(labels).Set(balance.Available.Float64())
metricsLastUpdateTimeBalance.With(prometheus.Labels{ metricsBalanceLockedMetrics.With(labels).Set(balance.Locked.Float64())
"exchange": session.ExchangeName.String(),
"margin": session.MarginType(), // margin metrics
"channel": "user", metricsBalanceDebtMetrics.With(labels).Set(balance.Debt().Float64())
"data_type": "balance", metricsBalanceBorrowedMetrics.With(labels).Set(balance.Borrowed.Float64())
"symbol": "", metricsBalanceInterestMetrics.With(labels).Set(balance.Interest.Float64())
"currency": currency,
metricsLastUpdateTimeMetrics.With(prometheus.Labels{
"exchange": session.ExchangeName.String(),
"margin_type": string(session.MarginType()),
"channel": "user",
"data_type": "balance",
"symbol": "",
"currency": currency,
}).SetToCurrentTime() }).SetToCurrentTime()
} }
} }
func (session *ExchangeSession) metricsOrderUpdater(order types.Order) { func (session *ExchangeSession) metricsOrderUpdater(order types.Order) {
metricsLastUpdateTimeBalance.With(prometheus.Labels{ metricsLastUpdateTimeMetrics.With(prometheus.Labels{
"exchange": session.ExchangeName.String(), "exchange": session.ExchangeName.String(),
"margin": session.MarginType(), "margin_type": string(session.MarginType()),
"channel": "user", "channel": "user",
"data_type": "order", "data_type": "order",
"symbol": order.Symbol, "symbol": order.Symbol,
"currency": "", "currency": "",
}).SetToCurrentTime() }).SetToCurrentTime()
} }
func (session *ExchangeSession) metricsTradeUpdater(trade types.Trade) { func (session *ExchangeSession) metricsTradeUpdater(trade types.Trade) {
labels := prometheus.Labels{ labels := prometheus.Labels{
"exchange": session.ExchangeName.String(), "exchange": session.ExchangeName.String(),
"margin": session.MarginType(), "margin_type": string(session.MarginType()),
"side": trade.Side.String(), "side": trade.Side.String(),
"symbol": trade.Symbol, "symbol": trade.Symbol,
"liquidity": trade.Liquidity(), "liquidity": trade.Liquidity(),
} }
metricsTradingVolume.With(labels).Add(trade.Quantity.Mul(trade.Price).Float64()) metricsTradingVolume.With(labels).Add(trade.Quantity.Mul(trade.Price).Float64())
metricsTradesTotal.With(labels).Inc() metricsTradesTotal.With(labels).Inc()
metricsLastUpdateTimeBalance.With(prometheus.Labels{ metricsLastUpdateTimeMetrics.With(prometheus.Labels{
"exchange": session.ExchangeName.String(), "exchange": session.ExchangeName.String(),
"margin": session.MarginType(), "margin_type": string(session.MarginType()),
"channel": "user", "channel": "user",
"data_type": "trade", "data_type": "trade",
"symbol": trade.Symbol, "symbol": trade.Symbol,
"currency": "", "currency": "",
}).SetToCurrentTime() }).SetToCurrentTime()
} }
func (session *ExchangeSession) bindMarketDataStreamMetrics(stream types.Stream) { func (session *ExchangeSession) bindMarketDataStreamMetrics(stream types.Stream) {
stream.OnBookUpdate(func(book types.SliceOrderBook) { stream.OnBookUpdate(func(book types.SliceOrderBook) {
metricsLastUpdateTimeBalance.With(prometheus.Labels{ metricsLastUpdateTimeMetrics.With(prometheus.Labels{
"exchange": session.ExchangeName.String(), "exchange": session.ExchangeName.String(),
"margin": session.MarginType(), "margin_type": string(session.MarginType()),
"channel": "market", "channel": "market",
"data_type": "book", "data_type": "book",
"symbol": book.Symbol, "symbol": book.Symbol,
"currency": "", "currency": "",
}).SetToCurrentTime() }).SetToCurrentTime()
}) })
stream.OnKLineClosed(func(kline types.KLine) { stream.OnKLineClosed(func(kline types.KLine) {
metricsLastUpdateTimeBalance.With(prometheus.Labels{ metricsLastUpdateTimeMetrics.With(prometheus.Labels{
"exchange": session.ExchangeName.String(), "exchange": session.ExchangeName.String(),
"margin": session.MarginType(), "margin_type": string(session.MarginType()),
"channel": "market", "channel": "market",
"data_type": "kline", "data_type": "kline",
"symbol": kline.Symbol, "symbol": kline.Symbol,
"currency": "", "currency": "",
}).SetToCurrentTime() }).SetToCurrentTime()
}) })
} }
@ -983,18 +990,18 @@ func (session *ExchangeSession) bindUserDataStreamMetrics(stream types.Stream) {
stream.OnOrderUpdate(session.metricsOrderUpdater) stream.OnOrderUpdate(session.metricsOrderUpdater)
stream.OnDisconnect(func() { stream.OnDisconnect(func() {
metricsConnectionStatus.With(prometheus.Labels{ metricsConnectionStatus.With(prometheus.Labels{
"channel": "user", "channel": "user",
"exchange": session.ExchangeName.String(), "exchange": session.ExchangeName.String(),
"margin": session.MarginType(), "margin_type": string(session.MarginType()),
"symbol": session.IsolatedMarginSymbol, "symbol": session.IsolatedMarginSymbol,
}).Set(0.0) }).Set(0.0)
}) })
stream.OnConnect(func() { stream.OnConnect(func() {
metricsConnectionStatus.With(prometheus.Labels{ metricsConnectionStatus.With(prometheus.Labels{
"channel": "user", "channel": "user",
"exchange": session.ExchangeName.String(), "exchange": session.ExchangeName.String(),
"margin": session.MarginType(), "margin_type": string(session.MarginType()),
"symbol": session.IsolatedMarginSymbol, "symbol": session.IsolatedMarginSymbol,
}).Set(1.0) }).Set(1.0)
}) })
} }