This repository has been archived by the owner on May 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
plugin.go
168 lines (133 loc) · 3.41 KB
/
plugin.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
package newrelic
import (
"bytes"
"net/http"
"sync"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/roadrunner-server/api/v2/plugins/config"
"github.com/spiral/errors"
"github.com/spiral/roadrunner/v2/utils"
)
const (
pluginName string = "new_relic"
path string = "http.new_relic"
rrNewRelicKey string = "Rr_newrelic"
rrNewRelicErr string = "Rr_newrelic_error"
newRelicTransactionKey string = "transaction_name"
)
type Plugin struct {
cfg *Config
app *newrelic.Application
writersPool sync.Pool
}
func (p *Plugin) Init(cfg config.Configurer) error {
const op = errors.Op("new_relic_mdw_init")
if !cfg.Has(path) {
return errors.E(op, errors.Disabled)
}
err := cfg.UnmarshalKey(path, &p.cfg)
if err != nil {
return err
}
err = p.cfg.InitDefaults()
if err != nil {
return err
}
app, err := newrelic.NewApplication(
newrelic.ConfigAppName(p.cfg.AppName),
newrelic.ConfigLicense(p.cfg.LicenseKey),
newrelic.ConfigDistributedTracerEnabled(true),
)
if err != nil {
return err
}
p.writersPool = sync.Pool{
New: func() interface{} {
wr := new(writer)
wr.code = -1
wr.data = nil
wr.hdrToSend = make(map[string][]string, 10)
return wr
},
}
p.app = app
return nil
}
func (p *Plugin) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
txn := p.app.StartTransaction(r.RequestURI)
defer txn.End()
w = txn.SetWebResponse(w)
txn.SetWebRequestHTTP(r)
// overwrite original rw, because we need to delete sensitive rr_newrelic headers
rrWriter := p.getWriter()
r = newrelic.RequestWithTransactionContext(r, txn)
next.ServeHTTP(rrWriter, r)
defer func() {
w.WriteHeader(rrWriter.code)
_, _ = w.Write(rrWriter.data)
p.putWriter(rrWriter)
}()
// handle error
if len(rrWriter.hdrToSend[rrNewRelicErr]) > 0 {
err := handleErr(rrWriter.hdrToSend[rrNewRelicErr])
txn.NoticeError(err)
// to be sure
delete(rrWriter.hdrToSend, rrNewRelicKey)
delete(rrWriter.hdrToSend, rrNewRelicErr)
for k := range rrWriter.hdrToSend {
for kk := range rrWriter.hdrToSend[k] {
w.Header().Add(k, rrWriter.hdrToSend[k][kk])
}
}
return
}
// no error, general case
hdr := rrWriter.hdrToSend[rrNewRelicKey]
if len(hdr) == 0 {
// to be sure
delete(rrWriter.hdrToSend, rrNewRelicKey)
for k := range rrWriter.hdrToSend {
for kk := range rrWriter.hdrToSend[k] {
w.Header().Add(k, rrWriter.hdrToSend[k][kk])
}
}
return
}
for i := 0; i < len(hdr); i++ {
key, value := split(utils.AsBytes(hdr[i]))
if key == nil || value == nil {
continue
}
if bytes.Equal(key, utils.AsBytes(newRelicTransactionKey)) {
txn.SetName(utils.AsString(value))
continue
}
txn.AddAttribute(utils.AsString(key), utils.AsString(value))
}
// delete sensitive information
delete(rrWriter.hdrToSend, rrNewRelicKey)
// send original data
for k := range rrWriter.hdrToSend {
for kk := range rrWriter.hdrToSend[k] {
w.Header().Add(k, rrWriter.hdrToSend[k][kk])
}
delete(rrWriter.hdrToSend, k)
}
})
}
func (p *Plugin) Name() string {
return pluginName
}
func (p *Plugin) getWriter() *writer {
wr := p.writersPool.Get().(*writer)
return wr
}
func (p *Plugin) putWriter(w *writer) {
w.code = -1
w.data = nil
for k := range w.hdrToSend {
delete(w.hdrToSend, k)
}
p.writersPool.Put(w)
}