forked from newrelic/go-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (64 loc) · 1.7 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
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// An application that illustrates Distributed Tracing or Cross Application
// Tracing when using http.Server or similar frameworks.
package main
import (
"fmt"
"io"
"net/http"
"os"
"time"
newrelic "github.com/newrelic/go-agent/v3/newrelic"
)
type handler struct {
App *newrelic.Application
}
func (h *handler) ServeHTTP(writer http.ResponseWriter, req *http.Request) {
// The call to StartTransaction must include the response writer and the
// request.
txn := h.App.StartTransaction("server-txn")
defer txn.End()
writer = txn.SetWebResponse(writer)
txn.SetWebRequestHTTP(req)
if req.URL.String() == "/segments" {
defer txn.StartSegment("f1").End()
func() {
defer txn.StartSegment("f2").End()
io.WriteString(writer, "segments!")
time.Sleep(10 * time.Millisecond)
}()
time.Sleep(10 * time.Millisecond)
} else {
// Transaction.WriteHeader has to be used instead of invoking
// WriteHeader on the response writer.
writer.WriteHeader(http.StatusNotFound)
}
}
func makeApplication() (*newrelic.Application, error) {
app, err := newrelic.NewApplication(
newrelic.ConfigAppName("HTTP Server App"),
newrelic.ConfigLicense(os.Getenv("NEW_RELIC_LICENSE_KEY")),
newrelic.ConfigDebugLogger(os.Stdout),
)
if nil != err {
return nil, err
}
// Wait for the application to connect.
if err = app.WaitForConnection(5 * time.Second); nil != err {
return nil, err
}
return app, nil
}
func main() {
app, err := makeApplication()
if nil != err {
fmt.Println(err)
os.Exit(1)
}
server := http.Server{
Addr: ":8000",
Handler: &handler{App: app},
}
server.ListenAndServe()
}