From 940575fdb06e6ac3dde5e9acb33dfee6a7c58a63 Mon Sep 17 00:00:00 2001 From: Jesse Haka Date: Wed, 7 Jun 2023 00:09:17 +0300 Subject: [PATCH] add tracing tests --- gin/sentrygin_test.go | 117 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 2 deletions(-) diff --git a/gin/sentrygin_test.go b/gin/sentrygin_test.go index 9508b126c..0992d7a5b 100644 --- a/gin/sentrygin_test.go +++ b/gin/sentrygin_test.go @@ -5,6 +5,7 @@ import ( "io" "net/http" "net/http/httptest" + "strconv" "strings" "testing" "time" @@ -25,7 +26,8 @@ func TestIntegration(t *testing.T) { Body string Handler gin.HandlerFunc - WantEvent *sentry.Event + WantEvent *sentry.Event + WantTransaction *sentry.Event }{ { Path: "/panic", @@ -33,7 +35,20 @@ func TestIntegration(t *testing.T) { Handler: func(c *gin.Context) { panic("test") }, - + WantTransaction: &sentry.Event{ + Level: sentry.LevelInfo, + Type: "transaction", + Transaction: "GET /panic", + Request: &sentry.Request{ + URL: "/panic", + Method: "GET", + Headers: map[string]string{ + "Accept-Encoding": "gzip", + "User-Agent": "Go-http-client/1.1", + }, + }, + TransactionInfo: &sentry.TransactionInfo{Source: "url"}, + }, WantEvent: &sentry.Event{ Level: sentry.LevelFatal, Message: "test", @@ -58,6 +73,23 @@ func TestIntegration(t *testing.T) { t.Error(err) } hub.CaptureMessage("post: " + string(body)) + c.JSON(http.StatusOK, gin.H{"status": "ok"}) + }, + WantTransaction: &sentry.Event{ + Level: sentry.LevelInfo, + Type: "transaction", + Transaction: "POST /post", + Request: &sentry.Request{ + URL: "/post", + Method: "POST", + Data: "payload", + Headers: map[string]string{ + "Content-Length": "7", + "Accept-Encoding": "gzip", + "User-Agent": "Go-http-client/1.1", + }, + }, + TransactionInfo: &sentry.TransactionInfo{Source: "url"}, }, WantEvent: &sentry.Event{ Level: sentry.LevelInfo, @@ -81,6 +113,20 @@ func TestIntegration(t *testing.T) { hub := sentry.GetHubFromContext(c.Request.Context()) hub.CaptureMessage("get") }, + WantTransaction: &sentry.Event{ + Level: sentry.LevelInfo, + Type: "transaction", + Transaction: "GET /get", + Request: &sentry.Request{ + URL: "/get", + Method: "GET", + Headers: map[string]string{ + "Accept-Encoding": "gzip", + "User-Agent": "Go-http-client/1.1", + }, + }, + TransactionInfo: &sentry.TransactionInfo{Source: "url"}, + }, WantEvent: &sentry.Event{ Level: sentry.LevelInfo, Message: "get", @@ -106,6 +152,21 @@ func TestIntegration(t *testing.T) { } hub.CaptureMessage(fmt.Sprintf("post: %d KB", len(body)/1024)) }, + WantTransaction: &sentry.Event{ + Level: sentry.LevelInfo, + Type: "transaction", + Transaction: "POST /post/large", + Request: &sentry.Request{ + URL: "/post/large", + Method: "POST", + Headers: map[string]string{ + "Accept-Encoding": "gzip", + "Content-Length": strconv.Itoa(len(largePayload)), + "User-Agent": "Go-http-client/1.1", + }, + }, + TransactionInfo: &sentry.TransactionInfo{Source: "url"}, + }, WantEvent: &sentry.Event{ Level: sentry.LevelInfo, Message: "post: 15 KB", @@ -130,6 +191,23 @@ func TestIntegration(t *testing.T) { hub := sentry.GetHubFromContext(c.Request.Context()) hub.CaptureMessage("body ignored") }, + WantTransaction: &sentry.Event{ + Level: sentry.LevelInfo, + Type: "transaction", + Transaction: "POST /post/body-ignored", + Request: &sentry.Request{ + URL: "/post/body-ignored", + Method: "POST", + // Actual request body omitted because not read. + Data: "", + Headers: map[string]string{ + "Accept-Encoding": "gzip", + "Content-Length": strconv.Itoa(len("client sends, server ignores, SDK doesn't read")), + "User-Agent": "Go-http-client/1.1", + }, + }, + TransactionInfo: &sentry.TransactionInfo{Source: "url"}, + }, WantEvent: &sentry.Event{ Level: sentry.LevelInfo, Message: "body ignored", @@ -149,11 +227,18 @@ func TestIntegration(t *testing.T) { } eventsCh := make(chan *sentry.Event, len(tests)) + transactionsCh := make(chan *sentry.Event, len(tests)) err := sentry.Init(sentry.ClientOptions{ + EnableTracing: true, + TracesSampleRate: 1.0, BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event { eventsCh <- event return event }, + BeforeSendTransaction: func(tx *sentry.Event, hint *sentry.EventHint) *sentry.Event { + transactionsCh <- tx + return tx + }, }) if err != nil { t.Fatal(err) @@ -173,11 +258,16 @@ func TestIntegration(t *testing.T) { c.Timeout = time.Second var want []*sentry.Event + var wanttrans []*sentry.Event for _, tt := range tests { wantRequest := tt.WantEvent.Request wantRequest.URL = srv.URL + wantRequest.URL wantRequest.Headers["Host"] = srv.Listener.Addr().String() + wantTransaction := tt.WantTransaction.Request + wantTransaction.URL = srv.URL + wantTransaction.URL + wantTransaction.Headers["Host"] = srv.Listener.Addr().String() want = append(want, tt.WantEvent) + wanttrans = append(wanttrans, tt.WantTransaction) req, err := http.NewRequest(tt.Method, srv.URL+tt.Path, strings.NewReader(tt.Body)) if err != nil { @@ -216,4 +306,27 @@ func TestIntegration(t *testing.T) { if diff := cmp.Diff(want, got, opts); diff != "" { t.Fatalf("Events mismatch (-want +got):\n%s", diff) } + + close(transactionsCh) + var gott []*sentry.Event + for e := range transactionsCh { + gott = append(gott, e) + } + + optstrans := cmp.Options{ + cmpopts.IgnoreFields( + sentry.Event{}, + "Contexts", "EventID", "Extra", "Platform", "Modules", + "Release", "Sdk", "ServerName", "Tags", "Timestamp", + "sdkMetaData", "StartTime", "Spans", + ), + cmpopts.IgnoreFields( + sentry.Request{}, + "Env", + ), + } + if diff := cmp.Diff(wanttrans, gott, optstrans); diff != "" { + t.Fatalf("Transaction mismatch (-want +got):\n%s", diff) + } + }