Skip to content

Commit

Permalink
add tracing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zetaab committed Jun 6, 2023
1 parent 0e76d47 commit 940575f
Showing 1 changed file with 115 additions and 2 deletions.
117 changes: 115 additions & 2 deletions gin/sentrygin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"time"
Expand All @@ -25,15 +26,29 @@ func TestIntegration(t *testing.T) {
Body string
Handler gin.HandlerFunc

WantEvent *sentry.Event
WantEvent *sentry.Event
WantTransaction *sentry.Event
}{
{
Path: "/panic",
Method: "GET",
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",
Expand All @@ -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,
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}

}

0 comments on commit 940575f

Please sign in to comment.