Skip to content

added userid and exposure event #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions localEvaluation/localEvaluation.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type variant struct {

type UserProperties struct {
OrgId string `json:"org_id,omitempty"`
UserId string `json:"user_id,omitempty"`
OrgName string `json:"org_name,omitempty"`
Username string `json:"username,omitempty"`
UserStatus string `json:"user_status,omitempty"`
Expand Down Expand Up @@ -82,7 +83,7 @@ func Initialize() {
}
}

func fetch(flagName string, user UserProperties) variant {
func fetch(flagName string, user UserProperties) map[string]experiment.Variant {
flagKeys := []string{flagName}
userProp := map[string]interface{}{
"org_id": user.OrgId,
Expand All @@ -97,25 +98,25 @@ func fetch(flagName string, user UserProperties) variant {
}

expUser := experiment.User{
UserId: user.UserId,
UserProperties: userProp,
}

variants, err := client.Evaluate(&expUser, flagKeys)
if err != nil {
return variant{}
return map[string]experiment.Variant{}
}

return variant(variants[flagName])
return variants
}

func GetFeatureFlagString(flagName string, user UserProperties) string {
data := fetch(flagName, user)
return data.Value
return data[flagName].Value
}

func GetFeatureFlagBool(flagName string, user UserProperties) bool {
data := fetch(flagName, user)
if val, err := strconv.ParseBool(data.Value); err == nil {
if val, err := strconv.ParseBool(data[flagName].Value); err == nil {
return val
}
return false
Expand All @@ -124,7 +125,7 @@ func GetFeatureFlagBool(flagName string, user UserProperties) bool {
func GetFeatureFlagPayload(flagName string, user UserProperties) map[string]interface{} {
data := fetch(flagName, user)
mapData := make(map[string]interface{})
mapData["value"] = data.Value
mapData["payload"] = data.Payload
mapData["value"] = data[flagName].Value
mapData["payload"] = data[flagName].Payload
return mapData
}
84 changes: 82 additions & 2 deletions pkg/experiment/local/client.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package local

import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/LambdaTest/lambda-featureflag-go-sdk/internal/evaluation"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"sync"

"github.com/LambdaTest/lambda-featureflag-go-sdk/internal/evaluation"

"github.com/LambdaTest/lambda-featureflag-go-sdk/pkg/experiment"

"github.com/LambdaTest/lambda-featureflag-go-sdk/internal/logger"
Expand All @@ -19,6 +21,8 @@ import (
var clients = map[string]*Client{}
var initMutex = sync.Mutex{}

const EXPOSURE_EVENT_TYPE = "$exposure"

type Client struct {
log *logger.Log
apiKey string
Expand All @@ -28,6 +32,20 @@ type Client struct {
flags *string
}

type Event struct {
EventType string `json:"event_type"`
UserId string `json:"user_id"`
EventProperties struct {
FlagKey string `json:"flag_key"`
Variant interface{} `json:"variant"`
} `json:"event_properties"`
}

type ExposurePayload struct {
ApiKey string `json:"api_key"`
Events []Event `json:"events"`
}

func Initialize(apiKey string, config *Config) *Client {
initMutex.Lock()
client := clients[apiKey]
Expand Down Expand Up @@ -82,6 +100,7 @@ func (c *Client) Evaluate(user *experiment.User, flagKeys []string) (map[string]

resultJson := evaluation.Evaluate(*c.flags, string(userJson))
c.log.Debug("evaluate result: %v\n", resultJson)
go c.exposure(resultJson, user.UserId)
var interopResult *interopResult
err = json.Unmarshal([]byte(resultJson), &interopResult)
if err != nil {
Expand Down Expand Up @@ -186,3 +205,64 @@ func contains(s []string, e string) bool {
}
return false
}

func (c *Client) exposure(resultJson string, userId string) {
parsePayload := map[string]interface{}{}
err := json.Unmarshal([]byte(resultJson), &parsePayload)
if err != nil {
c.log.Error("unable to parse string %s with error %s", resultJson, err.Error())
return
}
payload := ExposurePayload{}
payload.ApiKey = os.Getenv("ANALYTICS_API_KEY")
if result, ok := parsePayload["result"].(map[string]interface{}); ok {
for flagKey, flagValue := range result {
event := Event{}
event.EventType = EXPOSURE_EVENT_TYPE
event.UserId = userId
event.EventProperties.FlagKey = flagKey
if flagResult, ok := flagValue.(map[string]interface{}); ok {
if variant, ok := flagResult["variant"].(map[string]interface{}); ok {
event.EventProperties.Variant = variant
}
}
payload.Events = append(payload.Events, event)
}
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
c.log.Error("unable to marsal payload %+v with error %s", payload, err.Error())
return
}
c.log.Debug("exposure payload : %s", string(payloadBytes))

ctx, cancel := context.WithTimeout(context.Background(), c.config.FlagConfigPollerRequestTimeout)
defer cancel()
req, err := http.NewRequest(http.MethodPost, "https://api2.amplitude.com/2/httpapi", bytes.NewBuffer(payloadBytes))
if err != nil {
c.log.Error("unable to create request with error %s", err.Error())
return
}
req = req.WithContext(ctx)
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
var client = &http.Client{
Timeout: c.config.FlagConfigPollerRequestTimeout,
Transport: &http.Transport{
MaxIdleConns: 5,
MaxIdleConnsPerHost: 5,
DisableKeepAlives: true,
},
}
resp, err := client.Do(req)
if err != nil {
c.log.Error("error %s in making call to amplitude server", err.Error())
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
c.log.Error("error %s while reading response", err.Error())
return
}
c.log.Debug("exposure response: %s", string(body))
}
167 changes: 0 additions & 167 deletions pkg/experiment/remote/client.go

This file was deleted.

49 changes: 0 additions & 49 deletions pkg/experiment/remote/config.go

This file was deleted.

Loading