Skip to content

Commit cd0bc28

Browse files
fix: refactor telemetry imports for dynamic loading in categorization and matcher modules
1 parent 2240a2d commit cd0bc28

2 files changed

Lines changed: 35 additions & 12 deletions

File tree

src/lib/ai/categorization.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { generateObject } from "ai";
22
import { z } from "zod";
33
import { createLogger, DEBUG } from "@/lib/logger";
4-
import {
5-
endActiveSpan,
6-
flushSpanProcessor,
7-
updateObservation,
8-
updateTrace,
9-
} from "@/lib/observability/telemetry-helpers";
104
import { getAIModel } from "@/lib/providers/model-factory";
115
import type { AIProvider } from "@/lib/providers/registry";
126
import type { CompressedFieldData } from "@/types/autofill";
@@ -148,6 +142,9 @@ Be precise and consider context. For example:
148142
: `Information: ${answer}`;
149143

150144
if (DEBUG) {
145+
const { updateObservation, updateTrace } = await import(
146+
"@/lib/observability/telemetry-helpers"
147+
);
151148
await updateObservation({
152149
input: { answer, question },
153150
});
@@ -177,6 +174,9 @@ Be precise and consider context. For example:
177174
});
178175

179176
if (DEBUG) {
177+
const { updateObservation, updateTrace, endActiveSpan } = await import(
178+
"@/lib/observability/telemetry-helpers"
179+
);
180180
await updateObservation({
181181
output: result.object,
182182
});
@@ -192,6 +192,9 @@ Be precise and consider context. For example:
192192

193193
if (DEBUG) {
194194
try {
195+
const { updateObservation, updateTrace, endActiveSpan } = await import(
196+
"@/lib/observability/telemetry-helpers"
197+
);
195198
await updateObservation({
196199
output: error,
197200
level: "ERROR",
@@ -212,6 +215,9 @@ Be precise and consider context. For example:
212215
} finally {
213216
if (DEBUG) {
214217
try {
218+
const { flushSpanProcessor } = await import(
219+
"@/lib/observability/telemetry-helpers"
220+
);
215221
await flushSpanProcessor();
216222
} catch (telemetryError) {
217223
logger.error(
@@ -333,6 +339,9 @@ export const rephraseAgent = async (
333339
const userPrompt = `Original Question: "${question || "Not provided"}"\nOriginal Answer: "${answer}"`;
334340

335341
if (DEBUG) {
342+
const { updateObservation, updateTrace } = await import(
343+
"@/lib/observability/telemetry-helpers"
344+
);
336345
await updateObservation({
337346
input: { answer, question },
338347
});
@@ -353,6 +362,9 @@ export const rephraseAgent = async (
353362
});
354363

355364
if (DEBUG) {
365+
const { updateObservation, updateTrace, endActiveSpan } = await import(
366+
"@/lib/observability/telemetry-helpers"
367+
);
356368
await updateObservation({
357369
output: object,
358370
});
@@ -368,6 +380,9 @@ export const rephraseAgent = async (
368380

369381
if (DEBUG) {
370382
try {
383+
const { updateObservation, updateTrace, endActiveSpan } = await import(
384+
"@/lib/observability/telemetry-helpers"
385+
);
371386
await updateObservation({
372387
output: error,
373388
level: "ERROR",
@@ -385,6 +400,9 @@ export const rephraseAgent = async (
385400
} finally {
386401
if (DEBUG) {
387402
try {
403+
const { flushSpanProcessor } = await import(
404+
"@/lib/observability/telemetry-helpers"
405+
);
388406
await flushSpanProcessor();
389407
} catch (telemetryError) {
390408
logger.error("Telemetry flush error in rephrase:", telemetryError);

src/lib/ai/matcher.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ import {
77
roundConfidence,
88
} from "@/lib/autofill/mapping-utils";
99
import { createLogger, DEBUG } from "@/lib/logger";
10-
import {
11-
endActiveSpan,
12-
flushSpanProcessor,
13-
updateObservation,
14-
updateTrace,
15-
} from "@/lib/observability/telemetry-helpers";
1610
import { getAIModel } from "@/lib/providers/model-factory";
1711
import type { AIProvider } from "@/lib/providers/registry";
1812
import type {
@@ -211,6 +205,9 @@ export class AIMatcher {
211205
});
212206

213207
if (DEBUG) {
208+
const { updateObservation, updateTrace } = await import(
209+
"@/lib/observability/telemetry-helpers"
210+
);
214211
await updateObservation({
215212
input: { fields, memories, provider },
216213
});
@@ -243,6 +240,9 @@ export class AIMatcher {
243240
});
244241

245242
if (DEBUG) {
243+
const { updateObservation, updateTrace, endActiveSpan } = await import(
244+
"@/lib/observability/telemetry-helpers"
245+
);
246246
await updateObservation({
247247
output: result.object,
248248
});
@@ -258,6 +258,8 @@ export class AIMatcher {
258258

259259
if (DEBUG) {
260260
try {
261+
const { updateObservation, updateTrace, endActiveSpan } =
262+
await import("@/lib/observability/telemetry-helpers");
261263
await updateObservation({
262264
output: error,
263265
level: "ERROR",
@@ -275,6 +277,9 @@ export class AIMatcher {
275277
} finally {
276278
if (DEBUG) {
277279
try {
280+
const { flushSpanProcessor } = await import(
281+
"@/lib/observability/telemetry-helpers"
282+
);
278283
await flushSpanProcessor();
279284
} catch (telemetryError) {
280285
logger.error("Telemetry flush error in matching:", telemetryError);

0 commit comments

Comments
 (0)