-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
233 lines (190 loc) · 5.72 KB
/
main.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
package restqlnewrelic
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/b2wdigital/restQL-golang/v6/pkg/restql"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/pkg/errors"
)
const (
newRelicPluginName = "NewRelicPlugin"
newRelicResponseWriter = "__newRelicResponseWriter__"
newRelicTransaction = "__newRelicTransaction__"
newRelicExternalSegment = "__newRelicExternalSegment__"
newRelicExternalRequest = "__newRelicExternalRequest__"
)
func init() {
restql.RegisterPlugin(restql.PluginInfo{
Name: newRelicPluginName,
Type: restql.LifecyclePluginType,
New: func(logger restql.Logger) (restql.Plugin, error) {
return MakeNewRelicPlugin(logger)
},
})
}
func MakeNewRelicPlugin(logger restql.Logger) (restql.LifecyclePlugin, error) {
app, err := newrelic.NewApplication(
newrelic.ConfigFromEnvironment(),
ExtraConfigFromEnvironment(),
)
if err != nil {
logger.Error("failed to initialize new relic", err)
return nil, err
}
return &NewRelicPlugin{log: logger, application: app}, nil
}
type NewRelicPlugin struct {
log restql.Logger
application *newrelic.Application
}
func (n *NewRelicPlugin) Name() string {
return newRelicPluginName
}
func (n *NewRelicPlugin) BeforeTransaction(ctx context.Context, tr restql.TransactionRequest) context.Context {
txn := n.application.StartTransaction(txnName(tr.Method, tr.Url))
txn.SetWebRequest(newrelic.WebRequest{
Header: tr.Header,
URL: tr.Url,
Method: tr.Method,
})
txn.AddAttribute("query", tr.Url.Query().Encode())
w := txn.SetWebResponse(noOpResponseWriter{})
txnCtx := context.WithValue(ctx, newRelicTransaction, txn)
writerCtx := context.WithValue(txnCtx, newRelicResponseWriter, w)
return writerCtx
}
func (n *NewRelicPlugin) AfterTransaction(ctx context.Context, tr restql.TransactionResponse) context.Context {
txn, err := n.getTransactionFromContext(ctx)
if err != nil {
return ctx
}
defer txn.End()
w, ok := ctx.Value(newRelicResponseWriter).(http.ResponseWriter)
if !ok {
n.log.Error(
"failed to retrieve new relic writer from context",
errors.Errorf("incorrect writer type : %T", w),
)
return ctx
}
segment := txn.StartSegment("Flush")
defer segment.End()
//todo: set headers
w.WriteHeader(tr.Status)
_, err = w.Write(tr.Body)
if err != nil {
n.log.Error("failed to write response for new relic", err)
}
return ctx
}
func (n *NewRelicPlugin) BeforeQuery(ctx context.Context, query string, queryCtx restql.QueryContext) context.Context {
return ctx
}
func (n *NewRelicPlugin) AfterQuery(ctx context.Context, query string, result map[string]interface{}) context.Context {
return ctx
}
func (n *NewRelicPlugin) BeforeRequest(ctx context.Context, request restql.HTTPRequest) context.Context {
txn, err := n.getTransactionFromContext(ctx)
if err != nil {
return ctx
}
externalRequest, err := makeExternalRequest(request)
if err != nil {
n.log.Error("failed to make request to report for new relic", err)
return ctx
}
reqTxn := txn.NewGoroutine()
segment := newrelic.StartExternalSegment(reqTxn, externalRequest)
segmentCtx := context.WithValue(ctx, newRelicExternalSegment, segment)
extReqCtx := context.WithValue(segmentCtx, newRelicExternalRequest, externalRequest)
return extReqCtx
}
func (n *NewRelicPlugin) AfterRequest(ctx context.Context, request restql.HTTPRequest, response restql.HTTPResponse, errordetail error) context.Context {
seg := ctx.Value(newRelicExternalSegment)
if seg == nil {
return ctx
}
segment, ok := seg.(*newrelic.ExternalSegment)
if !ok {
n.log.Error(
"failed to retrieve new relic segment from context",
errors.Errorf("incorrect new relic segment type : %T", seg),
)
segment.End()
return ctx
}
extReq, ok := ctx.Value(newRelicExternalRequest).(*http.Request)
if !ok {
n.log.Error(
"failed to retrieve new relic external request from context",
errors.Errorf("incorrect new relic external request type : %T", seg),
)
segment.End()
return ctx
}
externalResponse, err := makeExternalResponse(extReq, response)
if err != nil {
n.log.Error("failed to make response to report for new relic", err)
segment.End()
return ctx
}
segment.AddAttribute("errordetail", errordetail)
segment.Response = externalResponse
segment.End()
// here you could send custom metrics to New Relic Insights using n.sendCustomEvent
return ctx
}
func (n *NewRelicPlugin) sendCustomEvent(name string, request restql.HTTPRequest, response restql.HTTPResponse, errs ...error) {
n.log.Debug("preparing custom event")
event := map[string]interface{}{
"url": request.Host,
"status": response.StatusCode,
"time": response.Duration.Milliseconds(),
"method": request.Method,
}
if len(errs) > 0 {
var errsStr string
b, err := json.Marshal(errs)
if err != nil {
errsStr = fmt.Sprintf("%v", errs)
} else {
errsStr = string(b)
}
event["errors"] = errsStr
}
n.application.RecordCustomEvent(name, event)
n.log.Debug("custom event send")
}
func (n *NewRelicPlugin) getTransactionFromContext(ctx context.Context) (*newrelic.Transaction, error) {
txn, ok := ctx.Value(newRelicTransaction).(*newrelic.Transaction)
if !ok {
err := errors.Errorf("incorrect transaction type : %T", txn)
n.log.Error(
"failed to retrieve new relic transaction from context",
err,
)
return nil, err
}
return txn, nil
}
func txnName(method string, reqUrl *url.URL) string {
return method + " " + reqUrl.Path
}
type noOpResponseWriter struct {
headers http.Header
body []byte
status int
}
func (n noOpResponseWriter) Header() http.Header {
return n.headers
}
func (n noOpResponseWriter) Write(body []byte) (int, error) {
n.body = body
return len(body), nil
}
func (n noOpResponseWriter) WriteHeader(statusCode int) {
n.status = statusCode
}