Merge pull request #408 from c9s/feature/prometheus

feature: add metricsLastUpdateTimeBalance metrics
This commit is contained in:
Yo-An Lin 2021-12-28 01:47:37 +08:00 committed by GitHub
commit e911530eab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 6 deletions

View File

@ -74,6 +74,12 @@ To use the latest version:
helm install --set existingConfigmap=bbgo-$INSTANCE --set image.tag=latest bbgo-$INSTANCE ./charts/bbgo helm install --set existingConfigmap=bbgo-$INSTANCE --set image.tag=latest bbgo-$INSTANCE ./charts/bbgo
``` ```
Or, if you have custom values.yaml to override the default values:
```sh
helm install --values deploy/my-bbgo-values.yaml bbgo-$INSTANCE ./charts/bbgo
```
To upgrade: To upgrade:
```sh ```sh

View File

@ -82,6 +82,21 @@ var (
"liquidity", // maker or taker "liquidity", // maker or taker
}, },
) )
metricsLastUpdateTimeBalance = 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
},
)
) )
func init() { func init() {
@ -92,5 +107,6 @@ func init() {
metricsAvailableBalances, metricsAvailableBalances,
metricsTradesTotal, metricsTradesTotal,
metricsTradingVolume, metricsTradingVolume,
metricsLastUpdateTimeBalance,
) )
} }

View File

@ -8,6 +8,7 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/c9s/bbgo/pkg/cmd/cmdutil" "github.com/c9s/bbgo/pkg/cmd/cmdutil"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
@ -323,8 +324,11 @@ func (session *ExchangeSession) Init(ctx context.Context, environ *Environment)
session.UserDataStream.OnOrderUpdate(session.OrderExecutor.EmitOrderUpdate) session.UserDataStream.OnOrderUpdate(session.OrderExecutor.EmitOrderUpdate)
session.Account.BindStream(session.UserDataStream) session.Account.BindStream(session.UserDataStream)
session.metricsBalancesUpdater(balances) // if metrics mode is enabled, we bind the callbacks to update metrics
session.bindUserDataStreamMetrics(session.UserDataStream) if viper.GetBool("metrics") {
session.metricsBalancesUpdater(balances)
session.bindUserDataStreamMetrics(session.UserDataStream)
}
} }
// TODO: move this logic to Environment struct // TODO: move this logic to Environment struct
@ -799,7 +803,27 @@ func (session *ExchangeSession) metricsBalancesUpdater(balances types.BalanceMap
metricsTotalBalances.With(labels).Set(balance.Total().Float64()) metricsTotalBalances.With(labels).Set(balance.Total().Float64())
metricsLockedBalances.With(labels).Set(balance.Locked.Float64()) metricsLockedBalances.With(labels).Set(balance.Locked.Float64())
metricsAvailableBalances.With(labels).Set(balance.Available.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,
}).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": "",
}).SetToCurrentTime()
} }
func (session *ExchangeSession) metricsTradeUpdater(trade types.Trade) { func (session *ExchangeSession) metricsTradeUpdater(trade types.Trade) {
@ -812,23 +836,55 @@ func (session *ExchangeSession) metricsTradeUpdater(trade types.Trade) {
} }
metricsTradingVolume.With(labels).Add(trade.Quantity * trade.Price) metricsTradingVolume.With(labels).Add(trade.Quantity * trade.Price)
metricsTradesTotal.With(labels).Inc() metricsTradesTotal.With(labels).Inc()
metricsLastUpdateTimeBalance.With(prometheus.Labels{
"exchange": session.ExchangeName.String(),
"margin": 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": "",
}).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": "",
}).SetToCurrentTime()
})
} }
func (session *ExchangeSession) bindUserDataStreamMetrics(stream types.Stream) { func (session *ExchangeSession) bindUserDataStreamMetrics(stream types.Stream) {
stream.OnBalanceUpdate(session.metricsBalancesUpdater) stream.OnBalanceUpdate(session.metricsBalancesUpdater)
stream.OnBalanceSnapshot(session.metricsBalancesUpdater) stream.OnBalanceSnapshot(session.metricsBalancesUpdater)
stream.OnTradeUpdate(session.metricsTradeUpdater) stream.OnTradeUpdate(session.metricsTradeUpdater)
stream.OnOrderUpdate(session.metricsOrderUpdater)
stream.OnDisconnect(func() { stream.OnDisconnect(func() {
metricsConnectionStatus.With(prometheus.Labels{ metricsConnectionStatus.With(prometheus.Labels{
"exchange": session.ExchangeName.String(), "exchange": session.ExchangeName.String(),
"margin": session.MarginType(), "margin": 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{
"exchange": session.ExchangeName.String(), "exchange": session.ExchangeName.String(),
"margin": session.MarginType(), "margin": session.MarginType(),
"symbol": session.IsolatedMarginSymbol, "symbol": session.IsolatedMarginSymbol,
}).Set(1.0) }).Set(1.0)
}) })