diff --git a/client.go b/client.go index ccff4602..ee17cfbc 100644 --- a/client.go +++ b/client.go @@ -3,6 +3,7 @@ package devcycle import ( "bytes" "context" + "encoding/json" "errors" "fmt" "io" @@ -17,6 +18,8 @@ import ( "strings" "time" + "github.com/google/uuid" + "github.com/devcyclehq/go-server-sdk/v2/util" "github.com/devcyclehq/go-server-sdk/v2/api" @@ -55,6 +58,7 @@ type Client struct { // Set to true when the client has been initialized, regardless of whether the config has loaded successfully. isInitialized bool internalOnInitializedChannel chan bool + clientUUID string } type LocalBucketing interface { @@ -151,6 +155,7 @@ func (c *Client) IsLocalBucketing() bool { func (c *Client) handleInitialization() { c.isInitialized = true + c.clientUUID = uuid.NewString() if c.DevCycleOptions.OnInitializedChannel != nil { go func() { c.DevCycleOptions.OnInitializedChannel <- true @@ -173,8 +178,13 @@ func (c *Client) GetRawConfig() (config []byte, etag string, err error) { if c.configManager == nil { return nil, "", errors.New("cannot read raw config; config manager is nil") } + configMap := map[string]interface{}{} + err = json.Unmarshal(c.configManager.rawConfig, &configMap) + if err != nil { + return nil, "", err + } if c.configManager.HasConfig() { - return c.configManager.rawConfig, c.configManager.configETag, nil + return c.configManager.rawConfig, configMap["eTag"].(string), nil } return nil, "", errors.New("cannot read raw config; config manager has no config") } @@ -477,8 +487,23 @@ func (c *Client) Track(user User, event Event) (bool, error) { return false, errors.New("event type is required") } + if(event.MetaData == nil) { + event.MetaData = make(map[string]interface{}) + } + event.MetaData["ClientId"] = c.clientUUID + event.ClientDate = time.Now() + if c.IsLocalBucketing() { if c.hasConfig() { + fmt.Println("configManager.rawconfig, ",string( c.configManager.rawConfig)) + var configMap map[string]interface{} + marshalErr := json.Unmarshal(c.configManager.rawConfig, &configMap) + if marshalErr != nil { + util.Errorf("Error marshaling config: %v", marshalErr) + return false, marshalErr + } + fmt.Println("etag: ", configMap["eTag"]) + event.MetaData["configEtag"] = configMap["eTag"] err := c.eventQueue.QueueEvent(user, event) if err != nil { util.Errorf("Error queuing event: %v", err) @@ -510,6 +535,7 @@ func (c *Client) Track(user User, event Event) (bool, error) { r, rBody, err := c.performRequest(path, httpMethod, postBody, headers, queryParams) if err != nil { + fmt.Println("error in track: ", err) return false, err } @@ -535,6 +561,8 @@ func (c *Client) FlushEvents() error { return nil } + fmt.Println("sending events") + err := c.eventQueue.FlushEvents() if err != nil { util.Errorf("Error flushing events: %v", err) diff --git a/configmanager.go b/configmanager.go index ffe902d8..76f0416f 100644 --- a/configmanager.go +++ b/configmanager.go @@ -137,16 +137,29 @@ func (e *EnvironmentConfigManager) fetchConfig(numRetriesRemaining int) (err err func (e *EnvironmentConfigManager) setConfigFromResponse(response *http.Response) error { config, err := io.ReadAll(response.Body) + + if err != nil { + return err + } + + configMap := make(map[string]interface{}) + err = json.Unmarshal(config, &configMap) + if err != nil { + return err + } + + configMap["eTag"] = response.Header.Get("Etag") + configWithTag, err := json.Marshal(configMap) if err != nil { return err } // Check - valid := json.Valid(config) + valid := json.Valid(configWithTag) if !valid { return fmt.Errorf("invalid JSON data received for config") } - err = e.setConfig(config, e.configETag) + err = e.setConfig(configWithTag, response.Header.Get("Etag")) if err != nil { return err diff --git a/event_manager.go b/event_manager.go index 17095b15..4a9a05bc 100644 --- a/event_manager.go +++ b/event_manager.go @@ -103,6 +103,7 @@ func (e *EventManager) QueueEvent(user User, event Event) error { default: } } + fmt.Println("QueueEvent") err = e.internalQueue.QueueEvent(user, event) if err != nil && errors.Is(err, ErrQueueFull) { return fmt.Errorf("event queue is full, dropping event: %+v", event) @@ -122,6 +123,7 @@ func (e *EventManager) FlushEvents() (err error) { defer e.flushMutex.Unlock() util.Debugf("Started flushing events") + fmt.Println("FlushEvents") defer func() { if r := recover(); r != nil { diff --git a/example/cloud/main.go b/example/cloud/main.go index 92b0d6fb..d5b3842d 100644 --- a/example/cloud/main.go +++ b/example/cloud/main.go @@ -17,7 +17,7 @@ func main() { if variableKey == "" { log.Fatal("DEVCYCLE_VARIABLE_KEY env var not set: set it to a variable key") } - user := devcycle.User{UserId: "test"} + user := devcycle.User{UserId: "suthar-test-user"} dvcOptions := devcycle.Options{ EnableEdgeDB: false, EnableCloudBucketing: true, diff --git a/example/local/main.go b/example/local/main.go index 4af111cb..83b541f9 100644 --- a/example/local/main.go +++ b/example/local/main.go @@ -19,7 +19,7 @@ func main() { log.Fatal("DEVCYCLE_VARIABLE_KEY env var not set: set it to a variable key") } - user := devcycle.User{UserId: "test"} + user := devcycle.User{UserId: "suthar-test-user"} dvcOptions := devcycle.Options{ EnableEdgeDB: false, EnableCloudBucketing: false, @@ -32,7 +32,9 @@ func main() { client, err := devcycle.NewClient(sdkKey, &dvcOptions) time.Sleep(10 * time.Second) - fmt.Println("Error? ", err) + if(err != nil) { + log.Fatalf("Error initializing client: %v", err) + } fmt.Println(client.GetRawConfig()) log.Printf("client initialized") @@ -78,4 +80,7 @@ func main() { if err != nil { log.Fatalf("Error tracking event: %v", err) } + + time.Sleep(10 * time.Second) + }