-
Notifications
You must be signed in to change notification settings - Fork 8
/
new_relic.go
92 lines (71 loc) · 2.75 KB
/
new_relic.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
package colibri_nr
import (
"context"
"fmt"
"github.com/colibri-project-io/colibri-sdk-go/pkg/base/monitoring/colibri-monitoring-base"
"net/http"
"net/url"
"github.com/colibri-project-io/colibri-sdk-go/pkg/base/config"
"github.com/colibri-project-io/colibri-sdk-go/pkg/base/logging"
_ "github.com/newrelic/go-agent/v3/integrations/nrpq"
"github.com/newrelic/go-agent/v3/newrelic"
)
const pgDriverName = "nrpostgres"
type MonitoringNewRelic struct {
*newrelic.Application
}
func StartNewRelicMonitoring() colibri_monitoring_base.Monitoring {
app, err := newrelic.NewApplication(
newrelic.ConfigAppName(config.APP_NAME),
newrelic.ConfigLicense(config.NEW_RELIC_LICENSE),
newrelic.ConfigDistributedTracerEnabled(true),
)
if err != nil {
logging.Fatal("An error occurred while loading the Monitoring provider. Error: %s", err)
}
return &MonitoringNewRelic{app}
}
func (m *MonitoringNewRelic) StartTransaction(ctx context.Context, name string) (interface{}, context.Context) {
transaction := m.Application.StartTransaction(name)
ctx = newrelic.NewContext(ctx, transaction)
return transaction, ctx
}
func (m *MonitoringNewRelic) EndTransaction(transaction interface{}) {
transaction.(*newrelic.Transaction).End()
}
func (m *MonitoringNewRelic) setWebRequest(transaction interface{}, header http.Header, url *url.URL, method string) {
transaction.(*newrelic.Transaction).SetWebRequest(newrelic.WebRequest{
Header: header,
URL: url,
Method: method,
Transport: newrelic.TransportHTTP,
})
}
func (m *MonitoringNewRelic) StartWebRequest(ctx context.Context, header http.Header, path string, method string) (interface{}, context.Context) {
txn, ctx := m.StartTransaction(ctx, fmt.Sprintf("%s %s", method, path))
m.setWebRequest(txn, header, &url.URL{Path: path}, method)
return txn, ctx
}
func (m *MonitoringNewRelic) SetWebResponse(transaction interface{}, w http.ResponseWriter) http.ResponseWriter {
return transaction.(*newrelic.Transaction).SetWebResponse(w)
}
func (m *MonitoringNewRelic) StartTransactionSegment(ctx context.Context, name string, attributes map[string]string) interface{} {
transaction := m.GetTransactionInContext(ctx)
segment := transaction.(*newrelic.Transaction).StartSegment(name)
for key, value := range attributes {
segment.AddAttribute(key, value)
}
return segment
}
func (m *MonitoringNewRelic) EndTransactionSegment(segment interface{}) {
segment.(*newrelic.Segment).End()
}
func (m *MonitoringNewRelic) GetTransactionInContext(ctx context.Context) interface{} {
return newrelic.FromContext(ctx)
}
func (m *MonitoringNewRelic) NoticeError(transaction interface{}, err error) {
transaction.(*newrelic.Transaction).NoticeError(err)
}
func (m *MonitoringNewRelic) GetSQLDBDriverName() string {
return pgDriverName
}