mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 07:40:56 +08:00 
			
		
		
		
	实现基础的统计指标
This commit is contained in:
		
							
								
								
									
										58
									
								
								internal/nodes/http_request_metrics.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								internal/nodes/http_request_metrics.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,58 @@
 | 
			
		||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
 | 
			
		||||
 | 
			
		||||
package nodes
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeNode/internal/metrics"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// 指标统计 - 响应
 | 
			
		||||
// 只需要在结束时调用指标进行统计
 | 
			
		||||
func (this *HTTPRequest) doMetricsResponse() {
 | 
			
		||||
	metrics.SharedManager.Add(this)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *HTTPRequest) MetricKey(key string) string {
 | 
			
		||||
	return this.Format(key)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *HTTPRequest) MetricValue(value string) (result int64, ok bool) {
 | 
			
		||||
	// TODO 需要忽略健康检查的请求,但是同时也要防止攻击者模拟健康检查
 | 
			
		||||
	switch value {
 | 
			
		||||
	case "${countRequest}":
 | 
			
		||||
		return 1, true
 | 
			
		||||
	case "${countTrafficOut}":
 | 
			
		||||
		// 这里不包括Header长度
 | 
			
		||||
		return this.writer.SentBodyBytes(), true
 | 
			
		||||
	case "${countTrafficIn}":
 | 
			
		||||
		var hl int64 = 0 // header length
 | 
			
		||||
		for k, values := range this.RawReq.Header {
 | 
			
		||||
			for _, v := range values {
 | 
			
		||||
				hl += int64(len(k) + len(v) + 2 /** k: v  **/)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		return this.RawReq.ContentLength + hl, true
 | 
			
		||||
	case "${countConnection}":
 | 
			
		||||
		metricNewConnMapLocker.Lock()
 | 
			
		||||
		_, ok := metricNewConnMap[this.RawReq.RemoteAddr]
 | 
			
		||||
		if ok {
 | 
			
		||||
			delete(metricNewConnMap, this.RawReq.RemoteAddr)
 | 
			
		||||
		}
 | 
			
		||||
		metricNewConnMapLocker.Unlock()
 | 
			
		||||
		if ok {
 | 
			
		||||
			return 1, true
 | 
			
		||||
		} else {
 | 
			
		||||
			return 0, false
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return 0, false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *HTTPRequest) MetricServerId() int64 {
 | 
			
		||||
	return this.Server.Id
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *HTTPRequest) MetricCategory() string {
 | 
			
		||||
	return serverconfigs.MetricItemCategoryHTTP
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user