Skip to content

Commit

Permalink
Merge pull request #2 from JSainsburyPLC/feature/add-build-version
Browse files Browse the repository at this point in the history
Add build version
  • Loading branch information
w32blaster authored Aug 23, 2019
2 parents 0cd9fd3 + 4e7bce2 commit 49ff45a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
9 changes: 6 additions & 3 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package xecho

import (
"fmt"
"net/http"

"github.com/labstack/echo"
"github.com/newrelic/go-agent"
"github.com/newrelic/go-agent/_integrations/nrlogrus"
"github.com/sirupsen/logrus"
"net/http"
)

type Config struct {
ProjectName string
AppName string
EnvName string
BuildVersion string
LogLevel logrus.Level
IsDebug bool
NewRelicLicense string
Expand All @@ -26,6 +28,7 @@ func NewConfig() Config {
ProjectName: "",
AppName: "",
EnvName: "",
BuildVersion: "",
LogLevel: logrus.InfoLevel,
IsDebug: false,
NewRelicLicense: "",
Expand All @@ -45,10 +48,10 @@ func Echo(conf Config) *echo.Echo {
e := echo.New()
e.HideBanner = true
e.HidePort = true
e.Logger = appScopeLogger(logger, conf.AppName, conf.EnvName)
e.Logger = appScopeLogger(logger, conf.AppName, conf.EnvName, conf.BuildVersion)

// the order of these middleware is important - context should be first, error should be after logging ones
e.Use(ContextMiddleware(conf.AppName, conf.EnvName, logger, conf.IsDebug, newRelicApp))
e.Use(ContextMiddleware(conf.AppName, conf.EnvName, conf.BuildVersion, logger, conf.IsDebug, newRelicApp))
e.Use(PanicHandlerMiddleware(conf.ErrorHandler))
if conf.UseDefaultHeaders {
e.Use(DefaultHeadersMiddleware())
Expand Down
6 changes: 5 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (c *Context) AddNewRelicAttribute(key string, val interface{}) {
func ContextMiddleware(
appName string,
envName string,
buildVersion string,
logger *logrus.Logger,
isDebug bool,
newRelicApp newrelic.Application,
Expand All @@ -64,9 +65,10 @@ func ContextMiddleware(
correlationID,
appName,
envName,
buildVersion,
)

cc := NewContext(c, newRelicApp, logger, correlationID, isDebug)
cc := NewContext(c, newRelicApp, logger, correlationID, isDebug, buildVersion)

return h(cc)
}
Expand All @@ -79,6 +81,7 @@ func NewContext(
logger *Logger,
correlationID string,
isDebug bool,
buildVersion string,
) *Context {
newRelicTx := newRelicApp.StartTransaction(
echoCtx.Request().URL.Path,
Expand All @@ -103,6 +106,7 @@ func NewContext(
customCtx.AddNewRelicAttribute("route", echoCtx.Path())
customCtx.AddNewRelicAttribute("correlationID", correlationID)
customCtx.AddNewRelicAttribute("ip", echoCtx.RealIP())
customCtx.AddNewRelicAttribute("buildVersion", buildVersion)

return customCtx
}
Expand Down
2 changes: 1 addition & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestEchoHandler(t *testing.T) {

func TestContextMiddleware(t *testing.T) {
ctx, _, _ := getEchoTestCtx()
mw := ContextMiddleware("testApp", "testEnv", NullLogger(), true, stubNewRelicApp())
mw := ContextMiddleware("testApp", "testEnv", "build-1.2.3", NullLogger(), true, stubNewRelicApp())
hCalled := false
h := EchoHandler(func(c *Context) error {
hCalled = true
Expand Down
17 changes: 11 additions & 6 deletions logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package xecho
import (
"bytes"
"encoding/json"
"github.com/labstack/echo"
"github.com/labstack/gommon/log"
"github.com/sirupsen/logrus"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"

"github.com/labstack/echo"
"github.com/labstack/gommon/log"
"github.com/sirupsen/logrus"
)

func RequestLoggerMiddleware() echo.MiddlewareFunc {
Expand Down Expand Up @@ -97,11 +98,13 @@ func appScopeLogger(
logger *logrus.Logger,
appName string,
envName string,
buildVersion string,
) *Logger {
entry := logger.WithFields(logrus.Fields{
"app": appName,
"env": envName,
"scope": "app",
"app": appName,
"env": envName,
"build_version": buildVersion,
"scope": "app",
})
return &Logger{entry}
}
Expand All @@ -114,10 +117,12 @@ func requestScopeLogger(
correlationID string,
appName string,
envName string,
buildVersion string,
) *Logger {
ctxLogger := logger.WithFields(logrus.Fields{
"app": appName,
"env": envName,
"build_version": buildVersion,
"scope": "request",
"correlation_id": correlationID,
"url": r.RequestURI,
Expand Down

0 comments on commit 49ff45a

Please sign in to comment.