12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- 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(),
- },
- })
- }
- }
|