服务相关流量统计增加Header

This commit is contained in:
GoEdgeLab
2022-03-26 12:29:34 +08:00
parent 597b18f493
commit bcaa1426b0
4 changed files with 25 additions and 6 deletions

View File

@@ -321,14 +321,14 @@ func (this *HTTPRequest) doEnd() {
if this.isCached { if this.isCached {
countCached = 1 countCached = 1
cachedBytes = this.writer.SentBodyBytes() cachedBytes = this.writer.SentBodyBytes() + this.writer.SentHeaderBytes()
} }
if this.isAttack { if this.isAttack {
countAttacks = 1 countAttacks = 1
attackBytes = this.CalculateSize() attackBytes = this.CalculateSize()
} }
stats.SharedTrafficStatManager.Add(this.ReqServer.Id, this.ReqHost, this.writer.SentBodyBytes(), cachedBytes, 1, countCached, countAttacks, attackBytes, this.ReqServer.ShouldCheckTrafficLimit(), this.ReqServer.PlanId()) stats.SharedTrafficStatManager.Add(this.ReqServer.Id, this.ReqHost, this.writer.SentBodyBytes()+this.writer.SentHeaderBytes(), cachedBytes, 1, countCached, countAttacks, attackBytes, this.ReqServer.ShouldCheckTrafficLimit(), this.ReqServer.PlanId())
} }
// 指标 // 指标
@@ -1292,7 +1292,7 @@ func (this *HTTPRequest) CalculateSize() (size int64) {
size += int64(len(this.RawReq.Proto)) + 1 size += int64(len(this.RawReq.Proto)) + 1
for k, v := range this.RawReq.Header { for k, v := range this.RawReq.Header {
for _, v1 := range v { for _, v1 := range v {
size += int64(len(k) + 2 /** : **/ + len(v1)) size += int64(len(k) + 2 /** : **/ + len(v1) + 1)
} }
} }

View File

@@ -304,6 +304,7 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) {
// 读取Header // 读取Header
var headerBuf = []byte{} var headerBuf = []byte{}
this.writer.SetSentHeaderBytes(reader.HeaderSize())
err = reader.ReadHeader(buf, func(n int) (goNext bool, err error) { err = reader.ReadHeader(buf, func(n int) (goNext bool, err error) {
headerBuf = append(headerBuf, buf[:n]...) headerBuf = append(headerBuf, buf[:n]...)
for { for {

View File

@@ -58,8 +58,9 @@ type HTTPWriter struct {
size int64 size int64
statusCode int statusCode int
sentBodyBytes int64 sentBodyBytes int64
sentHeaderBytes int64
isOk bool // 是否完全成功 isOk bool // 是否完全成功
isFinished bool // 是否已完成 isFinished bool // 是否已完成
@@ -692,6 +693,23 @@ func (this *HTTPWriter) SentBodyBytes() int64 {
return this.sentBodyBytes return this.sentBodyBytes
} }
// SentHeaderBytes 计算发送的Header字节数
func (this *HTTPWriter) SentHeaderBytes() int64 {
if this.sentHeaderBytes > 0 {
return this.sentHeaderBytes
}
for k, v := range this.Header() {
for _, v1 := range v {
this.sentHeaderBytes += int64(len(k) + 2 + len(v1) + 1)
}
}
return this.sentHeaderBytes
}
func (this *HTTPWriter) SetSentHeaderBytes(sentHeaderBytes int64) {
this.sentHeaderBytes = sentHeaderBytes
}
// WriteHeader 写入状态码 // WriteHeader 写入状态码
func (this *HTTPWriter) WriteHeader(statusCode int) { func (this *HTTPWriter) WriteHeader(statusCode int) {
if this.rawWriter != nil { if this.rawWriter != nil {

View File

@@ -101,7 +101,7 @@ func (this *TrafficStatManager) Add(serverId int64, domain string, bytes int64,
this.totalRequests++ this.totalRequests++
timestamp := utils.FloorUnixTime(300) var timestamp = utils.FloorUnixTime(300)
key := strconv.FormatInt(timestamp, 10) + strconv.FormatInt(serverId, 10) key := strconv.FormatInt(timestamp, 10) + strconv.FormatInt(serverId, 10)
this.locker.Lock() this.locker.Lock()