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
```
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:
```sh

View File

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

View File

@ -8,6 +8,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
"github.com/c9s/bbgo/pkg/fixedpoint"
@ -323,9 +324,12 @@ func (session *ExchangeSession) Init(ctx context.Context, environ *Environment)
session.UserDataStream.OnOrderUpdate(session.OrderExecutor.EmitOrderUpdate)
session.Account.BindStream(session.UserDataStream)
// if metrics mode is enabled, we bind the callbacks to update metrics
if viper.GetBool("metrics") {
session.metricsBalancesUpdater(balances)
session.bindUserDataStreamMetrics(session.UserDataStream)
}
}
// TODO: move this logic to Environment struct
// if back-test service is not set, meaning we are not back-testing
@ -799,7 +803,27 @@ func (session *ExchangeSession) metricsBalancesUpdater(balances types.BalanceMap
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,
}).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) {
@ -812,12 +836,44 @@ func (session *ExchangeSession) metricsTradeUpdater(trade types.Trade) {
}
metricsTradingVolume.With(labels).Add(trade.Quantity * trade.Price)
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) {
stream.OnBalanceUpdate(session.metricsBalancesUpdater)
stream.OnBalanceSnapshot(session.metricsBalancesUpdater)
stream.OnTradeUpdate(session.metricsTradeUpdater)
stream.OnOrderUpdate(session.metricsOrderUpdater)
stream.OnDisconnect(func() {
metricsConnectionStatus.With(prometheus.Labels{
"exchange": session.ExchangeName.String(),