Skip to main content
SpanBuilder[F[_]] is a builder interface for creating and configuring Span instances. It provides a fluent API for setting span attributes, links, parent relationships, and other span properties.

Overview

The SpanBuilder allows you to configure all aspects of a span before creating it. The builder uses an internal state that can be modified through various methods.

Properties

meta

Type: InstrumentMeta[F] The instrument’s metadata. Indicates whether instrumentation is enabled.

Methods

modifyState

f
State => State
required
The modification function to apply to the current state
Returns: SpanBuilder[F] Modifies the internal state using the provided function and returns the modified builder.

root

Returns: SpanBuilder[F] Indicates that the span should be the root span and the scope parent should be ignored. This is equivalent to calling modifyState(_.withParent(SpanBuilder.Parent.Root)).
val rootSpan = spanBuilder.root.build

build

Returns: SpanOps[F] Creates SpanOps using the current state of the builder. This is the final step that produces the actual span operations.
val spanOps: SpanOps[F] = spanBuilder.build

liftTo

G[_]
Type Parameter
required
The target effect type with a MonadCancelThrow instance
Returns: SpanBuilder[G] Modifies the context F using an implicit KindTransformer from F to G. Requirements:
  • Implicit MonadCancelThrow[F]
  • Implicit MonadCancelThrow[G]
  • Implicit KindTransformer[F, G]

Parent Selection

The SpanBuilder.Parent sealed trait defines the parent selection strategy:

Parent.propagate

Returns: Parent Use the span context that is currently available in the scope as a parent (if any).

Parent.root

Returns: Parent Indicates the span must be the root span.

Parent.explicit

parent
SpanContext
required
The parent span context to use
Returns: Parent Use the given parent span context as an explicit parent.

State

The SpanBuilder.State sealed trait represents the internal state of the builder.

State Properties

  • attributes: Attributes - The attributes added to the state
  • links: Vector[(SpanContext, Attributes)] - The links added to the state
  • parent: Parent - The parent selection strategy
  • finalizationStrategy: SpanFinalizer.Strategy - The selected finalization strategy
  • spanKind: Option[SpanKind] - The selected span kind
  • startTimestamp: Option[FiniteDuration] - The explicit start timestamp

State Methods

addAttribute

attribute
Attribute[A]
required
The attribute to add to the state
Returns: State Adds the given attribute to the state. If the state previously contained a mapping for the key, the old value is replaced.

addAttributes

attributes
immutable.Iterable[Attribute[_]]
required
The set of attributes to add
Returns: State Adds multiple attributes to the state. If the state previously contained a mapping for any of the keys, the old values are replaced.
spanContext
SpanContext
required
The context of the linked span
attributes
immutable.Iterable[Attribute[_]]
required
The set of attributes to associate with the link
Returns: State Adds a link to another span context with associated attributes.

withFinalizationStrategy

strategy
SpanFinalizer.Strategy
required
The finalization strategy to use
Returns: State Sets the finalization strategy for the span.

withSpanKind

spanKind
SpanKind
required
The kind of span to create
Returns: State Sets the SpanKind for the span.

withStartTimestamp

timestamp
FiniteDuration
required
The explicit start timestamp from the epoch
Returns: State Sets an explicit start timestamp. The timestamp should be based on Clock[F].realTime. Using Clock[F].monotonic may lead to a missing span.

withParent

parent
Parent
required
The parent selection strategy to use
Returns: State Sets the parent to use for the span.

Companion Object

State.init

Returns: State Creates an initial state with default values:
  • Empty attributes
  • No links
  • SpanFinalizer.Strategy.reportAbnormal finalization strategy
  • No span kind
  • No start timestamp
  • Parent.Propagate parent strategy

noop

back
Span.Backend[F]
required
The backend to use for the no-op span
F[_]
Type Parameter
required
The effect type with an Applicative instance
Returns: SpanBuilder[F] Creates a no-op implementation of the SpanBuilder that produces no-op spans.

Build docs developers (and LLMs) love