forked from elastic/apm-agent-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_test.go
118 lines (102 loc) · 3.01 KB
/
error_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package elasticapm_test
import (
"path/filepath"
"runtime"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/elastic/apm-agent-go"
"github.com/elastic/apm-agent-go/model"
"github.com/elastic/apm-agent-go/stacktrace"
"github.com/elastic/apm-agent-go/transport/transporttest"
)
func TestErrorsStackTrace(t *testing.T) {
modelError := sendError(t, &errorsStackTracer{
"zing", newErrorsStackTrace(0, 2),
})
exception := modelError.Exception
stacktrace := exception.Stacktrace
assert.Equal(t, "zing", exception.Message)
assert.Equal(t, "github.com/elastic/apm-agent-go_test", exception.Module)
assert.Equal(t, "errorsStackTracer", exception.Type)
assert.Len(t, stacktrace, 2)
assert.Equal(t, "newErrorsStackTrace", stacktrace[0].Function)
assert.Equal(t, "TestErrorsStackTrace", stacktrace[1].Function)
}
func TestInternalStackTrace(t *testing.T) {
// Absolute path on both windows (UNC) and *nix
abspath := filepath.FromSlash("//abs/path/file.go")
modelError := sendError(t, &internalStackTracer{
"zing", []stacktrace.Frame{
{Function: "pkg/path.FuncName"},
{Function: "FuncName2", File: abspath, Line: 123},
{Function: "encoding/json.Marshal"},
},
})
exception := modelError.Exception
stacktrace := exception.Stacktrace
assert.Equal(t, "zing", exception.Message)
assert.Equal(t, "github.com/elastic/apm-agent-go_test", exception.Module)
assert.Equal(t, "internalStackTracer", exception.Type)
assert.Equal(t, []model.StacktraceFrame{{
Function: "FuncName",
Module: "pkg/path",
}, {
AbsolutePath: abspath,
Function: "FuncName2",
File: "file.go",
Line: 123,
}, {
Function: "Marshal",
Module: "encoding/json",
LibraryFrame: true,
}}, stacktrace)
}
func sendError(t *testing.T, err error, f ...func(*elasticapm.Error)) *model.Error {
var r transporttest.RecorderTransport
tracer, newTracerErr := elasticapm.NewTracer("tracer_testing", "")
assert.NoError(t, newTracerErr)
defer tracer.Close()
error_ := tracer.NewError(err)
for _, f := range f {
f(error_)
}
tracer.Transport = &r
error_.Send()
tracer.Flush(nil)
payloads := r.Payloads()
require.Len(t, payloads, 1)
errors := payloads[0].Errors()
require.Len(t, errors, 1)
return errors[0]
}
type errorsStackTracer struct {
message string
stackTrace errors.StackTrace
}
func (e *errorsStackTracer) Error() string {
return e.message
}
func (e *errorsStackTracer) StackTrace() errors.StackTrace {
return e.stackTrace
}
func newErrorsStackTrace(skip, n int) errors.StackTrace {
callers := make([]uintptr, 2)
callers = callers[:runtime.Callers(1, callers)]
frames := make([]errors.Frame, len(callers))
for i, pc := range callers {
frames[i] = errors.Frame(pc)
}
return errors.StackTrace(frames)
}
type internalStackTracer struct {
message string
frames []stacktrace.Frame
}
func (e *internalStackTracer) Error() string {
return e.message
}
func (e *internalStackTracer) StackTrace() []stacktrace.Frame {
return e.frames
}