Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

trace-integration.test.ts 7.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
  1. import { evaluate } from '../../src/evaluator';
  2. import { getTraceStore } from '../../src/tracing/store';
  3. import type Eval from '../../src/models/eval';
  4. import type { EvaluateOptions, TestSuite } from '../../src/types';
  5. // Mock dependencies
  6. jest.mock('../../src/tracing/store');
  7. jest.mock('../../src/tracing/otlpReceiver', () => ({
  8. startOTLPReceiver: jest.fn(),
  9. stopOTLPReceiver: jest.fn(),
  10. }));
  11. // Mock evaluatorTracing module
  12. jest.mock('../../src/tracing/evaluatorTracing', () => ({
  13. generateTraceId: jest.fn(() => 'abcdef1234567890abcdef1234567890'),
  14. generateSpanId: jest.fn(() => '0123456789abcdef'),
  15. generateTraceparent: jest.fn((traceId, spanId) => `00-${traceId}-${spanId}-01`),
  16. generateTraceContextIfNeeded: jest.fn(),
  17. startOtlpReceiverIfNeeded: jest.fn(),
  18. stopOtlpReceiverIfNeeded: jest.fn(),
  19. isOtlpReceiverStarted: jest.fn(() => false),
  20. isTracingEnabled: jest.fn((test) => test.metadata?.tracingEnabled === true),
  21. }));
  22. describe('evaluator trace integration', () => {
  23. const mockTraceStore = {
  24. createTrace: jest.fn(),
  25. getTrace: jest.fn(),
  26. };
  27. const mockEval = {
  28. id: 'test-eval-id',
  29. addResult: jest.fn(),
  30. addPrompts: jest.fn(),
  31. fetchResultsByTestIdx: jest.fn(),
  32. setVars: jest.fn(),
  33. results: [],
  34. prompts: [],
  35. persisted: false,
  36. config: {
  37. outputPath: undefined,
  38. },
  39. } as unknown as Eval;
  40. beforeEach(() => {
  41. jest.clearAllMocks();
  42. (getTraceStore as jest.Mock).mockReturnValue(mockTraceStore);
  43. });
  44. it('should pass traceId through to assertions when tracing is enabled', async () => {
  45. // Mock trace creation and retrieval
  46. const testTraceId = 'abcdef1234567890abcdef1234567890';
  47. mockTraceStore.createTrace.mockResolvedValue(undefined);
  48. mockTraceStore.getTrace.mockResolvedValue({
  49. traceId: testTraceId,
  50. spans: [
  51. {
  52. spanId: 'test-span',
  53. name: 'test.operation',
  54. startTime: 1000,
  55. endTime: 2000,
  56. },
  57. ],
  58. });
  59. // Mock generateTraceContextIfNeeded
  60. const { generateTraceContextIfNeeded } = require('../../src/tracing/evaluatorTracing');
  61. generateTraceContextIfNeeded.mockResolvedValue({
  62. traceparent: `00-${testTraceId}-0123456789abcdef-01`,
  63. evaluationId: 'test-eval-id',
  64. testCaseId: 'test-case-id',
  65. });
  66. const testSuite: TestSuite = {
  67. providers: [
  68. {
  69. id: () => 'mock-provider',
  70. callApi: jest.fn().mockResolvedValue({
  71. output: 'Test response',
  72. tokenUsage: {},
  73. }),
  74. },
  75. ],
  76. prompts: [{ raw: 'Test prompt', label: 'test' }],
  77. tests: [
  78. {
  79. vars: { input: 'test' },
  80. metadata: {
  81. tracingEnabled: true,
  82. evaluationId: 'test-eval-id',
  83. },
  84. assert: [
  85. {
  86. type: 'javascript',
  87. value: `
  88. // Verify trace data is available
  89. if (!context.trace) return false;
  90. return context.trace.spans.length > 0 &&
  91. context.trace.spans[0].name === 'test.operation';
  92. `,
  93. },
  94. ],
  95. },
  96. ],
  97. tracing: {
  98. enabled: true,
  99. otlp: {
  100. http: {
  101. enabled: true,
  102. port: 4318,
  103. host: '0.0.0.0',
  104. acceptFormats: ['application/x-protobuf'],
  105. },
  106. },
  107. },
  108. };
  109. const options: EvaluateOptions = {
  110. maxConcurrency: 1,
  111. };
  112. // Run evaluation
  113. await evaluate(testSuite, mockEval, options);
  114. // Verify trace context was generated
  115. expect(generateTraceContextIfNeeded).toHaveBeenCalled();
  116. // Verify trace was fetched for assertion
  117. expect(mockTraceStore.getTrace).toHaveBeenCalledWith(testTraceId);
  118. // Verify result was added with passing assertion
  119. expect(mockEval.addResult).toHaveBeenCalledWith(
  120. expect.objectContaining({
  121. success: true,
  122. score: 1,
  123. }),
  124. );
  125. });
  126. it('should handle assertions gracefully when tracing is disabled', async () => {
  127. // Mock generateTraceContextIfNeeded to return null when tracing is disabled
  128. const { generateTraceContextIfNeeded } = require('../../src/tracing/evaluatorTracing');
  129. generateTraceContextIfNeeded.mockResolvedValue(null);
  130. const testSuite: TestSuite = {
  131. providers: [
  132. {
  133. id: () => 'mock-provider',
  134. callApi: jest.fn().mockResolvedValue({
  135. output: 'Test response',
  136. tokenUsage: {},
  137. }),
  138. },
  139. ],
  140. prompts: [{ raw: 'Test prompt', label: 'test' }],
  141. tests: [
  142. {
  143. vars: { input: 'test' },
  144. // No tracingEnabled in metadata
  145. assert: [
  146. {
  147. type: 'javascript',
  148. value: `
  149. // Should pass when trace is undefined
  150. return context.trace === undefined && output === 'Test response';
  151. `,
  152. },
  153. ],
  154. },
  155. ],
  156. // Tracing not enabled in test suite
  157. };
  158. const options: EvaluateOptions = {
  159. maxConcurrency: 1,
  160. };
  161. // Run evaluation
  162. await evaluate(testSuite, mockEval, options);
  163. // Verify trace was NOT created or fetched
  164. expect(mockTraceStore.createTrace).not.toHaveBeenCalled();
  165. expect(mockTraceStore.getTrace).not.toHaveBeenCalled();
  166. // Verify result was added with passing assertion
  167. expect(mockEval.addResult).toHaveBeenCalledWith(
  168. expect.objectContaining({
  169. success: true,
  170. score: 1,
  171. }),
  172. );
  173. });
  174. it('should extract traceId correctly from traceparent header', async () => {
  175. const testTraceId = '0af7651916cd43dd8448eb211c80319c';
  176. const testSpanId = 'b7ad6b7169203331';
  177. // Mock the trace context generation
  178. const { generateTraceContextIfNeeded } = require('../../src/tracing/evaluatorTracing');
  179. generateTraceContextIfNeeded.mockResolvedValue({
  180. traceparent: `00-${testTraceId}-${testSpanId}-01`,
  181. evaluationId: 'test-eval-id',
  182. testCaseId: 'test-case-id',
  183. });
  184. mockTraceStore.createTrace.mockResolvedValue(undefined);
  185. mockTraceStore.getTrace.mockResolvedValue({
  186. traceId: testTraceId,
  187. spans: [
  188. {
  189. spanId: 'test-span',
  190. name: 'extracted.correctly',
  191. startTime: 1000,
  192. endTime: 2000,
  193. },
  194. ],
  195. });
  196. const testSuite: TestSuite = {
  197. providers: [
  198. {
  199. id: () => 'mock-provider',
  200. callApi: jest.fn().mockResolvedValue({
  201. output: 'Test response',
  202. tokenUsage: {},
  203. }),
  204. },
  205. ],
  206. prompts: [{ raw: 'Test prompt', label: 'test' }],
  207. tests: [
  208. {
  209. vars: { input: 'test' },
  210. metadata: {
  211. tracingEnabled: true,
  212. evaluationId: 'test-eval-id',
  213. },
  214. assert: [
  215. {
  216. type: 'javascript',
  217. value: `
  218. // Verify the extracted traceId matches
  219. return context.trace && context.trace.traceId === '${testTraceId}';
  220. `,
  221. },
  222. ],
  223. },
  224. ],
  225. };
  226. const options: EvaluateOptions = {
  227. maxConcurrency: 1,
  228. };
  229. // Run evaluation
  230. await evaluate(testSuite, mockEval, options);
  231. // Verify trace was fetched with the correct traceId
  232. expect(mockTraceStore.getTrace).toHaveBeenCalledWith(testTraceId);
  233. // Verify result was added with passing assertion
  234. expect(mockEval.addResult).toHaveBeenCalledWith(
  235. expect.objectContaining({
  236. success: true,
  237. }),
  238. );
  239. });
  240. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...