From 10dfaca779dc9474cadbbf8aa132f26db4e27c00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0smail?= Date: Wed, 13 Nov 2024 15:25:23 +0300 Subject: [PATCH 1/2] Update event.go --- management/server/event.go | 57 ++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/management/server/event.go b/management/server/event.go index 93b809226..aa8d55d5b 100644 --- a/management/server/event.go +++ b/management/server/event.go @@ -3,6 +3,7 @@ package server import ( "context" "fmt" + "os" "time" log "github.com/sirupsen/logrus" @@ -11,6 +12,31 @@ import ( "github.com/netbirdio/netbird/management/server/status" ) +var eventActivityIsEnabled = 0 + +func isEnabled() bool { + + if eventActivityIsEnabled == 1 { + return true + } + + if eventActivityIsEnabled == 2 { + return false + } + + response := os.Getenv("NB_EVENT_ACTIVITY_ENABLED") + + if response == "" || response == "true" || response == "1" || response == "True" { + eventActivityIsEnabled = 1 + } else { + eventActivityIsEnabled = 2 + } + + log.Printf("response NB_EVENT_ACTIVITY_ENABLED: %s, eventActivityIsEnabled: %d", response, eventActivityIsEnabled) + + return isEnabled() +} + // GetEvents returns a list of activity events of an account func (am *DefaultAccountManager) GetEvents(ctx context.Context, accountID, userID string) ([]*activity.Event, error) { unlock := am.Store.AcquireWriteLockByUID(ctx, accountID) @@ -57,19 +83,20 @@ func (am *DefaultAccountManager) GetEvents(ctx context.Context, accountID, userI func (am *DefaultAccountManager) StoreEvent(ctx context.Context, initiatorID, targetID, accountID string, activityID activity.ActivityDescriber, meta map[string]any) { - go func() { - _, err := am.eventStore.Save(ctx, &activity.Event{ - Timestamp: time.Now().UTC(), - Activity: activityID, - InitiatorID: initiatorID, - TargetID: targetID, - AccountID: accountID, - Meta: meta, - }) - if err != nil { - // todo add metric - log.WithContext(ctx).Errorf("received an error while storing an activity event, error: %s", err) - } - }() - + if isEnabled() { + go func() { + _, err := am.eventStore.Save(ctx, &activity.Event{ + Timestamp: time.Now().UTC(), + Activity: activityID, + InitiatorID: initiatorID, + TargetID: targetID, + AccountID: accountID, + Meta: meta, + }) + if err != nil { + // todo add metric + log.WithContext(ctx).Errorf("received an error while storing an activity event, error: %s", err) + } + }() + } } From 2e1674e999c0eb28ca94fa57fc1a1dad0de0f942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0smail?= Date: Wed, 13 Nov 2024 15:54:44 +0300 Subject: [PATCH 2/2] Add disable option for event activities. --- management/server/event.go | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/management/server/event.go b/management/server/event.go index aa8d55d5b..b455cf785 100644 --- a/management/server/event.go +++ b/management/server/event.go @@ -12,29 +12,9 @@ import ( "github.com/netbirdio/netbird/management/server/status" ) -var eventActivityIsEnabled = 0 - func isEnabled() bool { - - if eventActivityIsEnabled == 1 { - return true - } - - if eventActivityIsEnabled == 2 { - return false - } - - response := os.Getenv("NB_EVENT_ACTIVITY_ENABLED") - - if response == "" || response == "true" || response == "1" || response == "True" { - eventActivityIsEnabled = 1 - } else { - eventActivityIsEnabled = 2 - } - - log.Printf("response NB_EVENT_ACTIVITY_ENABLED: %s, eventActivityIsEnabled: %d", response, eventActivityIsEnabled) - - return isEnabled() + response := os.Getenv("NB_EVENT_ACTIVITY_LOG_ENABLED") + return response == "" || response == "true" } // GetEvents returns a list of activity events of an account