Tracing ​
LensClient owns isolated trace and log providers for the Lens ingestion endpoint. Its Agent and Pipeline observers record application activity without registering global OpenTelemetry providers.
import { Agent, type CompletionModel } from '@anvia/core'
import { Pipeline } from '@anvia/core/pipeline'
import { LensClient } from '@anvia/lens'
import { z } from 'zod'
declare const model: CompletionModel
const lens = new LensClient({
serviceName: 'support-api',
environment: 'production',
release: process.env.APP_RELEASE,
})
const agentTracing = lens.observer({ captureMode: 'safe' })
const pipelineTracing = lens.pipelineObserver({ captureMode: 'safe' })Attach agentTracing through an Agent's named observers map and pipelineTracing through a Pipeline's constructor:
const agent = new Agent({
id: 'support',
model,
observability: {
observers: { lens: agentTracing },
primaryTrace: 'lens',
},
})
const pipeline = new Pipeline({
id: 'support-flow',
inputSchema: z.string(),
observability: {
observers: { lens: pipelineTracing },
primaryTrace: 'lens',
},
}).agent({
id: 'answer',
agent,
suspension: 'reject',
request: ({ input }) => ({ prompt: input }),
})Matching primaryTrace names produce one trace with Pipeline run and stage spans above the Agent's run, generation, tool, child-agent, error, trace, and usage information. Different names leave the Agent trace unparented.
Full capture adds Agent payloads plus Pipeline and stage inputs and outputs. Enable it only after a privacy review.
Call lens.flush() at a short-lived delivery boundary and lens.close() during final cleanup.
Durable runs and conversation grouping ​
Durable executions share one trace per run across retries and recovery; committed replayed steps do not add duplicate spans or observer usage. Set submission trace.sessionId to group turns, goal rounds, and subagents under one application chat without merging execution histories. LensClient installs the needed ID generator automatically. An application-owned OpenTelemetry provider must use createAnviaIdGenerator() to keep parentless durable attempts in the supplied trace. See Durable tracing for setup and HTTP context ownership.