The current registry snapshot, encoded — the single source of "take a snapshot" (the served
snapshot query and the live sampler both use it). Usable locally, without the resource.
export const const snapshotNow: Effect.Effect<MetricsSnapshot>const snapshotNow: {
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;
}
The current registry snapshot, encoded — the single source of "take a snapshot" (the served
snapshot query and the live sampler both use it). Usable locally, without the resource.
snapshotNow: import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<class MetricsSnapshotclass MetricsSnapshot {
ts: number;
metrics: ReadonlyArray<CounterDatum | GaugeDatum | HistogramDatum>;
}
A node's whole Metric registry, point-in-time — the served wire envelope.
MetricsSnapshot> = import EffectEffect.const all: <readonly [Effect.Effect<number, never, never>, Effect.Effect<readonly Metric.Metric<in Input, out State>.Snapshot[], never, never>], {
readonly concurrency?: Concurrency | undefined;
readonly discard?: boolean | undefined;
readonly mode?: "default" | "result" | undefined;
}>(arg: readonly [Effect.Effect<number, never, never>, Effect.Effect<readonly Metric.Metric.Snapshot[], never, never>], options?: {
readonly concurrency?: Concurrency | undefined;
readonly discard?: boolean | undefined;
readonly mode?: "default" | "result" | undefined;
} | undefined) => Effect.Effect<...>
Combines an iterable or record of effects into one effect whose success shape
follows the input.
When to use
Use to run a known collection of effects and collect results in the same
tuple, iterable, or record shape.
Details
Tuple and iterable inputs collect results in order. Record inputs collect
results under the same keys. By default, the combined effect fails on the
first failure; with concurrent execution, effects that have already started
may be interrupted, while effects not yet started are skipped.
Options:
Use concurrency to control sequential or concurrent execution. Use
mode: "result" to run every effect and collect each success or failure as a
Result in the same output shape. Use discard: true to ignore successful
values and return void.
Example (Collecting tuple results in order)
import { Console, Effect } from "effect"
const tupleOfEffects = [
Effect.succeed(42).pipe(Effect.tap(Console.log)),
Effect.succeed("Hello").pipe(Effect.tap(Console.log))
] as const
// ┌─── Effect<[number, string], never, never>
// ▼
const resultsAsTuple = Effect.all(tupleOfEffects)
Effect.runPromise(resultsAsTuple).then(console.log)
// Output:
// 42
// Hello
// [ 42, 'Hello' ]
Example (Collecting iterable results in order)
import { Console, Effect } from "effect"
const iterableOfEffects: Iterable<Effect.Effect<number>> = [1, 2, 3].map(
(n) => Effect.succeed(n).pipe(Effect.tap(Console.log))
)
// ┌─── Effect<number[], never, never>
// ▼
const resultsAsArray = Effect.all(iterableOfEffects)
Effect.runPromise(resultsAsArray).then(console.log)
// Output:
// 1
// 2
// 3
// [ 1, 2, 3 ]
Example (Collecting struct results by key)
import { Console, Effect } from "effect"
const structOfEffects = {
a: Effect.succeed(42).pipe(Effect.tap(Console.log)),
b: Effect.succeed("Hello").pipe(Effect.tap(Console.log))
}
// ┌─── Effect<{ a: number; b: string; }, never, never>
// ▼
const resultsAsStruct = Effect.all(structOfEffects)
Effect.runPromise(resultsAsStruct).then(console.log)
// Output:
// 42
// Hello
// { a: 42, b: 'Hello' }
Example (Collecting record results by key)
import { Console, Effect } from "effect"
const recordOfEffects: Record<string, Effect.Effect<number>> = {
key1: Effect.succeed(1).pipe(Effect.tap(Console.log)),
key2: Effect.succeed(2).pipe(Effect.tap(Console.log))
}
// ┌─── Effect<{ [x: string]: number; }, never, never>
// ▼
const resultsAsRecord = Effect.all(recordOfEffects)
Effect.runPromise(resultsAsRecord).then(console.log)
// Output:
// 1
// 2
// { key1: 1, key2: 2 }
Example (Stopping on the first failure)
import { Console, Effect } from "effect"
const program = Effect.all([
Effect.succeed("Task1").pipe(Effect.tap(Console.log)),
Effect.fail("Task2: Oh no!").pipe(Effect.tap(Console.log)),
// Won't execute due to earlier failure
Effect.succeed("Task3").pipe(Effect.tap(Console.log))
])
Effect.runPromiseExit(program).then(console.log)
// Output:
// Task1
// {
// _id: 'Exit',
// _tag: 'Failure',
// cause: { _id: 'Cause', _tag: 'Fail', failure: 'Task2: Oh no!' }
// }
all([
import ClockClock.const currentTimeMillis: Effect.Effect<
number,
never,
never
>
const currentTimeMillis: {
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;
}
Returns an Effect that succeeds with the current time in milliseconds.
When to use
Use to read wall-clock time from the active Clock service with millisecond
precision.
Example (Reading milliseconds)
import { Clock, Effect } from "effect"
const program = Effect.gen(function*() {
const currentTime = yield* Clock.currentTimeMillis
console.log(`Current time: ${currentTime}ms`)
return currentTime
})
currentTimeMillis,
import MetricMetric.const snapshot: Effect.Effect<
readonly Metric.Metric.Snapshot[],
never,
never
>
const snapshot: {
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;
}
Captures a snapshot of all registered metrics in the current context.
Details
Returns an array of metric snapshots, each containing the metric's metadata
(name, description, type) and current state (values, counts, etc.).
Example (Capturing metric snapshots)
import { Console, Data, Effect, Metric } from "effect"
class SnapshotError extends Data.TaggedError("SnapshotError")<{
readonly operation: string
}> {}
const program = Effect.gen(function*() {
// Create and update some metrics
const requestCounter = Metric.counter("http_requests", {
description: "Total HTTP requests"
})
const responseTime = Metric.histogram("response_time_ms", {
description: "Response time in milliseconds",
boundaries: Metric.linearBoundaries({ start: 0, width: 100, count: 5 })
})
// Update the metrics with some values
yield* Metric.update(requestCounter, 1)
yield* Metric.update(requestCounter, 1)
yield* Metric.update(responseTime, 150)
yield* Metric.update(responseTime, 75)
// Take a snapshot of all metrics
const snapshots = yield* Metric.snapshot
// Examine the snapshots
for (const snapshot of snapshots) {
yield* Console.log(`Metric: ${snapshot.id}`)
yield* Console.log(`Description: ${snapshot.description}`)
yield* Console.log(`Type: ${snapshot.type}`)
yield* Console.log(`State:`, snapshot.state)
}
return snapshots
})
snapshot,
]).Pipeable.pipe<Effect.Effect<[number, readonly Metric.Metric<in Input, out State>.Snapshot[]], never, never>, Effect.Effect<MetricsSnapshot, never, never>>(this: Effect.Effect<[number, readonly Metric.Metric.Snapshot[]], never, never>, ab: (_: Effect.Effect<[number, readonly Metric.Metric.Snapshot[]], never, never>) => Effect.Effect<MetricsSnapshot, never, never>): Effect.Effect<MetricsSnapshot, never, never> (+21 overloads)pipe(import EffectEffect.const map: <[number, readonly Metric.Metric<in Input, out State>.Snapshot[]], MetricsSnapshot>(f: (a: [number, readonly Metric.Metric.Snapshot[]]) => MetricsSnapshot) => <E, R>(self: Effect.Effect<[number, readonly Metric.Metric.Snapshot[]], E, R>) => Effect.Effect<MetricsSnapshot, E, R> (+1 overload)Transforms the value inside an effect by applying a function to it.
When to use
Use to transform an effect's success value with a function that returns a
plain value, producing a new effect without changing the original effect's
typed error or context requirements.
Details
map takes a function and applies it to the value contained within an
effect, creating a new effect with the transformed value.
It's important to note that effects are immutable, meaning that the original
effect is not modified. Instead, a new effect is returned with the updated
value.
Example (Choosing map syntax variants)
import { Effect, pipe } from "effect"
const myEffect = Effect.succeed(1)
const transformation = (n: number) => n + 1
const mappedWithPipe = pipe(myEffect, Effect.map(transformation))
const mappedWithDataFirst = Effect.map(myEffect, transformation)
const mappedWithMethod = myEffect.pipe(Effect.map(transformation))
Example (Adding a service charge)
import { Effect, pipe } from "effect"
const addServiceCharge = (amount: number) => amount + 1
const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
const finalAmount = pipe(
fetchTransactionAmount,
Effect.map(addServiceCharge)
)
Effect.runPromise(finalAmount).then(console.log)
// Output: 101
map(([ts: numberts, raw: readonly Metric.Metric.Snapshot[]raw]) => const encodeSnapshot: (
raw: ReadonlyArray<MetricSnapshotElem>,
ts: number
) => MetricsSnapshot
Encode a raw Metric.snapshot result + timestamp into the wire envelope. Pure.
encodeSnapshot(raw: readonly Metric.Metric.Snapshot[]raw, ts: numberts)));