| 1 | package telemetry |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "strings" |
| 8 | "sync/atomic" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | func TestTelemetryMetricsArePrivateAndBounded(t *testing.T) { |
| 14 | tel, err := New(context.Background(), Config{Version: "test" , DebugToken: "secret" }) |
| 15 | if err != nil { |
| 16 | t.Fatal(err) |
| 17 | } |
| 18 | defer closeTelemetry(t, tel) |
| 19 | |
| 20 | ctx, done := tel.Start(context.Background(), "publish" ) |
| 21 | if ctx == nil { |
| 22 | t.Fatal("Start returned nil context" ) |
| 23 | } |
| 24 | done("success" ) |
| 25 | tel.ObserveStorage("query" , "success" , 2*time.Millisecond) |
| 26 | tel.Connection(1) |
| 27 | tel.Subscription(1) |
| 28 | tel.Delivery("success" , time.Millisecond) |
| 29 | tel.Queue("outbox" , 3) |
| 30 | |
| 31 | req := httptest.NewRequest(http.MethodGet, "/metrics" , nil) |
| 32 | req.Header.Set("Authorization" , "Bearer secret" ) |
| 33 | rr := httptest.NewRecorder() |
| 34 | tel.Handler().ServeHTTP(rr, req) |
| 35 | if rr.Code != http.StatusOK { |
| 36 | t.Fatalf("metrics status = %d" , rr.Code) |
| 37 | } |
| 38 | body := rr.Body.String() |
| 39 | for _, forbidden := range []string{"secret" , "tenant-123" , "npub1rawvalue" } { |
| 40 | if strings.Contains(body, forbidden) { |
| 41 | t.Fatalf("metrics leaked unbounded value %q" , forbidden) |
| 42 | } |
| 43 | } |
| 44 | if !strings.Contains(body, "relay_connections" ) || !strings.Contains(body, "relay_message_duration_seconds" ) { |
| 45 | t.Fatal("expected relay metrics" ) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestDiagnosticsRequireToken(t *testing.T) { |
| 50 | tel, err := New(context.Background(), Config{DebugToken: "secret" }) |
| 51 | if err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | defer closeTelemetry(t, tel) |
| 55 | |
| 56 | for _, token := range []string{"" , "wrong" } { |
| 57 | req := httptest.NewRequest(http.MethodGet, "/debug/pprof/" , nil) |
| 58 | if token != "" { |
| 59 | req.Header.Set("Authorization" , "Bearer " +token) |
| 60 | } |
| 61 | rr := httptest.NewRecorder() |
| 62 | tel.Handler().ServeHTTP(rr, req) |
| 63 | if rr.Code != http.StatusUnauthorized { |
| 64 | t.Fatalf("token %q status = %d" , token, rr.Code) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | req := httptest.NewRequest(http.MethodGet, "/debug/pprof/" , nil) |
| 69 | req.Header.Set("Authorization" , "Bearer secret" ) |
| 70 | rr := httptest.NewRecorder() |
| 71 | tel.Handler().ServeHTTP(rr, req) |
| 72 | if rr.Code != http.StatusOK { |
| 73 | t.Fatalf("valid token status = %d" , rr.Code) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func TestHTTPOperationUsesHTTPMetricTransport(t *testing.T) { |
| 78 | tel, err := New(context.Background(), Config{DebugToken: "secret" }) |
| 79 | if err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | defer closeTelemetry(t, tel) |
| 83 | _, done := tel.Start(context.Background(), "http" ) |
| 84 | done("ok" ) |
| 85 | req := httptest.NewRequest(http.MethodGet, "/metrics" , nil) |
| 86 | req.Header.Set("Authorization" , "Bearer secret" ) |
| 87 | rr := httptest.NewRecorder() |
| 88 | tel.Handler().ServeHTTP(rr, req) |
| 89 | body := rr.Body.String() |
| 90 | if !strings.Contains(body, `relay_messages_received_total{outcome="ok",transport="http"} 1` ) { |
| 91 | t.Fatalf("missing HTTP transport metric: %s" , body) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func TestOTLPExportFlushesOnClose(t *testing.T) { |
| 96 | var requests atomic.Int32 |
| 97 | server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 98 | requests.Add(1) |
| 99 | w.WriteHeader(http.StatusAccepted) |
| 100 | })) |
| 101 | defer server.Close() |
| 102 | |
| 103 | tel, err := New(context.Background(), Config{OTLPEndpoint: server.URL, TraceSampleRatio: 1}) |
| 104 | if err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | _, done := tel.Start(context.Background(), "publish" ) |
| 108 | done("success" ) |
| 109 | if err := tel.Close(context.Background()); err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | if requests.Load() == 0 { |
| 113 | t.Fatal("expected OTLP export request during shutdown" ) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | func closeTelemetry(t *testing.T, tel *Telemetry) { |
| 118 | t.Helper() |
| 119 | if err := tel.Close(context.Background()); err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | } |