forked from newrelic/go-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
189 lines (159 loc) · 4.96 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
package main
import (
"errors"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"time"
newrelic "github.com/newrelic/go-agent"
)
var (
app newrelic.Application
)
func index(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "hello world")
}
func versionHandler(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "New Relic Go Agent Version: "+newrelic.Version)
}
func noticeError(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "noticing an error")
if txn, ok := w.(newrelic.Transaction); ok {
txn.NoticeError(errors.New("my error message"))
}
}
func customEvent(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "recording a custom event")
app.RecordCustomEvent("my_event_type", map[string]interface{}{
"myString": "hello",
"myFloat": 0.603,
"myInt": 123,
"myBool": true,
})
}
func setName(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "changing the transaction's name")
if txn, ok := w.(newrelic.Transaction); ok {
txn.SetName("other-name")
}
}
func addAttribute(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "adding attributes")
if txn, ok := w.(newrelic.Transaction); ok {
txn.AddAttribute("myString", "hello")
txn.AddAttribute("myInt", 123)
}
}
func background(w http.ResponseWriter, r *http.Request) {
// Transactions started without an http.Request are classified as
// background transactions.
txn := app.StartTransaction("background", nil, nil)
defer txn.End()
io.WriteString(w, "background transaction")
time.Sleep(150 * time.Millisecond)
}
func ignore(w http.ResponseWriter, r *http.Request) {
if coinFlip := (0 == rand.Intn(2)); coinFlip {
if txn, ok := w.(newrelic.Transaction); ok {
txn.Ignore()
}
io.WriteString(w, "ignoring the transaction")
} else {
io.WriteString(w, "not ignoring the transaction")
}
}
func segments(w http.ResponseWriter, r *http.Request) {
txn, _ := w.(newrelic.Transaction)
func() {
defer newrelic.StartSegment(txn, "f1").End()
func() {
defer newrelic.StartSegment(txn, "f2").End()
io.WriteString(w, "segments!")
time.Sleep(10 * time.Millisecond)
}()
time.Sleep(15 * time.Millisecond)
}()
time.Sleep(20 * time.Millisecond)
}
func mysql(w http.ResponseWriter, r *http.Request) {
txn, _ := w.(newrelic.Transaction)
s := newrelic.DatastoreSegment{
StartTime: newrelic.StartSegmentNow(txn),
Product: newrelic.DatastoreMySQL,
Collection: "users",
Operation: "INSERT",
ParameterizedQuery: "INSERT INTO users (name, age) VALUES ($1, $2)",
QueryParameters: map[string]interface{}{
"name": "Dracula",
"age": 439,
},
Host: "mysql-server-1",
PortPathOrID: "3306",
DatabaseName: "my_database",
}
defer s.End()
time.Sleep(20 * time.Millisecond)
io.WriteString(w, `performing fake query "INSERT * from users"`)
}
func external(w http.ResponseWriter, r *http.Request) {
url := "http://example.com/"
txn, _ := w.(newrelic.Transaction)
// This demonstrates an external segment where only the URL is known. If
// an http.Request is accessible then `StartExternalSegment` is
// recommended. See the implementation of `NewRoundTripper` for an
// example.
defer newrelic.ExternalSegment{
StartTime: newrelic.StartSegmentNow(txn),
URL: url,
}.End()
resp, err := http.Get(url)
if nil != err {
io.WriteString(w, err.Error())
return
}
defer resp.Body.Close()
io.Copy(w, resp.Body)
}
func roundtripper(w http.ResponseWriter, r *http.Request) {
client := &http.Client{}
txn, _ := w.(newrelic.Transaction)
client.Transport = newrelic.NewRoundTripper(txn, nil)
resp, err := client.Get("http://example.com/")
if nil != err {
io.WriteString(w, err.Error())
return
}
defer resp.Body.Close()
io.Copy(w, resp.Body)
}
func mustGetEnv(key string) string {
if val := os.Getenv(key); "" != val {
return val
}
panic(fmt.Sprintf("environment variable %s unset", key))
}
func main() {
cfg := newrelic.NewConfig("Example App", mustGetEnv("NEW_RELIC_LICENSE_KEY"))
cfg.Logger = newrelic.NewDebugLogger(os.Stdout)
var err error
app, err = newrelic.NewApplication(cfg)
if nil != err {
fmt.Println(err)
os.Exit(1)
}
http.HandleFunc(newrelic.WrapHandleFunc(app, "/", index))
http.HandleFunc(newrelic.WrapHandleFunc(app, "/version", versionHandler))
http.HandleFunc(newrelic.WrapHandleFunc(app, "/notice_error", noticeError))
http.HandleFunc(newrelic.WrapHandleFunc(app, "/custom_event", customEvent))
http.HandleFunc(newrelic.WrapHandleFunc(app, "/set_name", setName))
http.HandleFunc(newrelic.WrapHandleFunc(app, "/add_attribute", addAttribute))
http.HandleFunc(newrelic.WrapHandleFunc(app, "/ignore", ignore))
http.HandleFunc(newrelic.WrapHandleFunc(app, "/segments", segments))
http.HandleFunc(newrelic.WrapHandleFunc(app, "/mysql", mysql))
http.HandleFunc(newrelic.WrapHandleFunc(app, "/external", external))
http.HandleFunc(newrelic.WrapHandleFunc(app, "/roundtripper", roundtripper))
http.HandleFunc("/background", background)
http.ListenAndServe(":8000", nil)
}