-
Notifications
You must be signed in to change notification settings - Fork 0
/
newrelicmodule.go
81 lines (55 loc) · 1.34 KB
/
newrelicmodule.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
package newrelicmodule
import (
"fmt"
"net/http"
"os"
"github.com/newrelic/go-agent/v3/newrelic"
)
var app *newrelic.Application
func getEnv(key string) string {
var env string
var ok bool
if env, ok = os.LookupEnv(key); !ok {
return ""
}
return env
}
func createApplication() {
if app != nil {
return
}
var err error
app, err = newrelic.NewApplication(
newrelic.ConfigAppName(getEnv("NEW_RELIC_APP_NAME")),
newrelic.ConfigLicense(getEnv("NEW_RELIC_KEY")),
)
if nil != err {
fmt.Printf("Unable to create New Relic application..... Error %+v\n", err)
os.Exit(1)
}
}
func ProcessExternalSegment(externalSegmentRequestCh chan ExternalSegment, externalSegmentResponseCh chan ExternalSegment) {
createApplication()
for {
msg := <-externalSegmentRequestCh
txn := app.StartTransaction(msg.TransactionName)
client := &http.Client{}
seg := newrelic.StartExternalSegment(txn, msg.Request)
resp, err := client.Do(msg.Request)
if err != nil {
txn.NoticeError(err)
}
externalSegmentResponseCh <- ExternalSegment{TransactionName: msg.TransactionName, Request: msg.Request, Response: resp}
seg.End()
txn.End()
}
}
func LogError(errChan chan ErrorLog) {
createApplication()
for {
errLog := <-errChan
txn := app.StartTransaction(errLog.TransactionName)
txn.NoticeError(errLog.Error)
txn.End()
}
}