<Input, State>(self: Metric<Input, State>): Effect<State>Retrieves the current state of the specified Metric.
Details
The returned state depends on the metric type. Counters return
CounterState<number | bigint> with count and incremental, gauges return
GaugeState<number | bigint> with value, frequencies return
FrequencyState with occurrences, histograms return HistogramState with
buckets, count, min, max, and sum, and summaries return SummaryState with
quantiles, count, min, max, and sum.
Example (Reading metric state)
import { Effect, Metric } from "effect"
const requestCounter = Metric.counter("requests")
const responseTime = Metric.histogram("response_time", {
boundaries: [100, 500, 1000, 2000]
})
const program = Effect.gen(function*() {
// Update metrics
yield* Metric.update(requestCounter, 1)
yield* Metric.update(responseTime, 750)
// Get current values
const counterState = yield* Metric.value(requestCounter)
console.log(`Request count: ${counterState.count}`)
const histogramState = yield* Metric.value(responseTime)
console.log(`Response time stats:`, {
count: histogramState.count,
min: histogramState.min,
max: histogramState.max,
average: histogramState.sum / histogramState.count
})
})export const const value: <Input, State>(
self: Metric<Input, State>
) => Effect<State>
Retrieves the current state of the specified Metric.
Details
The returned state depends on the metric type. Counters return
CounterState<number | bigint> with count and incremental, gauges return
GaugeState<number | bigint> with value, frequencies return
FrequencyState with occurrences, histograms return HistogramState with
buckets, count, min, max, and sum, and summaries return SummaryState with
quantiles, count, min, max, and sum.
Example (Reading metric state)
import { Effect, Metric } from "effect"
const requestCounter = Metric.counter("requests")
const responseTime = Metric.histogram("response_time", {
boundaries: [100, 500, 1000, 2000]
})
const program = Effect.gen(function*() {
// Update metrics
yield* Metric.update(requestCounter, 1)
yield* Metric.update(responseTime, 750)
// Get current values
const counterState = yield* Metric.value(requestCounter)
console.log(`Request count: ${counterState.count}`)
const histogramState = yield* Metric.value(responseTime)
console.log(`Response time stats:`, {
count: histogramState.count,
min: histogramState.min,
max: histogramState.max,
average: histogramState.sum / histogramState.count
})
})
value = <function (type parameter) Input in <Input, State>(self: Metric<Input, State>): Effect<State>Input, function (type parameter) State in <Input, State>(self: Metric<Input, State>): Effect<State>State>(
self: Metric<Input, State>(parameter) self: {
Input: Contravariant<Input>;
State: Covariant<State>;
id: string;
type: Metric.Type;
description: string | undefined;
attributes: Metric.AttributeSet | undefined;
valueUnsafe: (context: Context.Context<never>) => State;
updateUnsafe: (input: Input, context: Context.Context<never>) => void;
modifyUnsafe: (input: Input, context: Context.Context<never>) => void;
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; <…;
}
self: interface Metric<in Input, out State>A Metric<Input, State> represents a concurrent metric which accepts update
values of type Input and are aggregated to a value of type State.
Details
For example, a counter metric would have type Metric<number, number>,
representing the fact that the metric can be updated with numbers (the amount
to increment or decrement the counter by), and the state of the counter is a
number.
There are five primitive metric types supported by Effect:
- Counters
- Frequencies
- Gauges
- Histograms
- Summaries
Example (Using multiple metric types)
import { Data, Effect, Metric } from "effect"
class MetricExample extends Data.TaggedError("MetricExample")<{
readonly operation: string
}> {}
const program = Effect.gen(function*() {
// Create different types of metrics
const requestCounter: Metric.Counter<number> = Metric.counter("requests", {
description: "Total requests processed"
})
const memoryGauge: Metric.Gauge<number> = Metric.gauge("memory_usage", {
description: "Current memory usage in MB"
})
const statusFrequency: Metric.Frequency = Metric.frequency("status_codes", {
description: "HTTP status code frequency"
})
// All metrics share the same interface for updates and reads
yield* Metric.update(requestCounter, 1)
yield* Metric.update(memoryGauge, 128)
yield* Metric.update(statusFrequency, "200")
// All metrics can be read with Metric.value
const counterState = yield* Metric.value(requestCounter)
const gaugeState = yield* Metric.value(memoryGauge)
const frequencyState = yield* Metric.value(statusFrequency)
// Metrics have common properties accessible through the interface:
// - id: unique identifier
// - type: metric type ("Counter", "Gauge", "Frequency", etc.)
// - description: optional human-readable description
// - attributes: optional key-value attributes for tagging
return {
counter: {
id: requestCounter.id,
type: requestCounter.type,
state: counterState
},
gauge: { id: memoryGauge.id, type: memoryGauge.type, state: gaugeState },
frequency: {
id: statusFrequency.id,
type: statusFrequency.type,
state: frequencyState
}
}
})
The Metric namespace provides a comprehensive system for collecting, aggregating, and observing
application metrics in Effect applications.
Example (Collecting application metrics)
import { Data, Effect, Metric } from "effect"
class MetricsError extends Data.TaggedError("MetricsError")<{
readonly operation: string
}> {}
const program = Effect.gen(function*() {
// Create different types of metrics
const requestCounter = Metric.counter("http_requests_total")
const responseTimeHistogram = Metric.histogram("http_response_time", {
boundaries: Metric.linearBoundaries({ start: 0, width: 10, count: 10 })
})
const activeConnectionsGauge = Metric.gauge("active_connections")
const statusFrequency = Metric.frequency("http_status_codes")
// Update metrics
yield* Metric.update(requestCounter, 1)
yield* Metric.update(responseTimeHistogram, 45.2)
yield* Metric.update(activeConnectionsGauge, 12)
yield* Metric.update(statusFrequency, "200")
// Get metric values
const counterValue = yield* Metric.value(requestCounter)
const histogramValue = yield* Metric.value(responseTimeHistogram)
const gaugeValue = yield* Metric.value(activeConnectionsGauge)
const frequencyValue = yield* Metric.value(statusFrequency)
return {
counter: counterValue,
histogram: histogramValue,
gauge: gaugeValue,
frequency: frequencyValue
}
})
Metric<function (type parameter) Input in <Input, State>(self: Metric<Input, State>): Effect<State>Input, function (type parameter) State in <Input, State>(self: Metric<Input, State>): Effect<State>State>
): import EffectEffect<function (type parameter) State in <Input, State>(self: Metric<Input, State>): Effect<State>State> =>
import InternalEffectInternalEffect.const flatMap: {
<A, B, E2, R2>(
f: (a: A) => Effect.Effect<B, E2, R2>
): <E, R>(
self: Effect.Effect<A, E, R>
) => Effect.Effect<B, E | E2, R | R2>
<A, E, R, B, E2, R2>(
self: Effect.Effect<A, E, R>,
f: (a: A) => Effect.Effect<B, E2, R2>
): Effect.Effect<B, E | E2, R | R2>
}
flatMap(
import InternalEffectInternalEffect.const context: <
R = never
>() => Effect.Effect<Context.Context<R>>
context(),
(context: Context.Context<never>(parameter) context: {
mapUnsafe: ReadonlyMap<string, any>;
mutable: boolean;
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) => import InternalEffectInternalEffect.const sync: <A>(
thunk: LazyArg<A>
) => Effect.Effect<A>
sync(() => self: Metric<Input, State>(parameter) self: {
Input: Contravariant<Input>;
State: Covariant<State>;
id: string;
type: Metric.Type;
description: string | undefined;
attributes: Metric.AttributeSet | undefined;
valueUnsafe: (context: Context.Context<never>) => State;
updateUnsafe: (input: Input, context: Context.Context<never>) => void;
modifyUnsafe: (input: Input, context: Context.Context<never>) => void;
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; <…;
}
self.Metric<Input, State>.valueUnsafe: (context: Context.Context<never>) => StatevalueUnsafe(context: Context.Context<never>(parameter) context: {
mapUnsafe: ReadonlyMap<string, any>;
mutable: boolean;
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))
)