使用ES存储访问日志时自动生成requestId

This commit is contained in:
刘祥超
2021-07-29 17:34:44 +08:00
parent bda407fc3f
commit 87f032bebd

View File

@@ -6,14 +6,16 @@ import (
"errors" "errors"
"fmt" "fmt"
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const" teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"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"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/logs"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"regexp" "regexp"
"strconv"
"strings" "strings"
"sync/atomic"
"time" "time"
) )
@@ -55,27 +57,31 @@ 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{}
id := time.Now().UnixNano()
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 {
id++ if len(accessLog.RequestId) == 0 {
accessLog.RequestId = strconv.FormatInt(time.Now().UnixNano(), 10) + strconv.FormatInt(atomic.AddInt64(&requestId, 1), 10) + fmt.Sprintf("%08d", 1)
}
opData, err := json.Marshal(map[string]interface{}{ opData, err := json.Marshal(map[string]interface{}{
"index": map[string]interface{}{ "index": map[string]interface{}{
"_index": indexName, "_index": indexName,
"_type": typeName, "_type": typeName,
"_id": fmt.Sprintf("%d", id), "_id": accessLog.RequestId,
}, },
}) })
if err != nil { if err != nil {
logs.Error(err) remotelogs.Error("ACCESS_LOG_ES_STORAGE", "write failed: "+err.Error())
continue continue
} }
data, err := this.Marshal(accessLog) data, err := this.Marshal(accessLog)
if err != nil { if err != nil {
logs.Error(err) remotelogs.Error("ACCESS_LOG_ES_STORAGE", "marshal data failed: "+err.Error())
continue continue
} }