Skip to content

fix: add usage tracking to config method #151

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 1 commit 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
1 change: 1 addition & 0 deletions pkgs/sdk/server-ai/src/LdAiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public LdAiClient(ILaunchDarklyClient client)
public ILdAiConfigTracker Config(string key, Context context, LdAiConfig defaultValue,
IReadOnlyDictionary<string, object> variables = null)
{
_client.Track("$ld:ai:config:function:single", context, LdValue.Of(key), 1);

var result = _client.JsonVariation(key, context, defaultValue.ToLdValue());

Expand Down
47 changes: 47 additions & 0 deletions pkgs/sdk/server-ai/test/LdAiClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,53 @@ public void ReturnsDefaultConfigWhenGivenInvalidVariation()
Assert.Equal(defaultConfig, tracker.Config);
}

[Fact]
public void ConfigMethodCallsTrackWithCorrectParameters()
{
var mockClient = new Mock<ILaunchDarklyClient>();
var context = Context.New(ContextKind.Default, "user-key");
var configKey = "test-config-key";

mockClient.Setup(c => c.JsonVariation(
It.IsAny<string>(),
It.IsAny<Context>(),
It.IsAny<LdValue>()))
.Returns(LdValue.ObjectFrom(new Dictionary<string, LdValue>
{
["_ldMeta"] = LdValue.ObjectFrom(new Dictionary<string, LdValue>
{
["enabled"] = LdValue.Of(true),
["variationKey"] = LdValue.Of("test-variation"),
["version"] = LdValue.Of(1)
}),
["model"] = LdValue.ObjectFrom(new Dictionary<string, LdValue>
{
["name"] = LdValue.Of("test-model")
}),
["provider"] = LdValue.ObjectFrom(new Dictionary<string, LdValue>
{
["name"] = LdValue.Of("test-provider")
}),
["messages"] = LdValue.ArrayOf()
}));

var mockLogger = new Mock<ILogger>();
mockClient.Setup(x => x.GetLogger()).Returns(mockLogger.Object);

var client = new LdAiClient(mockClient.Object);
var defaultConfig = LdAiConfig.New().Build();

var tracker = client.Config(configKey, context, defaultConfig);

mockClient.Verify(c => c.Track(
"$ld:ai:config:function:single",
context,
LdValue.Of(configKey),
1), Times.Once);

Assert.NotNull(tracker);
}

private const string MetaDisabledExplicitly = """
{
"_ldMeta": {"variationKey": "1", "enabled": false},
Expand Down
Loading