Skip to content

Commit

Permalink
fix logging
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiuhc committed Jul 1, 2024
1 parent d6d6b9a commit 8ba6b45
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
15 changes: 6 additions & 9 deletions pkg/experiment/local/cohort_download_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package local
import (
"encoding/base64"
"encoding/json"
"log"
"github.com/amplitude/experiment-go-server/internal/logger"
"net/http"
"strconv"
"time"
Expand All @@ -16,7 +16,7 @@ type DirectCohortDownloadApi struct {
CohortRequestDelayMillis int
ServerUrl string
Debug bool
Logger *log.Logger
log *logger.Log
}

func NewDirectCohortDownloadApi(apiKey, secretKey string, maxCohortSize, cohortRequestDelayMillis int, serverUrl string, debug bool) *DirectCohortDownloadApi {
Expand All @@ -27,23 +27,20 @@ func NewDirectCohortDownloadApi(apiKey, secretKey string, maxCohortSize, cohortR
CohortRequestDelayMillis: cohortRequestDelayMillis,
ServerUrl: serverUrl,
Debug: debug,
Logger: log.New(log.Writer(), "Amplitude: ", log.LstdFlags),
}
if debug {
api.Logger.SetFlags(log.LstdFlags | log.Lshortfile)
log: logger.New(debug),
}
return api
}

func (api *DirectCohortDownloadApi) GetCohort(cohortID string, cohort *Cohort) (*Cohort, error) {
api.Logger.Printf("getCohortMembers(%s): start", cohortID)
api.log.Debug("getCohortMembers(%s): start", cohortID)
errors := 0
client := &http.Client{}

for {
response, err := api.getCohortMembersRequest(client, cohortID, cohort)
if err != nil {
api.Logger.Printf("getCohortMembers(%s): request-status error %d - %v", cohortID, errors, err)
api.log.Error("getCohortMembers(%s): request-status error %d - %v", cohortID, errors, err)
errors++
if errors >= 3 || func(err error) bool {
switch err.(type) {
Expand All @@ -70,7 +67,7 @@ func (api *DirectCohortDownloadApi) GetCohort(cohortID string, cohort *Cohort) (
if err := json.NewDecoder(response.Body).Decode(&cohortInfo); err != nil {
return nil, err
}
api.Logger.Printf("getCohortMembers(%s): end - resultSize=%d", cohortID, cohortInfo.Size)
api.log.Debug("getCohortMembers(%s): end - resultSize=%d", cohortID, cohortInfo.Size)
return &Cohort{
ID: cohortInfo.Id,
LastModified: cohortInfo.LastModified,
Expand Down
31 changes: 14 additions & 17 deletions pkg/experiment/local/deployment_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package local

import (
"github.com/amplitude/experiment-go-server/internal/evaluation"
"log"
"github.com/amplitude/experiment-go-server/internal/logger"
"sync"
)

Expand All @@ -14,7 +14,7 @@ type DeploymentRunner struct {
cohortLoader *CohortLoader
lock sync.Mutex
poller *poller
logger *log.Logger
log *logger.Log
}

func NewDeploymentRunner(
Expand All @@ -30,10 +30,7 @@ func NewDeploymentRunner(
flagConfigStorage: flagConfigStorage,
cohortStorage: cohortStorage,
cohortLoader: cohortLoader,
logger: log.New(log.Writer(), "Amplitude: ", log.LstdFlags),
}
if config.Debug {
dr.logger.SetFlags(log.LstdFlags | log.Lshortfile)
log: logger.New(config.Debug),
}
dr.poller = newPoller()
return dr
Expand All @@ -44,13 +41,13 @@ func (dr *DeploymentRunner) Start() error {
defer dr.lock.Unlock()

if err := dr.refresh(); err != nil {
dr.logger.Printf("Initial refresh failed: %v", err)
dr.log.Error("Initial refresh failed: %v", err)
return err
}

dr.poller.Poll(dr.config.FlagConfigPollerInterval, func() {
if err := dr.periodicRefresh(); err != nil {
dr.logger.Printf("Periodic refresh failed: %v", err)
dr.log.Error("Periodic refresh failed: %v", err)
}
})
return nil
Expand All @@ -59,17 +56,17 @@ func (dr *DeploymentRunner) Start() error {
func (dr *DeploymentRunner) periodicRefresh() error {
defer func() {
if r := recover(); r != nil {
dr.logger.Printf("Recovered in periodicRefresh: %v", r)
dr.log.Error("Recovered in periodicRefresh: %v", r)
}
}()
return dr.refresh()
}

func (dr *DeploymentRunner) refresh() error {
dr.logger.Println("Refreshing flag configs.")
dr.log.Debug("Refreshing flag configs.")
flagConfigs, err := dr.flagConfigApi.GetFlagConfigs()
if err != nil {
dr.logger.Printf("Failed to fetch flag configs: %v", err)
dr.log.Error("Failed to fetch flag configs: %v", err)
return err
}

Expand All @@ -86,7 +83,7 @@ func (dr *DeploymentRunner) refresh() error {
for _, flagConfig := range flagConfigs {
cohortIDs := getAllCohortIDsFromFlag(flagConfig)
if dr.cohortLoader == nil || len(cohortIDs) == 0 {
dr.logger.Printf("Putting non-cohort flag %s", flagConfig.Key)
dr.log.Debug("Putting non-cohort flag %s", flagConfig.Key)
dr.flagConfigStorage.PutFlagConfig(flagConfig)
continue
}
Expand All @@ -95,17 +92,17 @@ func (dr *DeploymentRunner) refresh() error {

err := dr.loadCohorts(*flagConfig, cohortIDs)
if err != nil {
dr.logger.Printf("Failed to load all cohorts for flag %s. Using the old flag config.", flagConfig.Key)
dr.log.Error("Failed to load all cohorts for flag %s. Using the old flag config.", flagConfig.Key)
dr.flagConfigStorage.PutFlagConfig(oldFlagConfig)
return err
}

dr.flagConfigStorage.PutFlagConfig(flagConfig)
dr.logger.Printf("Stored flag config %s", flagConfig.Key)
dr.log.Debug("Stored flag config %s", flagConfig.Key)
}

dr.deleteUnusedCohorts()
dr.logger.Printf("Refreshed %d flag configs.", len(flagConfigs))
dr.log.Debug("Refreshed %d flag configs.", len(flagConfigs))
return nil
}

Expand All @@ -116,12 +113,12 @@ func (dr *DeploymentRunner) loadCohorts(flagConfig evaluation.Flag, cohortIDs ma
err := task.Wait()
if err != nil {
if _, ok := err.(*CohortNotModifiedException); !ok {
dr.logger.Printf("Failed to load cohort %s for flag %s: %v", cohortID, flagConfig.Key, err)
dr.log.Error("Failed to load cohort %s for flag %s: %v", cohortID, flagConfig.Key, err)
return err
}
continue
}
dr.logger.Printf("Cohort %s loaded for flag %s", cohortID, flagConfig.Key)
dr.log.Debug("Cohort %s loaded for flag %s", cohortID, flagConfig.Key)
}
return nil
}
Expand Down

0 comments on commit 8ba6b45

Please sign in to comment.