diff --git a/client_test.go b/client_test.go index 7f3f12d01..264bdd79e 100644 --- a/client_test.go +++ b/client_test.go @@ -13,6 +13,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" pkgErrors "github.com/pkg/errors" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -841,3 +842,16 @@ func TestClientSetsUpTransport(t *testing.T) { client, _ = NewClient(ClientOptions{}) require.IsType(t, &noopTransport{}, client.Transport) } + +func TestClientSetupInstrumenter(t *testing.T) { + client, err := NewClient(ClientOptions{Dsn: ""}) + require.NoError(t, err) + assert.Equal(t, "sentry", client.Options().Instrumenter) + + _, err = NewClient(ClientOptions{Dsn: "", Instrumenter: "foo"}) + require.Error(t, err) + + client, err = NewClient(ClientOptions{Dsn: "", Instrumenter: "otel"}) + require.NoError(t, err) + assert.Equal(t, "otel", client.Options().Instrumenter) +} diff --git a/gin/sentrygin_test.go b/gin/sentrygin_test.go index b75e0f1f0..91ceddb1c 100644 --- a/gin/sentrygin_test.go +++ b/gin/sentrygin_test.go @@ -16,18 +16,20 @@ import ( "github.com/gin-gonic/gin" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" + "github.com/stretchr/testify/require" ) func TestIntegration(t *testing.T) { largePayload := strings.Repeat("Large", 3*1024) // 15 KB tests := []struct { - RequestPath string - RoutePath string - Method string - WantStatus int - Body string - Handler gin.HandlerFunc + RequestPath string + RoutePath string + Method string + WantStatus int + Body string + Handler gin.HandlerFunc + Instrumenter string WantEvent *sentry.Event WantTransaction *sentry.Event @@ -284,25 +286,20 @@ func TestIntegration(t *testing.T) { }, WantEvent: nil, }, + { + RequestPath: "/404/1", + RoutePath: "/otel", + Method: "GET", + Instrumenter: "otel", + WantStatus: 404, + Handler: nil, + WantTransaction: nil, + WantEvent: nil, + }, } 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) - } router := gin.New() router.Use(sentrygin.New(sentrygin.Options{})) @@ -321,17 +318,34 @@ func TestIntegration(t *testing.T) { var wanttrans []*sentry.Event var wantCodes []sentry.SpanStatus for _, tt := range tests { + err := sentry.Init(sentry.ClientOptions{ + EnableTracing: true, + TracesSampleRate: 1.0, + Instrumenter: tt.Instrumenter, + 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 + }, + }) + require.NoError(t, err) + if tt.WantEvent != nil && tt.WantEvent.Request != nil { wantRequest := tt.WantEvent.Request wantRequest.URL = srv.URL + wantRequest.URL wantRequest.Headers["Host"] = srv.Listener.Addr().String() want = append(want, tt.WantEvent) } - wantTransaction := tt.WantTransaction.Request - wantTransaction.URL = srv.URL + wantTransaction.URL - wantTransaction.Headers["Host"] = srv.Listener.Addr().String() - wanttrans = append(wanttrans, tt.WantTransaction) - wantCodes = append(wantCodes, sentry.HTTPtoSpanStatus(tt.WantStatus)) + if tt.WantTransaction != nil { + wantTransaction := tt.WantTransaction.Request + wantTransaction.URL = srv.URL + wantTransaction.URL + wantTransaction.Headers["Host"] = srv.Listener.Addr().String() + wanttrans = append(wanttrans, tt.WantTransaction) + wantCodes = append(wantCodes, sentry.HTTPtoSpanStatus(tt.WantStatus)) + } req, err := http.NewRequest(tt.Method, srv.URL+tt.RequestPath, strings.NewReader(tt.Body)) if err != nil { diff --git a/http/sentryhttp_test.go b/http/sentryhttp_test.go index 45c3eb039..eedfe2c30 100644 --- a/http/sentryhttp_test.go +++ b/http/sentryhttp_test.go @@ -14,6 +14,8 @@ import ( "github.com/getsentry/sentry-go/internal/testutils" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestIntegration(t *testing.T) { @@ -222,3 +224,58 @@ func TestIntegration(t *testing.T) { t.Fatalf("Events mismatch (-want +got):\n%s", diff) } } + +func TestInstrumenters(t *testing.T) { + tests := []struct { + instrumenter string + expectedEvents int + }{ + { + instrumenter: "sentry", + expectedEvents: 1, + }, + { + instrumenter: "otel", + expectedEvents: 0, + }, + } + + for _, tt := range tests { + t.Run(tt.instrumenter, func(t *testing.T) { + sentEvents := make(map[string]struct{}) + err := sentry.Init(sentry.ClientOptions{ + Dsn: "http://example@example.com/123", + Debug: true, + EnableTracing: true, + TracesSampleRate: 1.0, + Instrumenter: tt.instrumenter, + BeforeSendTransaction: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event { + sentEvents[string(event.EventID)] = struct{}{} + return event + }, + }) + require.NoError(t, err) + + sentryHandler := sentryhttp.New(sentryhttp.Options{}) + noopHandler := func(w http.ResponseWriter, r *http.Request) {} + srv := httptest.NewServer(sentryHandler.HandleFunc(noopHandler)) + defer srv.Close() + + c := srv.Client() + c.Timeout = time.Second + + req, err := http.NewRequest("GET", srv.URL, http.NoBody) + require.NoError(t, err) + + resp, err := c.Do(req) + require.NoError(t, err) + defer resp.Body.Close() + + if ok := sentry.Flush(testutils.FlushTimeout()); !ok { + t.Fatal("sentry.Flush timed out") + } + + assert.Equal(t, tt.expectedEvents, len(sentEvents), "wrong number of sent events") + }) + } +}