@arizeai/openinference-instrumentation-beeai 1.1.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/middleware.ts CHANGED
@@ -207,99 +207,103 @@ export function createTelemetryMiddleware(
207
207
  );
208
208
  }
209
209
 
210
- /**
211
- * create groupId span level (id does not exist)
212
- * I use only the top-level groups like iterations other nested groups like tokens would introduce unuseful complexity
213
- */
214
- if (
215
- meta.groupId &&
216
- !meta.trace.parentRunId &&
217
- !groupIterations.includes(meta.groupId)
218
- ) {
219
- spansMap.set(
220
- meta.groupId,
221
- createSpan({
222
- id: meta.groupId,
223
- name: meta.groupId,
224
- target: "groupId",
225
- data: {
226
- [SemanticConventions.OPENINFERENCE_SPAN_KIND]: "Chain",
227
- },
228
- startedAt: convertDateToPerformance(meta.createdAt),
229
- }),
230
- );
231
- groupIterations.push(meta.groupId);
232
- }
210
+ try {
211
+ /**
212
+ * create groupId span level (id does not exist)
213
+ * I use only the top-level groups like iterations other nested groups like tokens would introduce unuseful complexity
214
+ */
215
+ if (
216
+ meta.groupId &&
217
+ !meta.trace.parentRunId &&
218
+ !groupIterations.includes(meta.groupId)
219
+ ) {
220
+ spansMap.set(
221
+ meta.groupId,
222
+ createSpan({
223
+ id: meta.groupId,
224
+ name: meta.groupId,
225
+ target: "groupId",
226
+ data: {
227
+ [SemanticConventions.OPENINFERENCE_SPAN_KIND]: "Chain",
228
+ },
229
+ startedAt: convertDateToPerformance(meta.createdAt),
230
+ }),
231
+ );
232
+ groupIterations.push(meta.groupId);
233
+ }
233
234
 
234
- const { spanId, parentSpanId } = idNameManager.getIds({
235
- path: meta.path,
236
- id: meta.id,
237
- runId: meta.trace.runId,
238
- parentRunId: meta.trace.parentRunId,
239
- groupId: meta.groupId,
240
- });
235
+ const { spanId, parentSpanId } = idNameManager.getIds({
236
+ path: meta.path,
237
+ id: meta.id,
238
+ runId: meta.trace.runId,
239
+ parentRunId: meta.trace.parentRunId,
240
+ groupId: meta.groupId,
241
+ });
241
242
 
242
- const serializedData = getSerializedObjectSafe(data, meta);
243
+ const serializedData = getSerializedObjectSafe(data, meta);
243
244
 
244
- // skip partialUpdate events with no data
245
- if (meta.name === partialUpdateEventName && isEmpty(serializedData)) {
246
- return;
247
- }
245
+ // skip partialUpdate events with no data
246
+ if (meta.name === partialUpdateEventName && isEmpty(serializedData)) {
247
+ return;
248
+ }
248
249
 
249
- const span = createSpan({
250
- id: spanId,
251
- name: meta.name,
252
- target: meta.path,
253
- ...(parentSpanId && { parent: { id: parentSpanId } }),
254
- ctx: meta.context,
255
- data: serializedData,
256
- error: getErrorSafe(data),
257
- startedAt: convertDateToPerformance(meta.createdAt),
258
- });
250
+ const span = createSpan({
251
+ id: spanId,
252
+ name: meta.name,
253
+ target: meta.path,
254
+ ...(parentSpanId && { parent: { id: parentSpanId } }),
255
+ ctx: meta.context,
256
+ data: serializedData,
257
+ error: getErrorSafe(data),
258
+ startedAt: convertDateToPerformance(meta.createdAt),
259
+ });
259
260
 
260
- const lastIteration = groupIterations[groupIterations.length - 1];
261
+ const lastIteration = groupIterations[groupIterations.length - 1];
261
262
 
262
- // delete the `partialUpdate` event if does not have nested spans
263
- const lastIterationEventSpanId = eventsIterationsMap
264
- .get(lastIteration)
265
- ?.get(meta.name);
266
- if (
267
- lastIterationEventSpanId &&
268
- partialUpdateEventName === meta.name &&
269
- spansMap.has(lastIterationEventSpanId)
270
- ) {
271
- const { context } = spansMap.get(lastIterationEventSpanId)!;
272
- if (parentIdsMap.has(context.span_id)) {
273
- spansToDeleteMap.set(lastIterationEventSpanId, undefined);
274
- } else {
275
- // delete span
276
- cleanSpanSources({ spanId: lastIterationEventSpanId });
277
- spansMap.delete(lastIterationEventSpanId);
263
+ // delete the `partialUpdate` event if does not have nested spans
264
+ const lastIterationEventSpanId = eventsIterationsMap
265
+ .get(lastIteration)
266
+ ?.get(meta.name);
267
+ if (
268
+ lastIterationEventSpanId &&
269
+ partialUpdateEventName === meta.name &&
270
+ spansMap.has(lastIterationEventSpanId)
271
+ ) {
272
+ const { context } = spansMap.get(lastIterationEventSpanId)!;
273
+ if (parentIdsMap.has(context.span_id)) {
274
+ spansToDeleteMap.set(lastIterationEventSpanId, undefined);
275
+ } else {
276
+ // delete span
277
+ cleanSpanSources({ spanId: lastIterationEventSpanId });
278
+ spansMap.delete(lastIterationEventSpanId);
279
+ }
278
280
  }
279
- }
280
-
281
- // create new span
282
- spansMap.set(span.context.span_id, span);
283
- // update number of nested spans for parent_id if exists
284
- if (span.parent_id) {
285
- parentIdsMap.set(
286
- span.parent_id,
287
- (parentIdsMap.get(span.parent_id) || 0) + 1,
288
- );
289
- }
290
281
 
291
- // save the last event for each iteration
292
- if (groupIterations.length > 0) {
293
- if (eventsIterationsMap.has(lastIteration)) {
294
- eventsIterationsMap
295
- .get(lastIteration)!
296
- .set(meta.name, span.context.span_id);
297
- } else {
298
- eventsIterationsMap.set(
299
- lastIteration,
300
- new Map().set(meta.name, span.context.span_id),
282
+ // create new span
283
+ spansMap.set(span.context.span_id, span);
284
+ // update number of nested spans for parent_id if exists
285
+ if (span.parent_id) {
286
+ parentIdsMap.set(
287
+ span.parent_id,
288
+ (parentIdsMap.get(span.parent_id) || 0) + 1,
301
289
  );
302
290
  }
291
+
292
+ // save the last event for each iteration
293
+ if (groupIterations.length > 0) {
294
+ if (eventsIterationsMap.has(lastIteration)) {
295
+ eventsIterationsMap
296
+ .get(lastIteration)!
297
+ .set(meta.name, span.context.span_id);
298
+ } else {
299
+ eventsIterationsMap.set(
300
+ lastIteration,
301
+ new Map().set(meta.name, span.context.span_id),
302
+ );
303
+ }
304
+ }
305
+ } catch (e) {
306
+ diag.warn("Instrumentation error", e);
303
307
  }
304
308
  });
305
309
 
@@ -309,16 +313,23 @@ export function createTelemetryMiddleware(
309
313
  event.name === successLLMEventName &&
310
314
  event.creator instanceof BaseAgent,
311
315
  (data: InferCallbackValue<ReActAgentCallbacks["success"]>) => {
312
- const { data: dataObject, memory } = data;
316
+ try {
317
+ const { data: dataObject, memory } = data;
313
318
 
314
- generatedMessage = {
315
- role: dataObject.role,
316
- text: dataObject.text,
317
- };
318
- history = memory.messages.map((msg) => ({
319
- text: msg.text,
320
- role: msg.role,
321
- }));
319
+ generatedMessage = {
320
+ role: dataObject.role,
321
+ text: dataObject.text,
322
+ };
323
+ history = memory.messages.map((msg) => ({
324
+ text: msg.text,
325
+ role: msg.role,
326
+ }));
327
+ } catch (e) {
328
+ diag.warn(
329
+ "Instrumentation error. Unable to map messages to history",
330
+ e,
331
+ );
332
+ }
322
333
  },
323
334
  );
324
335
  };