@@ -8,6 +8,8 @@ const sinon = require('sinon')
88const agent = require ( '../plugins/agent' )
99const NoopAIGuard = require ( '../../src/aiguard/noop' )
1010const AIGuard = require ( '../../src/aiguard/sdk' )
11+ const telemetryMetrics = require ( '../../src/telemetry/metrics' )
12+ const appsecNamespace = telemetryMetrics . manager . namespace ( 'appsec' )
1113
1214describe ( 'AIGuard SDK' , ( ) => {
1315 const config = {
@@ -29,6 +31,7 @@ describe('AIGuard SDK', () => {
2931 }
3032 let tracer
3133 let aiguard
34+ let count , inc
3235
3336 const toolCall = [
3437 { role : 'system' , content : 'You are a beautiful AI assistant' } ,
@@ -67,13 +70,20 @@ describe('AIGuard SDK', () => {
6770 originalFetch = global . fetch
6871 global . fetch = sinon . stub ( )
6972
73+ inc = sinon . spy ( )
74+ count = sinon . stub ( appsecNamespace , 'count' ) . returns ( {
75+ inc
76+ } )
77+ appsecNamespace . metrics . clear ( )
78+
7079 aiguard = new AIGuard ( tracer , config )
7180
7281 return agent . load ( null , [ ] )
7382 } )
7483
7584 afterEach ( ( ) => {
7685 global . fetch = originalFetch
86+ sinon . restore ( )
7787 agent . close ( )
7888 } )
7989
@@ -115,6 +125,10 @@ describe('AIGuard SDK', () => {
115125 } , { rejectFirst : true } )
116126 }
117127
128+ const assertTelemetry = ( metric , tags ) => {
129+ sinon . assert . calledWith ( count , metric , tags )
130+ }
131+
118132 const testSuite = [
119133 { action : 'ALLOW' , reason : 'Go ahead' } ,
120134 { action : 'DENY' , reason : 'Nope' } ,
@@ -144,6 +158,7 @@ describe('AIGuard SDK', () => {
144158 expect ( evaluation . reason ) . to . equal ( reason )
145159 }
146160
161+ assertTelemetry ( 'ai_guard.requests' , { error : false , action, block : blocking } )
147162 assertFetch ( messages )
148163 await assertAIGuardSpan ( {
149164 'ai_guard.target' : target ,
@@ -169,6 +184,7 @@ describe('AIGuard SDK', () => {
169184 err . name === 'AIGuardClientError' && JSON . stringify ( err . errors ) === JSON . stringify ( errors )
170185 )
171186
187+ assertTelemetry ( 'ai_guard.requests' , { error : true } )
172188 assertFetch ( toolCall )
173189 await assertAIGuardSpan ( {
174190 'ai_guard.target' : 'tool' ,
@@ -184,6 +200,7 @@ describe('AIGuard SDK', () => {
184200 err => err . name === 'AIGuardClientError'
185201 )
186202
203+ assertTelemetry ( 'ai_guard.requests' , { error : true } )
187204 assertFetch ( toolCall )
188205 await assertAIGuardSpan ( {
189206 'ai_guard.target' : 'tool' ,
@@ -199,6 +216,7 @@ describe('AIGuard SDK', () => {
199216 err => err . name === 'AIGuardClientError'
200217 )
201218
219+ assertTelemetry ( 'ai_guard.requests' , { error : true } )
202220 assertFetch ( toolCall )
203221 await assertAIGuardSpan ( {
204222 'ai_guard.target' : 'tool' ,
@@ -225,6 +243,7 @@ describe('AIGuard SDK', () => {
225243
226244 await aiguard . evaluate ( messages )
227245
246+ assertTelemetry ( 'ai_guard.truncated' , { type : 'messages' } )
228247 assertFetch ( messages )
229248 await assertAIGuardSpan (
230249 { 'ai_guard.target' : 'prompt' , 'ai_guard.action' : 'ALLOW' } ,
@@ -242,6 +261,7 @@ describe('AIGuard SDK', () => {
242261
243262 await aiguard . evaluate ( messages )
244263
264+ assertTelemetry ( 'ai_guard.truncated' , { type : 'content' } )
245265 assertFetch ( messages )
246266 await assertAIGuardSpan (
247267 { 'ai_guard.target' : 'prompt' , 'ai_guard.action' : 'ALLOW' } ,
0 commit comments