From 9b265903471e14c3460522c1268a9aa29b53a160 Mon Sep 17 00:00:00 2001 From: kruskall <99559985+kruskall@users.noreply.github.com> Date: Thu, 29 Aug 2024 16:51:54 +0200 Subject: [PATCH] feat: add extra goos support (#1646) syscall.Errno is not available is some GOOS causing compile errors Move syscall specific code to separate file behind build tags making it future proof and adding support for all goos supported by the go runtime --- error.go | 8 ------ error_go120_test.go => error_syscall.go | 33 +++++++++---------------- error_test.go | 12 +++++++++ error_unix.go | 3 +-- error_windows.go | 2 ++ 5 files changed, 27 insertions(+), 31 deletions(-) rename error_go120_test.go => error_syscall.go (55%) diff --git a/error.go b/error.go index 44aecef25..c9b736855 100644 --- a/error.go +++ b/error.go @@ -23,7 +23,6 @@ import ( "net" "os" "reflect" - "syscall" "time" "go.elastic.co/apm/v2/model" @@ -596,13 +595,6 @@ func init() { details.SetAttr("syscall", syscallErr.Syscall) details.Cause = append(details.Cause, syscallErr.Err) })) - RegisterTypeErrorDetailer(reflect.TypeOf(syscall.Errno(0)), ErrorDetailerFunc(func(err error, details *ErrorDetails) { - errno := err.(syscall.Errno) - details.Code.String = errnoName(errno) - if details.Code.String == "" { - details.Code.Number = float64(errno) - } - })) } func errTemporary(err error) bool { diff --git a/error_go120_test.go b/error_syscall.go similarity index 55% rename from error_go120_test.go rename to error_syscall.go index 623bbe373..0d286bcb6 100644 --- a/error_go120_test.go +++ b/error_syscall.go @@ -15,30 +15,21 @@ // specific language governing permissions and limitations // under the License. -//go:build go1.20 -// +build go1.20 +//go:build unix || windows -package apm_test +package apm // import "go.elastic.co/apm/v2" import ( - "context" - "errors" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "go.elastic.co/apm/v2" - "go.elastic.co/apm/v2/apmtest" + "reflect" + "syscall" ) -func TestErrorCauseUnwrapJoined(t *testing.T) { - err := errors.Join(errors.New("cause1"), errors.New("cause2")) - _, _, errors := apmtest.WithTransaction(func(ctx context.Context) { - apm.CaptureError(ctx, err).Send() - }) - require.Len(t, errors, 1) - require.Len(t, errors[0].Exception.Cause, 2) - assert.Equal(t, "cause1", errors[0].Exception.Cause[0].Message) - assert.Equal(t, "cause2", errors[0].Exception.Cause[1].Message) +func init() { + RegisterTypeErrorDetailer(reflect.TypeOf(syscall.Errno(0)), ErrorDetailerFunc(func(err error, details *ErrorDetails) { + errno := err.(syscall.Errno) + details.Code.String = errnoName(errno) + if details.Code.String == "" { + details.Code.Number = float64(errno) + } + })) } diff --git a/error_test.go b/error_test.go index 8ca9c32b0..6d105f995 100644 --- a/error_test.go +++ b/error_test.go @@ -19,6 +19,7 @@ package apm_test import ( "context" + goerrors "errors" "fmt" "net" "os" @@ -52,6 +53,17 @@ func TestErrorID(t *testing.T) { assert.Equal(t, model.TraceID(errorID), errors[0].ID) } +func TestErrorCauseUnwrapJoined(t *testing.T) { + err := goerrors.Join(errors.New("cause1"), errors.New("cause2")) + _, _, errors := apmtest.WithTransaction(func(ctx context.Context) { + apm.CaptureError(ctx, err).Send() + }) + require.Len(t, errors, 1) + require.Len(t, errors[0].Exception.Cause, 2) + assert.Equal(t, "cause1", errors[0].Exception.Cause[0].Message) + assert.Equal(t, "cause2", errors[0].Exception.Cause[1].Message) +} + func TestErrorsStackTrace(t *testing.T) { modelError := sendError(t, &errorsStackTracer{ "zing", newErrorsStackTrace(0, 2), diff --git a/error_unix.go b/error_unix.go index 2666acd97..c2a8a4aec 100644 --- a/error_unix.go +++ b/error_unix.go @@ -15,8 +15,7 @@ // specific language governing permissions and limitations // under the License. -//go:build !windows -// +build !windows +//go:build unix package apm // import "go.elastic.co/apm/v2" diff --git a/error_windows.go b/error_windows.go index e0e1fdcf9..7655179e1 100644 --- a/error_windows.go +++ b/error_windows.go @@ -15,6 +15,8 @@ // specific language governing permissions and limitations // under the License. +//go:build windows + package apm // import "go.elastic.co/apm/v2" import (