- 删除mongo
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
ec422e395e
commit
9d616ddad0
@ -1,113 +0,0 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.dtapp.net/dorm"
|
||||
"go.dtapp.net/gorequest"
|
||||
"go.dtapp.net/gotime"
|
||||
"go.dtapp.net/gotrace_id"
|
||||
"go.dtapp.net/gourl"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// 模型结构体
|
||||
type apiMongoLog struct {
|
||||
LogId primitive.ObjectID `json:"log_id,omitempty" bson:"_id,omitempty"` //【记录】编号
|
||||
TraceId string `json:"trace_id,omitempty" bson:"trace_id,omitempty"` //【系统】跟踪编号
|
||||
RequestTime int64 `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"` //【请求】方式
|
||||
RequestParams map[string]interface{} `json:"request_params,omitempty" bson:"request_params,omitempty"` //【请求】参数
|
||||
RequestHeader map[string]string `json:"request_header,omitempty" bson:"request_header,omitempty"` //【请求】头部
|
||||
ResponseHeader map[string][]string `json:"response_header,omitempty" bson:"response_header,omitempty"` //【返回】头部
|
||||
ResponseStatusCode int `json:"response_status_code,omitempty" bson:"response_status_code,omitempty"` //【返回】状态码
|
||||
ResponseBody interface{} `json:"response_body,omitempty" bson:"response_body,omitempty"` //【返回】内容
|
||||
ResponseContentLength int64 `json:"response_content_length,omitempty" bson:"response_content_length,omitempty"` //【返回】大小
|
||||
ResponseTime int64 `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
|
||||
GoVersion string `json:"go_version,omitempty" bson:"go_version,omitempty"` //【程序】Go版本
|
||||
SdkVersion string `json:"sdk_version,omitempty" bson:"sdk_version,omitempty"` //【程序】Sdk版本
|
||||
}
|
||||
|
||||
// 记录日志
|
||||
func (c *ApiClient) mongoRecord(ctx context.Context, mongoLog apiMongoLog) error {
|
||||
|
||||
mongoLog.SystemHostName = c.config.hostname
|
||||
if mongoLog.SystemInsideIp == "" {
|
||||
mongoLog.SystemInsideIp = c.config.insideIp
|
||||
}
|
||||
mongoLog.GoVersion = c.config.goVersion
|
||||
|
||||
mongoLog.TraceId = gotrace_id.GetTraceIdContext(ctx)
|
||||
|
||||
mongoLog.LogId = primitive.NewObjectID()
|
||||
|
||||
_, err := c.mongoClient.Database(c.config.databaseName).Collection(c.config.collectionName).InsertOne(mongoLog)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// MongoQuery 查询
|
||||
func (c *ApiClient) MongoQuery() *dorm.MongoClient {
|
||||
return c.mongoClient.Database(c.config.databaseName).Collection(c.config.collectionName)
|
||||
}
|
||||
|
||||
// MongoMiddleware 中间件
|
||||
func (c *ApiClient) MongoMiddleware(ctx context.Context, request gorequest.Response, sdkVersion string) {
|
||||
c.mongoRecord(ctx, apiMongoLog{
|
||||
RequestTime: gotime.SetCurrent(request.RequestTime).Timestamp(), //【请求】时间
|
||||
RequestUri: request.RequestUri, //【请求】链接
|
||||
RequestUrl: gourl.UriParse(request.RequestUri).Url, //【请求】链接
|
||||
RequestApi: gourl.UriParse(request.RequestUri).Path, //【请求】接口
|
||||
RequestMethod: request.RequestMethod, //【请求】方式
|
||||
RequestParams: request.RequestParams, //【请求】参数
|
||||
RequestHeader: request.RequestHeader, //【请求】头部
|
||||
ResponseHeader: request.ResponseHeader, //【返回】头部
|
||||
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
|
||||
ResponseBody: request.ResponseBody, //【返回】内容
|
||||
ResponseContentLength: request.ResponseContentLength, //【返回】大小
|
||||
ResponseTime: gotime.SetCurrent(request.ResponseTime).Timestamp(), //【返回】时间
|
||||
SdkVersion: sdkVersion, //【程序】Sdk版本
|
||||
})
|
||||
}
|
||||
|
||||
// MongoMiddlewareXml 中间件
|
||||
func (c *ApiClient) MongoMiddlewareXml(ctx context.Context, request gorequest.Response, sdkVersion string) {
|
||||
c.mongoRecord(ctx, apiMongoLog{
|
||||
RequestTime: gotime.SetCurrent(request.RequestTime).Timestamp(), //【请求】时间
|
||||
RequestUri: request.RequestUri, //【请求】链接
|
||||
RequestUrl: gourl.UriParse(request.RequestUri).Url, //【请求】链接
|
||||
RequestApi: gourl.UriParse(request.RequestUri).Path, //【请求】接口
|
||||
RequestMethod: request.RequestMethod, //【请求】方式
|
||||
RequestParams: request.RequestParams, //【请求】参数
|
||||
RequestHeader: request.RequestHeader, //【请求】头部
|
||||
ResponseHeader: request.ResponseHeader, //【返回】头部
|
||||
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
|
||||
ResponseBody: dorm.XmlDecodeNoError(request.ResponseBody), //【返回】内容
|
||||
ResponseContentLength: request.ResponseContentLength, //【返回】大小
|
||||
ResponseTime: gotime.SetCurrent(request.ResponseTime).Timestamp(), //【返回】时间
|
||||
SdkVersion: sdkVersion, //【程序】Sdk版本
|
||||
})
|
||||
}
|
||||
|
||||
// MongoMiddlewareCustom 中间件
|
||||
func (c *ApiClient) MongoMiddlewareCustom(ctx context.Context, api string, request gorequest.Response, sdkVersion string) {
|
||||
c.mongoRecord(ctx, apiMongoLog{
|
||||
RequestTime: gotime.SetCurrent(request.RequestTime).Timestamp(), //【请求】时间
|
||||
RequestUri: request.RequestUri, //【请求】链接
|
||||
RequestUrl: gourl.UriParse(request.RequestUri).Url, //【请求】链接
|
||||
RequestApi: api, //【请求】接口
|
||||
RequestMethod: request.RequestMethod, //【请求】方式
|
||||
RequestParams: request.RequestParams, //【请求】参数
|
||||
RequestHeader: request.RequestHeader, //【请求】头部
|
||||
ResponseHeader: request.ResponseHeader, //【返回】头部
|
||||
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
|
||||
ResponseBody: request.ResponseBody, //【返回】内容
|
||||
ResponseContentLength: request.ResponseContentLength, //【返回】大小
|
||||
ResponseTime: gotime.SetCurrent(request.ResponseTime).Timestamp(), //【返回】时间
|
||||
SdkVersion: sdkVersion, //【程序】Sdk版本
|
||||
})
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
package golog
|
||||
|
||||
const Version = "1.0.26"
|
||||
const Version = "1.0.27"
|
||||
|
@ -1,190 +0,0 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.dtapp.net/dorm"
|
||||
"go.dtapp.net/gotime"
|
||||
"go.dtapp.net/gotrace_id"
|
||||
"go.dtapp.net/gourl"
|
||||
"go.dtapp.net/goxml"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
)
|
||||
|
||||
// 模型结构体
|
||||
type ginMongoLog struct {
|
||||
LogId primitive.ObjectID `json:"log_id,omitempty" bson:"_id,omitempty"` //【记录】编号
|
||||
TraceId string `json:"trace_id,omitempty" bson:"trace_id,omitempty"` //【系统】链编号
|
||||
RequestTime int64 `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"` //【请求】请求客户端城市
|
||||
RequestIpRegion string `json:"request_ip_region,omitempty" bson:"request_ip_region,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"` //【请求】请求客户端运营商
|
||||
RequestHeader interface{} `json:"request_header,omitempty" bson:"request_header,omitempty"` //【请求】请求头
|
||||
ResponseTime int64 `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
|
||||
GoVersion string `json:"go_version,omitempty" bson:"go_version,omitempty"` //【程序】Go版本
|
||||
SdkVersion string `json:"sdk_version,omitempty" bson:"sdk_version,omitempty"` //【程序】Sdk版本
|
||||
}
|
||||
|
||||
// 记录日志
|
||||
func (c *GinClient) mongoRecord(mongoLog ginMongoLog) error {
|
||||
|
||||
mongoLog.SystemHostName = c.config.hostname
|
||||
if mongoLog.SystemInsideIp == "" {
|
||||
mongoLog.SystemInsideIp = c.config.insideIp
|
||||
}
|
||||
mongoLog.GoVersion = c.config.goVersion
|
||||
|
||||
mongoLog.SdkVersion = Version
|
||||
|
||||
mongoLog.LogId = primitive.NewObjectID()
|
||||
|
||||
_, err := c.mongoClient.Database(c.config.databaseName).Collection(c.config.collectionName).InsertOne(mongoLog)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// MongoQuery 查询
|
||||
func (c *GinClient) MongoQuery() *dorm.MongoClient {
|
||||
return c.mongoClient.Database(c.config.databaseName).Collection(c.config.collectionName)
|
||||
}
|
||||
|
||||
// MongoMiddleware 中间件
|
||||
func (c *GinClient) MongoMiddleware() gin.HandlerFunc {
|
||||
return func(ginCtx *gin.Context) {
|
||||
|
||||
// 开始时间
|
||||
startTime := gotime.Current().TimestampWithMillisecond()
|
||||
requestTime := gotime.Current().Time
|
||||
|
||||
// 获取
|
||||
data, _ := ioutil.ReadAll(ginCtx.Request.Body)
|
||||
|
||||
// 复用
|
||||
ginCtx.Request.Body = ioutil.NopCloser(bytes.NewBuffer(data))
|
||||
|
||||
blw := &bodyLogWriter{body: bytes.NewBufferString(""), ResponseWriter: ginCtx.Writer}
|
||||
ginCtx.Writer = blw
|
||||
|
||||
// 处理请求
|
||||
ginCtx.Next()
|
||||
|
||||
// 响应
|
||||
responseCode := ginCtx.Writer.Status()
|
||||
responseBody := blw.body.String()
|
||||
|
||||
//结束时间
|
||||
endTime := gotime.Current().TimestampWithMillisecond()
|
||||
|
||||
go func() {
|
||||
|
||||
// 解析请求内容
|
||||
var xmlBody map[string]string
|
||||
var jsonBody map[string]interface{}
|
||||
_ = json.Unmarshal(data, &jsonBody)
|
||||
if len(jsonBody) <= 0 {
|
||||
xmlBody = goxml.XmlDecode(string(data))
|
||||
}
|
||||
|
||||
requestClientIpCountry, requestClientIpRegion, requestClientIpProvince, requestClientIpCity, requestClientIpIsp := "", "", "", "", ""
|
||||
if c.ipService != nil {
|
||||
if net.ParseIP(ginCtx.ClientIP()).To4() != nil {
|
||||
// 判断是不是IPV4
|
||||
_, info := c.ipService.Ipv4(ginCtx.ClientIP())
|
||||
requestClientIpCountry = info.Country
|
||||
requestClientIpRegion = info.Region
|
||||
requestClientIpProvince = info.Province
|
||||
requestClientIpCity = info.City
|
||||
requestClientIpIsp = info.ISP
|
||||
} else if net.ParseIP(ginCtx.ClientIP()).To16() != nil {
|
||||
// 判断是不是IPV6
|
||||
info := c.ipService.Ipv6(ginCtx.ClientIP())
|
||||
requestClientIpCountry = info.Country
|
||||
requestClientIpProvince = info.Province
|
||||
requestClientIpCity = info.City
|
||||
}
|
||||
}
|
||||
|
||||
// 记录
|
||||
if c.mongoClient != nil {
|
||||
host := ""
|
||||
if ginCtx.Request.TLS == nil {
|
||||
host = "http://" + ginCtx.Request.Host
|
||||
} else {
|
||||
host = "https://" + ginCtx.Request.Host
|
||||
}
|
||||
if len(jsonBody) > 0 {
|
||||
c.mongoRecord(ginMongoLog{
|
||||
TraceId: gotrace_id.GetGinTraceId(ginCtx), //【系统】链编号
|
||||
RequestTime: gotime.SetCurrent(requestTime).Timestamp(), //【请求】时间
|
||||
RequestUri: host + ginCtx.Request.RequestURI, //【请求】请求链接
|
||||
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
|
||||
RequestBody: jsonBody, //【请求】请求主体
|
||||
RequestUrlQuery: ginCtx.Request.URL.Query(), //【请求】请求URL参数
|
||||
RequestIp: ginCtx.ClientIP(), //【请求】请求客户端Ip
|
||||
RequestIpCountry: requestClientIpCountry, //【请求】请求客户端城市
|
||||
RequestIpRegion: requestClientIpRegion, //【请求】请求客户端区域
|
||||
RequestIpProvince: requestClientIpProvince, //【请求】请求客户端省份
|
||||
RequestIpCity: requestClientIpCity, //【请求】请求客户端城市
|
||||
RequestIpIsp: requestClientIpIsp, //【请求】请求客户端运营商
|
||||
RequestHeader: ginCtx.Request.Header, //【请求】请求头
|
||||
ResponseTime: gotime.SetCurrent(gotime.Current().Time).Timestamp(), //【返回】时间
|
||||
ResponseCode: responseCode, //【返回】状态码
|
||||
ResponseData: c.jsonUnmarshal(responseBody), //【返回】数据
|
||||
CostTime: endTime - startTime, //【系统】花费时间
|
||||
})
|
||||
} else {
|
||||
c.mongoRecord(ginMongoLog{
|
||||
TraceId: gotrace_id.GetGinTraceId(ginCtx), //【系统】链编号
|
||||
RequestTime: gotime.SetCurrent(requestTime).Timestamp(), //【请求】时间
|
||||
RequestUri: host + ginCtx.Request.RequestURI, //【请求】请求链接
|
||||
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
|
||||
RequestBody: xmlBody, //【请求】请求主体
|
||||
RequestUrlQuery: ginCtx.Request.URL.Query(), //【请求】请求URL参数
|
||||
RequestIp: ginCtx.ClientIP(), //【请求】请求客户端Ip
|
||||
RequestIpCountry: requestClientIpCountry, //【请求】请求客户端城市
|
||||
RequestIpRegion: requestClientIpRegion, //【请求】请求客户端区域
|
||||
RequestIpProvince: requestClientIpProvince, //【请求】请求客户端省份
|
||||
RequestIpCity: requestClientIpCity, //【请求】请求客户端城市
|
||||
RequestIpIsp: requestClientIpIsp, //【请求】请求客户端运营商
|
||||
RequestHeader: ginCtx.Request.Header, //【请求】请求头
|
||||
ResponseTime: gotime.SetCurrent(gotime.Current().Time).Timestamp(), //【返回】时间
|
||||
ResponseCode: responseCode, //【返回】状态码
|
||||
ResponseData: c.jsonUnmarshal(responseBody), //【返回】数据
|
||||
CostTime: endTime - startTime, //【系统】花费时间
|
||||
})
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
@ -1,53 +0,0 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"go.dtapp.net/dorm"
|
||||
"go.dtapp.net/goip"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
logTypeGorm = "gorm"
|
||||
logTypeMongo = "mongo"
|
||||
)
|
||||
|
||||
// OperationAttr 操作属性
|
||||
type OperationAttr struct {
|
||||
gormClient *gorm.DB // 数据库驱动
|
||||
mongoClient *dorm.MongoClient // 数据库驱动
|
||||
ipService *goip.Client // ip服务
|
||||
logType string // 类型
|
||||
tableName string // 表名
|
||||
databaseName string // 库名
|
||||
collectionName string // 表名
|
||||
}
|
||||
|
||||
// WithGormClient 设置数据库驱动
|
||||
func WithGormClient(client *gorm.DB) *OperationAttr {
|
||||
return &OperationAttr{gormClient: client, logType: logTypeGorm}
|
||||
}
|
||||
|
||||
// WithMongoClient 设置数据库驱动
|
||||
func WithMongoClient(client *dorm.MongoClient) *OperationAttr {
|
||||
return &OperationAttr{mongoClient: client, logType: logTypeMongo}
|
||||
}
|
||||
|
||||
// WithTableName 设置表名
|
||||
func WithTableName(tableName string) *OperationAttr {
|
||||
return &OperationAttr{tableName: tableName}
|
||||
}
|
||||
|
||||
// WithDatabaseName 设置库名
|
||||
func WithDatabaseName(databaseName string) *OperationAttr {
|
||||
return &OperationAttr{databaseName: databaseName}
|
||||
}
|
||||
|
||||
// WithCollectionName 设置表名
|
||||
func WithCollectionName(collectionName string) *OperationAttr {
|
||||
return &OperationAttr{collectionName: collectionName}
|
||||
}
|
||||
|
||||
// WithIpService 设置ip服务
|
||||
func WithIpService(ipService *goip.Client) *OperationAttr {
|
||||
return &OperationAttr{ipService: ipService}
|
||||
}
|
@ -1 +0,0 @@
|
||||
package golog
|
Loading…
Reference in new issue