Context.Reference<FiberRuntimeMetricsService | undefined>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))
})export const 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 = import ContextContext.const Reference: <Service>(
key: string,
options: {
readonly defaultValue: () => Service
}
) => Reference<Service>
Creates a context key with a default value.
When to use
Use when you need to define a context key with a lazily computed default
value.
Details
Context.Reference allows you to create a key that can hold a value. You
can provide a default value for the service, which will automatically be used
when the context is accessed, or override it with a custom implementation
when needed. The default value is computed lazily and cached on the
reference.
Example (Creating references with default values)
import { Context } from "effect"
// Create a reference with a default value
const LoggerRef = Context.Reference("Logger", {
defaultValue: () => ({ log: (msg: string) => console.log(msg) })
})
// The reference provides the default value when accessed from an empty context
const context = Context.empty()
const logger = Context.get(context, LoggerRef)
// You can also override the default value
const customContext = Context.make(LoggerRef, {
log: (msg: string) => `Custom: ${msg}`
})
const customLogger = Context.get(customContext, LoggerRef)
Reference<FiberRuntimeMetricsService | undefined>(
import InternalMetricInternalMetric.const FiberRuntimeMetricsKey: "effect/observability/Metric/FiberRuntimeMetricsKey"FiberRuntimeMetricsKey,
{ defaultValue: LazyArg<undefined>defaultValue: const constUndefined: LazyArg<undefined>Returns undefined when called.
When to use
Use when you need a thunk that returns undefined on every invocation.
Example (Returning undefined from a thunk)
import { Function } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(Function.constUndefined(), undefined)
constUndefined }
)