diff --git a/api_gorm.go b/api_gorm.go index 1141a72..521b5d1 100644 --- a/api_gorm.go +++ b/api_gorm.go @@ -61,7 +61,7 @@ func (c *ApiClient) gormRecord(ctx context.Context, data apiPostgresqlLog) (err err = c.gormClient.GetDb().Table(c.gormConfig.tableName).Create(&data).Error if err != nil { - c.zapLog.WithTraceId(ctx).Sugar().Errorf("记录日志失败:%s", err) + c.zapLog.WithTraceId(ctx).Sugar().Errorf("记录接口日志失败:%s", err) } return } @@ -97,10 +97,7 @@ func (c *ApiClient) gormMiddleware(ctx context.Context, request gorequest.Respon } } - err := c.gormRecord(ctx, data) - if err != nil { - c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error()) - } + c.gormRecord(ctx, data) } // 中间件 @@ -125,10 +122,7 @@ func (c *ApiClient) gormMiddlewareXml(ctx context.Context, request gorequest.Res } } - err := c.gormRecord(ctx, data) - if err != nil { - c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error()) - } + c.gormRecord(ctx, data) } // 中间件 @@ -153,8 +147,5 @@ func (c *ApiClient) gormMiddlewareCustom(ctx context.Context, api string, reques } } - err := c.gormRecord(ctx, data) - if err != nil { - c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error()) - } + c.gormRecord(ctx, data) } diff --git a/api_mongo.go b/api_mongo.go index 4cd2d09..0a888cf 100644 --- a/api_mongo.go +++ b/api_mongo.go @@ -31,12 +31,16 @@ type apiMongolLog struct { ResponseBody interface{} `json:"response_body,omitempty" bson:"response_body,omitempty"` //【返回】内容 ResponseContentLength int64 `json:"response_content_length,omitempty" bson:"response_content_length,omitempty"` //【返回】大小 ResponseTime dorm.BsonTime `json:"response_time,omitempty" bson:"response_time,omitempty"` //【返回】时间 - SystemHostName string `json:"system_host_name,omitempty" bson:"system_host_name,omitempty"` //【系统】主机名 - SystemInsideIp string `json:"system_inside_ip,omitempty" bson:"system_inside_ip,omitempty"` //【系统】内网ip - SystemOs string `json:"system_os,omitempty" bson:"system_os,omitempty"` //【系统】系统类型 - SystemArch string `json:"system_arch,omitempty" bson:"system_arch,omitempty"` //【系统】系统架构 - GoVersion string `json:"go_version,omitempty" bson:"go_version,omitempty"` //【程序】Go版本 - SdkVersion string `json:"sdk_version,omitempty" bson:"sdk_version,omitempty"` //【程序】Sdk版本 + System struct { + HostName string `json:"host_name,omitempty" bson:"host_name,omitempty"` //【系统】主机名 + InsideIp string `json:"inside_ip,omitempty" bson:"inside_ip,omitempty"` //【系统】内网ip + Os string `json:"os,omitempty" bson:"os,omitempty"` //【系统】系统类型 + Arch string `json:"arch,omitempty" bson:"arch,omitempty"` //【系统】系统架构 + } `json:"system,omitempty" bson:"system,omitempty"` //【系统】信息 + Version struct { + Go string `json:"go,omitempty" bson:"go,omitempty"` //【程序】Go版本 + Sdk string `json:"sdk,omitempty" bson:"sdk,omitempty"` //【程序】Sdk版本 + } `json:"version,omitempty" bson:"version,omitempty"` //【程序】版本信息 } // 创建时间序列集合 @@ -68,20 +72,21 @@ func (c *ApiClient) MongoDelete(ctx context.Context, hour int64) (*mongo.DeleteR } // 记录日志 -func (c *ApiClient) mongoRecord(ctx context.Context, mongoLog apiMongolLog) (err error) { +func (c *ApiClient) mongoRecord(ctx context.Context, mongoLog apiMongolLog, sdkVersion string) (err error) { - mongoLog.SystemHostName = c.config.systemHostName //【系统】主机名 - mongoLog.SystemInsideIp = c.config.systemInsideIp //【系统】内网ip - mongoLog.GoVersion = c.config.goVersion //【程序】Go版本 + mongoLog.System.HostName = c.config.systemHostName //【系统】主机名 + mongoLog.System.InsideIp = c.config.systemInsideIp //【系统】内网ip + mongoLog.System.Os = c.config.systemOs //【系统】系统类型 + mongoLog.System.Arch = c.config.systemArch //【系统】系统架构 + mongoLog.Version.Go = c.config.goVersion //【程序】Go版本 + mongoLog.Version.Sdk = sdkVersion //【程序】Sdk版本 mongoLog.TraceId = gotrace_id.GetTraceIdContext(ctx) //【记录】跟踪编号 mongoLog.RequestIp = c.config.systemOutsideIp //【请求】请求Ip - mongoLog.SystemOs = c.config.systemOs //【系统】系统类型 - mongoLog.SystemArch = c.config.systemArch //【系统】系统架构 mongoLog.LogId = primitive.NewObjectID() //【记录】编号 _, err = c.mongoClient.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).InsertOne(ctx, mongoLog) if err != nil { - c.zapLog.WithTraceId(ctx).Sugar().Errorf("记录日志失败:%s", err) + c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存接口日志失败:%s", err) } return err } @@ -101,7 +106,6 @@ func (c *ApiClient) mongoMiddleware(ctx context.Context, request gorequest.Respo ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码 ResponseContentLength: request.ResponseContentLength, //【返回】大小 ResponseTime: dorm.NewBsonTimeFromTime(request.ResponseTime), //【返回】时间 - SdkVersion: sdkVersion, //【程序】Sdk版本 } if !request.HeaderIsImg() { if len(request.ResponseBody) > 0 { @@ -109,10 +113,7 @@ func (c *ApiClient) mongoMiddleware(ctx context.Context, request gorequest.Respo } } - err := c.mongoRecord(ctx, data) - if err != nil { - c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error()) - } + c.mongoRecord(ctx, data, sdkVersion) } // 中间件 @@ -130,7 +131,6 @@ func (c *ApiClient) mongoMiddlewareXml(ctx context.Context, request gorequest.Re ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码 ResponseContentLength: request.ResponseContentLength, //【返回】大小 ResponseTime: dorm.NewBsonTimeFromTime(request.ResponseTime), //【返回】时间 - SdkVersion: sdkVersion, //【程序】Sdk版本 } if !request.HeaderIsImg() { if len(request.ResponseBody) > 0 { @@ -138,10 +138,7 @@ func (c *ApiClient) mongoMiddlewareXml(ctx context.Context, request gorequest.Re } } - err := c.mongoRecord(ctx, data) - if err != nil { - c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error()) - } + c.mongoRecord(ctx, data, sdkVersion) } // 中间件 @@ -159,7 +156,6 @@ func (c *ApiClient) mongoMiddlewareCustom(ctx context.Context, api string, reque ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码 ResponseContentLength: request.ResponseContentLength, //【返回】大小 ResponseTime: dorm.NewBsonTimeFromTime(request.ResponseTime), //【返回】时间 - SdkVersion: sdkVersion, //【程序】Sdk版本 } if !request.HeaderIsImg() { if len(request.ResponseBody) > 0 { @@ -167,8 +163,5 @@ func (c *ApiClient) mongoMiddlewareCustom(ctx context.Context, api string, reque } } - err := c.mongoRecord(ctx, data) - if err != nil { - c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error()) - } + c.mongoRecord(ctx, data, sdkVersion) } diff --git a/const.go b/const.go index 0d37aa8..0e2f855 100644 --- a/const.go +++ b/const.go @@ -1,5 +1,5 @@ package golog const ( - Version = "1.0.92" + Version = "1.0.93" ) diff --git a/gin.go b/gin.go index 490db40..3c3ca96 100644 --- a/gin.go +++ b/gin.go @@ -106,7 +106,7 @@ func NewGinClient(config *GinClientConfig) (*GinClient, error) { } // 创建时间序列集合 - c.mongoCreateCollection(ctx) + //c.mongoCreateCollection(ctx) // 创建索引 c.mongoCreateIndexes(ctx) @@ -180,21 +180,10 @@ func (c *GinClient) Middleware() gin.HandlerFunc { } clientIp := gorequest.ClientIp(ginCtx.Request) + var info = goip.AnalyseResult{} - var requestClientIpCountry string - var requestClientIpProvince string - var requestClientIpCity string - var requestClientIpIsp string - var requestClientIpLocationLatitude float64 - var requestClientIpLocationLongitude float64 if c.ipService != nil { - info := c.ipService.Analyse(clientIp) - requestClientIpCountry = info.Country - requestClientIpProvince = info.Province - requestClientIpCity = info.City - requestClientIpIsp = info.Isp - requestClientIpLocationLatitude = info.LocationLatitude - requestClientIpLocationLongitude = info.LocationLongitude + info = c.ipService.Analyse(clientIp) } var traceId = gotrace_id.GetGinTraceId(ginCtx) @@ -202,16 +191,16 @@ func (c *GinClient) Middleware() gin.HandlerFunc { // 记录 if c.gormConfig.stats { if dataJson { - c.gormRecordJson(ginCtx, traceId, requestTime, data, responseCode, responseBody, startTime, endTime, clientIp, requestClientIpCountry, requestClientIpProvince, requestClientIpCity, requestClientIpIsp, requestClientIpLocationLatitude, requestClientIpLocationLongitude) + c.gormRecordJson(ginCtx, traceId, requestTime, data, responseCode, responseBody, startTime, endTime, info) } else { - c.gormRecordXml(ginCtx, traceId, requestTime, data, responseCode, responseBody, startTime, endTime, clientIp, requestClientIpCountry, requestClientIpProvince, requestClientIpCity, requestClientIpIsp, requestClientIpLocationLatitude, requestClientIpLocationLongitude) + c.gormRecordXml(ginCtx, traceId, requestTime, data, responseCode, responseBody, startTime, endTime, info) } } if c.mongoConfig.stats { if dataJson { - c.mongoRecordJson(ginCtx, traceId, requestTime, data, responseCode, responseBody, startTime, endTime, clientIp, requestClientIpCountry, requestClientIpProvince, requestClientIpCity, requestClientIpIsp, requestClientIpLocationLatitude, requestClientIpLocationLongitude) + c.mongoRecordJson(ginCtx, traceId, requestTime, data, responseCode, responseBody, startTime, endTime, info) } else { - c.mongoRecordXml(ginCtx, traceId, requestTime, data, responseCode, responseBody, startTime, endTime, clientIp, requestClientIpCountry, requestClientIpProvince, requestClientIpCity, requestClientIpIsp, requestClientIpLocationLatitude, requestClientIpLocationLongitude) + c.mongoRecordXml(ginCtx, traceId, requestTime, data, responseCode, responseBody, startTime, endTime, info) } } }() diff --git a/gin_gorm.go b/gin_gorm.go index 1c548cf..d90617d 100644 --- a/gin_gorm.go +++ b/gin_gorm.go @@ -4,6 +4,7 @@ import ( "context" "github.com/gin-gonic/gin" "go.dtapp.net/dorm" + "go.dtapp.net/goip" "go.dtapp.net/gotime" "go.dtapp.net/gourl" "time" @@ -64,12 +65,12 @@ func (c *GinClient) gormRecord(data ginPostgresqlLog) (err error) { err = c.gormClient.GetDb().Table(c.gormConfig.tableName).Create(&data).Error if err != nil { - c.zapLog.WithTraceIdStr(data.TraceId).Sugar().Errorf("记录日志失败:%s", err) + c.zapLog.WithTraceIdStr(data.TraceId).Sugar().Errorf("记录框架日志失败:%s", err) } return } -func (c *GinClient) gormRecordJson(ginCtx *gin.Context, traceId string, requestTime time.Time, requestBody []byte, responseCode int, responseBody string, startTime, endTime int64, clientIp, requestClientIpCountry, requestClientIpProvince, requestClientIpCity, requestClientIpIsp string, requestClientIpLocationLatitude, requestClientIpLocationLongitude float64) { +func (c *GinClient) gormRecordJson(ginCtx *gin.Context, traceId string, requestTime time.Time, requestBody []byte, responseCode int, responseBody string, startTime, endTime int64, ipInfo goip.AnalyseResult) { data := ginPostgresqlLog{ TraceId: traceId, //【系统】跟踪编号 @@ -81,13 +82,13 @@ func (c *GinClient) gormRecordJson(ginCtx *gin.Context, traceId string, requestT RequestUa: ginCtx.Request.UserAgent(), //【请求】请求UA RequestReferer: ginCtx.Request.Referer(), //【请求】请求referer RequestUrlQuery: dorm.JsonEncodeNoError(ginCtx.Request.URL.Query()), //【请求】请求URL参数 - RequestIp: clientIp, //【请求】请求客户端Ip - RequestIpCountry: requestClientIpCountry, //【请求】请求客户端城市 - RequestIpProvince: requestClientIpProvince, //【请求】请求客户端省份 - RequestIpCity: requestClientIpCity, //【请求】请求客户端城市 - RequestIpIsp: requestClientIpIsp, //【请求】请求客户端运营商 - RequestIpLatitude: requestClientIpLocationLatitude, //【请求】请求客户端纬度 - RequestIpLongitude: requestClientIpLocationLongitude, //【请求】请求客户端经度 + RequestIp: ipInfo.Ip, //【请求】请求客户端Ip + RequestIpCountry: ipInfo.Country, //【请求】请求客户端城市 + RequestIpProvince: ipInfo.Province, //【请求】请求客户端省份 + RequestIpCity: ipInfo.City, //【请求】请求客户端城市 + RequestIpIsp: ipInfo.Isp, //【请求】请求客户端运营商 + RequestIpLatitude: ipInfo.LocationLatitude, //【请求】请求客户端纬度 + RequestIpLongitude: ipInfo.LocationLongitude, //【请求】请求客户端经度 RequestHeader: dorm.JsonEncodeNoError(ginCtx.Request.Header), //【请求】请求头 ResponseTime: gotime.Current().Time, //【返回】时间 ResponseCode: responseCode, //【返回】状态码 @@ -104,13 +105,10 @@ func (c *GinClient) gormRecordJson(ginCtx *gin.Context, traceId string, requestT data.RequestBody = dorm.JsonEncodeNoError(requestBody) //【请求】请求主体 } - err := c.gormRecord(data) - if err != nil { - c.zapLog.WithTraceIdStr(traceId).Sugar().Errorf("保存失败:%s", err.Error()) - } + c.gormRecord(data) } -func (c *GinClient) gormRecordXml(ginCtx *gin.Context, traceId string, requestTime time.Time, requestBody []byte, responseCode int, responseBody string, startTime, endTime int64, clientIp, requestClientIpCountry, requestClientIpProvince, requestClientIpCity, requestClientIpIsp string, requestClientIpLocationLatitude, requestClientIpLocationLongitude float64) { +func (c *GinClient) gormRecordXml(ginCtx *gin.Context, traceId string, requestTime time.Time, requestBody []byte, responseCode int, responseBody string, startTime, endTime int64, ipInfo goip.AnalyseResult) { data := ginPostgresqlLog{ TraceId: traceId, //【系统】跟踪编号 @@ -122,13 +120,13 @@ func (c *GinClient) gormRecordXml(ginCtx *gin.Context, traceId string, requestTi RequestUa: ginCtx.Request.UserAgent(), //【请求】请求UA RequestReferer: ginCtx.Request.Referer(), //【请求】请求referer RequestUrlQuery: dorm.JsonEncodeNoError(ginCtx.Request.URL.Query()), //【请求】请求URL参数 - RequestIp: clientIp, //【请求】请求客户端Ip - RequestIpCountry: requestClientIpCountry, //【请求】请求客户端城市 - RequestIpProvince: requestClientIpProvince, //【请求】请求客户端省份 - RequestIpCity: requestClientIpCity, //【请求】请求客户端城市 - RequestIpIsp: requestClientIpIsp, //【请求】请求客户端运营商 - RequestIpLatitude: requestClientIpLocationLatitude, //【请求】请求客户端纬度 - RequestIpLongitude: requestClientIpLocationLongitude, //【请求】请求客户端经度 + RequestIp: ipInfo.Ip, //【请求】请求客户端Ip + RequestIpCountry: ipInfo.Country, //【请求】请求客户端城市 + RequestIpProvince: ipInfo.Province, //【请求】请求客户端省份 + RequestIpCity: ipInfo.City, //【请求】请求客户端城市 + RequestIpIsp: ipInfo.Isp, //【请求】请求客户端运营商 + RequestIpLatitude: ipInfo.LocationLatitude, //【请求】请求客户端纬度 + RequestIpLongitude: ipInfo.LocationLongitude, //【请求】请求客户端经度 RequestHeader: dorm.JsonEncodeNoError(ginCtx.Request.Header), //【请求】请求头 ResponseTime: gotime.Current().Time, //【返回】时间 ResponseCode: responseCode, //【返回】状态码 @@ -145,10 +143,7 @@ func (c *GinClient) gormRecordXml(ginCtx *gin.Context, traceId string, requestTi data.RequestBody = dorm.XmlEncodeNoError(dorm.XmlDecodeNoError(requestBody)) //【请求】请求内容 } - err := c.gormRecord(data) - if err != nil { - c.zapLog.WithTraceIdStr(traceId).Sugar().Errorf("保存失败:%s", err.Error()) - } + c.gormRecord(data) } // GormDelete 删除 diff --git a/gin_mongo.go b/gin_mongo.go index 26683f1..61d2b6b 100644 --- a/gin_mongo.go +++ b/gin_mongo.go @@ -4,6 +4,7 @@ import ( "context" "github.com/gin-gonic/gin" "go.dtapp.net/dorm" + "go.dtapp.net/goip" "go.dtapp.net/gotime" "go.dtapp.net/gotrace_id" "go.dtapp.net/gourl" @@ -21,37 +22,43 @@ type ginMongoLogRequestIpLocationLocation struct { // 模型结构体 type ginMongoLog struct { - LogId primitive.ObjectID `json:"log_id,omitempty" bson:"_id,omitempty"` //【记录】编号 - LogTime primitive.DateTime `json:"log_time,omitempty" bson:"log_time"` //【记录】时间 - TraceId string `json:"trace_id,omitempty" bson:"trace_id,omitempty"` //【记录】跟踪编号 - RequestTime dorm.BsonTime `json:"request_time,omitempty" bson:"request_time,omitempty"` //【请求】时间 - RequestUri string `json:"request_uri,omitempty" bson:"request_uri,omitempty"` //【请求】请求链接 域名+路径+参数 - RequestUrl string `json:"request_url,omitempty" bson:"request_url,omitempty"` //【请求】请求链接 域名+路径 - RequestApi string `json:"request_api,omitempty" bson:"request_api,omitempty"` //【请求】请求接口 路径 - RequestMethod string `json:"request_method,omitempty" bson:"request_method,omitempty"` //【请求】请求方式 - RequestProto string `json:"request_proto,omitempty" bson:"request_proto,omitempty"` //【请求】请求协议 - RequestUa string `json:"request_ua,omitempty" bson:"request_ua,omitempty"` //【请求】请求UA - RequestReferer string `json:"request_referer,omitempty" bson:"request_referer,omitempty"` //【请求】请求referer - RequestBody interface{} `json:"request_body,omitempty" bson:"request_body,omitempty"` //【请求】请求主体 - RequestUrlQuery interface{} `json:"request_url_query,omitempty" bson:"request_url_query,omitempty"` //【请求】请求URL参数 - RequestIp string `json:"request_ip,omitempty" bson:"request_ip,omitempty"` //【请求】请求客户端Ip - RequestIpCountry string `json:"request_ip_country,omitempty" bson:"request_ip_country,omitempty"` //【请求】请求客户端国家 - RequestIpProvince string `json:"request_ip_province,omitempty" bson:"request_ip_province,omitempty"` //【请求】请求客户端省份 - RequestIpCity string `json:"request_ip_city,omitempty" bson:"request_ip_city,omitempty"` //【请求】请求客户端城市 - RequestIpIsp string `json:"request_ip_isp,omitempty" bson:"request_ip_isp,omitempty"` //【请求】请求客户端运营商 - RequestIpLocation interface{} `json:"request_ip_location,omitempty" bson:"request_ip_location,omitempty"` //【请求】请求客户端位置 - RequestHeader interface{} `json:"request_header,omitempty" bson:"request_header,omitempty"` //【请求】请求头 - ResponseTime dorm.BsonTime `json:"response_time,omitempty" bson:"response_time,omitempty"` //【返回】时间 - ResponseCode int `json:"response_code,omitempty" bson:"response_code,omitempty"` //【返回】状态码 - ResponseMsg string `json:"response_msg,omitempty" bson:"response_msg,omitempty"` //【返回】描述 - ResponseData interface{} `json:"response_data,omitempty" bson:"response_data,omitempty"` //【返回】数据 - CostTime int64 `json:"cost_time,omitempty" bson:"cost_time,omitempty"` //【系统】花费时间 - SystemHostName string `json:"system_host_name,omitempty" bson:"system_host_name,omitempty"` //【系统】主机名 - SystemInsideIp string `json:"system_inside_ip,omitempty" bson:"system_inside_ip,omitempty"` //【系统】内网ip - SystemOs string `json:"system_os,omitempty" bson:"system_os,omitempty"` //【系统】系统类型 - SystemArch string `json:"system_arch,omitempty" bson:"system_arch,omitempty"` //【系统】系统架构 - GoVersion string `json:"go_version,omitempty" bson:"go_version,omitempty"` //【程序】Go版本 - SdkVersion string `json:"sdk_version,omitempty" bson:"sdk_version,omitempty"` //【程序】Sdk版本 + LogId primitive.ObjectID `json:"log_id,omitempty" bson:"_id,omitempty"` //【记录】编号 + TraceId string `json:"trace_id,omitempty" bson:"trace_id,omitempty"` //【记录】跟踪编号 + RequestTime dorm.BsonTime `json:"request_time,omitempty" bson:"request_time,omitempty"` //【请求】时间 + RequestUri string `json:"request_uri,omitempty" bson:"request_uri,omitempty"` //【请求】请求链接 域名+路径+参数 + RequestUrl string `json:"request_url,omitempty" bson:"request_url,omitempty"` //【请求】请求链接 域名+路径 + RequestApi string `json:"request_api,omitempty" bson:"request_api,omitempty"` //【请求】请求接口 路径 + RequestMethod string `json:"request_method,omitempty" bson:"request_method,omitempty"` //【请求】请求方式 + RequestProto string `json:"request_proto,omitempty" bson:"request_proto,omitempty"` //【请求】请求协议 + RequestUa string `json:"request_ua,omitempty" bson:"request_ua,omitempty"` //【请求】请求UA + RequestReferer string `json:"request_referer,omitempty" bson:"request_referer,omitempty"` //【请求】请求referer + RequestBody interface{} `json:"request_body,omitempty" bson:"request_body,omitempty"` //【请求】请求主体 + RequestUrlQuery interface{} `json:"request_url_query,omitempty" bson:"request_url_query,omitempty"` //【请求】请求URL参数 + RequestIp struct { + Ip string `json:"ip,omitempty" bson:"ip,omitempty"` //【请求】请求客户端Ip + Continent string `json:"continent,omitempty" bson:"continent,omitempty"` //【请求】请求客户端大陆 + Country string `json:"country,omitempty" bson:"country,omitempty"` //【请求】请求客户端国家 + Province string `json:"province,omitempty" bson:"province,omitempty"` //【请求】请求客户端省份 + City string `json:"city,omitempty" bson:"city,omitempty"` //【请求】请求客户端城市 + Isp string `json:"isp,omitempty" bson:"isp,omitempty"` //【请求】请求客户端运营商 + } `json:"request_ip,omitempty" bson:"request_ip,omitempty"` //【请求】请求客户端信息 + RequestIpLocation interface{} `json:"request_ip_location,omitempty" bson:"request_ip_location,omitempty"` //【请求】请求客户端位置 + RequestHeader interface{} `json:"request_header,omitempty" bson:"request_header,omitempty"` //【请求】请求头 + ResponseTime dorm.BsonTime `json:"response_time,omitempty" bson:"response_time,omitempty"` //【返回】时间 + ResponseCode int `json:"response_code,omitempty" bson:"response_code,omitempty"` //【返回】状态码 + ResponseMsg string `json:"response_msg,omitempty" bson:"response_msg,omitempty"` //【返回】描述 + ResponseData interface{} `json:"response_data,omitempty" bson:"response_data,omitempty"` //【返回】数据 + CostTime int64 `json:"cost_time,omitempty" bson:"cost_time,omitempty"` //【系统】花费时间 + System struct { + HostName string `json:"host_name,omitempty" bson:"host_name,omitempty"` //【系统】主机名 + InsideIp string `json:"inside_ip,omitempty" bson:"inside_ip,omitempty"` //【系统】内网ip + Os string `json:"os,omitempty" bson:"os,omitempty"` //【系统】系统类型 + Arch string `json:"arch,omitempty" bson:"arch,omitempty"` //【系统】系统架构 + } `json:"system,omitempty" bson:"system,omitempty"` //【系统】信息 + Version struct { + Go string `json:"go,omitempty" bson:"go,omitempty"` //【程序】Go版本 + Sdk string `json:"sdk,omitempty" bson:"sdk,omitempty"` //【程序】Sdk版本 + } `json:"version,omitempty" bson:"version,omitempty"` //【程序】版本信息 } // 创建集合 @@ -75,32 +82,12 @@ func (c *GinClient) mongoCreateIndexes(ctx context.Context) { }}, }, { Keys: bson.D{{ - Key: "request_method", - Value: 1, - }}, - }, { - Keys: bson.D{{ - Key: "request_ip", - Value: 1, - }}, - }, { - Keys: bson.D{{ - Key: "request_ip_country", - Value: 1, - }}, - }, { - Keys: bson.D{{ - Key: "request_ip_province", - Value: 1, - }}, - }, { - Keys: bson.D{{ - Key: "request_ip_city", - Value: 1, + Key: "request_time", + Value: -1, }}, }, { Keys: bson.D{{ - Key: "request_ip_isp", + Key: "request_method", Value: 1, }}, }, { @@ -113,31 +100,6 @@ func (c *GinClient) mongoCreateIndexes(ctx context.Context) { Key: "response_code", Value: 1, }}, - }, { - Keys: bson.D{{ - Key: "system_host_name", - 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, - }}, }, { Keys: bson.D{{ Key: "request_ip_location", @@ -152,53 +114,47 @@ func (c *GinClient) mongoCreateIndexes(ctx context.Context) { // MongoDelete 删除 func (c *GinClient) MongoDelete(ctx context.Context, hour int64) (*mongo.DeleteResult, error) { - filter := bson.D{{"log_time", bson.D{{"$lt", primitive.NewDateTimeFromTime(gotime.Current().BeforeHour(hour).Time)}}}} + filter := bson.D{{"request_time", bson.D{{"$lt", dorm.NewBsonTimeFromTime(gotime.Current().BeforeHour(hour).Time)}}}} return c.mongoClient.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).DeleteMany(ctx, filter) } // 记录日志 func (c *GinClient) mongoRecord(ctx context.Context, mongoLog ginMongoLog) (err error) { - mongoLog.SystemHostName = c.config.systemHostName //【系统】主机名 - mongoLog.SystemInsideIp = c.config.systemInsideIp //【系统】内网ip - mongoLog.GoVersion = c.config.goVersion //【程序】Go版本 - mongoLog.SdkVersion = c.config.sdkVersion //【程序】Sdk版本 - mongoLog.SystemOs = c.config.systemOs //【系统】系统类型 - mongoLog.SystemArch = c.config.systemArch //【系统】系统架构 - mongoLog.LogId = primitive.NewObjectID() //【记录】编号 + mongoLog.System.HostName = c.config.systemHostName //【系统】主机名 + mongoLog.System.InsideIp = c.config.systemInsideIp //【系统】内网ip + mongoLog.System.Os = c.config.systemOs //【系统】系统类型 + mongoLog.System.Arch = c.config.systemArch //【系统】系统架构 + mongoLog.Version.Go = c.config.goVersion //【程序】Go版本 + mongoLog.Version.Sdk = c.config.sdkVersion //【程序】Sdk版本 + mongoLog.LogId = primitive.NewObjectID() //【记录】编号 _, err = c.mongoClient.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).InsertOne(ctx, mongoLog) if err != nil { - c.zapLog.WithTraceIdStr(mongoLog.TraceId).Sugar().Errorf("记录日志失败:%s", err) + c.zapLog.WithTraceIdStr(mongoLog.TraceId).Sugar().Errorf("保存框架日志失败:%s", err) } return err } -func (c *GinClient) mongoRecordJson(ginCtx *gin.Context, traceId string, requestTime time.Time, requestBody []byte, responseCode int, responseBody string, startTime, endTime int64, clientIp, requestClientIpCountry, requestClientIpProvince, requestClientIpCity, requestClientIpIsp string, requestClientIpLocationLatitude, requestClientIpLocationLongitude float64) { +func (c *GinClient) mongoRecordJson(ginCtx *gin.Context, traceId string, requestTime time.Time, requestBody []byte, responseCode int, responseBody string, startTime, endTime int64, ipInfo goip.AnalyseResult) { var ctx = gotrace_id.SetGinTraceIdContext(context.Background(), ginCtx) data := ginMongoLog{ - TraceId: traceId, //【记录】跟踪编号 - LogTime: primitive.NewDateTimeFromTime(requestTime), //【记录】时间 - RequestTime: dorm.NewBsonTimeFromTime(requestTime), //【请求】时间 - RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接 - RequestApi: gourl.UriFilterExcludeQueryString(ginCtx.Request.RequestURI), //【请求】请求接口 - RequestMethod: ginCtx.Request.Method, //【请求】请求方式 - RequestProto: ginCtx.Request.Proto, //【请求】请求协议 - RequestUa: ginCtx.Request.UserAgent(), //【请求】请求UA - RequestReferer: ginCtx.Request.Referer(), //【请求】请求referer - RequestUrlQuery: ginCtx.Request.URL.Query(), //【请求】请求URL参数 - RequestIp: clientIp, //【请求】请求客户端Ip - RequestIpCountry: requestClientIpCountry, //【请求】请求客户端国家 - RequestIpProvince: requestClientIpProvince, //【请求】请求客户端省份 - RequestIpCity: requestClientIpCity, //【请求】请求客户端城市 - RequestIpIsp: requestClientIpIsp, //【请求】请求客户端运营商 - RequestHeader: ginCtx.Request.Header, //【请求】请求头 - ResponseTime: dorm.NewBsonTimeCurrent(), //【返回】时间 - ResponseCode: responseCode, //【返回】状态码 - ResponseData: c.jsonUnmarshal(responseBody), //【返回】数据 - CostTime: endTime - startTime, //【系统】花费时间 + TraceId: traceId, //【记录】跟踪编号 + RequestTime: dorm.NewBsonTimeFromTime(requestTime), //【请求】时间 + RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接 + RequestApi: gourl.UriFilterExcludeQueryString(ginCtx.Request.RequestURI), //【请求】请求接口 + RequestMethod: ginCtx.Request.Method, //【请求】请求方式 + RequestProto: ginCtx.Request.Proto, //【请求】请求协议 + RequestUa: ginCtx.Request.UserAgent(), //【请求】请求UA + RequestReferer: ginCtx.Request.Referer(), //【请求】请求referer + RequestUrlQuery: ginCtx.Request.URL.Query(), //【请求】请求URL参数 + RequestHeader: ginCtx.Request.Header, //【请求】请求头 + ResponseTime: dorm.NewBsonTimeCurrent(), //【返回】时间 + ResponseCode: responseCode, //【返回】状态码 + ResponseData: dorm.JsonDecodeNoError([]byte(responseBody)), //【返回】数据 + CostTime: endTime - startTime, //【系统】花费时间 } if ginCtx.Request.TLS == nil { data.RequestUri = "http://" + ginCtx.Request.Host + ginCtx.Request.RequestURI //【请求】请求链接 @@ -210,44 +166,42 @@ func (c *GinClient) mongoRecordJson(ginCtx *gin.Context, traceId string, request data.RequestBody = dorm.JsonDecodeNoError(requestBody) //【请求】请求主体 } - if requestClientIpLocationLatitude != 0 && requestClientIpLocationLongitude != 0 { + //【请求】请求客户端信息 + data.RequestIp.Ip = ipInfo.Ip + data.RequestIp.Continent = ipInfo.Continent + data.RequestIp.Country = ipInfo.Country + data.RequestIp.Province = ipInfo.Province + data.RequestIp.City = ipInfo.City + data.RequestIp.City = ipInfo.Isp + if ipInfo.LocationLatitude != 0 && ipInfo.LocationLongitude != 0 { data.RequestIpLocation = ginMongoLogRequestIpLocationLocation{ Type: "Point", - Coordinates: []float64{requestClientIpLocationLongitude, requestClientIpLocationLatitude}, + Coordinates: []float64{ipInfo.LocationLongitude, ipInfo.LocationLatitude}, } } - err := c.mongoRecord(ctx, data) - if err != nil { - c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error()) - } + c.mongoRecord(ctx, data) } -func (c *GinClient) mongoRecordXml(ginCtx *gin.Context, traceId string, requestTime time.Time, requestBody []byte, responseCode int, responseBody string, startTime, endTime int64, clientIp, requestClientIpCountry, requestClientIpProvince, requestClientIpCity, requestClientIpIsp string, requestClientIpLocationLatitude, requestClientIpLocationLongitude float64) { +func (c *GinClient) mongoRecordXml(ginCtx *gin.Context, traceId string, requestTime time.Time, requestBody []byte, responseCode int, responseBody string, startTime, endTime int64, ipInfo goip.AnalyseResult) { var ctx = gotrace_id.SetGinTraceIdContext(context.Background(), ginCtx) data := ginMongoLog{ - TraceId: traceId, //【记录】跟踪编号 - LogTime: primitive.NewDateTimeFromTime(requestTime), //【记录】时间 - RequestTime: dorm.NewBsonTimeFromTime(requestTime), //【请求】时间 - RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接 - RequestApi: gourl.UriFilterExcludeQueryString(ginCtx.Request.RequestURI), //【请求】请求接口 - RequestMethod: ginCtx.Request.Method, //【请求】请求方式 - RequestProto: ginCtx.Request.Proto, //【请求】请求协议 - RequestUa: ginCtx.Request.UserAgent(), //【请求】请求UA - RequestReferer: ginCtx.Request.Referer(), //【请求】请求referer - RequestUrlQuery: ginCtx.Request.URL.Query(), //【请求】请求URL参数 - RequestIp: clientIp, //【请求】请求客户端Ip - RequestIpCountry: requestClientIpCountry, //【请求】请求客户端国家 - RequestIpProvince: requestClientIpProvince, //【请求】请求客户端省份 - RequestIpCity: requestClientIpCity, //【请求】请求客户端城市 - RequestIpIsp: requestClientIpIsp, //【请求】请求客户端运营商 - RequestHeader: ginCtx.Request.Header, //【请求】请求头 - ResponseTime: dorm.NewBsonTimeCurrent(), //【返回】时间 - ResponseCode: responseCode, //【返回】状态码 - ResponseData: c.jsonUnmarshal(responseBody), //【返回】数据 - CostTime: endTime - startTime, //【系统】花费时间 + TraceId: traceId, //【记录】跟踪编号 + RequestTime: dorm.NewBsonTimeFromTime(requestTime), //【请求】时间 + RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接 + RequestApi: gourl.UriFilterExcludeQueryString(ginCtx.Request.RequestURI), //【请求】请求接口 + RequestMethod: ginCtx.Request.Method, //【请求】请求方式 + RequestProto: ginCtx.Request.Proto, //【请求】请求协议 + RequestUa: ginCtx.Request.UserAgent(), //【请求】请求UA + RequestReferer: ginCtx.Request.Referer(), //【请求】请求referer + RequestUrlQuery: ginCtx.Request.URL.Query(), //【请求】请求URL参数 + RequestHeader: ginCtx.Request.Header, //【请求】请求头 + ResponseTime: dorm.NewBsonTimeCurrent(), //【返回】时间 + ResponseCode: responseCode, //【返回】状态码 + ResponseData: dorm.JsonDecodeNoError([]byte(responseBody)), //【返回】数据 + CostTime: endTime - startTime, //【系统】花费时间 } if ginCtx.Request.TLS == nil { data.RequestUri = "http://" + ginCtx.Request.Host + ginCtx.Request.RequestURI //【请求】请求链接 @@ -259,15 +213,19 @@ func (c *GinClient) mongoRecordXml(ginCtx *gin.Context, traceId string, requestT data.RequestBody = dorm.XmlDecodeNoError(requestBody) //【请求】请求主体 } - if requestClientIpLocationLatitude != 0 && requestClientIpLocationLongitude != 0 { + //【请求】请求客户端信息 + data.RequestIp.Ip = ipInfo.Ip + data.RequestIp.Continent = ipInfo.Continent + data.RequestIp.Country = ipInfo.Country + data.RequestIp.Province = ipInfo.Province + data.RequestIp.City = ipInfo.City + data.RequestIp.City = ipInfo.Isp + if ipInfo.LocationLatitude != 0 && ipInfo.LocationLongitude != 0 { data.RequestIpLocation = ginMongoLogRequestIpLocationLocation{ Type: "Point", - Coordinates: []float64{requestClientIpLocationLongitude, requestClientIpLocationLatitude}, + Coordinates: []float64{ipInfo.LocationLongitude, ipInfo.LocationLatitude}, } } - err := c.mongoRecord(ctx, data) - if err != nil { - c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error()) - } + c.mongoRecord(ctx, data) } diff --git a/go.mod b/go.mod index e36b17c..ec188bb 100644 --- a/go.mod +++ b/go.mod @@ -77,7 +77,7 @@ require ( go.uber.org/multierr v1.8.0 // indirect golang.org/x/crypto v0.0.0-20220919173607-35f4265a4bc0 // indirect golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect - golang.org/x/net v0.0.0-20220920203100-d0c6ba3f52d9 // indirect + golang.org/x/net v0.0.0-20220921203646-d300de134e69 // indirect golang.org/x/sync v0.0.0-20220907140024-f12130a52804 // indirect golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect golang.org/x/text v0.3.7 // indirect diff --git a/go.sum b/go.sum index abae04d..0ac85c7 100644 --- a/go.sum +++ b/go.sum @@ -591,8 +591,8 @@ golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20220920203100-d0c6ba3f52d9 h1:asZqf0wXastQr+DudYagQS8uBO8bHKeYD1vbAvGmFL8= -golang.org/x/net v0.0.0-20220920203100-d0c6ba3f52d9/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= +golang.org/x/net v0.0.0-20220921203646-d300de134e69 h1:hUJpGDpnfwdJW8iNypFjmSY0sCBEL+spFTZ2eO+Sfps= +golang.org/x/net v0.0.0-20220921203646-d300de134e69/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=