seedTestData populates the test DB with sample data for API tests.
(t *testing.T, mux *http.ServeMux)
| 9 | |
| 10 | // seedTestData populates the test DB with sample data for API tests. |
| 11 | func seedTestData(t *testing.T, mux *http.ServeMux) { |
| 12 | t.Helper() |
| 13 | db := setupTestDB(t) |
| 14 | registerAPI(mux, db) |
| 15 | |
| 16 | // Insert error events. |
| 17 | for i, evt := range []struct { |
| 18 | eventID string |
| 19 | level string |
| 20 | excType string |
| 21 | excVal string |
| 22 | traceID string |
| 23 | }{ |
| 24 | {"evt-1", "error", "RuntimeException", "Something broke", "trace-abc"}, |
| 25 | {"evt-2", "error", "RuntimeException", "Something broke", "trace-abc"}, |
| 26 | {"evt-3", "warning", "LogicException", "Bad logic", ""}, |
| 27 | } { |
| 28 | payload, _ := json.Marshal(map[string]any{ |
| 29 | "event_id": evt.eventID, |
| 30 | "level": evt.level, |
| 31 | "environment": "production", |
| 32 | "exception": map[string]any{"values": []map[string]any{{"type": evt.excType, "value": evt.excVal}}}, |
| 33 | }) |
| 34 | var ev ErrorEvent |
| 35 | json.Unmarshal(payload, &ev) |
| 36 | if evt.traceID != "" { |
| 37 | ev.Contexts = &Contexts{Trace: &TraceContext{TraceID: evt.traceID, SpanID: "span-" + evt.eventID}} |
| 38 | } |
| 39 | _, err := storeErrorEvent(db, &ev, payload, "default") |
| 40 | if err != nil { |
| 41 | t.Fatalf("seed error event %d: %v", i, err) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // Insert a transaction with spans. |
| 46 | txnPayload, _ := json.Marshal(map[string]any{ |
| 47 | "event_id": "txn-1", |
| 48 | "type": "transaction", |
| 49 | "transaction": "GET /api/users", |
| 50 | "start_timestamp": 1678272500.0, |
| 51 | "timestamp": 1678272503.0, |
| 52 | "contexts": map[string]any{"trace": map[string]any{"trace_id": "trace-abc", "span_id": "root"}}, |
| 53 | "spans": []map[string]any{ |
| 54 | {"span_id": "s1", "trace_id": "trace-abc", "op": "db.query", "description": "SELECT *", "start_timestamp": 1678272501.0, "timestamp": 1678272502.0, "status": "ok", "data": map[string]string{"server.address": "mysql:3306"}}, |
| 55 | {"span_id": "s2", "trace_id": "trace-abc", "op": "http.client", "description": "stripe.com", "start_timestamp": 1678272502.0, "timestamp": 1678272502.5, "status": "internal_error", "data": map[string]string{"http.url": "https://stripe.com/charge"}}, |
| 56 | }, |
| 57 | }) |
| 58 | var txn Transaction |
| 59 | json.Unmarshal(txnPayload, &txn) |
| 60 | storeTransaction(db, &txn, txnPayload) |
| 61 | |
| 62 | // Insert logs. |
| 63 | storeLogs(db, []LogRecord{ |
| 64 | {TraceID: "trace-abc", Level: "info", Body: "Request started", SeverityNumber: 9}, |
| 65 | {TraceID: "trace-abc", Level: "warning", Body: "Slow query", SeverityNumber: 13}, |
| 66 | {Level: "error", Body: "Connection refused", SeverityNumber: 17}, |
| 67 | }) |
| 68 | } |
no test coverage detected