Skip to content

Commit 0f65ef8

Browse files
ctawiahcursoragent
andcommitted
fix: report agent-configs usage count excluding null entries (AIC-2663)
The $ld:ai:usage:agent-configs metric was reported with the full request list size before the loop, while null entries are skipped during evaluation. This could make the reported usage value/count exceed the number of configs actually retrieved. Count only the non-null requests that are evaluated and report the metric after the loop. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 7f5ad89 commit 0f65ef8

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

lib/sdk/server-ai/src/main/java/com/launchdarkly/sdk/server/ai/LDAIClientImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,21 @@ public AIAgentConfig agentConfig(
111111
@Override
112112
public Map<String, AIAgentConfig> agentConfigs(
113113
List<AIAgentConfigRequest> agentConfigs, LDContext context) {
114-
int count = agentConfigs == null ? 0 : agentConfigs.size();
115-
client.trackMetric(TRACK_USAGE_AGENT_CONFIGS, context, LDValue.of(count), count);
116-
117114
Map<String, AIAgentConfig> result = new LinkedHashMap<>();
115+
int count = 0;
118116
if (agentConfigs != null) {
119117
for (AIAgentConfigRequest request : agentConfigs) {
120118
if (request == null) {
121119
continue;
122120
}
121+
count++;
123122
result.put(
124123
request.getKey(),
125124
evaluateAgent(request.getKey(), context, request.getDefaultValue(), request.getVariables()));
126125
}
127126
}
127+
client.trackMetric(TRACK_USAGE_AGENT_CONFIGS, context, LDValue.of(count), count);
128+
128129
return result;
129130
}
130131

lib/sdk/server-ai/src/test/java/com/launchdarkly/sdk/server/ai/LDAIClientImplTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.contains;
5+
import static org.hamcrest.Matchers.containsInAnyOrder;
56
import static org.hamcrest.Matchers.containsString;
67
import static org.hamcrest.Matchers.empty;
78
import static org.hamcrest.Matchers.hasSize;
@@ -275,6 +276,19 @@ public void agentConfigsHandlesEmptyList() {
275276
verify(client).trackMetric(eq("$ld:ai:usage:agent-configs"), eq(context), eq(LDValue.of(0)), eq(0.0));
276277
}
277278

279+
@Test
280+
public void agentConfigsUsageCountExcludesNullEntries() {
281+
when(client.jsonValueVariation(anyString(), any(), any())).thenReturn(LDValue.ofNull());
282+
List<AIAgentConfigRequest> requests = Arrays.asList(
283+
AIAgentConfigRequest.builder("a").build(),
284+
null,
285+
AIAgentConfigRequest.builder("b").build());
286+
Map<String, AIAgentConfig> agents = ai.agentConfigs(requests, context);
287+
288+
assertThat(agents.keySet(), containsInAnyOrder("a", "b"));
289+
verify(client).trackMetric(eq("$ld:ai:usage:agent-configs"), eq(context), eq(LDValue.of(2)), eq(2.0));
290+
}
291+
278292
private static Map<String, Object> variables() {
279293
return new HashMap<>();
280294
}

0 commit comments

Comments
 (0)