Skip to content
中文

createProviderObservationScope ​

SUPPORT · function · Runtime value

Bind provider observations to a caller-owned destination across diagnostic child spans.

attach returns a scoped diagnostic context; contexts that were not attached through this scope produce no observations. Child contexts inherit the owner and a parent/child span path.

Import ​

ts
import { createProviderObservationScope } from '@jangts/agent-core/support';
js
import { createProviderObservationScope } from '@jangts/agent-core/support';

Other public entry points:@jangts/agent-core/support, @jangts/agent-core/support/model.

Definition ​

Type parameters ​

Type parameterConstraintDefault type
Owner——

Call ​

ts
createProviderObservationScope<Owner>(sink: { accepts?(owner: Owner, descriptor: import("../../structs/support/model/provider-observer.js").ProviderEventDescriptor): boolean; write(owner: Owner, observation: ProviderObservation, spanId: string, content: "metadata" | "content"): void | Promise<void>; }): Readonly<{ attach: (context: DiagnosticContext, owner: Owner, spanId?: string) => DiagnosticContext; observer: Readonly<ProviderObserver>; }>

Bind provider observations to a caller-owned destination across diagnostic child spans.

attach returns a scoped diagnostic context; contexts that were not attached through this scope produce no observations. Child contexts inherit the owner and a parent/child span path.

Type parameterConstraintDefault type
Owner——
ParameterTypePassingDescription
sink{ accepts?(owner: Owner, descriptor: import("../../structs/support/model/provider-observer.js").ProviderEventDescriptor): boolean; write(owner: Owner, observation: ProviderObservation, spanId: string, content: "metadata" | "content"): void | Promise<void>; }Required—
Fields of sink
FieldTypeRequiredDescription
accepts((owner: Owner, descriptor: import("../../structs/support/model/provider-observer.js").ProviderEventDescriptor) => boolean) | undefinedOptionalSee the type definition and usage context.
write(owner: Owner, observation: ProviderObservation, spanId: string, content: "metadata" | "content") => void | Promise<void>RequiredSee the type definition and usage context.

Returns: Readonly<{ attach: (context: DiagnosticContext, owner: Owner, spanId?: string) => DiagnosticContext; observer: Readonly<ProviderObserver>; }>.

Return value structure
FieldTypeRequiredDescription
attach(context: DiagnosticContext, owner: Owner, spanId?: string) => DiagnosticContextRequiredSee the type definition and usage context.
observerReadonly<ProviderObserver>RequiredSee the type definition and usage context.

Examples ​

Observe metadata only ​

Beginner · Node.js

ts
import { Support } from '@jangts/agent-core';
const scope = Support.createProviderObservationScope<{ requestId: string }>({
  accepts(_owner, descriptor) { return descriptor.content === 'metadata'; },
  write(owner, event, spanId) { console.log(owner.requestId, spanId, event.event); },
});
console.log(scope.observer);
js
import { Support } from '@jangts/agent-core';
const scope = Support.createProviderObservationScope({
    accepts(_owner, descriptor) { return descriptor.content === 'metadata'; },
    write(owner, event, spanId) { console.log(owner.requestId, spanId, event.event); },
});
console.log(scope.observer);

Bind your own correlation value per request ​

Configuration · Node.js

This example only assembles; for actual calls, configure that diagnostics into the corresponding request execution rather than forcing an App ID into the Core identity.

ts
import { Support } from '@jangts/agent-core';
import { createDiagnostics } from '@jangts/agent-core/diagnostics';
const scope = Support.createProviderObservationScope<{ requestId: string }>({
  accepts: (_owner, descriptor) => descriptor.content === 'metadata',
  write(owner, event, spanId) { console.log(owner.requestId, spanId, event.event); },
});
const provider = new Support.CompatibleModelProvider({
  config: { baseUrl: process.env.MODEL_BASE_URL!, apiKey: process.env.MODEL_API_KEY!, model: process.env.MODEL_NAME! },
  observe: { provider: scope.observer },
});
const diagnostics = scope.attach(createDiagnostics(), { requestId: 'app-request-1' });
console.log(provider, diagnostics.identity?.runId);
js
import { Support } from '@jangts/agent-core';
import { createDiagnostics } from '@jangts/agent-core/diagnostics';
const scope = Support.createProviderObservationScope({
    accepts: (_owner, descriptor) => descriptor.content === 'metadata',
    write(owner, event, spanId) { console.log(owner.requestId, spanId, event.event); },
});
const provider = new Support.CompatibleModelProvider({
    config: { baseUrl: process.env.MODEL_BASE_URL, apiKey: process.env.MODEL_API_KEY, model: process.env.MODEL_NAME },
    observe: { provider: scope.observer },
});
const diagnostics = scope.attach(createDiagnostics(), { requestId: 'app-request-1' });
console.log(provider, diagnostics.identity?.runId);

API reference, usage examples and developer guides.