Skip to main content

SpanKind

SpanKind is a sealed enumeration that classifies a span by its role in a trace — internal work, the client or server side of a synchronous RPC, or the producer or consumer side of an asynchronous message. It aligns with the OpenTelemetry SpanKind specification, and a Tracer records it on each Span so backends can reconstruct client↔server and producer↔consumer relationships across services.

sealed trait SpanKind
object SpanKind {
case object Internal extends SpanKind
case object Server extends SpanKind
case object Client extends SpanKind
case object Producer extends SpanKind
case object Consumer extends SpanKind
}

Each kind describes what the span covers and how it relates to a span in another process:

KindRepresentsRelationship in a trace
InternalWork performed internally in the application. The default when no kind is given.No remote parent or child.
ServerThe server side of a synchronous RPC — begins when the server starts processing the request and ends when it sends the response.Usually the child of a remote Client span.
ClientThe client side of a synchronous RPC — begins when the request is sent and ends when the response arrives.Usually the parent of a remote Server span.
ProducerThe producer side of an asynchronous message — begins when the message is sent and ends once it is dispatched.Parent that often completes before the Consumer begins.
ConsumerThe consumer side of an asynchronous message — begins when the message is received and ends when processing finishes.Child of a Producer span.

Usage​

Pass a SpanKind as the second argument to trace.span (or Tracer#span) to classify a span; omit it to accept the Internal default.

import zio.blocks.telemetry._

// Server span: handling an inbound request
trace.span("handle-order", SpanKind.Server) { _ =>
// Client span: an outbound call made while handling it
trace.span("charge-payment", SpanKind.Client)(_ => ())
}

Setting the kind accurately lets a tracing backend pair a Client span in one service with the Server span it triggers in another, and a Producer span with the Consumer that eventually handles its message.

See Also​