newrelicAppName := os.Getenv("NEW_RELIC_APP_NAME") // example: isucon9-qualify-muroon
newrelicLicense := os.Getenv("NEW_RELIC_LICENSE")
err = apm.Setup(newrelicAppName, newrelicLicense)
if err != nil {
log.Fatalf("failed to NewRelic: %s.", err.Error())
}
apm.HandleFunc(mux, pat.Post("/initialize"), postInitialize)
NewRelic Transactionのミドルウェアを設定
mux := goji.NewMux()
mux.Use(apm.MiddlewareNewRelicTransaction)
// API
mux.HandleFunc(pat.Post("/initialize"), postInitialize)
mux.HandleFunc(pat.Get("/new_items.json"), getNewItems)
(省略)
http.RequestにNewRelicのコンテキストを付与
var (
client = &http.Client{
Transport: newrelic.NewRoundTripper(nil),
}
)
(省略)
res, err := client.Do(apm.RequestWithContext(ctx, req))
req, err := http.newrequest(http.methodpost, url, bytes.newbuffer(b))
req.header.set("user-agent", useragent)
req.header.set("content-type", "application/json")
res, err := getclient().do(req)
RequestWithContextとGetClientと送信(cliend.Do)をまとめて行う
res, err := apm.RequestDoWithContext(ctx, req)
DB(MySQL)のAPM
使用例
// tx is parent APM transaction
s := apm.startdatastoresegment(tx, "select * from `categories` where `id` = ?", categoryid)
err = sqlx.get(q, &category, "select * from `categories` where `id` = ?", categoryid)
s.End()
使用するにはDB情報を事前にセット(必須ではない)
apm.SetupDB(host, port, dbname)