(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"
}))))export const const parentSpan: (
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"
}))))
parentSpan = (span: Tracer.AnySpanspan: import TracerTracer.type AnySpan =
| Tracer.Span
| Tracer.ExternalSpan
A span value that can participate in tracing, either an Effect-managed
Span or an ExternalSpan propagated from another tracing system.
Example (Accepting any span)
import { Effect, Tracer } from "effect"
// Function that accepts any span type
const logSpan = (span: Tracer.AnySpan) => {
console.log(`Span ID: ${span.spanId}, Trace ID: ${span.traceId}`)
return Effect.succeed(span)
}
// Works with both Span and ExternalSpan
const externalSpan = Tracer.externalSpan({
spanId: "span-123",
traceId: "trace-456"
})
AnySpan): interface Layer<in ROut, out E = never, out RIn = never>A Layer describes how to build one or more services for dependency injection.
When to use
Use to model construction of application services for dependency injection,
especially when services have dependencies, can fail during construction, or
need scoped setup and release.
Details
A Layer<ROut, E, RIn> represents ROut as the services this layer
provides, E as the possible errors during layer construction, and RIn as
the services this layer requires as dependencies.
Layer<import TracerTracer.class ParentSpanclass ParentSpan {
Service: Service;
key: Identifier;
}
Context service containing the Span or ExternalSpan to use as the parent
of newly-created child spans.
Example (Accessing the parent span)
import { Effect, Tracer } from "effect"
// Access the parent span from the context
const program = Effect.gen(function*() {
const parentSpan = yield* Effect.service(Tracer.ParentSpan)
console.log(`Parent span: ${parentSpan.spanId}`)
})
ParentSpan> =>
const succeedContext: <A>(
context: Context.Context<A>
) => Layer<A>
Constructs a layer that provides all services in an already available
Context.
When to use
Use when you need a Layer built from an existing Context, including when
you need to provide multiple services at once.
Details
This is a more general version of succeed that allows you to provide
multiple services at once through a Context.
Example (Providing multiple services from a context)
import { Context, Effect, Layer } from "effect"
class Database extends Context.Service<Database, {
readonly query: (sql: string) => Effect.Effect<string>
}>()("Database") {}
class Logger extends Context.Service<Logger, {
readonly log: (msg: string) => Effect.Effect<void>
}>()("Logger") {}
const context = Context.make(Database, {
query: Effect.fn("Database.query")((sql: string) => Effect.succeed("result"))
}).pipe(
Context.add(Logger, {
log: (msg: string) => Effect.sync(() => console.log(msg))
})
)
const layer = Layer.succeedContext(context)
succeedContext(import TracerTracer.class ParentSpanclass ParentSpan {
key: Identifier;
of: (this: void, self: Tracer.AnySpan) => Tracer.AnySpan;
context: (self: Tracer.AnySpan) => Context.Context<Tracer.ParentSpan>;
use: (f: (service: Tracer.AnySpan) => Effect<A, E, R>) => Effect<A, E, Tracer.ParentSpan | R>;
useSync: (f: (service: Tracer.AnySpan) => A) => Effect<A, never, Tracer.ParentSpan>;
Identifier: Identifier;
Service: Shape;
stack: string | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
Context service containing the Span or ExternalSpan to use as the parent
of newly-created child spans.
Example (Accessing the parent span)
import { Effect, Tracer } from "effect"
// Access the parent span from the context
const program = Effect.gen(function*() {
const parentSpan = yield* Effect.service(Tracer.ParentSpan)
console.log(`Parent span: ${parentSpan.spanId}`)
})
ParentSpan.context(span: Tracer.AnySpanspan))