Skip to content

Commit

Permalink
Merge branch 'master' into sdk-identifier
Browse files Browse the repository at this point in the history
# Conflicts:
#	client_test.go
  • Loading branch information
cleptric committed Aug 29, 2023
2 parents 08e53f1 + 82a00ab commit 4e2a22f
Show file tree
Hide file tree
Showing 23 changed files with 826 additions and 299 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: "1.20"
go-version: "1.21"
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@639cd343e1d3b897ff35927a75193d57cfcba299 # [email protected]
with:
version: v1.51
only-new-issues: true
Expand Down
18 changes: 8 additions & 10 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
with:
go-version: ${{ matrix.go }}
- uses: actions/checkout@v3
- uses: actions/cache@v2
- uses: actions/cache@v3
with:
# In order:
# * Module download cache
Expand All @@ -48,23 +48,21 @@ jobs:
run: make mod-tidy
if: ${{ matrix.go == '1.20' }}
- name: Test
run: make test
run: make test-coverage
- name: Upload coverage to Codecov
uses: codecov/codecov-action@eaaf4bedf32dbdc6b720b63067d99c4d77d6047d # [email protected]
with:
directory: .coverage
- name: Test (with race detection)
run: make test-race
# The race detector adds considerable runtime overhead. To save time on
# pull requests, only run this step for a single job in the matrix. For
# all other workflow triggers (e.g., pushes to a release branch) run
# this step for the whole matrix.
if: ${{ github.event_name != 'pull_request' || (matrix.go == '1.20' && matrix.os == 'ubuntu') }}
- name: Collect coverage
run: make test-coverage
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
directory: .coverage
if: ${{ github.event_name != 'pull_request' || (matrix.go == '1.21' && matrix.os == 'ubuntu') }}
timeout-minutes: 15
strategy:
matrix:
go: ["1.20", "1.19", "1.18"]
go: ["1.21", "1.20", "1.19", "1.18"]
os: [ubuntu, windows, macos]
fail-fast: false
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
# Changelog

## Unreleased

### Features

- Run single profiler even when profiling from multiple parallel goroutines ([#655](https://github.com/getsentry/sentry-go/pull/655))

### Bug fixes

- Fix trace function name parsing in profiler on go1.21+ ([#695](https://github.com/getsentry/sentry-go/pull/695))

## 0.23.0

The Sentry SDK team is happy to announce the immediate availability of Sentry Go SDK v0.23.0.

### Features

- Initial support for [Cron Monitoring](https://docs.sentry.io/product/crons/) ([#661](https://github.com/getsentry/sentry-go/pull/661))

This is how the basic usage of the feature looks like:
Expand Down Expand Up @@ -56,6 +67,7 @@ The Sentry SDK team is happy to announce the immediate availability of Sentry Go
- Expose SpanFromContext function ([#672](https://github.com/getsentry/sentry-go/pull/672))
### Bug fixes
- Make `Span.Finish` a no-op when the span is already finished ([#660](https://github.com/getsentry/sentry-go/pull/660))
## 0.22.0
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ test-coverage: $(COVERAGE_REPORT_DIR) clean-report-dir ## Test with coverage en
DIR_ABS=$$(python -c 'import os, sys; print(os.path.realpath(sys.argv[1]))' $${dir}) ; \
REPORT_NAME=$$(basename $${DIR_ABS}); \
(cd "$${dir}" && \
$(GO) test -count=1 -coverpkg=./... -covermode=$(COVERAGE_MODE) -coverprofile="$(COVERAGE_PROFILE)" ./... && \
$(GO) test -count=1 -timeout $(TIMEOUT)s -coverpkg=./... -covermode=$(COVERAGE_MODE) -coverprofile="$(COVERAGE_PROFILE)" ./... && \
cp $(COVERAGE_PROFILE) "$(COVERAGE_REPORT_DIR_ABS)/$${REPORT_NAME}_$(COVERAGE_PROFILE)" && \
$(GO) tool cover -html=$(COVERAGE_PROFILE) -o coverage.html); \
done;
Expand Down
2 changes: 1 addition & 1 deletion _examples/profiling/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ func main() {
wg.Add(10)
for i := 0; i < 10; i++ {
go func(num int) {
defer wg.Done()
span := tx.StartChild(fmt.Sprintf("Goroutine %d", num))
defer span.Finish()
for i := 0; i < num; i++ {
_ = findPrimeNumber(50000)
runtime.Gosched() // we need to manually yield this busy loop
}
fmt.Printf("routine %d done\n", num)
wg.Done()
}(i)
}
wg.Wait()
Expand Down
13 changes: 12 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/require"
)

func TestNewClientAllowsEmptyDSN(t *testing.T) {
Expand Down Expand Up @@ -609,6 +610,7 @@ func TestSampleRate(t *testing.T) {
func BenchmarkProcessEvent(b *testing.B) {
c, err := NewClient(ClientOptions{
SampleRate: 0.25,
Transport: &TransportMock{},
})
if err != nil {
b.Fatal(err)
Expand Down Expand Up @@ -708,7 +710,8 @@ func TestCustomMaxSpansProperty(t *testing.T) {
assertEqual(t, client.Options().MaxSpans, 2000)

properClient, _ := NewClient(ClientOptions{
MaxSpans: 3000,
MaxSpans: 3000,
Transport: &TransportMock{},
})

assertEqual(t, properClient.Options().MaxSpans, 3000)
Expand All @@ -721,3 +724,11 @@ func TestSDKIdentifier(t *testing.T) {
client.SetSDKIdentifier("sentry.go.test")
assertEqual(t, client.GetSDKIdentifier(), "sentry.go.test")
}

func TestClientSetsUpTransport(t *testing.T) {
client, _ := NewClient(ClientOptions{Dsn: testDsn})
require.IsType(t, &HTTPTransport{}, client.Transport)

client, _ = NewClient(ClientOptions{})
require.IsType(t, &noopTransport{}, client.Transport)
}
3 changes: 2 additions & 1 deletion fasthttp/sentryfasthttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/getsentry/sentry-go"
sentryfasthttp "github.com/getsentry/sentry-go/fasthttp"
"github.com/getsentry/sentry-go/internal/testutils"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/valyala/fasthttp"
Expand Down Expand Up @@ -194,7 +195,7 @@ func TestIntegration(t *testing.T) {
}
}

if ok := sentry.Flush(time.Second); !ok {
if ok := sentry.Flush(testutils.FlushTimeout()); !ok {
t.Fatal("sentry.Flush timed out")
}
close(eventsCh)
Expand Down
3 changes: 2 additions & 1 deletion gin/sentrygin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/getsentry/sentry-go"
sentrygin "github.com/getsentry/sentry-go/gin"
"github.com/getsentry/sentry-go/internal/testutils"
"github.com/gin-gonic/gin"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
Expand Down Expand Up @@ -317,7 +318,7 @@ func TestIntegration(t *testing.T) {
res.Body.Close()
}

if ok := sentry.Flush(time.Second); !ok {
if ok := sentry.Flush(testutils.FlushTimeout()); !ok {
t.Fatal("sentry.Flush timed out")
}
close(eventsCh)
Expand Down
3 changes: 2 additions & 1 deletion http/sentryhttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/getsentry/sentry-go"
sentryhttp "github.com/getsentry/sentry-go/http"
"github.com/getsentry/sentry-go/internal/testutils"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
Expand Down Expand Up @@ -197,7 +198,7 @@ func TestIntegration(t *testing.T) {
res.Body.Close()
}

if ok := sentry.Flush(time.Second); !ok {
if ok := sentry.Flush(testutils.FlushTimeout()); !ok {
t.Fatal("sentry.Flush timed out")
}
close(eventsCh)
Expand Down
6 changes: 3 additions & 3 deletions hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
const testDsn = "http://[email protected]/1337"

func setupHubTest() (*Hub, *Client, *Scope) {
client, _ := NewClient(ClientOptions{Dsn: testDsn})
client, _ := NewClient(ClientOptions{Dsn: testDsn, Transport: &TransportMock{}})
scope := NewScope()
hub := NewHub(client, scope)
return hub, client, scope
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestPopScopeCannotLeaveStackEmpty(t *testing.T) {
func TestBindClient(t *testing.T) {
hub, client, _ := setupHubTest()
hub.PushScope()
newClient, _ := NewClient(ClientOptions{Dsn: testDsn})
newClient, _ := NewClient(ClientOptions{Dsn: testDsn, Transport: &TransportMock{}})
hub.BindClient(newClient)

if (*hub.stack)[0].client == (*hub.stack)[1].client {
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestWithScopeBindClient(t *testing.T) {
hub, client, _ := setupHubTest()

hub.WithScope(func(scope *Scope) {
newClient, _ := NewClient(ClientOptions{Dsn: testDsn})
newClient, _ := NewClient(ClientOptions{Dsn: testDsn, Transport: &TransportMock{}})
hub.BindClient(newClient)
if hub.stackTop().client != newClient {
t.Error("should use newly bound client")
Expand Down
19 changes: 19 additions & 0 deletions internal/testutils/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package testutils

import (
"os"
"time"
)

func IsCI() bool {
return os.Getenv("CI") != ""
}

func FlushTimeout() time.Duration {
if IsCI() {
// CI is very overloaded so we need to allow for a long wait time.
return 5 * time.Second
}

return time.Second
}
9 changes: 8 additions & 1 deletion internal/traceparser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,14 @@ var createdByPrefix = []byte("created by ")

func (f *Frame) Func() []byte {
if bytes.HasPrefix(f.line1, createdByPrefix) {
return f.line1[len(createdByPrefix):]
// Since go1.21, the line ends with " in goroutine X", saying which goroutine created this one.
// We currently don't have use for that so just remove it.
var line = f.line1[len(createdByPrefix):]
var spaceAt = bytes.IndexByte(line, ' ')
if spaceAt < 0 {
return line
}
return line[:spaceAt]
}

var end = bytes.LastIndexByte(f.line1, '(')
Expand Down
73 changes: 50 additions & 23 deletions internal/traceparser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,39 @@ package traceparser
import (
"bytes"
"fmt"
"runtime"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

func TestGenerateTrace(t *testing.T) {
stacks := make(chan string)
go func() {
var stacksBuffer = make([]byte, 1000)
for {
// Capture stacks for all existing goroutines.
// Note: runtime.GoroutineProfile() would be better but we can't use it at the moment because
// it doesn't give us `gid` for each routine, see https://github.com/golang/go/issues/59663
n := runtime.Stack(stacksBuffer, true)

// If we couldn't read everything, increase the buffer and try again.
if n >= len(stacksBuffer) {
stacksBuffer = make([]byte, n*2)
} else {
stacks <- string(stacksBuffer[0:n])
break
}
}
}()

t.Log(<-stacks)

// Note: uncomment to show the output so you can update it manually in tests below.
// t.Fail()
}

func TestParseEmpty(t *testing.T) {
var require = require.New(t)

Expand All @@ -17,11 +44,11 @@ func TestParseEmpty(t *testing.T) {
}

var tracetext = []byte(`
goroutine 18 [running]:
testing.(*M).startAlarm.func1()
C:/Users/name/scoop/apps/go/current/src/testing/testing.go:2241 +0x3c5
created by time.goFunc
C:/Users/name/scoop/apps/go/current/src/time/sleep.go:176 +0x32
goroutine 7 [running]:
github.com/getsentry/sentry-go/internal/traceparser.TestGenerateTrace.func1()
c:/dev/sentry-go/internal/traceparser/parser_test.go:23 +0x6c
created by github.com/getsentry/sentry-go/internal/traceparser.TestGenerateTrace in goroutine 6
c:/dev/sentry-go/internal/traceparser/parser_test.go:17 +0x7f
goroutine 1 [chan receive]:
testing.(*T).Run(0xc00006f6c0, {0x672288?, 0x180fd3?}, 0x6b5f98)
Expand Down Expand Up @@ -78,10 +105,10 @@ func TestParse(t *testing.T) {
i++
}

checkTrace(18, `testing.(*M).startAlarm.func1()
C:/Users/name/scoop/apps/go/current/src/testing/testing.go:2241 +0x3c5
created by time.goFunc
C:/Users/name/scoop/apps/go/current/src/time/sleep.go:176 +0x32`)
checkTrace(7, `github.com/getsentry/sentry-go/internal/traceparser.TestGenerateTrace.func1()
c:/dev/sentry-go/internal/traceparser/parser_test.go:23 +0x6c
created by github.com/getsentry/sentry-go/internal/traceparser.TestGenerateTrace in goroutine 6
c:/dev/sentry-go/internal/traceparser/parser_test.go:17 +0x7f`)

checkTrace(1, `testing.(*T).Run(0xc00006f6c0, {0x672288?, 0x180fd3?}, 0x6b5f98)
C:/Users/name/scoop/apps/go/current/src/testing/testing.go:1630 +0x405
Expand Down Expand Up @@ -144,13 +171,13 @@ func TestFrames(t *testing.T) {
}

var expected = strings.Split(strings.TrimLeft(`
Trace 0: goroutine 18 with at most 2 frames
Func = testing.(*M).startAlarm.func1
File = C:/Users/name/scoop/apps/go/current/src/testing/testing.go
Line = 2241
Func = time.goFunc
File = C:/Users/name/scoop/apps/go/current/src/time/sleep.go
Line = 176
Trace 0: goroutine 7 with at most 2 frames
Func = github.com/getsentry/sentry-go/internal/traceparser.TestGenerateTrace.func1
File = c:/dev/sentry-go/internal/traceparser/parser_test.go
Line = 23
Func = github.com/getsentry/sentry-go/internal/traceparser.TestGenerateTrace
File = c:/dev/sentry-go/internal/traceparser/parser_test.go
Line = 17
Trace 1: goroutine 1 with at most 6 frames
Func = testing.(*T).Run
File = C:/Users/name/scoop/apps/go/current/src/testing/testing.go
Expand Down Expand Up @@ -228,13 +255,13 @@ func TestFramesReversed(t *testing.T) {
}

var expected = strings.Split(strings.TrimLeft(`
Trace 0: goroutine 18 with at most 2 frames
Func = time.goFunc
File = C:/Users/name/scoop/apps/go/current/src/time/sleep.go
Line = 176
Func = testing.(*M).startAlarm.func1
File = C:/Users/name/scoop/apps/go/current/src/testing/testing.go
Line = 2241
Trace 0: goroutine 7 with at most 2 frames
Func = github.com/getsentry/sentry-go/internal/traceparser.TestGenerateTrace
File = c:/dev/sentry-go/internal/traceparser/parser_test.go
Line = 17
Func = github.com/getsentry/sentry-go/internal/traceparser.TestGenerateTrace.func1
File = c:/dev/sentry-go/internal/traceparser/parser_test.go
Line = 23
Trace 1: goroutine 1 with at most 6 frames
Func = main.main
File = _testmain.go
Expand Down
5 changes: 3 additions & 2 deletions logrus/logrusentry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/sirupsen/logrus"

"github.com/getsentry/sentry-go"
"github.com/getsentry/sentry-go/internal/testutils"
)

func TestNew(t *testing.T) {
Expand All @@ -36,7 +37,7 @@ func TestNew(t *testing.T) {
if id := h.hub.CaptureEvent(&sentry.Event{}); id == nil {
t.Error("CaptureEvent failed")
}
if !h.Flush(5 * time.Second) {
if !h.Flush(testutils.FlushTimeout()) {
t.Error("flush failed")
}
})
Expand All @@ -59,7 +60,7 @@ func TestFire(t *testing.T) {
t.Fatal(err)
}

if !hook.Flush(5 * time.Second) {
if !hook.Flush(testutils.FlushTimeout()) {
t.Error("flush failed")
}
}
Expand Down
Loading

0 comments on commit 4e2a22f

Please sign in to comment.