effect-io-ai

Package: effect
Module: Metric

Metric.mapInput

Returns a new metric that is powered by this one, but which accepts updates of the specified new type, which must be transformable to the input type of this metric.

Example (Mapping metric inputs)

import { Data, Effect, Metric } from "effect"

class MetricError extends Data.TaggedError("MetricError")<{
  readonly operation: string
}> {}

// Create a histogram that expects Duration values
const durationHistogram = Metric.histogram("request_duration_ms", {
  description: "Request duration in milliseconds",
  boundaries: Metric.linearBoundaries({ start: 0, width: 100, count: 10 })
})

// Transform to accept number values representing milliseconds
const numberHistogram = Metric.mapInput(
  durationHistogram,
  (ms: number) => ms // Direct mapping from number to expected input
)

const program = Effect.gen(function*() {
  // Now we can update with a plain number
  yield* Metric.update(numberHistogram, 250)

  // Get metric value to see the recorded state
  const value = yield* Metric.value(numberHistogram)
  return value
})

Signature

declare const mapInput: { <Input, Input2 extends Input>(f: (input: Input2, context: Context.Context<never>) => Input): <State>(self: Metric<Input, State>) => Metric<Input2, State>; <Input, State, Input2>(self: Metric<Input, State>, f: (input: Input2, context: Context.Context<never>) => Input): Metric<Input2, State>; }

Source

Since v2.0.0