cleaner server example
package main
import (
- "context"
"errors"
"fmt"
"io"
@@ -247,6 +246,32 @@ func browser(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "browser header page")
}
+func logMessage(w http.ResponseWriter, r *http.Request) {
+ txn := newrelic.FromContext(r.Context())
+ app := txn.Application()
+
+ app.RecordLog(newrelic.LogData{
+ Message: "Log Message",
+ Severity: "info",
+ })
+
+ io.WriteString(w, "A log message was recorded")
+}
+
+func logTxnMessage(w http.ResponseWriter, r *http.Request) {
+ ctx := r.Context()
+ txn := newrelic.FromContext(ctx)
+ app := txn.Application()
+
+ app.RecordLog(newrelic.LogData{
+ Message: "Log Message",
+ Severity: "info",
+ Context: ctx,
+ })
+
+ io.WriteString(w, "A log message was recorded as part of a transaction")
+}
+
func main() {
app, err := newrelic.NewApplication(
newrelic.ConfigAppName("Example App"),
@@ -276,28 +301,8 @@ func main() {
http.HandleFunc(newrelic.WrapHandleFunc(app, "/browser", browser))
http.HandleFunc(newrelic.WrapHandleFunc(app, "/async", async))
http.HandleFunc(newrelic.WrapHandleFunc(app, "/message", message))
- http.HandleFunc("/log", func(w http.ResponseWriter, req *http.Request) {
- app.RecordLog(newrelic.LogData{
- Message: "Log Message",
- Severity: "info",
- })
-
- io.WriteString(w, "A log message was recorded")
- })
-
- http.HandleFunc("/transaction_log", func(w http.ResponseWriter, req *http.Request) {
- txn := app.StartTransaction("Log Transaction")
- defer txn.End()
- ctx := newrelic.NewContext(context.Background(), txn)
-
- app.RecordLog(newrelic.LogData{
- Message: "Transaction Log Message",
- Severity: "info",
- Context: ctx,
- })
-
- io.WriteString(w, "A log message was recorded as part of a transaction")
- })
+ http.HandleFunc(newrelic.WrapHandleFunc(app, "/log", logMessage))
+ http.HandleFunc(newrelic.WrapHandleFunc(app, "/transaction_log", logTxnMessage))
http.HandleFunc("/background", func(w http.ResponseWriter, req *http.Request) {
// Transactions started without an http.Request are classified as
