mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 08:31:55 +08:00
refactor(metrics): Optimize atomic counter initialization and status code tracking
- Refactor counter initialization to use pointer creation for better type safety - Simplify atomic counter handling in RecordRequest method - Add status code statistics collection with atomic load operations - Improve consistency in counter initialization across different metrics tracking
This commit is contained in:
parent
9de17edcbd
commit
4b1c774509
@ -119,8 +119,9 @@ func (c *Collector) RecordRequest(path string, status int, latency time.Duration
|
|||||||
if counter, ok := c.intervalStatusCodes.Load(statusKey); ok {
|
if counter, ok := c.intervalStatusCodes.Load(statusKey); ok {
|
||||||
atomic.AddInt64(counter.(*int64), 1)
|
atomic.AddInt64(counter.(*int64), 1)
|
||||||
} else {
|
} else {
|
||||||
var count int64 = 1
|
counter := new(int64)
|
||||||
c.intervalStatusCodes.Store(statusKey, &count)
|
*counter = 1
|
||||||
|
c.intervalStatusCodes.Store(statusKey, counter)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新最小和最大响应时间
|
// 更新最小和最大响应时间
|
||||||
@ -179,18 +180,31 @@ func (c *Collector) RecordRequest(path string, status int, latency time.Duration
|
|||||||
if counter, ok := c.errorTypes.Load(errKey); ok {
|
if counter, ok := c.errorTypes.Load(errKey); ok {
|
||||||
atomic.AddInt64(counter.(*int64), 1)
|
atomic.AddInt64(counter.(*int64), 1)
|
||||||
} else {
|
} else {
|
||||||
var count int64 = 1
|
counter := new(int64)
|
||||||
c.errorTypes.Store(errKey, &count)
|
*counter = 1
|
||||||
|
c.errorTypes.Store(errKey, counter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 收集状态码统计
|
||||||
|
statusCodeStats := make(map[string]int64)
|
||||||
|
c.statusCodeStats.Range(func(key, value interface{}) bool {
|
||||||
|
if counter, ok := value.(*int64); ok {
|
||||||
|
statusCodeStats[key.(string)] = atomic.LoadInt64(counter)
|
||||||
|
} else {
|
||||||
|
statusCodeStats[key.(string)] = value.(int64)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
// 更新状态码统计
|
// 更新状态码统计
|
||||||
statusKey = fmt.Sprintf("%d", status)
|
statusKey = fmt.Sprintf("%d", status)
|
||||||
if counter, ok := c.statusCodeStats.Load(statusKey); ok {
|
if counter, ok := c.statusCodeStats.Load(statusKey); ok {
|
||||||
atomic.AddInt64(counter.(*int64), 1)
|
atomic.AddInt64(counter.(*int64), 1)
|
||||||
} else {
|
} else {
|
||||||
var count int64 = 1
|
counter := new(int64)
|
||||||
c.statusCodeStats.Store(statusKey, &count)
|
*counter = 1
|
||||||
|
c.statusCodeStats.Store(statusKey, counter)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新路径统计
|
// 更新路径统计
|
||||||
|
Loading…
x
Reference in New Issue
Block a user