This commit is contained in:
wangchuxiao
2022-09-15 01:23:57 +08:00
8 changed files with 178 additions and 153 deletions
+4 -5
View File
@@ -1,7 +1,6 @@
package main
import "Open_IM/pkg/common/db"
func main() {
db.DB.BatchInsertChat()
}
//
//func main() {
// db.DB.BatchInsertChat()
//}
+1
View File
@@ -37,6 +37,7 @@ var (
func Init() {
cmdCh = make(chan Cmd2Value, 10000)
w = new(sync.Mutex)
initPrometheus()
persistentCH.Init() // ws2mschat save mysql
historyCH.Init(cmdCh) //
historyMongoCH.Init()
@@ -18,13 +18,7 @@ import (
"github.com/Shopify/sarama"
"github.com/golang/protobuf/proto"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
msgInsertMysqlCounter prometheus.Counter
msgInsertFailedMysqlCounter prometheus.Counter
promePkg "Open_IM/pkg/common/prometheus"
)
type PersistentConsumerHandler struct {
@@ -38,32 +32,18 @@ func (pc *PersistentConsumerHandler) Init() {
pc.persistentConsumerGroup = kfk.NewMConsumerGroup(&kfk.MConsumerGroupConfig{KafkaVersion: sarama.V2_0_0_0,
OffsetsInitial: sarama.OffsetNewest, IsReturnErr: false}, []string{config.Config.Kafka.Ws2mschat.Topic},
config.Config.Kafka.Ws2mschat.Addr, config.Config.Kafka.ConsumerGroupID.MsgToMySql)
if config.Config.Prometheus.Enable {
pc.initPrometheus()
}
}
func (pc *PersistentConsumerHandler) initPrometheus() {
// counter
msgInsertMysqlCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "insert_mysql_msg_total",
Help: "The total number of msg insert mysql events",
})
msgInsertFailedMysqlCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "insert_mysql_failed_msg_total",
Help: "The total number of msg insert mysql events",
})
// 启动计时器
// requestDurations := prometheus.NewHistogram(prometheus.HistogramOpts{
// Name: "http_request_duration_seconds",
// Help: "A histogram of the HTTP request durations in seconds.",
// Buckets: []float64{0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10},
// })
// 开始
// timer := prometheus.NewTimer(requestDurations)
// 停止
// timer.ObserveDuration()
func initPrometheus() {
promePkg.NewSeqGetSuccessCounter()
promePkg.NewSeqGetFailedCounter()
promePkg.NewSeqSetSuccessCounter()
promePkg.NewSeqSetFailedCounter()
promePkg.NewMsgInsertRedisSuccessCounter()
promePkg.NewMsgInsertRedisFailedCounter()
promePkg.NewMsgInsertMongoSuccessCounter()
promePkg.NewMsgInsertMongoFailedCounter()
}
func (pc *PersistentConsumerHandler) handleChatWs2Mysql(cMsg *sarama.ConsumerMessage, msgKey string, _ sarama.ConsumerGroupSession) {
@@ -97,14 +77,8 @@ func (pc *PersistentConsumerHandler) handleChatWs2Mysql(cMsg *sarama.ConsumerMes
log.NewInfo(msgFromMQ.OperationID, "msg_transfer msg persisting", string(msg))
if err = im_mysql_msg_model.InsertMessageToChatLog(msgFromMQ); err != nil {
log.NewError(msgFromMQ.OperationID, "Message insert failed", "err", err.Error(), "msg", msgFromMQ.String())
if config.Config.Prometheus.Enable {
msgInsertFailedMysqlCounter.Inc()
}
return
}
if config.Config.Prometheus.Enable {
msgInsertMysqlCounter.Inc()
}
}
}