-
Notifications
You must be signed in to change notification settings - Fork 0
/
exapmles_test.go
198 lines (165 loc) · 5.42 KB
/
exapmles_test.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
package altnrslog_test
import (
"encoding/json"
"fmt"
"io"
"log"
"log/slog"
"net/http"
"os"
"github.com/miyamo2/altnrslog"
"github.com/newrelic/go-agent/v3/newrelic"
)
func Example() {
type Request struct {
Name string `json:"name"`
}
nr, err := newrelic.NewApplication(
newrelic.ConfigAppName(os.Getenv("NEW_RELIC_CONFIG_APP_NAME")),
newrelic.ConfigLicense(os.Getenv("NEW_RELIC_CONFIG_LICENSE")),
newrelic.ConfigAppLogForwardingEnabled(true),
)
if err != nil {
panic(err)
}
httpHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
tx := newrelic.FromContext(ctx)
logHandler := altnrslog.NewTransactionalHandler(nr, tx)
logger := slog.New(logHandler)
var req Request
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
logger.InfoContext(ctx, "START", slog.Group("request", slog.String("name", req.Name)))
response := fmt.Sprintf("Hello, %s!", req.Name)
defer logger.InfoContext(ctx, "END", slog.String("response", response))
w.Write([]byte(response))
})
http.Handle(newrelic.WrapHandle(nr, "/introduce", httpHandler))
log.Fatal(http.ListenAndServe(":8080", nil))
}
func Example_withCustomMiddleware() {
type Request struct {
Name string `json:"name"`
}
nr, err := newrelic.NewApplication(
newrelic.ConfigAppName(os.Getenv("NEW_RELIC_CONFIG_APP_NAME")),
newrelic.ConfigLicense(os.Getenv("NEW_RELIC_CONFIG_LICENSE")),
newrelic.ConfigAppLogForwardingEnabled(true),
)
if err != nil {
panic(err)
}
httpHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
tx := newrelic.FromContext(ctx)
logHandler := altnrslog.NewTransactionalHandler(nr, tx)
logger := slog.New(logHandler)
var req Request
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
logger.InfoContext(ctx, "START", slog.Group("request", slog.String("name", req.Name)))
response := fmt.Sprintf("Hello, %s!", req.Name)
defer logger.InfoContext(ctx, "END", slog.String("response", response))
w.Write([]byte(response))
})
middleware := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
tx := newrelic.FromContext(ctx)
logHandler := altnrslog.NewTransactionalHandler(nr, tx)
logger := slog.New(logHandler)
ctx, err := altnrslog.StoreToContext(ctx, logger)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
next.ServeHTTP(w, r.WithContext(ctx))
})
}
http.Handle(newrelic.WrapHandle(nr, "/introduce", middleware(httpHandler)))
log.Fatal(http.ListenAndServe(":8080", nil))
}
func ExampleNewTransactionalHandler() {
app, err := newrelic.NewApplication(
newrelic.ConfigAppName(os.Getenv("NEW_RELIC_CONFIG_APP_NAME")),
newrelic.ConfigLicense(os.Getenv("NEW_RELIC_CONFIG_LICENSE")),
newrelic.ConfigAppLogForwardingEnabled(true),
)
tx := app.StartTransaction("ExampleNewTransactionalHandler")
if err != nil {
panic(err)
}
txHandler := altnrslog.NewTransactionalHandler(app, tx)
slog.New(txHandler)
}
func ExampleNewTransactionalHandler_withInnerHandlerProvider() {
app, err := newrelic.NewApplication(
newrelic.ConfigAppName(os.Getenv("NEW_RELIC_CONFIG_APP_NAME")),
newrelic.ConfigLicense(os.Getenv("NEW_RELIC_CONFIG_LICENSE")),
newrelic.ConfigAppLogForwardingEnabled(true),
)
tx := app.StartTransaction("ExampleNewTransactionalHandler_withInnerHandlerProvider")
if err != nil {
panic(err)
}
handlerOpt := slog.HandlerOptions{
AddSource: true,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
return slog.Attr{
Key: fmt.Sprintf("replaced.%s.%s", groups[0], a.Key),
Value: a.Value,
}
},
}
jsonHandlerProvider := func(w io.Writer) slog.Handler {
return slog.NewJSONHandler(w, &handlerOpt)
}
txHandler := altnrslog.NewTransactionalHandler(app, tx,
altnrslog.WithInnerHandlerProvider(jsonHandlerProvider))
slog.New(txHandler)
}
func ExampleNewTransactionalHandler_withInnerWriter() {
app, err := newrelic.NewApplication(
newrelic.ConfigAppName(os.Getenv("NEW_RELIC_CONFIG_APP_NAME")),
newrelic.ConfigLicense(os.Getenv("NEW_RELIC_CONFIG_LICENSE")),
newrelic.ConfigAppLogForwardingEnabled(true),
)
tx := app.StartTransaction("ExampleNewTransactionalHandler_withInnerWriter")
if err != nil {
panic(err)
}
logFile, err := os.OpenFile("log.txt", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
panic(err)
}
mw := io.MultiWriter(os.Stdout, logFile)
txHandler := altnrslog.NewTransactionalHandler(app, tx, altnrslog.WithInnerWriter(mw))
slog.New(txHandler)
}
func ExampleNewTransactionalHandler_withSlogHandlerSpecify() {
app, err := newrelic.NewApplication(
newrelic.ConfigAppName(os.Getenv("NEW_RELIC_CONFIG_APP_NAME")),
newrelic.ConfigLicense(os.Getenv("NEW_RELIC_CONFIG_LICENSE")),
newrelic.ConfigAppLogForwardingEnabled(true),
)
tx := app.StartTransaction("ExampleNewTransactionalHandler_withSlogHandlerSpecify")
if err != nil {
panic(err)
}
txHandler := altnrslog.NewTransactionalHandler(app, tx,
altnrslog.WithSlogHandlerSpecify(true, &slog.HandlerOptions{
AddSource: true,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
return slog.Attr{
Key: fmt.Sprintf("replaced.%s.%s", groups[0], a.Key),
Value: a.Value,
}
},
}))
slog.New(txHandler)
}