- ErrorMetrics 并发安全测试 - AsyncLogWriter 错误指标真实记录测试 - HTTP Server 超时配置真实验证 - Prometheus 指标 HTTP 端点真实测试 - 日志文件输出真实写入测试
116 lines
2.6 KiB
Go
116 lines
2.6 KiB
Go
package metrics
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestMetricsEndpoint_RealHTTP(t *testing.T) {
|
|
// 设置一些指标值
|
|
SetActiveHosts(42)
|
|
SetActiveProviders(5)
|
|
RecordLogFlushError()
|
|
RecordLogDroppedEvent()
|
|
|
|
// 创建真实的 HTTP 服务器
|
|
server := httptest.NewServer(Handler())
|
|
defer server.Close()
|
|
|
|
// 使用真实 HTTP 客户端访问
|
|
resp, err := http.Get(server.URL + "/metrics")
|
|
if err != nil {
|
|
t.Fatalf("Failed to GET /metrics: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Errorf("Expected status 200, got %d", resp.StatusCode)
|
|
}
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
t.Fatalf("Failed to read body: %v", err)
|
|
}
|
|
|
|
bodyStr := string(body)
|
|
|
|
// 验证 Prometheus 格式
|
|
requiredElements := []string{
|
|
"# HELP",
|
|
"# TYPE",
|
|
"active_hosts 42",
|
|
"active_providers 5",
|
|
"log_flush_errors_total",
|
|
"log_dropped_events_total",
|
|
}
|
|
|
|
for _, elem := range requiredElements {
|
|
if !strings.Contains(bodyStr, elem) {
|
|
t.Errorf("Response missing: %q", elem)
|
|
}
|
|
}
|
|
|
|
// 验证内容类型
|
|
contentType := resp.Header.Get("Content-Type")
|
|
if !strings.Contains(contentType, "text/plain") {
|
|
t.Errorf("Content-Type = %q, want text/plain", contentType)
|
|
}
|
|
}
|
|
|
|
func TestMiddleware_RealRequest(t *testing.T) {
|
|
// 创建一个使用 middleware 的处理器
|
|
handler := Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusTeapot)
|
|
w.Write([]byte("I'm a teapot"))
|
|
}))
|
|
|
|
server := httptest.NewServer(handler)
|
|
defer server.Close()
|
|
|
|
// 发送真实请求
|
|
resp, err := http.Get(server.URL + "/test-path")
|
|
if err != nil {
|
|
t.Fatalf("Failed to GET: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusTeapot {
|
|
t.Errorf("Expected status %d, got %d", http.StatusTeapot, resp.StatusCode)
|
|
}
|
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
if string(body) != "I'm a teapot" {
|
|
t.Errorf("Body = %q, want I'm a teapot", string(body))
|
|
}
|
|
}
|
|
|
|
func TestRecordHTTPRequest_RealMetrics(t *testing.T) {
|
|
// 记录请求
|
|
RecordHTTPRequest("GET", "/api/test", 200, 100*time.Millisecond)
|
|
|
|
// 启动服务器
|
|
server := httptest.NewServer(Handler())
|
|
defer server.Close()
|
|
|
|
resp, err := http.Get(server.URL + "/metrics")
|
|
if err != nil {
|
|
t.Fatalf("Failed to GET: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
bodyStr := string(body)
|
|
|
|
// 验证请求被记录
|
|
if !strings.Contains(bodyStr, "http_requests_total") {
|
|
t.Error("Missing http_requests_total metric")
|
|
}
|
|
if !strings.Contains(bodyStr, "http_request_duration_seconds") {
|
|
t.Error("Missing http_request_duration_seconds metric")
|
|
}
|
|
}
|