1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-12-09 09:56:01 +02:00
imgproxy/newrelic.go
2020-02-27 21:44:59 +06:00

71 lines
1.6 KiB
Go

package main
import (
"context"
"fmt"
"net/http"
"time"
newrelic "github.com/newrelic/go-agent"
)
var (
newRelicEnabled = false
newRelicApp newrelic.Application
newRelicTransactionCtxKey = ctxKey("newRelicTransaction")
)
func initNewrelic() error {
if len(conf.NewRelicKey) == 0 {
return nil
}
name := conf.NewRelicAppName
if len(name) == 0 {
name = "imgproxy"
}
var err error
config := newrelic.NewConfig(name, conf.NewRelicKey)
newRelicApp, err = newrelic.NewApplication(config)
if err != nil {
return fmt.Errorf("Can't init New Relic agent: %s", err)
}
newRelicEnabled = true
return nil
}
func startNewRelicTransaction(ctx context.Context, rw http.ResponseWriter, r *http.Request) (context.Context, context.CancelFunc) {
txn := newRelicApp.StartTransaction("request", rw, r)
cancel := func() { txn.End() }
return context.WithValue(ctx, newRelicTransactionCtxKey, txn), cancel
}
func startNewRelicSegment(ctx context.Context, name string) context.CancelFunc {
txn := ctx.Value(newRelicTransactionCtxKey).(newrelic.Transaction)
segment := newrelic.StartSegment(txn, name)
return func() { segment.End() }
}
func sendErrorToNewRelic(ctx context.Context, err error) {
txn := ctx.Value(newRelicTransactionCtxKey).(newrelic.Transaction)
txn.NoticeError(err)
}
func sendTimeoutToNewRelic(ctx context.Context, d time.Duration) {
txn := ctx.Value(newRelicTransactionCtxKey).(newrelic.Transaction)
txn.NoticeError(newrelic.Error{
Message: "Timeout",
Class: "Timeout",
Attributes: map[string]interface{}{
"time": d.Seconds(),
},
})
}