From bf44c47977ca6e4f460d3aaf99aadd3281bff68a Mon Sep 17 00:00:00 2001 From: Miguel Espinosa Date: Mon, 29 Apr 2024 12:04:01 +0100 Subject: [PATCH] GO-10572: Add 'service_name' attribute to logger to attach logs to APM entity in NewRelic --- app.go | 24 +++++++++++++++--------- context.go | 6 +----- context_test.go | 5 ++++- logging.go | 20 +------------------- 4 files changed, 21 insertions(+), 34 deletions(-) diff --git a/app.go b/app.go index d98503b..5272ea9 100644 --- a/app.go +++ b/app.go @@ -69,10 +69,10 @@ func newEcho(conf Config) (*echo.Echo, newrelic.Application) { e := echo.New() e.HideBanner = true e.HidePort = true - e.Logger = appScopeLogger(logger, conf.AppName, conf.EnvName) + e.Logger = &Logger{logger} // the order of these middleware is important - context should be first, error should be after logging ones - e.Use(ContextMiddleware(conf.AppName, conf.EnvName, conf.BuildVersion, logger, conf.IsDebug, newRelicApp)) + e.Use(ContextMiddleware(conf.BuildVersion, logger, conf.IsDebug, newRelicApp)) e.Use(PanicHandlerMiddleware(conf.ErrorHandler)) if conf.UseDefaultHeaders { e.Use(DefaultHeadersMiddleware()) @@ -108,26 +108,32 @@ func getHostName() string { return name } -func logger(conf Config) *logrus.Logger { +func getServiceName(projectName, appName, envName string) string { + return fmt.Sprintf("%s-%s-%s", projectName, appName, envName) +} + +func logger(conf Config) *logrus.Entry { logger := logrus.New() logger.SetLevel(conf.LogLevel) logger.SetFormatter(conf.LogFormatter) - logger.WithFields( + entry := logger.WithFields( logrus.Fields{ + "service_name": getServiceName(conf.ProjectName, conf.AppName, conf.EnvName), "project": conf.ProjectName, "application": conf.AppName, "environment": conf.EnvName, "build_version": conf.BuildVersion, "hostname": getHostName(), - }).Infof("XEcho app created %s(%s)", conf.AppName, conf.BuildVersion) - return logger + }) + entry.Infof("XEcho app created %s(%s)", conf.AppName, conf.BuildVersion) + return entry } -func createNewRelicApp(conf Config, logger *logrus.Logger) newrelic.Application { - nrConf := newrelic.NewConfig(fmt.Sprintf("%s-%s-%s", conf.ProjectName, conf.AppName, conf.EnvName), conf.NewRelicLicense) +func createNewRelicApp(conf Config, logger *logrus.Entry) newrelic.Application { + nrConf := newrelic.NewConfig(getServiceName(conf.ProjectName, conf.AppName, conf.EnvName), conf.NewRelicLicense) nrConf.CrossApplicationTracer.Enabled = false nrConf.DistributedTracer.Enabled = true - nrConf.Logger = nrlogrus.Transform(logger) + nrConf.Logger = nrlogrus.Transform(logger.Logger) nrConf.Enabled = conf.NewRelicEnabled nrConf.Labels = map[string]string{"Env": conf.EnvName, "Project": conf.ProjectName} app, err := newrelic.NewApplication(nrConf) diff --git a/context.go b/context.go index 4df3441..f7c7c5f 100644 --- a/context.go +++ b/context.go @@ -47,10 +47,8 @@ func (c *Context) AddNewRelicAttribute(key string, val interface{}) { } func ContextMiddleware( - appName string, - envName string, buildVersion string, - logger *logrus.Logger, + logger *logrus.Entry, isDebug bool, newRelicApp newrelic.Application, ) echo.MiddlewareFunc { @@ -64,8 +62,6 @@ func ContextMiddleware( c.Path(), ip, correlationID, - appName, - envName, ) cc := NewContext(c, newRelicApp, logger, correlationID, isDebug, buildVersion) diff --git a/context_test.go b/context_test.go index f43e618..d67a9ba 100644 --- a/context_test.go +++ b/context_test.go @@ -22,7 +22,10 @@ func TestEchoHandler(t *testing.T) { func TestContextMiddleware(t *testing.T) { ctx, _, _ := getEchoTestCtx() - mw := ContextMiddleware("testApp", "testEnv", "build-1.2.3", NullLogger(), true, stubNewRelicApp()) + mw := ContextMiddleware("build-1.2.3", NullLogger().WithFields(logrus.Fields{ + "application": "testApp", + "environment": "testEnv", + }), true, stubNewRelicApp()) hCalled := false h := EchoHandler(func(c *Context) error { hCalled = true diff --git a/logging.go b/logging.go index 1e581e9..81852d7 100644 --- a/logging.go +++ b/logging.go @@ -77,32 +77,14 @@ func dumpResponse(c *Context, drw *debugResponseWriter) { } } -func appScopeLogger( - logger *logrus.Logger, - appName string, - envName string, -) *Logger { - entry := logger.WithFields(logrus.Fields{ - "application": appName, - "env": envName, - "scope": "application", - }) - return &Logger{entry} -} - func requestScopeLogger( - logger *logrus.Logger, + logger *logrus.Entry, r *http.Request, route string, ip string, correlationID string, - appName string, - envName string, ) *Logger { ctxLogger := logger.WithFields(logrus.Fields{ - "application": appName, - "env": envName, - "scope": "request", "correlation_id": correlationID, "url": r.RequestURI, "route": route,