Skip to content

Commit

Permalink
add transaction_id to context logger
Browse files Browse the repository at this point in the history
  • Loading branch information
karinamzalez committed Jan 31, 2024
1 parent bc8cce8 commit b1fd62b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/twinj/uuid v1.0.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.15.0 // indirect
Expand All @@ -42,4 +43,3 @@ require (
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/twinj/uuid v1.0.0 h1:fzz7COZnDrXGTAOHGuUGYd6sG+JMq+AoE7+Jlu0przk=
github.com/twinj/uuid v1.0.0/go.mod h1:mMgcE1RHFUFqe5AfiwlINXisXfDGro23fWdPUfOMjRY=
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
golang.org/x/crypto v0.0.0-20180723164146-c126467f60eb/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down Expand Up @@ -199,4 +201,3 @@ gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=
gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

2 changes: 1 addition & 1 deletion ssas/service/admin/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func routes() *chi.Mux {
r := chi.NewRouter()
m := monitoring.GetMonitor()

r.Use(gcmw.RequestID, service.NewAPILogger(), service.ConnectionClose, service.NewCtxLogger)
r.Use(gcmw.RequestID, service.NewTransactionID, service.NewAPILogger(), service.ConnectionClose, service.NewCtxLogger)
r.With(requireBasicAuth).Post(m.WrapHandler("/group", createGroup))
r.With(requireBasicAuth).Get(m.WrapHandler("/group", listGroups))
r.With(requireBasicAuth).Put(m.WrapHandler("/group/{id}", updateGroup))
Expand Down
20 changes: 20 additions & 0 deletions ssas/service/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/go-chi/chi/v5/middleware"
"github.com/sirupsen/logrus"
"github.com/twinj/uuid"

"github.com/CMSgov/bcda-ssas-app/log"
"github.com/CMSgov/bcda-ssas-app/ssas"
Expand Down Expand Up @@ -50,6 +51,10 @@ func (l *APILogger) NewLogEntry(r *http.Request) middleware.LogEntry {
logFields["okta_id"] = rd.OktaID
}

if tid, ok := r.Context().Value(CtxTransactionKey).(string); ok {
logFields["transaction_id"] = tid
}

entry.Logger = entry.Logger.WithFields(logFields)

entry.Logger.Infoln("request started")
Expand All @@ -74,8 +79,23 @@ func NewCtxLogger(next http.Handler) http.Handler {
if rd, ok := r.Context().Value("rd").(ssas.AuthRegData); ok {
logFields["okta_id"] = rd.OktaID
}
logFields["transaction_id"] = r.Context().Value(CtxTransactionKey).(string)
newLogEntry := &log.APILoggerEntry{Logger: log.Logger.WithFields(logFields)}
r = r.WithContext(context.WithValue(r.Context(), log.CtxLoggerKey, newLogEntry))
next.ServeHTTP(w, r)
})
}

// type to create context.Context key
type CtxTransactionKeyType string

// context.Context key to get the transaction ID from the request context
const CtxTransactionKey CtxTransactionKeyType = "ctxTransaction"

// Adds a transaction ID to the request context
func NewTransactionID(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(context.WithValue(r.Context(), CtxTransactionKey, uuid.NewV4().String()))
next.ServeHTTP(w, r)
})
}
2 changes: 1 addition & 1 deletion ssas/service/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func start(ps *service.Server, as *service.Server, forwarder *http.Server) {

func newForwardingRouter() http.Handler {
r := chi.NewRouter()
r.Use(gcmw.RequestID, service.NewAPILogger(), service.ConnectionClose, service.NewCtxLogger)
r.Use(gcmw.RequestID, service.NewTransactionID, service.NewAPILogger(), service.ConnectionClose, service.NewCtxLogger)
r.Get("/*", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// TODO only forward requests for paths in our own host or resource server
url := "https://" + req.Host + req.URL.String()
Expand Down
2 changes: 1 addition & 1 deletion ssas/service/public/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func routes() *chi.Mux {
router := chi.NewRouter()
m := monitoring.GetMonitor()
//v1 Routes
router.Use(gcmw.RequestID, service.NewAPILogger(), service.ConnectionClose, service.NewCtxLogger)
router.Use(gcmw.RequestID, service.NewTransactionID, service.NewAPILogger(), service.ConnectionClose, service.NewCtxLogger)
router.Post(m.WrapHandler("/token", token))
router.Post(m.WrapHandler("/introspect", introspect))
router.With(parseToken, requireRegTokenAuth, readGroupID).Post(m.WrapHandler("/register", RegisterSystem))
Expand Down

0 comments on commit b1fd62b

Please sign in to comment.