<A, E, R>(self: Effect<A, E, R>): Effect<A, E, R>Disables automatic collection of fiber runtime metrics for the provided Effect.
When to use
Use when you need to disable runtime metrics for a specific effect while keeping them enabled elsewhere.
Example (Disabling runtime metrics for an effect)
import { Console, Data, Effect, Layer, Metric } from "effect"
class DisableMetricsError extends Data.TaggedError("DisableMetricsError")<{
readonly operation: string
}> {}
const program = Effect.gen(function*() {
// This section will have runtime metrics enabled
const normalOperation = Effect.gen(function*() {
const tasks = Array.from({ length: 5 }, (_, i) =>
Effect.gen(function*() {
yield* Effect.sleep(`${100 + i * 20} millis`)
return `Normal task ${i} completed`
}))
return yield* Effect.all(tasks, { concurrency: 3 })
})
// This section will have runtime metrics disabled for performance
const highPerformanceOperation = Metric.disableRuntimeMetrics(
Effect.gen(function*() {
// Performance-critical code where metrics overhead should be avoided
const hotPath = Array.from(
{ length: 1000 },
(_, i) =>
Effect.gen(function*() {
// Simulate intensive computation
const result = i * i + (i % 10) / 10
return result
})
)
return yield* Effect.all(hotPath, { concurrency: 100 })
})
)
yield* Console.log("Running operations with selective metrics...")
// Run both operations
const [normalResults, performanceResults] = yield* Effect.all([
normalOperation, // Will generate fiber metrics
highPerformanceOperation // Will NOT generate fiber metrics
])
// Check collected metrics - should only see metrics from normalOperation
const metrics = yield* Metric.snapshot
const runtimeMetrics = metrics.filter((m) => m.id.startsWith("child_fiber"))
yield* Console.log(`Normal operation results: ${normalResults.length}`)
yield* Console.log(
`Performance operation results: ${performanceResults.length}`
)
yield* Console.log(`Runtime metrics collected: ${runtimeMetrics.length}`)
// The runtime metrics will only reflect the fibers from normalOperation
// The highPerformanceOperation fibers were not tracked due to disableRuntimeMetrics
return { normalResults, performanceResults, runtimeMetrics }
})
// Enable runtime metrics globally, then selectively disable where needed
const BaseAppLayer = Layer.empty // Your base application layers
const AppLayerWithMetrics = BaseAppLayer.pipe(
Layer.provide(Metric.enableRuntimeMetricsLayer)
)
const finalProgram = program.pipe(
Effect.provide(AppLayerWithMetrics)
)export const const disableRuntimeMetrics: <A, E, R>(
self: Effect<A, E, R>
) => Effect<A, E, R>
Disables automatic collection of fiber runtime metrics for the provided Effect.
When to use
Use when you need to disable runtime metrics for a specific effect while
keeping them enabled elsewhere.
Example (Disabling runtime metrics for an effect)
import { Console, Data, Effect, Layer, Metric } from "effect"
class DisableMetricsError extends Data.TaggedError("DisableMetricsError")<{
readonly operation: string
}> {}
const program = Effect.gen(function*() {
// This section will have runtime metrics enabled
const normalOperation = Effect.gen(function*() {
const tasks = Array.from({ length: 5 }, (_, i) =>
Effect.gen(function*() {
yield* Effect.sleep(`${100 + i * 20} millis`)
return `Normal task ${i} completed`
}))
return yield* Effect.all(tasks, { concurrency: 3 })
})
// This section will have runtime metrics disabled for performance
const highPerformanceOperation = Metric.disableRuntimeMetrics(
Effect.gen(function*() {
// Performance-critical code where metrics overhead should be avoided
const hotPath = Array.from(
{ length: 1000 },
(_, i) =>
Effect.gen(function*() {
// Simulate intensive computation
const result = i * i + (i % 10) / 10
return result
})
)
return yield* Effect.all(hotPath, { concurrency: 100 })
})
)
yield* Console.log("Running operations with selective metrics...")
// Run both operations
const [normalResults, performanceResults] = yield* Effect.all([
normalOperation, // Will generate fiber metrics
highPerformanceOperation // Will NOT generate fiber metrics
])
// Check collected metrics - should only see metrics from normalOperation
const metrics = yield* Metric.snapshot
const runtimeMetrics = metrics.filter((m) => m.id.startsWith("child_fiber"))
yield* Console.log(`Normal operation results: ${normalResults.length}`)
yield* Console.log(
`Performance operation results: ${performanceResults.length}`
)
yield* Console.log(`Runtime metrics collected: ${runtimeMetrics.length}`)
// The runtime metrics will only reflect the fibers from normalOperation
// The highPerformanceOperation fibers were not tracked due to disableRuntimeMetrics
return { normalResults, performanceResults, runtimeMetrics }
})
// Enable runtime metrics globally, then selectively disable where needed
const BaseAppLayer = Layer.empty // Your base application layers
const AppLayerWithMetrics = BaseAppLayer.pipe(
Layer.provide(Metric.enableRuntimeMetricsLayer)
)
const finalProgram = program.pipe(
Effect.provide(AppLayerWithMetrics)
)
disableRuntimeMetrics: <function (type parameter) A in <A, E, R>(self: Effect<A, E, R>): Effect<A, E, R>A, function (type parameter) E in <A, E, R>(self: Effect<A, E, R>): Effect<A, E, R>E, function (type parameter) R in <A, E, R>(self: Effect<A, E, R>): Effect<A, E, R>R>(self: Effect<A, E, R>(parameter) self: {
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;
}
self: import EffectEffect<function (type parameter) A in <A, E, R>(self: Effect<A, E, R>): Effect<A, E, R>A, function (type parameter) E in <A, E, R>(self: Effect<A, E, R>): Effect<A, E, R>E, function (type parameter) R in <A, E, R>(self: Effect<A, E, R>): Effect<A, E, R>R>) => import EffectEffect<function (type parameter) A in <A, E, R>(self: Effect<A, E, R>): Effect<A, E, R>A, function (type parameter) E in <A, E, R>(self: Effect<A, E, R>): Effect<A, E, R>E, function (type parameter) R in <A, E, R>(self: Effect<A, E, R>): Effect<A, E, R>R> = import InternalEffectInternalEffect.const provideService: {
<I, S>(service: Context.Key<I, S>): {
(implementation: S): <A, E, R>(
self: Effect.Effect<A, E, R>
) => Effect.Effect<A, E, Exclude<R, I>>
<A, E, R>(
self: Effect.Effect<A, E, R>,
implementation: S
): Effect.Effect<A, E, Exclude<R, I>>
}
<I, S>(
key: Context.Key<I, S>,
implementation: S
): <A, E, R>(
self: Effect.Effect<A, E, R>
) => Effect.Effect<A, E, Exclude<R, I>>
<A, E, R, I, S>(
self: Effect.Effect<A, E, R>,
service: Context.Key<I, S>,
implementation: S
): Effect.Effect<A, E, Exclude<R, I>>
}
provideService(
const FiberRuntimeMetrics: Context.Reference<
FiberRuntimeMetricsService | undefined
>
const FiberRuntimeMetrics: {
defaultValue: () => Shape;
of: (this: void, self: FiberRuntimeMetricsService | undefined) => FiberRuntimeMetricsService | undefined;
context: (self: FiberRuntimeMetricsService | undefined) => Context.Context<never>;
use: (f: (service: FiberRuntimeMetricsService | undefined) => Effect<A, E, R>) => Effect<A, E, R>;
useSync: (f: (service: FiberRuntimeMetricsService | undefined) => 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 the optional service that records fiber runtime
metrics.
When to use
Use to provide or inspect the service that receives fiber start and end
notifications for automatic runtime metrics.
Details
When provided, the runtime can notify the service about child-fiber start and
end events. When the reference is undefined, automatic fiber runtime metric
collection is disabled.
Example (Accessing the fiber runtime metrics service)
import { Data, Effect, Metric } from "effect"
class MetricsError extends Data.TaggedError("MetricsError")<{
readonly operation: string
}> {}
const program = Effect.gen(function*() {
// Access the fiber runtime metrics service
const metricsService = yield* Metric.FiberRuntimeMetrics
if (metricsService) {
console.log("Runtime metrics are enabled")
} else {
console.log("Runtime metrics are disabled")
}
// Enable runtime metrics for the application
const enabledLayer = Metric.enableRuntimeMetricsLayer
return yield* Effect.gen(function*() {
// Create some concurrent fibers to see metrics in action
yield* Effect.all([
Effect.sleep("100 millis"),
Effect.sleep("200 millis"),
Effect.sleep("300 millis")
], { concurrency: "unbounded" })
// Create test metrics to demonstrate the service
const testCounter = Metric.counter("test_counter")
yield* Metric.update(testCounter, 5)
const counterValue = yield* Metric.value(testCounter)
return { counterValue, metricsEnabled: true }
}).pipe(Effect.provide(enabledLayer))
})
FiberRuntimeMetrics,
var undefinedundefined
)