forked from remind101/newrelic
/
tx.go
270 lines (231 loc) · 6.2 KB
/
tx.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package newrelic
import (
"net/http"
"path/filepath"
"runtime"
"sync"
"github.com/sky-uk/newrelic-go-agent/Godeps/_workspace/src/golang.org/x/net/context"
)
// Tx represents a transaction.
type Tx interface {
Start() error
End() error
StartGeneric(name string) error
StartDatastore(table, operation, sql, rollupName string) error
StartExternal(host, name string) error
EndSegment() error
ReportError(exceptionType, errorMessage, stackTrace, stackFrameDelim string) error
}
// tx implements the Tx interface.
type tx struct {
Tracer TxTracer
Reporter TxReporter
id int64
name string
url string
category string
txnType TransactionType
ss *SegmentStack
mtx *sync.Mutex
}
// NewTx returns a new transaction.
func NewTx(name string) *tx {
return &tx{
Tracer: &NRTxTracer{},
Reporter: &NRTxReporter{},
name: name,
txnType: WebTransaction,
ss: NewSegmentStack(),
mtx: &sync.Mutex{},
}
}
// NewRequestTx returns a new transaction with a request url.
func NewRequestTx(name string, url string) *tx {
t := NewTx(name)
t.url = url
return t
}
// NewBackgroundTx returns a new background transaction
func NewBackgroundTx(name string, category string) *tx {
t := NewTx(name)
t.txnType = OtherTransaction
t.category = category
return t
}
// Start starts a transaction, setting the id.
func (t *tx) Start() (err error) {
if t.id != 0 {
return ErrTxAlreadyStarted
}
if t.id, err = t.Tracer.BeginTransaction(); err != nil {
return err
}
if err = t.Tracer.SetTransactionName(t.id, t.name); err != nil {
return err
}
if err = t.Tracer.SetTransactionType(t.id, t.txnType); err != nil {
return err
}
if t.url != "" {
if err = t.Tracer.SetTransactionRequestURL(t.id, t.url); err != nil {
return err
}
}
if t.category != "" {
if err = t.Tracer.SetTransactionCategory(t.id, t.category); err != nil {
return err
}
}
return nil
}
// End ends a transaction.
func (t *tx) End() error {
t.mtx.Lock()
defer t.mtx.Unlock()
for t.ss.Peek() != rootSegment {
t.EndSegment() // discarding errors?
}
return t.Tracer.EndTransaction(t.id)
}
// StartGeneric starts a generic segment.
func (t *tx) StartGeneric(name string) error {
t.mtx.Lock()
defer t.mtx.Unlock()
id, err := t.Tracer.BeginGenericSegment(t.id, t.ss.Peek(), name)
if err != nil {
return err
}
t.ss.Push(id)
return nil
}
// StartDatastore starts a datastore segment.
func (t *tx) StartDatastore(table, operation, sql, rollupName string) error {
t.mtx.Lock()
defer t.mtx.Unlock()
id, err := t.Tracer.BeginDatastoreSegment(t.id, t.ss.Peek(), table, operation, sql, rollupName)
if err != nil {
return err
}
t.ss.Push(id)
return nil
}
// StartExternal starts an external segment.
func (t *tx) StartExternal(host, name string) error {
t.mtx.Lock()
defer t.mtx.Unlock()
id, err := t.Tracer.BeginExternalSegment(t.id, t.ss.Peek(), host, name)
if err != nil {
return err
}
t.ss.Push(id)
return nil
}
// EndSegment ends the segment at the top of the stack.
func (t *tx) EndSegment() error {
t.mtx.Lock()
defer t.mtx.Unlock()
if id, ok := t.ss.Pop(); ok {
return t.Tracer.EndSegment(t.id, id)
}
return nil
}
// ReportError reports an error that occured during the transaction.
func (t *tx) ReportError(exceptionType, errorMessage, stackTrace, stackFrameDelim string) error {
t.mtx.Lock()
defer t.mtx.Unlock()
_, err := t.Reporter.ReportError(t.id, exceptionType, errorMessage, stackTrace, stackFrameDelim)
return err
}
// WithTx inserts a newrelic.Tx into the provided context.
func WithTx(ctx context.Context, t Tx) context.Context {
return context.WithValue(ctx, txKey, t)
}
// FromContext returns a newrelic.Tx from the context.
func FromContext(ctx context.Context) (Tx, bool) {
t, ok := ctx.Value(txKey).(Tx)
return t, ok
}
type Trace struct {
err error
done func() error
}
func (t *Trace) Err() error {
return t.err
}
func (t *Trace) Done() {
if t.err == nil {
t.err = t.done()
}
}
// TraceReqest traces an http request. It returns a new context with the transaction
// included in it, and a trace object.
//
// Usage:
//
// ctx, t := TraceRequest(ctx, name, req)
// defer t.Done()
func TraceRequest(ctx context.Context, name string, req *http.Request) (context.Context, *Trace) {
tx := NewRequestTx(name, req.URL.String())
ctx = WithTx(ctx, tx)
err := tx.Start()
return ctx, &Trace{
err: err,
done: func() error {
return tx.End()
},
}
}
// TraceExternal adds an external segment to the newrelic transaction, if one exists in the context.
func TraceExternal(ctx context.Context, host, name string) *Trace {
return trace(ctx, name, func(tx Tx) error {
return tx.StartExternal(host, name)
})
}
// TraceGeneric adds a generic segment to the newrelic transaction, if one exists in the context.
func TraceGeneric(ctx context.Context, name string) *Trace {
return trace(ctx, name, func(tx Tx) error {
return tx.StartGeneric(name)
})
}
// TraceDatastore adds a datastore segment to the newrelic transaction, if one exists in the context.
func TraceDatastore(ctx context.Context, table, operation, sql, rollupName string) *Trace {
return trace(ctx, rollupName, func(tx Tx) error {
return tx.StartDatastore(table, operation, sql, rollupName)
})
}
// TraceFunc adds a generic segment, autodetecting the function name with runtime.Caller().
func TraceFunc(ctx context.Context) *Trace {
name := caller(2) // Get the caller that called TraceFunc.
return trace(ctx, name, func(tx Tx) error {
return tx.StartGeneric(name)
})
}
// trace is a helper function for TraceExternal and TraceGeneric, you probably don't want
// to use it directly.
func trace(ctx context.Context, name string, fn func(Tx) error) *Trace {
if tx, ok := FromContext(ctx); ok {
err := fn(tx)
return &Trace{
err: err,
done: func() error {
return tx.EndSegment()
},
}
}
return &Trace{nil, func() error { return nil }}
}
// caller returns the name of the function that called the function this function was called from.
// n = 1 => caller of caller()
// n = 2 => caller of caller of call()
// etc.
func caller(n int) string {
name := "unknown"
if pc, _, _, ok := runtime.Caller(n); ok {
name = filepath.Base(runtime.FuncForPC(pc).Name())
}
return name
}
type key int
const (
txKey key = iota
)