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
import { createProviderObservationScope } from '@jangts/agent-core/support';import { createProviderObservationScope } from '@jangts/agent-core/support';Other public entry points:@jangts/agent-core/support, @jangts/agent-core/support/model.
Definition
Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
Owner | — | — |
Call
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 parameter | Constraint | Default type |
|---|---|---|
Owner | — | — |
| Parameter | Type | Passing | Description |
|---|---|---|---|
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
| Field | Type | Required | Description |
|---|---|---|---|
accepts | ((owner: Owner, descriptor: import("../../structs/support/model/provider-observer.js").ProviderEventDescriptor) => boolean) | undefined | Optional | See the type definition and usage context. |
write | (owner: Owner, observation: ProviderObservation, spanId: string, content: "metadata" | "content") => void | Promise<void> | Required | See the type definition and usage context. |
Returns: Readonly<{ attach: (context: DiagnosticContext, owner: Owner, spanId?: string) => DiagnosticContext; observer: Readonly<ProviderObserver>; }>.
Return value structure
| Field | Type | Required | Description |
|---|---|---|---|
attach | (context: DiagnosticContext, owner: Owner, spanId?: string) => DiagnosticContext | Required | See the type definition and usage context. |
observer | Readonly<ProviderObserver> | Required | See the type definition and usage context. |
Examples
- Provider observation and the App's own recording targets: A stable Provider binds observation rules; each request is associated only with its own target, and child scopes inherit the association.
Observe metadata only
Beginner · Node.js
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);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.
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);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);