mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-03 23:20:26 +08:00
优化访问日志的requestId
This commit is contained in:
@@ -13,9 +13,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -57,14 +55,12 @@ func (this *ESStorage) Write(accessLogs []*pb.HTTPAccessLog) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var requestId int64 = 1_0000_0000_0000_0000
|
|
||||||
|
|
||||||
bulk := &strings.Builder{}
|
bulk := &strings.Builder{}
|
||||||
indexName := this.FormatVariables(this.config.Index)
|
indexName := this.FormatVariables(this.config.Index)
|
||||||
typeName := this.FormatVariables(this.config.MappingType)
|
typeName := this.FormatVariables(this.config.MappingType)
|
||||||
for _, accessLog := range accessLogs {
|
for _, accessLog := range accessLogs {
|
||||||
if len(accessLog.RequestId) == 0 {
|
if len(accessLog.RequestId) == 0 {
|
||||||
accessLog.RequestId = strconv.FormatInt(time.Now().UnixNano(), 10) + strconv.FormatInt(atomic.AddInt64(&requestId, 1), 10) + fmt.Sprintf("%08d", 1)
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
opData, err := json.Marshal(map[string]interface{}{
|
opData, err := json.Marshal(map[string]interface{}{
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package configs
|
package configs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||||
"github.com/go-yaml/yaml"
|
"github.com/go-yaml/yaml"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
@@ -11,7 +10,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var sharedAPIConfig *APIConfig = nil
|
var sharedAPIConfig *APIConfig = nil
|
||||||
var PaddingId string
|
|
||||||
|
|
||||||
// APIConfig API节点配置
|
// APIConfig API节点配置
|
||||||
type APIConfig struct {
|
type APIConfig struct {
|
||||||
@@ -100,7 +98,6 @@ func SharedAPIConfig() (*APIConfig, error) {
|
|||||||
func (this *APIConfig) SetNumberId(numberId int64) {
|
func (this *APIConfig) SetNumberId(numberId int64) {
|
||||||
this.numberId = numberId
|
this.numberId = numberId
|
||||||
teaconst.NodeId = numberId
|
teaconst.NodeId = numberId
|
||||||
PaddingId = fmt.Sprintf("%08d", numberId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NumberId 获取数字ID
|
// NumberId 获取数字ID
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/configs"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
@@ -18,7 +17,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -82,7 +80,7 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLogsWithDAO(tx *dbs.Tx, daoWrapper
|
|||||||
fields["nodeId"] = accessLog.NodeId
|
fields["nodeId"] = accessLog.NodeId
|
||||||
fields["status"] = accessLog.Status
|
fields["status"] = accessLog.Status
|
||||||
fields["createdAt"] = accessLog.Timestamp
|
fields["createdAt"] = accessLog.Timestamp
|
||||||
fields["requestId"] = accessLog.RequestId + strconv.FormatInt(time.Now().UnixNano(), 10) + configs.PaddingId
|
fields["requestId"] = accessLog.RequestId
|
||||||
fields["firewallPolicyId"] = accessLog.FirewallPolicyId
|
fields["firewallPolicyId"] = accessLog.FirewallPolicyId
|
||||||
fields["firewallRuleGroupId"] = accessLog.FirewallRuleGroupId
|
fields["firewallRuleGroupId"] = accessLog.FirewallRuleGroupId
|
||||||
fields["firewallRuleSetId"] = accessLog.FirewallRuleSetId
|
fields["firewallRuleSetId"] = accessLog.FirewallRuleSetId
|
||||||
@@ -407,7 +405,7 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, s
|
|||||||
|
|
||||||
// FindAccessLogWithRequestId 根据请求ID获取访问日志
|
// FindAccessLogWithRequestId 根据请求ID获取访问日志
|
||||||
func (this *HTTPAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId string) (*HTTPAccessLog, error) {
|
func (this *HTTPAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId string) (*HTTPAccessLog, error) {
|
||||||
if !regexp.MustCompile(`^\d{30,}`).MatchString(requestId) {
|
if !regexp.MustCompile(`^\d{11,}`).MatchString(requestId) {
|
||||||
return nil, errors.New("invalid requestId")
|
return nil, errors.New("invalid requestId")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/configs"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@@ -14,7 +13,6 @@ import (
|
|||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -78,7 +76,7 @@ func (this *NSAccessLogDAO) CreateNSAccessLogsWithDAO(tx *dbs.Tx, daoWrapper *NS
|
|||||||
fields["domainId"] = accessLog.NsDomainId
|
fields["domainId"] = accessLog.NsDomainId
|
||||||
fields["recordId"] = accessLog.NsRecordId
|
fields["recordId"] = accessLog.NsRecordId
|
||||||
fields["createdAt"] = accessLog.Timestamp
|
fields["createdAt"] = accessLog.Timestamp
|
||||||
fields["requestId"] = accessLog.RequestId + strconv.FormatInt(time.Now().UnixNano(), 10) + configs.PaddingId
|
fields["requestId"] = accessLog.RequestId
|
||||||
|
|
||||||
content, err := json.Marshal(accessLog)
|
content, err := json.Marshal(accessLog)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -259,7 +257,7 @@ func (this *NSAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, siz
|
|||||||
|
|
||||||
// FindAccessLogWithRequestId 根据请求ID获取访问日志
|
// FindAccessLogWithRequestId 根据请求ID获取访问日志
|
||||||
func (this *NSAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId string) (*NSAccessLog, error) {
|
func (this *NSAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId string) (*NSAccessLog, error) {
|
||||||
if !regexp.MustCompile(`^\d{30,}`).MatchString(requestId) {
|
if !regexp.MustCompile(`^\d{11,}`).MatchString(requestId) {
|
||||||
return nil, errors.New("invalid requestId")
|
return nil, errors.New("invalid requestId")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user