Hyperlinkv0.8.0-beta.28

Metric

Metric.mapInputconsteffect/Metric.ts:2843
<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>

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
})
mapping
Source effect/Metric.ts:284326 lines
export 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>
} = dual<
  <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>
>(2, <Input, State, Input2>(
  self: Metric<Input, State>,
  f: (input: Input2, context: Context.Context<never>) => Input
): Metric<Input2, State> =>
  new MetricTransform(
    self,
    (context) => self.valueUnsafe(context),
    (input, context) => self.updateUnsafe(f(input, context), context),
    (input, context) => self.modifyUnsafe(f(input, context), context)
  ))
Referenced by 3 symbols