You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.2 KiB
Go

package metrics
import "sync/atomic"
// Metrics 指标接口
type Metrics interface {
// IncConnections 增加连接数
IncConnections()
// DecConnections 减少连接数
DecConnections()
// GetConnections 获取当前连接数
GetConnections() int64
// IncRequests 增加请求数
IncRequests()
// GetRequests 获取请求总数
GetRequests() int64
// IncErrors 增加错误数
IncErrors()
// GetErrors 获取错误总数
GetErrors() int64
// AddBytesReceived 增加接收字节数
AddBytesReceived(bytes int64)
// GetBytesReceived 获取接收字节总数
GetBytesReceived() int64
// AddBytesSent 增加发送字节数
AddBytesSent(bytes int64)
// GetBytesSent 获取发送字节总数
GetBytesSent() int64
}
// metrics 指标实现
type metrics struct {
connections int64
requests int64
errors int64
bytesReceived int64
bytesSent int64
}
// NewMetrics 创建指标实例
func NewMetrics() Metrics {
return &metrics{}
}
// IncConnections 增加连接数
func (m *metrics) IncConnections() {
atomic.AddInt64(&m.connections, 1)
}
// DecConnections 减少连接数
func (m *metrics) DecConnections() {
atomic.AddInt64(&m.connections, -1)
}
// GetConnections 获取当前连接数
func (m *metrics) GetConnections() int64 {
return atomic.LoadInt64(&m.connections)
}
// IncRequests 增加请求数
func (m *metrics) IncRequests() {
atomic.AddInt64(&m.requests, 1)
}
// GetRequests 获取请求总数
func (m *metrics) GetRequests() int64 {
return atomic.LoadInt64(&m.requests)
}
// IncErrors 增加错误数
func (m *metrics) IncErrors() {
atomic.AddInt64(&m.errors, 1)
}
// GetErrors 获取错误总数
func (m *metrics) GetErrors() int64 {
return atomic.LoadInt64(&m.errors)
}
// AddBytesReceived 增加接收字节数
func (m *metrics) AddBytesReceived(bytes int64) {
atomic.AddInt64(&m.bytesReceived, bytes)
}
// GetBytesReceived 获取接收字节总数
func (m *metrics) GetBytesReceived() int64 {
return atomic.LoadInt64(&m.bytesReceived)
}
// AddBytesSent 增加发送字节数
func (m *metrics) AddBytesSent(bytes int64) {
atomic.AddInt64(&m.bytesSent, bytes)
}
// GetBytesSent 获取发送字节总数
func (m *metrics) GetBytesSent() int64 {
return atomic.LoadInt64(&m.bytesSent)
}