Skip to content

Commit bba79d5

Browse files
[ai-form-recognizer] Add tracing spans and tests (Azure#20646)
* [ai-form-recognizer] Add tracing spans and tests. * Fixed copyright header.
1 parent 66db7fb commit bba79d5

File tree

3 files changed

+431
-152
lines changed

3 files changed

+431
-152
lines changed

sdk/formrecognizer/ai-form-recognizer/src/documentAnalysisClient.ts

Lines changed: 106 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -336,19 +336,23 @@ export class DocumentAnalysisClient {
336336
input: string | FormRecognizerRequestBody,
337337
options: AnalyzeDocumentOptions<unknown> = {}
338338
): Promise<AnalysisPoller<unknown>> {
339-
const initialModelId = typeof model === "string" ? model : model.modelId;
340-
341-
const analysisPoller = this.createAnalysisPoller<unknown>(input, {
342-
initialModelId,
339+
return this._tracing.withSpan(
340+
"DocumentAnalysisClient.beginAnalyzeDocument",
343341
options,
344-
transformResult: (result) =>
345-
toAnalyzeResultFromGenerated(
346-
result,
347-
typeof model === "string" ? toAnalyzedDocumentFromGenerated : getMapper(model)
348-
),
349-
});
342+
(finalOptions) => {
343+
const initialModelId = typeof model === "string" ? model : model.modelId;
350344

351-
return analysisPoller;
345+
return this.createAnalysisPoller<unknown>(input, {
346+
initialModelId,
347+
options: finalOptions,
348+
transformResult: (result) =>
349+
toAnalyzeResultFromGenerated(
350+
result,
351+
typeof model === "string" ? toAnalyzedDocumentFromGenerated : getMapper(model)
352+
),
353+
});
354+
}
355+
);
352356
}
353357

354358
/**
@@ -410,11 +414,16 @@ export class DocumentAnalysisClient {
410414
// eslint-disable-next-line @azure/azure-sdk/ts-naming-options
411415
options: AnalyzeDocumentOptions<LayoutResult> = {}
412416
): Promise<AnalysisPoller<LayoutResult>> {
413-
return this.createAnalysisPoller(input, {
414-
initialModelId: "prebuilt-layout",
417+
return this._tracing.withSpan(
418+
"DocumentAnalysisClient.beginExtractLayout",
415419
options,
416-
transformResult: (res) => toLayoutResult(toAnalyzeResultFromGenerated(res, identity)),
417-
});
420+
(finalOptions) =>
421+
this.createAnalysisPoller(input, {
422+
initialModelId: "prebuilt-layout",
423+
options: finalOptions,
424+
transformResult: (res) => toLayoutResult(toAnalyzeResultFromGenerated(res, identity)),
425+
})
426+
);
418427
}
419428

420429
/**
@@ -486,12 +495,17 @@ export class DocumentAnalysisClient {
486495
// eslint-disable-next-line @azure/azure-sdk/ts-naming-options
487496
options: AnalyzeDocumentOptions<GeneralDocumentResult> = {}
488497
): Promise<AnalysisPoller<GeneralDocumentResult>> {
489-
return this.createAnalysisPoller(input, {
490-
initialModelId: "prebuilt-document",
498+
return this._tracing.withSpan(
499+
"DocumentAnalysisClient.beginExtractGeneralDocument",
491500
options,
492-
transformResult: (res) =>
493-
toGeneralDocumentResult(toAnalyzeResultFromGenerated(res, identity)),
494-
});
501+
(finalOptions) =>
502+
this.createAnalysisPoller(input, {
503+
initialModelId: "prebuilt-document",
504+
options: finalOptions,
505+
transformResult: (res) =>
506+
toGeneralDocumentResult(toAnalyzeResultFromGenerated(res, identity)),
507+
})
508+
);
495509
}
496510

497511
/**
@@ -552,11 +566,16 @@ export class DocumentAnalysisClient {
552566
// eslint-disable-next-line @azure/azure-sdk/ts-naming-options
553567
options: AnalyzeDocumentOptions<ReadResult> = {}
554568
): Promise<AnalysisPoller<ReadResult>> {
555-
return this.createAnalysisPoller(input, {
556-
initialModelId: "prebuilt-read",
569+
return this._tracing.withSpan(
570+
"DocumentAnalysisClient.beginReadDocument",
557571
options,
558-
transformResult: (res) => toReadResult(toAnalyzeResultFromGenerated(res, identity)),
559-
});
572+
(finalOptions) =>
573+
this.createAnalysisPoller(input, {
574+
initialModelId: "prebuilt-read",
575+
options: finalOptions,
576+
transformResult: (res) => toReadResult(toAnalyzeResultFromGenerated(res, identity)),
577+
})
578+
);
560579
}
561580

562581
/**
@@ -578,14 +597,14 @@ export class DocumentAnalysisClient {
578597
// TODO: what should we do if resumeFrom.modelId is different from initialModelId?
579598
// And what do we do with the redundant input??
580599

581-
const getAnalyzeResult = async (operationLocation: string): Promise<AnalyzeResultOperation> =>
600+
const getAnalyzeResult = (operationLocation: string): Promise<AnalyzeResultOperation> =>
582601
this._tracing.withSpan(
583-
"DocumentAnalysisClient.createAnalysisPoller-toInit",
602+
"DocumentAnalysisClient.createAnalysisPoller-getAnalyzeResult",
584603
definition.options,
585-
(updatedOptions) =>
604+
(finalOptions) =>
586605
this._restClient.sendOperationRequest<AnalyzeResultOperation>(
587606
{
588-
options: updatedOptions,
607+
options: finalOptions,
589608
},
590609
{
591610
path: operationLocation,
@@ -608,63 +627,78 @@ export class DocumentAnalysisClient {
608627
const toInit =
609628
// If the user gave us a stored token, we'll poll it again
610629
resumeFrom !== undefined
611-
? async () => {
612-
const { operationLocation, modelId } = JSON.parse(resumeFrom) as {
613-
operationLocation: string;
614-
modelId: string;
615-
};
630+
? async () =>
631+
this._tracing.withSpan(
632+
"DocumentAnalysisClient.createAnalysisPoller-resume",
633+
definition.options,
634+
async () => {
635+
const { operationLocation, modelId } = JSON.parse(resumeFrom) as {
636+
operationLocation: string;
637+
modelId: string;
638+
};
616639

617-
const result = await getAnalyzeResult(operationLocation);
640+
const result = await getAnalyzeResult(operationLocation);
618641

619-
return toDocumentAnalysisPollOperationState(
620-
definition,
621-
modelId,
622-
operationLocation,
623-
result
624-
);
625-
}
642+
return toDocumentAnalysisPollOperationState(
643+
definition,
644+
modelId,
645+
operationLocation,
646+
result
647+
);
648+
}
649+
)
626650
: // Otherwise, we'll start a new operation from the initialModelId
627-
async () => {
628-
const [contentType, analyzeRequest] = toAnalyzeRequest(input);
651+
async () =>
652+
this._tracing.withSpan(
653+
"DocumentAnalysisClient.createAnalysisPoller-start",
654+
definition.options,
655+
async () => {
656+
const [contentType, analyzeRequest] = toAnalyzeRequest(input);
629657

630-
const { operationLocation } = await this._restClient.analyzeDocument(
631-
definition.initialModelId,
632-
contentType as any,
633-
{
634-
...definition.options,
635-
analyzeRequest,
636-
}
637-
);
658+
const { operationLocation } = await this._restClient.analyzeDocument(
659+
definition.initialModelId,
660+
contentType as any,
661+
{
662+
...definition.options,
663+
analyzeRequest,
664+
}
665+
);
638666

639-
if (operationLocation === undefined) {
640-
throw new Error(
641-
"Unable to start analysis operation: no Operation-Location received."
642-
);
643-
}
667+
if (operationLocation === undefined) {
668+
throw new Error(
669+
"Unable to start analysis operation: no Operation-Location received."
670+
);
671+
}
644672

645-
const result = await getAnalyzeResult(operationLocation);
673+
const result = await getAnalyzeResult(operationLocation);
646674

647-
return toDocumentAnalysisPollOperationState(
648-
definition,
649-
definition.initialModelId,
650-
operationLocation,
651-
result
675+
return toDocumentAnalysisPollOperationState(
676+
definition,
677+
definition.initialModelId,
678+
operationLocation,
679+
result
680+
);
681+
}
652682
);
653-
};
654683

655684
const poller = await lro<Result, DocumentAnalysisPollOperationState<Result>>(
656685
{
657686
init: toInit,
658-
poll: async ({ operationLocation, modelId }) => {
659-
const result = await getAnalyzeResult(operationLocation);
687+
poll: async ({ operationLocation, modelId }) =>
688+
this._tracing.withSpan(
689+
"DocumentAnalysisClient.createAnalysisPoller-poll",
690+
{},
691+
async () => {
692+
const result = await getAnalyzeResult(operationLocation);
660693

661-
return toDocumentAnalysisPollOperationState(
662-
definition,
663-
modelId,
664-
operationLocation,
665-
result
666-
);
667-
},
694+
return toDocumentAnalysisPollOperationState(
695+
definition,
696+
modelId,
697+
operationLocation,
698+
result
699+
);
700+
}
701+
),
668702
serialize: ({ operationLocation, modelId }) =>
669703
JSON.stringify({ modelId, operationLocation }),
670704
},

0 commit comments

Comments
 (0)