- update mongo

master v1.0.46
李光春 2 years ago
parent 04f079daf9
commit 242112f779

@ -8,7 +8,10 @@ import (
"go.dtapp.net/gorequest"
"go.dtapp.net/gotrace_id"
"go.dtapp.net/gourl"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"os"
"runtime"
@ -56,6 +59,34 @@ func NewApiMongoClient(config *ApiMongoClientConfig) (*ApiClient, error) {
c.mongoConfig.insideIp = goip.GetInsideIp(ctx)
c.mongoConfig.goVersion = runtime.Version()
// 创建索引
name, err := c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.D{
{"trace_id", 1},
{"request_time", -1},
{"request_method", 1},
{"response_status_code", 1},
{"response_time", -1},
{"system_host_name", 1},
{"system_inside_ip", 1},
{"go_version", -1},
{"sdk_version", -1},
}})
log.Println("创建索引:", name, err)
// 创建时间序列集合
var commandResult bson.M
commandErr := c.mongoClient.Db.Database(c.mongoConfig.databaseName).RunCommand(context.TODO(), bson.D{{
"listCollections", 1,
}}).Decode(&commandResult)
if commandErr != nil {
log.Println("检查时间序列集合:", commandErr)
} else {
err = c.mongoClient.Db.Database(c.mongoConfig.databaseName).CreateCollection(context.TODO(), c.mongoConfig.collectionName, options.CreateCollection().SetTimeSeriesOptions(options.TimeSeries().SetTimeField("request_time")))
if err != nil {
log.Println("创建时间序列集合:", err)
}
}
return c, nil
}

@ -1,5 +1,5 @@
package golog
const (
Version = "1.0.45"
Version = "1.0.46"
)

@ -13,7 +13,10 @@ import (
"go.dtapp.net/gotrace_id"
"go.dtapp.net/gourl"
"go.dtapp.net/goxml"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"io/ioutil"
"log"
"net"
@ -68,6 +71,41 @@ func NewGinMongoClient(config *GinMongoClientConfig) (*GinClient, error) {
c.mongoConfig.insideIp = goip.GetInsideIp(ctx)
c.mongoConfig.goVersion = runtime.Version()
// 创建索引
name, err := c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.D{
{"trace_id", 1},
{"request_time", -1},
{"request_method", 1},
{"request_proto", 1},
{"request_ip", 1},
{"request_ip_country", 1},
{"request_ip_region", 1},
{"request_ip_province", 1},
{"request_ip_city", 1},
{"request_ip_isp", 1},
{"response_time", -1},
{"response_code", 1},
{"system_host_name", 1},
{"system_inside_ip", 1},
{"go_version", -1},
{"sdk_version", -1},
}})
log.Println("创建索引:", name, err)
// 创建时间序列集合
var commandResult bson.M
commandErr := c.mongoClient.Db.Database(c.mongoConfig.databaseName).RunCommand(context.TODO(), bson.D{{
"listCollections", 1,
}}).Decode(&commandResult)
if commandErr != nil {
log.Println("检查时间序列集合:", commandErr)
} else {
err = c.mongoClient.Db.Database(c.mongoConfig.databaseName).CreateCollection(context.TODO(), c.mongoConfig.collectionName, options.CreateCollection().SetTimeSeriesOptions(options.TimeSeries().SetTimeField("request_time")))
if err != nil {
log.Println("创建时间序列集合:", err)
}
}
return c, nil
}

Loading…
Cancel
Save