- update mongo

master
李光春 2 years ago
parent 694b738017
commit f38cafd8f7

@ -115,9 +115,6 @@ func NewApiClient(config *ApiClientConfig) (*ApiClient, error) {
// 创建时间序列集合 // 创建时间序列集合
c.mongoCreateCollection(ctx) c.mongoCreateCollection(ctx)
// 创建索引
c.mongoCreateIndexes(ctx)
c.mongoConfig.stats = true c.mongoConfig.stats = true
} }

@ -41,68 +41,12 @@ type apiMongolLog struct {
// 创建时间序列集合 // 创建时间序列集合
func (c *ApiClient) mongoCreateCollection(ctx context.Context) { func (c *ApiClient) mongoCreateCollection(ctx context.Context) {
err := c.mongoClient.Db.Database(c.mongoConfig.databaseName).CreateCollection(ctx, c.mongoConfig.collectionName, options.CreateCollection().SetTimeSeriesOptions(options.TimeSeries().SetTimeField("log_time"))) err := c.mongoClient.Database(c.mongoConfig.databaseName).CreateCollection(ctx, c.mongoConfig.collectionName, options.CreateCollection().SetTimeSeriesOptions(options.TimeSeries().SetTimeField("log_time")))
if err != nil { if err != nil {
c.zapLog.WithTraceId(ctx).Sugar().Errorf("创建时间序列集合:%s", err) c.zapLog.WithTraceId(ctx).Sugar().Errorf("创建时间序列集合:%s", err)
} }
} }
// 创建索引
func (c *ApiClient) mongoCreateIndexes(ctx context.Context) {
indexes, err := c.mongoClient.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).CreateManyIndexes(ctx, []mongo.IndexModel{
{
Keys: bson.D{{
Key: "trace_id",
Value: 1,
}},
}, {
Keys: bson.D{{
Key: "request_time",
Value: -1,
}},
}, {
Keys: bson.D{{
Key: "request_method",
Value: 1,
}},
}, {
Keys: bson.D{{
Key: "response_status_code",
Value: 1,
}},
}, {
Keys: bson.D{{
Key: "response_time",
Value: -1,
}},
}, {
Keys: bson.D{{
Key: "system_os",
Value: 1,
}},
}, {
Keys: bson.D{{
Key: "system_arch",
Value: -1,
}},
}, {
Keys: bson.D{{
Key: "go_version",
Value: -1,
}},
}, {
Keys: bson.D{{
Key: "sdk_version",
Value: -1,
}},
},
})
if err != nil {
c.zapLog.WithTraceId(ctx).Sugar().Errorf("创建索引:%s", err)
}
c.zapLog.WithTraceId(ctx).Sugar().Infof("创建索引:%s", indexes)
}
// 记录日志 // 记录日志
func (c *ApiClient) mongoRecord(ctx context.Context, mongoLog apiMongolLog) (err error) { func (c *ApiClient) mongoRecord(ctx context.Context, mongoLog apiMongolLog) (err error) {

@ -1,5 +1,5 @@
package golog package golog
const ( const (
Version = "1.0.86" Version = "1.0.87"
) )

@ -54,11 +54,14 @@ type ginMongoLog struct {
SdkVersion string `json:"sdk_version,omitempty" bson:"sdk_version,omitempty"` //【程序】Sdk版本 SdkVersion string `json:"sdk_version,omitempty" bson:"sdk_version,omitempty"` //【程序】Sdk版本
} }
// 创建时间序列集合 // 创建集合
func (c *GinClient) mongoCreateCollection(ctx context.Context) { func (c *GinClient) mongoCreateCollection(ctx context.Context) {
err := c.mongoClient.Db.Database(c.mongoConfig.databaseName).CreateCollection(ctx, c.mongoConfig.collectionName, options.CreateCollection().SetTimeSeriesOptions(options.TimeSeries().SetTimeField("log_time"))) err := c.mongoClient.Database(c.mongoConfig.databaseName).CreateCollection(ctx, c.mongoConfig.collectionName, options.CreateCollection().SetCollation(&options.Collation{
Locale: "request_time",
Strength: -1,
}))
if err != nil { if err != nil {
c.zapLog.WithTraceId(ctx).Sugar().Error("创建时间序列集合:", err) c.zapLog.WithTraceId(ctx).Sugar().Error("创建集合:", err)
} }
} }
@ -68,11 +71,6 @@ func (c *GinClient) mongoCreateIndexes(ctx context.Context) {
{ {
Keys: bson.D{{ Keys: bson.D{{
Key: "trace_id", Key: "trace_id",
Value: 1,
}},
}, {
Keys: bson.D{{
Key: "request_time",
Value: -1, Value: -1,
}}, }},
}, { }, {

Loading…
Cancel
Save