Skip to content

fix: add usage tracking to config method #307

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: v7
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
2 changes: 2 additions & 0 deletions ldai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func (c *Client) Config(
defaultValue Config,
variables map[string]interface{},
) (Config, *Tracker) {
_ = c.sdk.TrackMetric("$ld:ai:config:function:single", context, 1, ldvalue.String(key))

result, _ := c.sdk.JSONVariation(key, context, defaultValue.AsLdValue())

// The spec requires the config to at least be an object (although all properties are optional, so it may be an
Expand Down
49 changes: 45 additions & 4 deletions ldai/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@ import (
)

type mockServerSDK struct {
log *ldlogtest.MockLog
json []byte
err error
log *ldlogtest.MockLog
json []byte
err error
events []mockEvent
}

type mockEvent struct {
eventName string
context ldcontext.Context
metricValue float64
data ldvalue.Value
}

func newMockSDK(json []byte, err error) *mockServerSDK {
return &mockServerSDK{json: json, err: err, log: ldlogtest.NewMockLog()}
return &mockServerSDK{json: json, err: err, log: ldlogtest.NewMockLog(), events: []mockEvent{}}
}

func (m *mockServerSDK) JSONVariation(
Expand All @@ -44,6 +52,12 @@ func (m *mockServerSDK) Loggers() interfaces.LDLoggers {
}

func (m *mockServerSDK) TrackMetric(eventName string, context ldcontext.Context, metricValue float64, data ldvalue.Value) error {
m.events = append(m.events, mockEvent{
eventName: eventName,
context: context,
metricValue: metricValue,
data: data,
})
return nil
}

Expand Down Expand Up @@ -288,6 +302,33 @@ func TestCanSetDefaultConfigFields(t *testing.T) {
assert.Equal(t, datamodel.System, msg[1].Role)
}

func TestConfigMethodTracking(t *testing.T) {
mockSDK := newMockSDK(nil, nil)
client, err := NewClient(mockSDK)
require.NoError(t, err)
require.NotNil(t, client)

defaultConfig := NewConfig().WithEnabled(false).Build()
context := ldcontext.New("user-key")
configKey := "test-config-key"

config, tracker := client.Config(configKey, context, defaultConfig, nil)

require.NotNil(t, config)
require.NotNil(t, tracker)

expectedEvents := []mockEvent{
{
eventName: "$ld:ai:config:function:single",
context: context,
metricValue: 1,
data: ldvalue.String(configKey),
},
}

assert.ElementsMatch(t, expectedEvents, mockSDK.events)
}

func TestCanSetModelParameters(t *testing.T) {
client, err := NewClient(newMockSDK(nil, nil))
require.NoError(t, err)
Expand Down