github.com/TeaOSLab/EdgeNode@v1.3.8/internal/nodes/http_request_metrics.go (about)

     1  // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package nodes
     4  
     5  import (
     6  	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
     7  	"github.com/TeaOSLab/EdgeNode/internal/metrics"
     8  )
     9  
    10  // 指标统计 - 响应
    11  // 只需要在结束时调用指标进行统计
    12  func (this *HTTPRequest) doMetricsResponse() {
    13  	metrics.SharedManager.Add(this)
    14  }
    15  
    16  func (this *HTTPRequest) MetricKey(key string) string {
    17  	return this.Format(key)
    18  }
    19  
    20  func (this *HTTPRequest) MetricValue(value string) (result int64, ok bool) {
    21  	// TODO 需要忽略健康检查的请求,但是同时也要防止攻击者模拟健康检查
    22  	switch value {
    23  	case "${countRequest}":
    24  		return 1, true
    25  	case "${countTrafficOut}":
    26  		// 这里不包括Header长度
    27  		return this.writer.SentBodyBytes(), true
    28  	case "${countTrafficIn}":
    29  		var hl int64 = 0 // header length
    30  		for k, values := range this.RawReq.Header {
    31  			for _, v := range values {
    32  				hl += int64(len(k) + len(v) + 2 /** k: v  **/)
    33  			}
    34  		}
    35  		return this.RawReq.ContentLength + hl, true
    36  	case "${countConnection}":
    37  		return 1, true
    38  	}
    39  	return 0, false
    40  }
    41  
    42  func (this *HTTPRequest) MetricServerId() int64 {
    43  	return this.ReqServer.Id
    44  }
    45  
    46  func (this *HTTPRequest) MetricCategory() string {
    47  	return serverconfigs.MetricItemCategoryHTTP
    48  }