Skip to content
pleach
Gateway

Gateway · OpenTelemetry integration

OTel surface for `@pleach/gateway` — one `llm.invocation` span per routed call, plus cost and token counters from the cost-event adapter.

@pleach/gateway emits two kinds of OpenTelemetry signal:

  • Spans. One llm.invocation span per routed call, from GatewayClient.route() and from a transport-served GatewayRuntime.routeChatCompletion().
  • Counters. The cost-event adapter records a cost counter and a token counter per CostEvent.

Cost counters

The cost-event adapter at @pleach/gateway/cost emits two counters per CostEvent:

pleach_gateway_cost_usd        # Counter, double, USD
pleach_gateway_tokens_total    # Counter, long,   {token}

pleach_gateway_tokens_total carries a token.kind = "prompt" | "completion" dimension, so the breakdown isn't collapsed into one sum.

Wiring

routeChatCompletion's output lacks tenantId, family, callClass, byokActive, and the full routingDecision. Merge those from the request and your routing context, then build the event in computeCost. The full mapper and an example are in Cost events. Pass createOtelCostEmitter({ meter }) as the middleware's emitter.

Attributes recorded on every measurement

AttributeSource
tenant.idevent.tenantId
provider.familyevent.family
call.classevent.callClass
model.invokedevent.modelInvoked
byok.activeevent.byokActive
cost.event.type"domain.gateway.cost.recorded"

token.kind ("prompt" / "completion") is added to pleach_gateway_tokens_total measurements only.

The OTel SDK is an optional peer

createOtelCostEmitter accepts any meter shaped like @opentelemetry/api's Meter, such as a test stub or an in-house facade. It throws a TypeError if meter lacks createCounter(name, options?).

Override counter names

const emitter = createOtelCostEmitter({
  meter,
  costCounterName: "myco.pleach.gateway.cost.usd",
  tokensCounterName: "myco.pleach.gateway.tokens.total",
})

Soft-fail semantics

emit() swallows internal exceptions, so a metric backend failure never propagates up the gateway call.

llm.invocation spans

The gateway emits one span named llm.invocation per routed call, the same span name core's seams emit:

  • GatewayClient.route() emits one span per call that reaches a transport.
  • GatewayRuntime.routeChatCompletion() emits one span per transport-served call. The stub path, with no transport wired, emits none because no provider was invoked.

Cascade and failover hops are attributes on that one span. The gateway emits no llm.attempt children; those come only from core's seams (see OTel spans). Spans the transport opens, such as HTTP client instrumentation, nest under it.

Choosing a tracer

import { trace } from "@opentelemetry/api"
import { GatewayClient } from "@pleach/gateway"

const client = new GatewayClient({
  tenantId,
  transports,
  tracer: trace.getTracer("my-app"), // or null to turn spans off
})
  • A tracer you pass wins. It's typed structurally, so any @opentelemetry/api Tracer fits.
  • tracer: null disables span emission.
  • With no tracer, the gateway lazily imports @opentelemetry/api (an optional peer, ^1.0.0) on the first routed call and uses trace.getTracer("@pleach/gateway"). If the package isn't installed, emission is a silent no-op.

GatewayRuntimeConfig.tracer takes the same values. A throwing tracer never fails a routed call; it degrades to no span.

Webpack may print a "Critical dependency" warning for the lazy import. Pass tracer explicitly, or null, to avoid it.

Attributes

The tenant, call-class, family, and token keys come from @pleach/core/otel, so gateway and core traces filter on the same keys. pleach.model_id uses the same spelling core's seam spans use.

Attributeroute()routeChatCompletion()
pleach.tenant_idyesyes
pleach.sub_tenant_id—when input.subTenantId is set
pleach.call_classyes (the class that served, after any cascade)yes (input.callClass, default "converse")
pleach.familyyesyes
pleach.model_idmodel that servedmodel that served
pleach.tokens.input · pleach.tokens.outputyeswhen the transport reports usage
pleach.cost.usdyesyes
pleach.cost.incompleteyes (true when no price was known)—
pleach.gateway.requested_modelyesyes
pleach.gateway.provider—provider that served
pleach.gateway.region—when a region was resolved
pleach.gateway.byok_activeyes—
pleach.gateway.fallback_hopsyesyes
pleach.gateway.fallback_chainmodels attempted before the one that servedproviders that failed before the one that served
pleach.gateway.family_exhaustedyes—
pleach.gateway.routing_decision_id—yes

GATEWAY_SPAN_ATTRIBUTE_KEYS and LLM_INVOCATION_SPAN_NAME are exported from the package root, along with the GatewayTracerLike and GatewaySpanLike types.

Token, cost, and served-model attributes are set when the call succeeds. If the transport result has no usage, the token attributes are omitted, never set to 0.

Errors

A failed call ends the span with status ERROR. It records only:

  • error.type: the error class name, when it's a plain identifier (otherwise "Error"), never the message.
  • http.response.status_code: when the error carries a numeric HTTP status.

The status message and recorded exception use the fixed text llm invocation failed. Transport error messages can embed the upstream response body, so the raw message is never copied onto the span.

For route(), the span ends after the cost event is emitted, so a cost-sink failure shows as an ERROR span.

callClass on routeChatCompletion()

RouteChatCompletionInput.callClass is optional and defaults to "converse"; a value outside core's ALL_CALL_CLASSES throws TypeError. It feeds pleach.call_class and the provenance row. It's attribution only: GatewayRuntime selects by family and model.

Public surface

  • OtelMeter / OtelCounter duck-typed interfaces and the createOtelCostEmitter factory, from @pleach/gateway/cost/adapters/otel.
  • CostMiddleware, which composes the OTel adapter with any route response shape, from @pleach/gateway/cost.
  • The LLM invocation span (LLM_INVOCATION_SPAN_NAME, GATEWAY_SPAN_ATTRIBUTE_KEYS), from @pleach/gateway.

Where to go next