mirror of
https://github.com/imgproxy/imgproxy.git
synced 2024-11-24 08:12:38 +02:00
89 lines
1.8 KiB
Go
89 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/newrelic/go-agent/v3/newrelic"
|
|
)
|
|
|
|
const (
|
|
newRelicTransactionCtxKey = ctxKey("newRelicTransaction")
|
|
)
|
|
|
|
var (
|
|
newRelicEnabled = false
|
|
|
|
newRelicApp *newrelic.Application
|
|
)
|
|
|
|
func initNewrelic() error {
|
|
if len(conf.NewRelicKey) == 0 {
|
|
return nil
|
|
}
|
|
|
|
name := conf.NewRelicAppName
|
|
if len(name) == 0 {
|
|
name = "imgproxy"
|
|
}
|
|
|
|
var err error
|
|
|
|
newRelicApp, err = newrelic.NewApplication(
|
|
newrelic.ConfigAppName(name),
|
|
newrelic.ConfigLicense(conf.NewRelicKey),
|
|
)
|
|
|
|
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, http.ResponseWriter) {
|
|
if !newRelicEnabled {
|
|
return ctx, func() {}, rw
|
|
}
|
|
|
|
txn := newRelicApp.StartTransaction("request")
|
|
txn.SetWebRequestHTTP(r)
|
|
newRw := txn.SetWebResponse(rw)
|
|
cancel := func() { txn.End() }
|
|
return context.WithValue(ctx, newRelicTransactionCtxKey, txn), cancel, newRw
|
|
}
|
|
|
|
func startNewRelicSegment(ctx context.Context, name string) context.CancelFunc {
|
|
if !newRelicEnabled {
|
|
return func() {}
|
|
}
|
|
|
|
txn := ctx.Value(newRelicTransactionCtxKey).(*newrelic.Transaction)
|
|
segment := txn.StartSegment(name)
|
|
return func() { segment.End() }
|
|
}
|
|
|
|
func sendErrorToNewRelic(ctx context.Context, err error) {
|
|
if newRelicEnabled {
|
|
txn := ctx.Value(newRelicTransactionCtxKey).(*newrelic.Transaction)
|
|
txn.NoticeError(err)
|
|
}
|
|
}
|
|
|
|
func sendTimeoutToNewRelic(ctx context.Context, d time.Duration) {
|
|
if newRelicEnabled {
|
|
txn := ctx.Value(newRelicTransactionCtxKey).(*newrelic.Transaction)
|
|
txn.NoticeError(newrelic.Error{
|
|
Message: "Timeout",
|
|
Class: "Timeout",
|
|
Attributes: map[string]interface{}{
|
|
"time": d.Seconds(),
|
|
},
|
|
})
|
|
}
|
|
}
|