Context.Reference<boolean>Context reference for controlling whether tracing is enabled globally. When set to false, spans will not be registered with the tracer and tracing overhead is minimized.
When to use
Use to disable or re-enable span registration in the current context.
Example (Toggling tracing)
import { Effect, References } from "effect"
const tracingControl = Effect.gen(function*() {
// Check if tracing is enabled (default is true)
const current = yield* References.TracerEnabled
console.log(current) // true
// Disable tracing globally
yield* Effect.provideService(
Effect.gen(function*() {
const isEnabled = yield* References.TracerEnabled
console.log(isEnabled) // false
// Spans will not be traced in this context
yield* Effect.log("This will not be traced")
}),
References.TracerEnabled,
false
)
// Re-enable tracing
yield* Effect.provideService(
Effect.gen(function*() {
const isEnabled = yield* References.TracerEnabled
console.log(isEnabled) // true
// All subsequent spans will be traced
yield* Effect.log("This will be traced")
}),
References.TracerEnabled,
true
)
})export const const TracerEnabled: Context.Reference<boolean>const TracerEnabled: {
defaultValue: () => Shape;
of: (this: void, self: boolean) => boolean;
context: (self: boolean) => Context.Context<never>;
use: (f: (service: boolean) => Effect<A, E, R>) => Effect<A, E, R>;
useSync: (f: (service: boolean) => A) => Effect<A, never, never>;
Identifier: Identifier;
Service: Shape;
key: string;
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 reference for controlling whether tracing is enabled globally. When set to false,
spans will not be registered with the tracer and tracing overhead is minimized.
When to use
Use to disable or re-enable span registration in the current context.
Example (Toggling tracing)
import { Effect, References } from "effect"
const tracingControl = Effect.gen(function*() {
// Check if tracing is enabled (default is true)
const current = yield* References.TracerEnabled
console.log(current) // true
// Disable tracing globally
yield* Effect.provideService(
Effect.gen(function*() {
const isEnabled = yield* References.TracerEnabled
console.log(isEnabled) // false
// Spans will not be traced in this context
yield* Effect.log("This will not be traced")
}),
References.TracerEnabled,
false
)
// Re-enable tracing
yield* Effect.provideService(
Effect.gen(function*() {
const isEnabled = yield* References.TracerEnabled
console.log(isEnabled) // true
// All subsequent spans will be traced
yield* Effect.log("This will be traced")
}),
References.TracerEnabled,
true
)
})
TracerEnabled: import ContextContext.interface Reference<in out Shape>Service key with a lazily computed default value.
Details
When a Reference is requested from a Context that does not contain an
override, Context getters that resolve references return the cached default
value instead of failing.
Example (Defining a reference with a default value)
import { Context } from "effect"
// Define a reference with a default value
const LoggerRef: Context.Reference<{ log: (msg: string) => void }> =
Context.Reference("Logger", {
defaultValue: () => ({ log: (msg: string) => console.log(msg) })
})
// The reference can be used without explicit provision
const context = Context.empty()
const logger = Context.get(context, LoggerRef) // Uses default value
Creates a context key with a default value.
When to use
Use when you need to define a context key with a lazily computed default
value.
Details
Context.Reference allows you to create a key that can hold a value. You
can provide a default value for the service, which will automatically be used
when the context is accessed, or override it with a custom implementation
when needed. The default value is computed lazily and cached on the
reference.
Example (Creating references with default values)
import { Context } from "effect"
// Create a reference with a default value
const LoggerRef = Context.Reference("Logger", {
defaultValue: () => ({ log: (msg: string) => console.log(msg) })
})
// The reference provides the default value when accessed from an empty context
const context = Context.empty()
const logger = Context.get(context, LoggerRef)
// You can also override the default value
const customContext = Context.make(LoggerRef, {
log: (msg: string) => `Custom: ${msg}`
})
const customLogger = Context.get(customContext, LoggerRef)
Reference<boolean> = import referencesreferences.const TracerEnabled: Context.Reference<boolean>const TracerEnabled: {
defaultValue: () => Shape;
of: (this: void, self: boolean) => boolean;
context: (self: boolean) => Context.Context<never>;
use: (f: (service: boolean) => Effect<A, E, R>) => Effect<A, E, R>;
useSync: (f: (service: boolean) => A) => Effect<A, never, never>;
Identifier: Identifier;
Service: Shape;
key: string;
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;
}
TracerEnabled