From e2dcadbb85766462ea1ca3f51f3805a0a4b89d54 Mon Sep 17 00:00:00 2001 From: David Suarez Date: Sat, 26 Jun 2021 19:52:37 -0500 Subject: [PATCH] Add context to enable userID to be auto printed and any future needed context items --- events/events.go | 24 ++++++++++++++++++------ events/events_test.go | 14 ++++++++++---- security/auth.go | 3 +++ 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/events/events.go b/events/events.go index 8b5fac9..28b1787 100644 --- a/events/events.go +++ b/events/events.go @@ -1,9 +1,11 @@ package events import ( + "context" "os" "github.com/rs/zerolog" + "github.com/suared/core/security" "github.com/suared/core/types" ) @@ -20,12 +22,17 @@ func init() { //Debug write debug message to stdout //Using Loc as user provided to enable flex on how to define - e.g. domain.method or unique identifier, etc as makes sense. Will ensure it is always thought about to start -func Debug(loc string, msg string, keyVals ...types.KeyVal) { +func Debug(ctx context.Context, loc string, msg string, keyVals ...types.KeyVal) { + auth := security.GetAuth(ctx) + userID := "" + if auth != nil { + userID = auth.GetUser() + } if keyVals == nil { - debugLogger.Log().Str("loc", loc).Msg(msg) + debugLogger.Log().Str("loc", loc).Str("userID", userID).Msg(msg) } else { //dictionary, string, int, float - logger := debugLogger.Log().Str("loc", loc) + logger := debugLogger.Log().Str("loc", loc).Str("userID", userID) for _, keyVal := range keyVals { switch keyVal.Typ { case types.TYPESTRING: @@ -44,12 +51,17 @@ func Debug(loc string, msg string, keyVals ...types.KeyVal) { //Event write event message to stdout - note: events are handled outside the scope of the module by design //See debug comments, same concept here, is just marked as event to enable later processing of event stream -func Event(loc string, msg string, keyVals ...types.KeyVal) { +func Event(ctx context.Context, loc string, msg string, keyVals ...types.KeyVal) { + auth := security.GetAuth(ctx) + userID := "" + if auth != nil { + userID = auth.GetUser() + } if keyVals == nil { - eventLogger.Log().Str("loc", loc).Msg(msg) + eventLogger.Log().Str("loc", loc).Str("userID", userID).Msg(msg) } else { //dictionary, string, int, float - logger := eventLogger.Log().Str("loc", loc) + logger := eventLogger.Log().Str("loc", loc).Str("userID", userID) for _, keyVal := range keyVals { switch keyVal.Typ { case types.TYPESTRING: diff --git a/events/events_test.go b/events/events_test.go index 89106a8..7d1b3bf 100644 --- a/events/events_test.go +++ b/events/events_test.go @@ -8,11 +8,17 @@ import ( ) func TestEvents(t *testing.T) { - Debug("testA.x", "Whatever") + Debug(nil, "testA.x", "Whatever") time.Sleep(1 * time.Second) - Debug("testB.x", "Dude") + Debug(nil, "testB.x", "Dude") time.Sleep(1 * time.Second) - Debug("test1.x", "First Test!", types.Str("name", "david"), types.Int("age", 46)) + Debug(nil, "test1.x", "First Test!", types.Str("name", "david"), types.Int("age", 46)) time.Sleep(1 * time.Second) - Event("test2.x", "Second Test!", types.Str("name", "colleen"), types.Int("age", 48)) + Event(nil, "test2.x", "Second Test!", types.Str("name", "colleen"), types.Int("age", 48)) + Event(nil, "test3.1", "Third Test!", types.Str("name", "colleen"), types.Int("age", 48)) + Event(nil, "test3.2", "Third Test!", types.Str("name", "colleen"), types.Int("age", 48)) + Event(nil, "test3.3", "Third Test!", types.Str("name", "colleen"), types.Int("age", 48)) + Event(nil, "test3.4", "Third Test!", types.Str("name", "colleen"), types.Int("age", 48)) + //doesn't drop, good - uncomment to test in future if create buffer + //panic("not drop test3.4") } diff --git a/security/auth.go b/security/auth.go index c1b60fb..05c349f 100644 --- a/security/auth.go +++ b/security/auth.go @@ -61,6 +61,9 @@ func (t *BasicAuth) GetAuthHeader() string { //GetAuth - returns Auth from the provided context func GetAuth(ctx context.Context) Auth { + if ctx == nil { + return nil + } authKey := ctx.Value(authKey) if authKey == nil { return nil