Share via


InvokeAgentScope class

Provides OpenTelemetry tracing scope for AI agent invocation operations.

Extends

Methods

recordInputMessages(InputMessagesParam)

Records the input messages for telemetry tracking. Accepts a single string, an array of strings (auto-wrapped as OTEL ChatMessage with role user), or a versioned InputMessages wrapper.

recordOutputMessages(OutputMessagesParam)

Records the output messages for telemetry tracking. Accepts a single string, an array of strings (auto-wrapped as OTEL OutputMessage with role assistant), or a versioned OutputMessages wrapper.

recordResponse(string)

Records response information for telemetry tracking.

start(Request, InvokeAgentScopeDetails, AgentDetails, CallerDetails, SpanDetails)

Creates and starts a new scope for agent invocation tracing.

Inherited Methods

dispose()

Legacy dispose method for compatibility

getSpanContext()

Gets the span context for this scope. This can be used to create a ParentSpanRef for explicit parent-child linking across async boundaries.

recordAttributes(undefined | null | Iterable<[string, AttributeValue]> | Record<string, AttributeValue>)

Records multiple attribute key/value pairs for telemetry tracking.

recordCancellation(string)

Records a cancellation event on the span. Sets the span status to ERROR with the cancellation reason and marks the error type as 'TaskCanceledException'.

recordError(Error)

Records an error that occurred during the operation

setEndTime(TimeInput)

Sets a custom end time for the scope. When set, dispose will pass this value to span.end() instead of using the current wall-clock time. This is useful when the actual end time of the operation is known before the scope is disposed.

withActiveSpanAsync<T>(() => Promise<T>)

Makes this span active for the duration of the async callback execution

Method Details

recordInputMessages(InputMessagesParam)

Records the input messages for telemetry tracking. Accepts a single string, an array of strings (auto-wrapped as OTEL ChatMessage with role user), or a versioned InputMessages wrapper.

function recordInputMessages(messages: InputMessagesParam)

Parameters

messages
InputMessagesParam

A string, array of strings, or an InputMessages wrapper

recordOutputMessages(OutputMessagesParam)

Records the output messages for telemetry tracking. Accepts a single string, an array of strings (auto-wrapped as OTEL OutputMessage with role assistant), or a versioned OutputMessages wrapper.

function recordOutputMessages(messages: OutputMessagesParam)

Parameters

messages
OutputMessagesParam

A string, array of strings, or an OutputMessages wrapper

recordResponse(string)

Records response information for telemetry tracking.

function recordResponse(response: string)

Parameters

response

string

The invocation response

start(Request, InvokeAgentScopeDetails, AgentDetails, CallerDetails, SpanDetails)

Creates and starts a new scope for agent invocation tracing.

static function start(request: Request, invokeScopeDetails: InvokeAgentScopeDetails, agentDetails: AgentDetails, callerDetails?: CallerDetails, spanDetails?: SpanDetails): InvokeAgentScope

Parameters

request
Request

Request payload (channel, conversationId, content, sessionId).

invokeScopeDetails
InvokeAgentScopeDetails

Scope-level details

agentDetails
AgentDetails

The agent identity. Tenant ID is derived from agentDetails.tenantId (required).

callerDetails
CallerDetails

Optional caller information. Supports three scenarios:

  • Human caller only: { userDetails: { userId, userName, ... } }
  • Agent caller only: { callerAgentDetails: { agentId, agentName, ... } }
  • Both (A2A with human in chain): { userDetails: { ... }, callerAgentDetails: { ... } }
spanDetails
SpanDetails

Optional span configuration (parentContext, startTime, endTime, spanKind, spanLinks).

Returns

A new InvokeAgentScope instance.

Inherited Method Details

dispose()

Legacy dispose method for compatibility

function dispose()

Inherited From OpenTelemetryScope.dispose

getSpanContext()

Gets the span context for this scope. This can be used to create a ParentSpanRef for explicit parent-child linking across async boundaries.

function getSpanContext(): SpanContext

Returns

SpanContext

The SpanContext containing traceId and spanId

Inherited From OpenTelemetryScope.getSpanContext

recordAttributes(undefined | null | Iterable<[string, AttributeValue]> | Record<string, AttributeValue>)

Records multiple attribute key/value pairs for telemetry tracking.

function recordAttributes(attributes: undefined | null | Iterable<[string, AttributeValue]> | Record<string, AttributeValue>)

Parameters

attributes

undefined | null | Iterable<[string, AttributeValue]> | Record<string, AttributeValue>

Collection of attribute key/value pairs (array or iterable of [key, value] or object map).

Inherited From OpenTelemetryScope.recordAttributes

recordCancellation(string)

Records a cancellation event on the span. Sets the span status to ERROR with the cancellation reason and marks the error type as 'TaskCanceledException'.

function recordCancellation(reason?: string)

Parameters

reason

string

Optional cancellation reason. Defaults to 'Task was cancelled'.

Inherited From OpenTelemetryScope.recordCancellation

recordError(Error)

Records an error that occurred during the operation

function recordError(error: Error)

Parameters

error

Error

The error that occurred

Inherited From OpenTelemetryScope.recordError

setEndTime(TimeInput)

Sets a custom end time for the scope. When set, dispose will pass this value to span.end() instead of using the current wall-clock time. This is useful when the actual end time of the operation is known before the scope is disposed.

function setEndTime(endTime: TimeInput)

Parameters

endTime

TimeInput

The end time as milliseconds since epoch, a Date, or an HrTime tuple.

Inherited From OpenTelemetryScope.setEndTime

withActiveSpanAsync<T>(() => Promise<T>)

Makes this span active for the duration of the async callback execution

function withActiveSpanAsync<T>(callback: () => Promise<T>): Promise<T>

Parameters

callback

() => Promise<T>

Returns

Promise<T>

Inherited From OpenTelemetryScope.withActiveSpanAsync