Telemetry and middleware

Tracing and metrics#

PipelineTelemetry.make() adds native Effect tracing and low-cardinality metrics:

ts
import * as PipelineTelemetry from "effect-rpc-pipeline/Telemetry"

const RpcRoutes = PipelineServer.layerHttp({
  group: AppRpcs,
  path: "/rpc",
  middleware: [PipelineTelemetry.make()]
}).pipe(Layer.provide(AppLive))

The default integration records:

  • A span for the complete pipeline.

  • Child spans for root and capability RPC invocations.

  • Metrics for request count, duration, graph size, invocation count, invocation duration, and outcomes.

Telemetry uses Effect’s tracer and metric registry. Provide an Effect OpenTelemetry Layer to export it; no separate pipeline exporter is needed.

Tracing and metrics can be configured independently:

ts
const telemetry = PipelineTelemetry.make({
  tracing: {
    spanPrefix: "TodoPipeline",
    invocations: true
  },
  metrics: {
    prefix: "todo_rpc"
  }
})

Set either option to false to disable it. Payloads, headers, user IDs, request IDs, client IDs, and capability IDs are not included in telemetry by default.

SQL and telemetry use the same simple server surface:

ts
middleware: [
  PipelineTelemetry.make(),
  PipelineSql.transaction()
]

Advanced server middleware#

Most applications should use the supplied SQL and telemetry integrations. PipelineServer.makeMiddleware is the escape hatch for logging, resource leasing, timeouts, auditing, or another application-specific concern:

ts
const AuditMiddleware = PipelineServer.makeMiddleware({
  request: (effect, { request, metadata }) =>
    Effect.annotateLogs(effect, {
      pipeline: request.definitionId,
      requestId: String(metadata.requestId)
    }),
  invocation: (effect, { rpc, kind }) =>
    Effect.annotateLogs(effect, {
      rpc: rpc._tag,
      kind
    })
})

const RpcRoutes = PipelineServer.layerHttp({
  group: AppRpcs,
  path: "/rpc",
  middleware: [AuditMiddleware]
})

Choose a hook based on the work it should surround:

HookWraps
requestThe decoded request and subsequent validation/execution flow
executionThe validated graph, including its transaction boundary
invocationEach actual root or capability handler invocation

Hooks transform ordinary Effects, so their errors and requirements compose normally. The first middleware entry is outermost when several entries use the same hook.