diff --git a/api_mongo.go b/api_mongo.go index 92e46a6..6c807e8 100644 --- a/api_mongo.go +++ b/api_mongo.go @@ -59,20 +59,6 @@ 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{{ @@ -81,12 +67,41 @@ func NewApiMongoClient(config *ApiMongoClientConfig) (*ApiClient, error) { 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"))) + 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) } } + // 创建索引 + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.D{ + {"trace_id", 1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.D{ + {"request_time", -1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.D{ + {"request_method", 1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.D{ + {"response_status_code", 1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.D{ + {"response_time", -1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.D{ + {"system_host_name", 1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.D{ + {"system_inside_ip", 1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.D{ + {"go_version", -1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.D{ + {"sdk_version", -1}, + }})) + return c, nil } diff --git a/const.go b/const.go index 12b6d29..740c6c0 100644 --- a/const.go +++ b/const.go @@ -1,5 +1,5 @@ package golog const ( - Version = "1.0.46" + Version = "1.0.47" ) diff --git a/gin_mongo.go b/gin_mongo.go index eac46f1..228d53a 100644 --- a/gin_mongo.go +++ b/gin_mongo.go @@ -71,27 +71,6 @@ 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{{ @@ -100,12 +79,78 @@ func NewGinMongoClient(config *GinMongoClientConfig) (*GinClient, error) { 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"))) + 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) } } + // 创建索引 + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{ + Keys: bson.D{ + {"trace_id", 1}, + }})) + log.Printf(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{ + Keys: bson.D{ + {"request_time", -1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{ + Keys: bson.D{ + {"request_method", 1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{ + Keys: bson.D{ + {"request_proto", 1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{ + Keys: bson.D{ + {"request_ip", 1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{ + Keys: bson.D{ + {"request_ip_country", 1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{ + Keys: bson.D{ + {"request_ip_region", 1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{ + Keys: bson.D{ + {"request_ip_province", 1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{ + Keys: bson.D{ + {"request_ip_city", 1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{ + Keys: bson.D{ + {"request_ip_isp", 1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{ + Keys: bson.D{ + {"response_time", -1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{ + Keys: bson.D{ + {"response_code", 1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{ + Keys: bson.D{ + {"system_host_name", 1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{ + Keys: bson.D{ + {"system_inside_ip", 1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{ + Keys: bson.D{ + {"go_version", -1}, + }})) + log.Println(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{ + Keys: bson.D{ + {"sdk_version", -1}, + }})) + return c, nil }