Skip to content
Closed
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
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,24 @@ require (
github.com/json-iterator/go v1.1.12
github.com/pkg/errors v0.9.1
github.com/pkg/profile v1.7.0
github.com/stretchr/testify v1.8.2
github.com/stretchr/testify v1.8.4
github.com/twmb/murmur3 v1.1.6
go.opentelemetry.io/otel v1.19.0
go.opentelemetry.io/otel/trace v1.19.0
golang.org/x/sync v0.1.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/fgprof v0.9.3 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
17 changes: 15 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/felixge/fgprof v0.9.3 h1:VvyZxILNuCiUCSXtPtYmmtGvb65nqXh2QFWc0Wpf2/g=
github.com/felixge/fgprof v0.9.3/go.mod h1:RdbpDgzqYVh/T9fPELJyV7EYJuHB55UTEULNun8eiPw=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd h1:1FjCyPC+syAzJ5/2S8fqdZK1R22vvA0J7JZKcuOIQ7Y=
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd/go.mod h1:KgnwoLYCZ8IQu3XUZ8Nc/bM9CCZFOyjUNOSygVozoDg=
Expand Down Expand Up @@ -37,10 +44,16 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/twmb/murmur3 v1.1.6 h1:mqrRot1BRxm+Yct+vavLMou2/iJt0tNVTTC0QoIjaZg=
github.com/twmb/murmur3 v1.1.6/go.mod h1:Qq/R7NUyOfr65zD+6Q5IHKsJLwP7exErjN6lyyq3OSQ=
go.opentelemetry.io/otel v1.19.0 h1:MuS/TNf4/j4IXsZuJegVzI1cwut7Qc00344rgH7p8bs=
go.opentelemetry.io/otel v1.19.0/go.mod h1:i0QyjOq3UPoTzff0PJB2N66fb4S0+rSbSB15/oyH9fY=
go.opentelemetry.io/otel/metric v1.19.0 h1:aTzpGtV0ar9wlV4Sna9sdJyII5jTVJEvKETPiOKwvpE=
go.opentelemetry.io/otel/metric v1.19.0/go.mod h1:L5rUsV9kM1IxCj1MmSdS+JQAcVm319EUrDVLrt7jqt8=
go.opentelemetry.io/otel/trace v1.19.0 h1:DFVQmlVbfVeOuBRrwdtaehRrWiL1JoVs9CPIQ1Dzxpg=
go.opentelemetry.io/otel/trace v1.19.0/go.mod h1:mfaSyvGyEJEI0nyV2I4qhNQnbBOUUmYZpYojqMnX2vo=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
11 changes: 9 additions & 2 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package client

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -37,6 +38,7 @@ import (
pkgOdpSegment "github.com/optimizely/go-sdk/pkg/odp/segment"
pkgOdpUtils "github.com/optimizely/go-sdk/pkg/odp/utils"
"github.com/optimizely/go-sdk/pkg/optimizelyjson"
"github.com/optimizely/go-sdk/pkg/tracing"
"github.com/optimizely/go-sdk/pkg/utils"

"github.com/hashicorp/go-multierror"
Expand All @@ -52,6 +54,7 @@ type OptimizelyClient struct {
execGroup *utils.ExecGroup
logger logging.OptimizelyLogProducer
defaultDecideOptions *decide.Options
tracer tracing.Tracer
}

// CreateUserContext creates a context of the user for which decision APIs will be called.
Expand Down Expand Up @@ -82,7 +85,6 @@ func (o *OptimizelyClient) decide(userContext OptimizelyUserContext, key string,
o.logger.Debug(string(debug.Stack()))
}
}()

decisionContext := decision.FeatureDecisionContext{
ForcedDecisionService: userContext.forcedDecisionService,
}
Expand Down Expand Up @@ -838,7 +840,7 @@ func (o *OptimizelyClient) GetVariation(experimentKey string, userContext entiti

// Track generates a conversion event with the given event key if it exists and queues it up to be sent to the Optimizely
// log endpoint for results processing.
func (o *OptimizelyClient) Track(eventKey string, userContext entities.UserContext, eventTags map[string]interface{}) (err error) {
func (o *OptimizelyClient) Track(ctx context.Context, eventKey string, userContext entities.UserContext, eventTags map[string]interface{}) (err error) {

defer func() {
if r := recover(); r != nil {
Expand All @@ -856,6 +858,11 @@ func (o *OptimizelyClient) Track(eventKey string, userContext entities.UserConte
}
}()

_, span := o.tracer.StartSpan(ctx, "trackHandler", "trackSDK")
defer span.End()

span.SetAttibutes("testTrackKey", "testTrackValue")

projectConfig, e := o.getProjectConfig()
if e != nil {
o.logger.Error("Optimizely SDK tracking error", e)
Expand Down
38 changes: 22 additions & 16 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/optimizely/go-sdk/pkg/odp"
"github.com/optimizely/go-sdk/pkg/odp/segment"
pkgOdpUtils "github.com/optimizely/go-sdk/pkg/odp/utils"
"github.com/optimizely/go-sdk/pkg/tracing"
"github.com/optimizely/go-sdk/pkg/utils"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -322,9 +323,10 @@ func TestTrack(t *testing.T) {
DecisionService: mockDecisionService,
EventProcessor: mockProcessor,
logger: logging.GetLogger("", ""),
tracer: &tracing.NoopTracer{},
}

err := client.Track("sample_conversion", entities.UserContext{ID: "1212121", Attributes: map[string]interface{}{}}, map[string]interface{}{})
err := client.Track(context.Background(), "sample_conversion", entities.UserContext{ID: "1212121", Attributes: map[string]interface{}{}}, map[string]interface{}{})

assert.NoError(t, err)
assert.True(t, len(mockProcessor.Events) == 1)
Expand All @@ -342,9 +344,10 @@ func TestTrackFailEventNotFound(t *testing.T) {
DecisionService: mockDecisionService,
EventProcessor: mockProcessor,
logger: logging.GetLogger("", ""),
tracer: &tracing.NoopTracer{},
}

err := client.Track("bob", entities.UserContext{ID: "1212121", Attributes: map[string]interface{}{}}, map[string]interface{}{})
err := client.Track(context.Background(), "bob", entities.UserContext{ID: "1212121", Attributes: map[string]interface{}{}}, map[string]interface{}{})

assert.NoError(t, err)
assert.True(t, len(mockProcessor.Events) == 0)
Expand All @@ -360,9 +363,10 @@ func TestTrackPanics(t *testing.T) {
DecisionService: mockDecisionService,
EventProcessor: mockProcessor,
logger: logging.GetLogger("", ""),
tracer: &tracing.NoopTracer{},
}

err := client.Track("bob", entities.UserContext{ID: "1212121", Attributes: map[string]interface{}{}}, map[string]interface{}{})
err := client.Track(context.Background(), "bob", entities.UserContext{ID: "1212121", Attributes: map[string]interface{}{}}, map[string]interface{}{})

assert.Error(t, err)
assert.True(t, len(mockProcessor.Events) == 0)
Expand Down Expand Up @@ -2730,6 +2734,7 @@ func (s *ClientTestSuiteTrackEvent) SetupTest() {
EventProcessor: s.mockProcessor,
notificationCenter: notification.NewNotificationCenter(),
logger: logging.GetLogger("", ""),
tracer: &tracing.NoopTracer{},
}
}

Expand All @@ -2750,7 +2755,7 @@ func (s *ClientTestSuiteTrackEvent) TestTrackWithNotification() {
s.Equal(1, id)
s.NoError(err)

err = s.client.Track("sample_conversion", expectedUserContext, map[string]interface{}{})
err = s.client.Track(context.Background(), "sample_conversion", expectedUserContext, map[string]interface{}{})
s.NoError(err)
s.True(isTrackCalled)
s.Equal(1, len(s.mockProcessor.Events))
Expand All @@ -2776,7 +2781,7 @@ func (s *ClientTestSuiteTrackEvent) TestTrackWithNotificationAndEventTag() {
}

s.client.OnTrack(onTrack)
err := s.client.Track("sample_conversion", expectedUserContext, expectedEvenTags)
err := s.client.Track(context.Background(), "sample_conversion", expectedUserContext, expectedEvenTags)

s.NoError(err)
s.True(isTrackCalled)
Expand Down Expand Up @@ -2804,7 +2809,7 @@ func (s *ClientTestSuiteTrackEvent) TestTrackWithNotificationAndUserEvent() {
}

s.client.OnTrack(onTrack)
err := s.client.Track("sample_conversion", expectedUserContext, expectedEventTags)
err := s.client.Track(context.Background(), "sample_conversion", expectedUserContext, expectedEventTags)

s.NoError(err)
s.True(isTrackCalled)
Expand All @@ -2822,7 +2827,7 @@ func (s *ClientTestSuiteTrackEvent) TestTrackNotificationNotCalledWhenEventProce
}

s.client.OnTrack(onTrack)
err := s.client.Track("sample_conversion", entities.UserContext{ID: "1212121", Attributes: map[string]interface{}{}}, map[string]interface{}{})
err := s.client.Track(context.Background(), "sample_conversion", entities.UserContext{ID: "1212121", Attributes: map[string]interface{}{}}, map[string]interface{}{})
s.NoError(err)
s.Equal(0, len(s.mockProcessor.Events))
s.False(isTrackCalled)
Expand All @@ -2840,7 +2845,7 @@ func (s *ClientTestSuiteTrackEvent) TestTrackNotificationNotCalledWhenNoNotifica
}
s.client.notificationCenter = nil
s.client.OnTrack(onTrack)
err := s.client.Track("sample_conversion", userContext, map[string]interface{}{})
err := s.client.Track(context.Background(), "sample_conversion", userContext, map[string]interface{}{})

s.NoError(err)
s.False(isTrackCalled)
Expand All @@ -2854,7 +2859,7 @@ func (s *ClientTestSuiteTrackEvent) TestTrackNotificationNotCalledWhenInvalidEve
}

s.client.OnTrack(onTrack)
err := s.client.Track("bob", entities.UserContext{ID: "1212121", Attributes: map[string]interface{}{}}, map[string]interface{}{})
err := s.client.Track(context.Background(), "bob", entities.UserContext{ID: "1212121", Attributes: map[string]interface{}{}}, map[string]interface{}{})

s.NoError(err)
s.Equal(0, len(s.mockProcessor.Events))
Expand Down Expand Up @@ -2883,7 +2888,7 @@ func (s *ClientTestSuiteTrackEvent) TestTrackNotificationNotCalledWhenSendThrows
mockNotificationCenter.On("AddHandler", notification.Track, mock.AnythingOfType("func(interface {})")).Return(1, nil)
s.client.notificationCenter = mockNotificationCenter
s.client.OnTrack(onTrack)
err = s.client.Track("sample_conversion", expectedUserContext, map[string]interface{}{})
err = s.client.Track(context.Background(), "sample_conversion", expectedUserContext, map[string]interface{}{})

s.NoError(err)
s.Equal(1, len(s.mockProcessor.Events))
Expand All @@ -2909,6 +2914,7 @@ func (s *ClientTestSuiteTrackNotification) SetupTest() {
EventProcessor: s.mockProcessor,
notificationCenter: notification.NewNotificationCenter(),
logger: logging.GetLogger("", ""),
tracer: &tracing.NoopTracer{},
}
}

Expand All @@ -2931,7 +2937,7 @@ func (s *ClientTestSuiteTrackNotification) TestMultipleOnTrack() {

// Add 5 on track callbacks
addOnTrack(5)
err := s.client.Track("sample_conversion", userContext, map[string]interface{}{})
err := s.client.Track(context.Background(), "sample_conversion", userContext, map[string]interface{}{})
s.NoError(err)
s.Equal(5, numberOfCalls)
}
Expand All @@ -2954,7 +2960,7 @@ func (s *ClientTestSuiteTrackNotification) TestMultipleRemoveOnTrack() {
s.NoError(err)
}

err := s.client.Track("sample_conversion", userContext, map[string]interface{}{})
err := s.client.Track(context.Background(), "sample_conversion", userContext, map[string]interface{}{})
s.NoError(err)
s.Equal(5, numberOfCalls)

Expand All @@ -2965,7 +2971,7 @@ func (s *ClientTestSuiteTrackNotification) TestMultipleRemoveOnTrack() {
s.NoError(err)
}

err = s.client.Track("sample_conversion", userContext, map[string]interface{}{})
err = s.client.Track(context.Background(), "sample_conversion", userContext, map[string]interface{}{})
s.NoError(err)
s.Equal(0, numberOfCalls)
}
Expand All @@ -2989,7 +2995,7 @@ func (s *ClientTestSuiteTrackNotification) TestOnTrackAfterRemoveOnTrack() {

// Add 5 on track callbacks
addOnTrack(5)
err := s.client.Track("sample_conversion", userContext, map[string]interface{}{})
err := s.client.Track(context.Background(), "sample_conversion", userContext, map[string]interface{}{})
s.NoError(err)
s.Equal(5, numberOfCalls)

Expand All @@ -2999,13 +3005,13 @@ func (s *ClientTestSuiteTrackNotification) TestOnTrackAfterRemoveOnTrack() {
err = s.client.RemoveOnTrack(callbackIds[i])
s.NoError(err)
}
err = s.client.Track("sample_conversion", userContext, map[string]interface{}{})
err = s.client.Track(context.Background(), "sample_conversion", userContext, map[string]interface{}{})
s.NoError(err)
s.Equal(0, numberOfCalls)

// Add 2 on track callbacks
addOnTrack(2)
err = s.client.Track("sample_conversion", userContext, map[string]interface{}{})
err = s.client.Track(context.Background(), "sample_conversion", userContext, map[string]interface{}{})
s.NoError(err)
s.Equal(2, numberOfCalls)
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/client/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/optimizely/go-sdk/pkg/odp"
pkgUtils "github.com/optimizely/go-sdk/pkg/odp/utils"
"github.com/optimizely/go-sdk/pkg/registry"
"github.com/optimizely/go-sdk/pkg/tracing"
"github.com/optimizely/go-sdk/pkg/utils"
)

Expand All @@ -48,6 +49,7 @@ type OptimizelyFactory struct {
eventDispatcher event.Dispatcher
eventProcessor event.Processor
metricsRegistry metrics.Registry
tracer tracing.Tracer
overrideStore decision.ExperimentOverrideStore
userProfileService decision.UserProfileService
notificationCenter notification.Center
Expand Down Expand Up @@ -113,6 +115,12 @@ func (f *OptimizelyFactory) Client(clientOptions ...OptionFunc) (*OptimizelyClie
appClient.notificationCenter = registry.GetNotificationCenter(f.SDKKey)
}

if f.tracer != nil {
appClient.tracer = f.tracer
} else {
appClient.tracer = &tracing.NoopTracer{}
}

if f.configManager != nil {
appClient.ConfigManager = f.configManager
} else {
Expand Down Expand Up @@ -300,6 +308,12 @@ func WithNotificationCenter(nc notification.Center) OptionFunc {
}
}

func WithTracer(tracer tracing.Tracer) OptionFunc {
return func(f *OptimizelyFactory) {
f.tracer = tracer
}
}

// StaticClient returns a client initialized with a static project config.
func (f *OptimizelyFactory) StaticClient() (optlyClient *OptimizelyClient, err error) {

Expand Down
3 changes: 2 additions & 1 deletion pkg/client/optimizely_user_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package client

import (
"context"
"errors"
"sync"

Expand Down Expand Up @@ -154,7 +155,7 @@ func (o *OptimizelyUserContext) TrackEvent(eventKey string, eventTags map[string
ID: o.GetUserID(),
Attributes: o.GetUserAttributes(),
}
return o.optimizely.Track(eventKey, userContext, eventTags)
return o.optimizely.Track(context.Background(), eventKey, userContext, eventTags)
}

// SetForcedDecision sets the forced decision (variation key) for a given decision context (flag key and optional rule key).
Expand Down
Loading