Skip to content

Configure tracing

Create one Lens tracing instance when the server process starts. Configuration may come from environment variables, explicit options, or both.

Create an ingestion key

Open the project in Lens, choose Connect, then create or select an ingestion key in Project settings. A key pair belongs to one project.

Store both values in the server-side secret manager used by the application:

dotenv
ANVIA_LENS_BASE_URL=https://lens.example.com
ANVIA_LENS_PUBLIC_KEY=pk-lens-...
ANVIA_LENS_SECRET_KEY=sk-lens-...
ANVIA_LENS_SERVICE_NAME=support-api
ANVIA_LENS_ENVIRONMENT=production
ANVIA_LENS_RELEASE=2026.08.11

ANVIA_LENS_BASE_URL is the same origin used to open Lens in a browser. Do not append /api or an OTLP endpoint; @anvia/lens constructs the trace and log ingestion paths.

Configuration reference

OptionEnvironment variableDefaultPurpose
baseUrlANVIA_LENS_BASE_URLRequiredBrowser-facing Lens origin.
publicKeyANVIA_LENS_PUBLIC_KEYRequiredIdentifies the destination project.
secretKeyANVIA_LENS_SECRET_KEYRequiredAuthenticates ingestion.
serviceNameANVIA_LENS_SERVICE_NAMERequiredStable name of the emitting application or service.
environmentANVIA_LENS_ENVIRONMENTNODE_ENV, then defaultDeployment stage such as development, staging, or production.
releaseANVIA_LENS_RELEASENoneImmutable deployed version, commonly a build number or Git SHA.
timeoutMs30000Export request and force-flush timeout in milliseconds.
captureModesafeWhether trace payload bodies are exported.
captureMaxBytes262144Per captured value limit in bytes.

Explicit options take precedence over environment variables:

ts
import { lens } from '@anvia/lens'

export const tracing = lens.create({
  serviceName: 'support-api',
  environment: 'production',
  release: process.env.GIT_SHA,
  timeoutMs: 15_000,
})

Prefer environment variables for credentials and explicit options for application-owned settings. This keeps secrets outside the source while making service identity visible in code.

Choose stable dimensions

Use a stable serviceName for one deployable service. Do not include pod ids, hostnames, or release numbers in it; Lens already has dedicated environment and release dimensions.

Good values are support-api, checkout-worker, or agent-evals. Set environment to the deployment stage and release to the exact deployed artifact. Together they make production-versus-staging filters and release comparisons meaningful.

Attach one observer to multiple agents

One tracing instance can observe multiple agents in the same process:

ts
const supportAgent = new AgentBuilder('support', supportModel)
  .observe(tracing)
  .build()

const triageAgent = new AgentBuilder('triage', triageModel)
  .observe(tracing)
  .build()

The agent id and name distinguish their traces. Sharing the observer also gives the process one place to flush and shut down its exporters.

Make Lens optional in local environments

Use createFromEnv() when the integration is optional outside deployed environments:

ts
const tracing = lens.createFromEnv({
  optional: true,
  serviceName: 'support-api',
})

console.log(tracing.enabled)

When all three connection variables—base URL, public key, and secret key—are absent, optional: true returns a disabled no-op observer. Partial connection configuration still throws, which prevents a mistyped deployment from silently losing telemetry.

Use tracing.enabled only for diagnostics or optional UI behavior. The disabled observer is safe to pass to .observe(tracing) directly.

Verify the connection

Run one agent request, flush the observer, then open Traces with the 24-hour range and no filters:

ts
const response = await supportAgent.prompt('Connection check').send()
await tracing.flush()

console.log(response.trace?.traceId)

The printed trace id should match the trace detail in Lens. If nothing appears, check that the key pair is active and belongs to the intended project, the base URL has no extra path, and the application can reach Lens over the network.

Next, add request-level identity with Trace context.

Built for Anvia.