Hyperlinkv0.8.0-beta.28

Layer

Layer.withParentSpanconsteffect/Layer.ts:2738
(span: Tracer.AnySpan, options?: Tracer.TraceOptions): <A, E, R>(
  self: Layer<A, E, R>
) => Layer<A, E, Exclude<R, Tracer.ParentSpan>>
<A, E, R>(
  self: Layer<A, E, R>,
  span: Tracer.AnySpan,
  options?: Tracer.TraceOptions
): Layer<A, E, Exclude<R, Tracer.ParentSpan>>

Wraps a layer so spans created during its construction use the supplied span as their parent.

Details

Use this to attach layer construction to an existing trace hierarchy. This API does not create or end the supplied parent span.

When the supplied span is a native Span, layer construction also receives diagnostic information that helps associate failures with the layer call site. External spans are only installed as the parent span and do not add this diagnostic call-site information.

Example (Attaching layers to an existing parent span)

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

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

class Cache extends Context.Service<Cache, {
  readonly get: (key: string) => Effect.Effect<string | null>
}>()("Cache") {}

// Create layers
const DatabaseLayer = Layer.effect(Database, Effect.gen(function*() {
  yield* Effect.log("Connecting to database")
  return {
    query: Effect.fn("Database.query")((sql: string) => Effect.succeed(`DB: ${sql}`))
  }
}))

const CacheLayer = Layer.effect(Cache, Effect.gen(function*() {
  yield* Effect.log("Connecting to cache")
  return {
    get: Effect.fn("Cache.get")((key: string) => Effect.succeed(`Cache: ${key}`))
  }
}))

// Use with an existing parent span from Effect.withSpan
const program = Effect.withSpan("application-startup")(
  Effect.gen(function*() {
    const parentSpan = yield* Tracer.ParentSpan

    // Both layers will be children of "application-startup" span
    const AppLayer = Layer.mergeAll(DatabaseLayer, CacheLayer).pipe(
      Layer.withParentSpan(parentSpan)
    )

    const context = yield* Layer.build(AppLayer)
    const database = Context.get(context, Database)
    const cache = Context.get(context, Cache)

    const dbResult = yield* database.query("SELECT * FROM users")
    const cacheResult = yield* cache.get("user:123")

    return { dbResult, cacheResult }
  })
)
tracing
Source effect/Layer.ts:273827 lines
export const withParentSpan: {
  (
    span: Tracer.AnySpan,
    options?: Tracer.TraceOptions
  ): <A, E, R>(
    self: Layer<A, E, R>
  ) => Layer<A, E, Exclude<R, Tracer.ParentSpan>>
  <A, E, R>(
    self: Layer<A, E, R>,
    span: Tracer.AnySpan,
    options?: Tracer.TraceOptions
  ): Layer<A, E, Exclude<R, Tracer.ParentSpan>>
} = function() {
  const dataFirst = isLayer(arguments[0])
  const span: Tracer.AnySpan = dataFirst ? arguments[1] : arguments[0]
  let options = dataFirst ? arguments[2] : arguments[1]
  let provideStackFrame: <A, E, R>(self: Layer<A, E, R>) => Layer<A, E, R> = identity
  if (span._tag === "Span") {
    options = internalTracer.addSpanStackTrace(options)
    provideStackFrame = provideSpanStackFrame(span.name, options?.captureStackTrace)
  }
  const parentSpanLayer = parentSpan(span)
  if (dataFirst) {
    return provide(provideStackFrame(arguments[0]), parentSpanLayer)
  }
  return (self: Layer<any, any, any>) => provide(provideStackFrame(self), parentSpanLayer)
} as any
Referenced by 1 symbols