Hyperlinkv0.8.0-beta.28

Metric

Metric.FiberRuntimeMetricsconsteffect/Metric.ts:3622
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))
})
runtime metrics
Source effect/Metric.ts:36224 lines
export const FiberRuntimeMetrics = Context.Reference<FiberRuntimeMetricsService | undefined>(
  InternalMetric.FiberRuntimeMetricsKey,
  { defaultValue: constUndefined }
)
Referenced by 4 symbols