Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TT-13766] Bump newrelic dependency #6809

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions gateway/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"github.com/gocraft/health"
"github.com/justinas/alice"
newrelic "github.com/newrelic/go-agent"
"github.com/paulbellamy/ratecounter"
"github.com/sirupsen/logrus"
"golang.org/x/sync/singleflight"
Expand All @@ -27,6 +26,7 @@ import (
"github.com/TykTechnologies/tyk/internal/middleware"
"github.com/TykTechnologies/tyk/internal/otel"
"github.com/TykTechnologies/tyk/internal/policy"
"github.com/TykTechnologies/tyk/internal/service/newrelic"
"github.com/TykTechnologies/tyk/request"
"github.com/TykTechnologies/tyk/rpc"
"github.com/TykTechnologies/tyk/storage"
Expand Down Expand Up @@ -138,8 +138,8 @@ func (gw *Gateway) createMiddleware(actualMW TykMiddleware) func(http.Handler) h
logger := mw.Base().SetRequestLogger(r)

if gw.GetConfig().NewRelic.AppName != "" {
if txn, ok := w.(newrelic.Transaction); ok {
defer newrelic.StartSegment(txn, mw.Name()).End()
if txn := newrelic.Context.Get(r); txn != nil {
defer txn.StartSegment(mw.Name()).End()
}
}

Expand Down
100 changes: 0 additions & 100 deletions gateway/newrelic.go

This file was deleted.

9 changes: 7 additions & 2 deletions gateway/proxy_muxer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/TykTechnologies/again"
"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/internal/httputil"
"github.com/TykTechnologies/tyk/internal/service/newrelic"
"github.com/TykTechnologies/tyk/tcp"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -96,9 +97,13 @@ func (h *handleWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

if NewRelicApplication != nil {
txn := NewRelicApplication.StartTransaction(r.URL.Path, w, r)
txn := NewRelicApplication.StartTransaction(r.URL.Path)
w = txn.SetWebResponse(w)

newrelic.Context.Set(r, txn)

defer txn.End()
h.router.ServeHTTP(txn, r)
h.router.ServeHTTP(w, r)
return
}
h.router.ServeHTTP(w, r)
Expand Down
31 changes: 29 additions & 2 deletions gateway/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
grayloghook "github.com/gemnasium/logrus-graylog-hook"
"github.com/gorilla/mux"
"github.com/lonelycode/osin"
newrelic "github.com/newrelic/go-agent"
"github.com/sirupsen/logrus"
logrussyslog "github.com/sirupsen/logrus/hooks/syslog"

Expand Down Expand Up @@ -67,6 +66,7 @@ import (
"github.com/TykTechnologies/tyk/internal/cache"
"github.com/TykTechnologies/tyk/internal/model"
"github.com/TykTechnologies/tyk/internal/netutil"
"github.com/TykTechnologies/tyk/internal/service/newrelic"
)

var (
Expand All @@ -78,7 +78,7 @@ var (
rawLog = logger.GetRaw()

memProfFile *os.File
NewRelicApplication newrelic.Application
NewRelicApplication *newrelic.Application

// confPaths is the series of paths to try to use as config files. The
// first one to exist will be used. If none exists, a default config
Expand Down Expand Up @@ -256,6 +256,33 @@ func NewGateway(config config.Config, ctx context.Context) *Gateway {
return gw
}

// SetupNewRelic creates new newrelic.Application instance.
func (gw *Gateway) SetupNewRelic() (app *newrelic.Application) {
var (
err error
gwConfig = gw.GetConfig()
)

log := log.WithFields(logrus.Fields{"prefix": "newrelic"})

cfg := []newrelic.ConfigOption{
newrelic.ConfigAppName(gwConfig.NewRelic.AppName),
newrelic.ConfigLicense(gwConfig.NewRelic.LicenseKey),
newrelic.ConfigEnabled(gwConfig.NewRelic.AppName != ""),
newrelic.ConfigDistributedTracerEnabled(gwConfig.NewRelic.EnableDistributedTracing),
newrelic.ConfigLogger(newrelic.NewLogger(log)),
}

if app, err = newrelic.NewApplication(cfg...); err != nil {
log.Warn("Error initializing NewRelic, skipping... ", err)
return
}

instrument.AddSink(newrelic.NewSink(app))

return
}

func (gw *Gateway) UnmarshalJSON(data []byte) error {
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions gateway/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,10 @@ func (s *Test) newGateway(genConf func(globalConf *config.Config)) *Gateway {
gwConfig.BundleBaseURL = testHttpBundles
gwConfig.MiddlewarePath = testMiddlewarePath

if err := config.FillEnv(&gwConfig); err != nil {
log.WithError(err).Error("error filling test config from env")
}

// force ipv4 for now, to work around the docker bug affecting
// Go 1.8 and earlier
gwConfig.ListenAddress = "127.0.0.1"
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ require (
github.com/goccy/go-json v0.10.4
github.com/google/go-cmp v0.6.0
github.com/nats-io/nats.go v1.38.0
github.com/newrelic/go-agent v2.13.0+incompatible
github.com/newrelic/go-agent/v3 v3.35.1
github.com/newrelic/go-agent/v3/integrations/nrgorilla v1.2.2
github.com/testcontainers/testcontainers-go v0.34.0
github.com/testcontainers/testcontainers-go/modules/kafka v0.33.0
github.com/testcontainers/testcontainers-go/modules/nats v0.33.0
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,10 @@ github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/nats-io/stan.go v0.10.4 h1:19GS/eD1SeQJaVkeM9EkvEYattnvnWrZ3wkSWSw4uXw=
github.com/nats-io/stan.go v0.10.4/go.mod h1:3XJXH8GagrGqajoO/9+HgPyKV5MWsv7S5ccdda+pc6k=
github.com/newrelic/go-agent v2.13.0+incompatible h1:Dl6m75MHAzfB0kicv9GiLxzQatRjTLUAdrnYyoT8s4M=
github.com/newrelic/go-agent v2.13.0+incompatible/go.mod h1:a8Fv1b/fYhFSReoTU6HDkTYIMZeSVNffmoS726Y0LzQ=
github.com/newrelic/go-agent/v3 v3.35.1 h1:N43qBNDILmnwLDCSfnE1yy6adyoVEU95nAOtdUgG4vA=
github.com/newrelic/go-agent/v3 v3.35.1/go.mod h1:GNTda53CohAhkgsc7/gqSsJhDZjj8vaky5u+vKz7wqM=
github.com/newrelic/go-agent/v3/integrations/nrgorilla v1.2.2 h1:YaFf6tmxSKNVgS9ZHx6O8HSpckWiyNSBZQKwaXfG1fQ=
github.com/newrelic/go-agent/v3/integrations/nrgorilla v1.2.2/go.mod h1:NlYWXdP4WVAg8v7ZM0FRWulv0OtssOS3l4R6pYlWGf0=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nsf/jsondiff v0.0.0-20230430225905-43f6cf3098c1 h1:dOYG7LS/WK00RWZc8XGgcUTlTxpp3mKhdR2Q9z9HbXM=
github.com/nsf/jsondiff v0.0.0-20230430225905-43f6cf3098c1/go.mod h1:mpRZBD8SJ55OIICQ3iWH0Yz3cjzA61JdqMLoWXeB2+8=
Expand Down
99 changes: 99 additions & 0 deletions internal/service/newrelic/newrelic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package newrelic

import (
"fmt"
"strconv"

"github.com/newrelic/go-agent/v3/integrations/nrgorilla"
"github.com/newrelic/go-agent/v3/newrelic"

"github.com/gocraft/health"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"

"github.com/TykTechnologies/tyk/internal/httpctx"
)

type (
Application = newrelic.Application
Transaction = newrelic.Transaction
ConfigOption = newrelic.ConfigOption
)

var (
NewApplication = newrelic.NewApplication

Context = httpctx.NewValue[*Transaction]("internal:new-relic-transaction")

ConfigLogger = newrelic.ConfigLogger
ConfigEnabled = newrelic.ConfigEnabled
ConfigAppName = newrelic.ConfigAppName
ConfigLicense = newrelic.ConfigLicense
ConfigDistributedTracerEnabled = newrelic.ConfigDistributedTracerEnabled
)

// AddNewRelicInstrumentation adds NewRelic instrumentation to the router
func AddNewRelicInstrumentation(app *newrelic.Application, r *mux.Router) {
r.Use(nrgorilla.Middleware(app))
}

type Logger struct{ *logrus.Entry }

func NewLogger(e *logrus.Entry) *Logger {
return &Logger{e}
}

func (l *Logger) Error(msg string, c map[string]interface{}) {
l.WithFields(c).Error(msg)
}
func (l *Logger) Warn(msg string, c map[string]interface{}) {
l.WithFields(c).Warn(msg)
}
func (l *Logger) Info(msg string, c map[string]interface{}) {
l.WithFields(c).Info(msg)
}
func (l *Logger) Debug(msg string, c map[string]interface{}) {
l.WithFields(c).Debug(msg)
}
func (l *Logger) DebugEnabled() bool {
return l.Level >= logrus.DebugLevel
}

type Sink struct {
relic *newrelic.Application
health.Sink
}

func NewSink(relic *newrelic.Application) *Sink {
return &Sink{
relic: relic,
}
}

func (s *Sink) EmitEvent(job string, event string, kvs map[string]string) {
s.relic.RecordCustomEvent(job+":"+event, makeParams(kvs))
}

func (s *Sink) EmitEventErr(job string, event string, err error, kvs map[string]string) {
s.relic.RecordCustomEvent(job+":"+event+":msg:"+err.Error(), makeParams(kvs))
}

func (s *Sink) EmitTiming(job string, event string, nanoseconds int64, kvs map[string]string) {
s.relic.RecordCustomEvent(job+":"+event+":duration_ns:"+strconv.FormatInt(nanoseconds, 10), makeParams(kvs))
}

func (s *Sink) EmitComplete(job string, status health.CompletionStatus, nanoseconds int64, kvs map[string]string) {
s.relic.RecordCustomEvent(job+":health:"+status.String()+":duration_ns:"+strconv.FormatInt(nanoseconds, 10), makeParams(kvs))
}

func (s *Sink) EmitGauge(job string, event string, value float64, kvs map[string]string) {
s.relic.RecordCustomEvent(job+":"+event+":value:"+fmt.Sprintf("%.2f", value), makeParams(kvs))
}

func makeParams(kvs map[string]string) (params map[string]interface{}) {
params = make(map[string]interface{}, len(kvs))
for k, v := range kvs {
params[k] = v
}
return
}
Loading