Hyperlinkv0.8.0-beta.28

Layer

Layer.parentSpanconsteffect/Layer.ts:2564
(span: Tracer.AnySpan): Layer<Tracer.ParentSpan>

Constructs a layer that provides an existing span as the current parent span.

Details

The supplied span is made available through Tracer.ParentSpan for layers that are built with this layer. This API does not create, end, or close the span; the caller remains responsible for the span's lifetime.

Example (Referencing an existing parent span)

import { Console, Context, Effect, Layer, Tracer } from "effect"

class Database extends Context.Service<Database, {
  readonly query: (sql: string) => Effect.Effect<string>
}>()("Database") {}

// Create a layer that uses an existing span as parent
const databaseLayer = Layer.effect(
  Database,
  Effect.gen(function*() {
    yield* Effect.log("Initializing database")

    const parentSpan = yield* Effect.currentParentSpan
    yield* Console.log(parentSpan.spanId) // "42"

    return {
      query: Effect.fn("Database.query")((sql: string) => Effect.succeed(`Result: ${sql}`))
    }
  })
).pipe(Layer.provide(Layer.parentSpan(Tracer.externalSpan({
  spanId: "42",
  traceId: "000"
}))))
tracing
Source effect/Layer.ts:25642 lines
export const parentSpan = (span: Tracer.AnySpan): Layer<Tracer.ParentSpan> =>
  succeedContext(Tracer.ParentSpan.context(span))
Referenced by 1 symbols