Ai
1 Star 0 Fork 0

danlansky/go-library

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
mongo_plugin.go 3.29 KB
一键复制 编辑 原始数据 按行查看 历史
zhangminghua 提交于 2024-08-20 14:32 +08:00 . feat:基础工具包
package mongo
import (
"context"
"github.com/qiniu/qmgo/operator"
"github.com/qiniu/qmgo/options"
"time"
)
type callback = func(ctx context.Context, doc interface{}, opType operator.OpType, opts ...interface{}) error
// MonMongoPlugin mon打点插件。其中的doc或者opt中必须传指针类型的
func MonMongoPlugin(ctx context.Context, doc interface{}, opType operator.OpType, opts ...interface{}) error {
// do anything
if hook, ok := doc.(*MonMongoHook); ok {
hook.executeHook(opType)
return nil
}
if len(opts) > 0 {
if hook, ok := opts[0].(*MonMongoHook); ok {
hook.executeHook(opType)
}
}
return nil
}
// NewQueryMonOpt 使用时将其放置到Find的opt中
// 示例: cli := WrapperMongo.Client.Database("test_db").Collection("test_collection")
// cli.Find(ctx, bson.M{"_id": objId}, mongo.NewQueryMonOpt("test_db", "test_collection")).One(&res)
func NewQueryMonOpt(database, collection string) options.FindOptions {
return options.FindOptions{
QueryHook: &MonMongoHook{
DB: database,
Collection: collection,
OperationName: "query",
},
}
}
// NewInsertMonOpt 使用时将其放置到Insert的opt中
// 示例:cli.InsertOne(ctx, UserInfo{}, mongo.NewInsertMonOpt("test_db", "test_collection"))
func NewInsertMonOpt(database, collection string) options.InsertOneOptions {
return options.InsertOneOptions{
InsertHook: &MonMongoHook{
DB: database,
Collection: collection,
OperationName: "insert",
},
}
}
func NewInsertManyMonOpt(database, collection string) options.InsertManyOptions {
return options.InsertManyOptions{
InsertHook: &MonMongoHook{
DB: database,
Collection: collection,
OperationName: "insert_many",
},
}
}
func NewUpsertMonOpt(database, collection string) options.UpsertOptions {
return options.UpsertOptions{
UpsertHook: &MonMongoHook{
DB: database,
Collection: collection,
OperationName: "upsert",
},
}
}
func NewUpdateMonOpt(database, collection string) options.UpdateOptions {
return options.UpdateOptions{
UpdateHook: &MonMongoHook{
DB: database,
Collection: collection,
OperationName: "update",
},
}
}
func NewReplaceMonOpt(database, collection string) options.ReplaceOptions {
return options.ReplaceOptions{
UpdateHook: &MonMongoHook{
DB: database,
Collection: collection,
OperationName: "replace",
},
}
}
func NewRemoveMonOpt(database, collection string) options.RemoveOptions {
return options.RemoveOptions{
RemoveHook: &MonMongoHook{
DB: database,
Collection: collection,
OperationName: "remove",
},
}
}
func (p *MonMongoHook) executeHook(opType operator.OpType) {
switch opType {
case operator.BeforeInsert, operator.BeforeUpsert, operator.BeforeReplace, operator.BeforeUpdate, operator.BeforeQuery, operator.BeforeRemove:
_ = p.Before()
case operator.AfterInsert, operator.AfterUpsert, operator.AfterReplace, operator.AfterUpdate, operator.AfterQuery, operator.AfterRemove:
_ = p.After()
}
}
type MonMongoHook struct {
DB string
Collection string
OperationName string
StartTime time.Time
}
func (p *MonMongoHook) Before() error {
p.StartTime = time.Now()
return nil
}
func (p *MonMongoHook) After() error {
// mod打点,没有该功能
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/danlansky/go-library.git
git@gitee.com:danlansky/go-library.git
danlansky
go-library
go-library
v1.0.8

搜索帮助