Counter<Input>A Counter metric that tracks cumulative values that typically only increase.
When to use
Use when counters are useful for tracking monotonically increasing values like request counts, bytes processed, errors encountered, or any value that accumulates over time.
Example (Using counter metrics)
import { Data, Effect, Metric } from "effect"
class CounterInterfaceError extends Data.TaggedError("CounterInterfaceError")<{
readonly operation: string
}> {}
const program = Effect.gen(function*() {
// Create different types of counters
const requestCounter: Metric.Counter<number> = Metric.counter(
"http_requests",
{
description: "Total HTTP requests processed",
incremental: true // Only allows increments
}
)
const bytesCounter: Metric.Counter<bigint> = Metric.counter(
"bytes_processed",
{
description: "Total bytes processed",
bigint: true,
attributes: { service: "data-processor" }
}
)
// Update counters
yield* Metric.update(requestCounter, 1) // Increment by 1
yield* Metric.update(requestCounter, 5) // Increment by 5 (total: 6)
yield* Metric.update(bytesCounter, 1024n) // Add 1024 bytes
// Read counter state
const requestState: Metric.CounterState<number> = yield* Metric.value(
requestCounter
)
const bytesState: Metric.CounterState<bigint> = yield* Metric.value(
bytesCounter
)
// Counter state contains:
// - count: current accumulated value
// - incremental: whether only increments are allowed
return {
requests: {
count: requestState.count,
incremental: requestState.incremental
},
bytes: { count: bytesState.count, incremental: bytesState.incremental }
}
})export interface interface Counter<in Input extends number | bigint>A Counter metric that tracks cumulative values that typically only increase.
When to use
Use when counters are useful for tracking monotonically increasing values like request counts,
bytes processed, errors encountered, or any value that accumulates over time.
Example (Using counter metrics)
import { Data, Effect, Metric } from "effect"
class CounterInterfaceError extends Data.TaggedError("CounterInterfaceError")<{
readonly operation: string
}> {}
const program = Effect.gen(function*() {
// Create different types of counters
const requestCounter: Metric.Counter<number> = Metric.counter(
"http_requests",
{
description: "Total HTTP requests processed",
incremental: true // Only allows increments
}
)
const bytesCounter: Metric.Counter<bigint> = Metric.counter(
"bytes_processed",
{
description: "Total bytes processed",
bigint: true,
attributes: { service: "data-processor" }
}
)
// Update counters
yield* Metric.update(requestCounter, 1) // Increment by 1
yield* Metric.update(requestCounter, 5) // Increment by 5 (total: 6)
yield* Metric.update(bytesCounter, 1024n) // Add 1024 bytes
// Read counter state
const requestState: Metric.CounterState<number> = yield* Metric.value(
requestCounter
)
const bytesState: Metric.CounterState<bigint> = yield* Metric.value(
bytesCounter
)
// Counter state contains:
// - count: current accumulated value
// - incremental: whether only increments are allowed
return {
requests: {
count: requestState.count,
incremental: requestState.incremental
},
bytes: { count: bytesState.count, incremental: bytesState.incremental }
}
})
Counter<in function (type parameter) Input in Counter<in Input extends number | bigint>Input extends number | bigint> extends 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 Counter<in Input extends number | bigint>Input, interface CounterState<in Input extends number | bigint>State interface for Counter metrics containing the current count and increment mode.
Example (Reading counter state)
import { Data, Effect, Metric } from "effect"
class CounterStateError extends Data.TaggedError("CounterStateError")<{
readonly operation: string
}> {}
const program = Effect.gen(function*() {
// Create different types of counters
const requestCounter = Metric.counter("http_requests_total")
const errorCounter = Metric.counter("errors_total", { incremental: true })
const byteCounter = Metric.counter("bytes_processed", { bigint: true })
// Update counters
yield* Metric.update(requestCounter, 5) // Add 5 requests
yield* Metric.update(requestCounter, -2) // Subtract 2 (allowed for non-incremental)
yield* Metric.update(errorCounter, 3) // Add 3 errors
yield* Metric.update(errorCounter, -1) // Attempt to subtract (ignored for incremental)
yield* Metric.update(byteCounter, 1024000n) // Add bytes as bigint
// Read counter states
const requestState: Metric.CounterState<number> = yield* Metric.value(
requestCounter
)
const errorState: Metric.CounterState<number> = yield* Metric.value(
errorCounter
)
const byteState: Metric.CounterState<bigint> = yield* Metric.value(
byteCounter
)
// CounterState contains:
// - count: current count value (number or bigint based on counter type)
// - incremental: whether counter only allows increases
return {
requests: {
total: requestState.count, // 3 (5 - 2, decrements allowed)
canDecrease: !requestState.incremental // true
},
errors: {
total: errorState.count, // 3 (subtract ignored)
canDecrease: !errorState.incremental // false
},
bytes: {
total: byteState.count, // 1024000n
canDecrease: !byteState.incremental // true
}
}
})
CounterState<function (type parameter) Input in Counter<in Input extends number | bigint>Input>> {}