From f38cafd8f7e86d3fd1ab45bc4ef9d6bbfa7dd1b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Tue, 20 Sep 2022 15:07:39 +0800 Subject: [PATCH] - update mongo --- api.go | 3 --- api_mongo.go | 58 +--------------------------------------------------- const.go | 2 +- gin_mongo.go | 14 ++++++------- 4 files changed, 8 insertions(+), 69 deletions(-) diff --git a/api.go b/api.go index 3e8458e..73a66e1 100644 --- a/api.go +++ b/api.go @@ -115,9 +115,6 @@ func NewApiClient(config *ApiClientConfig) (*ApiClient, error) { // 创建时间序列集合 c.mongoCreateCollection(ctx) - // 创建索引 - c.mongoCreateIndexes(ctx) - c.mongoConfig.stats = true } diff --git a/api_mongo.go b/api_mongo.go index 0984bce..4d855d0 100644 --- a/api_mongo.go +++ b/api_mongo.go @@ -41,68 +41,12 @@ type apiMongolLog struct { // 创建时间序列集合 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 { 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) { diff --git a/const.go b/const.go index a60d583..a1d2d50 100644 --- a/const.go +++ b/const.go @@ -1,5 +1,5 @@ package golog const ( - Version = "1.0.86" + Version = "1.0.87" ) diff --git a/gin_mongo.go b/gin_mongo.go index 26b027a..f1d312b 100644 --- a/gin_mongo.go +++ b/gin_mongo.go @@ -54,11 +54,14 @@ type ginMongoLog struct { SdkVersion string `json:"sdk_version,omitempty" bson:"sdk_version,omitempty"` //【程序】Sdk版本 } -// 创建时间序列集合 +// 创建集合 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 { - 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{{ Key: "trace_id", - Value: 1, - }}, - }, { - Keys: bson.D{{ - Key: "request_time", Value: -1, }}, }, {