effect-io-ai

Package: effect
Module: Metric

Metric.snapshot

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
})

Signature

declare const snapshot: Effect<ReadonlyArray<Metric.Snapshot>, never, never>

Source

Since v2.0.0