Skip to content

Commit

Permalink
Merge pull request #13 from JSainsburyPLC/miguel/go-10572
Browse files Browse the repository at this point in the history
GO-10572: Add 'service_name' attribute to logger to attach logs to APM entity in NewRelic
  • Loading branch information
miguelespinosa-ss authored Apr 30, 2024
2 parents 7b5e30d + bf44c47 commit 23f1e34
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 34 deletions.
24 changes: 15 additions & 9 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -64,8 +62,6 @@ func ContextMiddleware(
c.Path(),
ip,
correlationID,
appName,
envName,
)

cc := NewContext(c, newRelicApp, logger, correlationID, isDebug, buildVersion)
Expand Down
5 changes: 4 additions & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 1 addition & 19 deletions logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 23f1e34

Please sign in to comment.