241 lines
6.8 KiB
Go
241 lines
6.8 KiB
Go
package monitoring
|
||
|
||
import (
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/prometheus/client_golang/prometheus"
|
||
"github.com/prometheus/client_golang/prometheus/testutil"
|
||
)
|
||
|
||
func TestMetricsCollector(t *testing.T) {
|
||
// 创建指标收集器
|
||
collector := NewMetricsCollector()
|
||
|
||
// 测试写入操作指标
|
||
t.Run("WriteMetrics", func(t *testing.T) {
|
||
// 记录写入操作
|
||
collector.RecordWrite(10 * time.Millisecond)
|
||
|
||
// 验证写入总数
|
||
writeTotal := testutil.ToFloat64(collector.writeTotal)
|
||
if writeTotal != 1 {
|
||
t.Errorf("Expected write_total to be 1, got %v", writeTotal)
|
||
}
|
||
|
||
// 对于Histogram类型,我们只验证它是否被正确注册和收集
|
||
// 而不是尝试获取其具体值
|
||
registry := prometheus.NewPedanticRegistry()
|
||
registry.MustRegister(collector.writeLatency)
|
||
metrics, err := registry.Gather()
|
||
if err != nil {
|
||
t.Errorf("Failed to gather metrics: %v", err)
|
||
}
|
||
if len(metrics) == 0 {
|
||
t.Error("Expected write_latency to be collected, but got no metrics")
|
||
}
|
||
})
|
||
|
||
// 测试查询操作指标
|
||
t.Run("QueryMetrics", func(t *testing.T) {
|
||
// 记录查询操作
|
||
collector.RecordQuery(20 * time.Millisecond)
|
||
|
||
// 验证查询总数
|
||
queryTotal := testutil.ToFloat64(collector.queryTotal)
|
||
if queryTotal != 1 {
|
||
t.Errorf("Expected query_total to be 1, got %v", queryTotal)
|
||
}
|
||
|
||
// 对于Histogram类型,我们只验证它是否被正确注册和收集
|
||
registry := prometheus.NewPedanticRegistry()
|
||
registry.MustRegister(collector.queryLatency)
|
||
metrics, err := registry.Gather()
|
||
if err != nil {
|
||
t.Errorf("Failed to gather metrics: %v", err)
|
||
}
|
||
if len(metrics) == 0 {
|
||
t.Error("Expected query_latency to be collected, but got no metrics")
|
||
}
|
||
})
|
||
|
||
// 测试连接数指标
|
||
t.Run("ConnectionMetrics", func(t *testing.T) {
|
||
// 增加连接数
|
||
collector.IncActiveConnections()
|
||
collector.IncActiveConnections()
|
||
|
||
// 验证活跃连接数
|
||
activeConns := testutil.ToFloat64(collector.activeConnections)
|
||
if activeConns != 2 {
|
||
t.Errorf("Expected active_connections to be 2, got %v", activeConns)
|
||
}
|
||
|
||
// 减少连接数
|
||
collector.DecActiveConnections()
|
||
|
||
// 验证更新后的活跃连接数
|
||
activeConns = testutil.ToFloat64(collector.activeConnections)
|
||
if activeConns != 1 {
|
||
t.Errorf("Expected active_connections to be 1, got %v", activeConns)
|
||
}
|
||
})
|
||
|
||
// 测试数据点数量指标
|
||
t.Run("DataPointsMetrics", func(t *testing.T) {
|
||
// 设置数据点数量
|
||
collector.SetDataPointsCount(100)
|
||
|
||
// 验证数据点数量
|
||
dataPoints := testutil.ToFloat64(collector.dataPointsCount)
|
||
if dataPoints != 100 {
|
||
t.Errorf("Expected data_points_count to be 100, got %v", dataPoints)
|
||
}
|
||
})
|
||
|
||
// 测试持久化指标
|
||
t.Run("PersistenceMetrics", func(t *testing.T) {
|
||
// 记录持久化操作
|
||
collector.RecordPersistence(30*time.Millisecond, nil)
|
||
|
||
// 对于Histogram类型,我们只验证它是否被正确注册和收集
|
||
registry := prometheus.NewPedanticRegistry()
|
||
registry.MustRegister(collector.persistenceLatency)
|
||
metrics, err := registry.Gather()
|
||
if err != nil {
|
||
t.Errorf("Failed to gather metrics: %v", err)
|
||
}
|
||
if len(metrics) == 0 {
|
||
t.Error("Expected persistence_latency to be collected, but got no metrics")
|
||
}
|
||
|
||
// 验证持久化错误数(应该为0,因为没有错误)
|
||
persistenceErrors := testutil.ToFloat64(collector.persistenceErrors)
|
||
if persistenceErrors != 0 {
|
||
t.Errorf("Expected persistence_errors to be 0, got %v", persistenceErrors)
|
||
}
|
||
|
||
// 记录持久化错误
|
||
collector.RecordPersistence(30*time.Millisecond, errTestPersistence)
|
||
|
||
// 验证持久化错误数(应该为1)
|
||
persistenceErrors = testutil.ToFloat64(collector.persistenceErrors)
|
||
if persistenceErrors != 1 {
|
||
t.Errorf("Expected persistence_errors to be 1, got %v", persistenceErrors)
|
||
}
|
||
})
|
||
|
||
// 测试消息系统指标
|
||
t.Run("MessagingMetrics", func(t *testing.T) {
|
||
// 记录消息操作
|
||
collector.RecordMessaging(40*time.Millisecond, nil)
|
||
|
||
// 对于Histogram类型,我们只验证它是否被正确注册和收集
|
||
registry := prometheus.NewPedanticRegistry()
|
||
registry.MustRegister(collector.messagingLatency)
|
||
metrics, err := registry.Gather()
|
||
if err != nil {
|
||
t.Errorf("Failed to gather metrics: %v", err)
|
||
}
|
||
if len(metrics) == 0 {
|
||
t.Error("Expected messaging_latency to be collected, but got no metrics")
|
||
}
|
||
|
||
// 验证消息错误数(应该为0,因为没有错误)
|
||
messagingErrors := testutil.ToFloat64(collector.messagingErrors)
|
||
if messagingErrors != 0 {
|
||
t.Errorf("Expected messaging_errors to be 0, got %v", messagingErrors)
|
||
}
|
||
|
||
// 记录消息错误
|
||
collector.RecordMessaging(40*time.Millisecond, errTestMessaging)
|
||
|
||
// 验证消息错误数(应该为1)
|
||
messagingErrors = testutil.ToFloat64(collector.messagingErrors)
|
||
if messagingErrors != 1 {
|
||
t.Errorf("Expected messaging_errors to be 1, got %v", messagingErrors)
|
||
}
|
||
})
|
||
|
||
// 测试WebSocket连接指标
|
||
t.Run("WebSocketMetrics", func(t *testing.T) {
|
||
// 增加WebSocket连接数
|
||
collector.IncWebSocketConnections()
|
||
collector.IncWebSocketConnections()
|
||
|
||
// 验证WebSocket连接数
|
||
wsConns := testutil.ToFloat64(collector.websocketConnections)
|
||
if wsConns != 2 {
|
||
t.Errorf("Expected websocket_connections to be 2, got %v", wsConns)
|
||
}
|
||
|
||
// 减少WebSocket连接数
|
||
collector.DecWebSocketConnections()
|
||
|
||
// 验证更新后的WebSocket连接数
|
||
wsConns = testutil.ToFloat64(collector.websocketConnections)
|
||
if wsConns != 1 {
|
||
t.Errorf("Expected websocket_connections to be 1, got %v", wsConns)
|
||
}
|
||
})
|
||
|
||
// 测试指标注册
|
||
t.Run("MetricsRegistration", func(t *testing.T) {
|
||
registry := prometheus.NewRegistry()
|
||
|
||
// 注册指标收集器
|
||
err := registry.Register(collector)
|
||
if err != nil {
|
||
t.Errorf("Failed to register metrics collector: %v", err)
|
||
}
|
||
|
||
// 验证所有指标都已注册
|
||
metricFamilies, err := registry.Gather()
|
||
if err != nil {
|
||
t.Errorf("Failed to gather metrics: %v", err)
|
||
}
|
||
|
||
expectedMetrics := []string{
|
||
"gotidb_write_total",
|
||
"gotidb_query_total",
|
||
"gotidb_write_latency_seconds",
|
||
"gotidb_query_latency_seconds",
|
||
"gotidb_active_connections",
|
||
"gotidb_data_points_count",
|
||
"gotidb_persistence_latency_seconds",
|
||
"gotidb_persistence_errors_total",
|
||
"gotidb_messaging_latency_seconds",
|
||
"gotidb_messaging_errors_total",
|
||
"gotidb_websocket_connections",
|
||
}
|
||
|
||
for _, metricName := range expectedMetrics {
|
||
found := false
|
||
for _, mf := range metricFamilies {
|
||
if *mf.Name == metricName {
|
||
found = true
|
||
break
|
||
}
|
||
}
|
||
if !found {
|
||
t.Errorf("Expected metric %s not found in registry", metricName)
|
||
}
|
||
}
|
||
})
|
||
}
|
||
|
||
// 测试错误
|
||
var (
|
||
errTestPersistence = &testError{msg: "test persistence error"}
|
||
errTestMessaging = &testError{msg: "test messaging error"}
|
||
)
|
||
|
||
// 测试错误类型
|
||
type testError struct {
|
||
msg string
|
||
}
|
||
|
||
func (e *testError) Error() string {
|
||
return e.msg
|
||
}
|