Skip to content

Commit

Permalink
Feature: #22 Add Examples (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
miyamo2 authored Feb 27, 2024
1 parent fa32e35 commit c56b87a
Showing 1 changed file with 120 additions and 4 deletions.
124 changes: 120 additions & 4 deletions exapmles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Example() {
panic(err)
}

http.HandleFunc(newrelic.WrapHandleFunc(nr, "/introduce", func(w http.ResponseWriter, r *http.Request) {
httpHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
tx := newrelic.FromContext(ctx)
logHandler := altnrslog.NewTransactionalHandler(nr, tx)
Expand All @@ -45,18 +45,88 @@ func Example() {
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 ExampleNewTransactionalHandler_WithInnerHandlerProvider() {
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")
tx := app.StartTransaction("ExampleNewTransactionalHandler_withInnerHandlerProvider")
if err != nil {
panic(err)
}
Expand All @@ -80,3 +150,49 @@ func ExampleNewTransactionalHandler_WithInnerHandlerProvider() {

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)
}

0 comments on commit c56b87a

Please sign in to comment.