Hyperlinkv0.8.0-beta.28

Metric

Metric.withConstantInputconsteffect/Metric.ts:2906
<Input>(input: Input): <State>(
  self: Metric<Input, State>
) => Metric<unknown, State>
<Input, State>(self: Metric<Input, State>, input: Input): Metric<
  unknown,
  State
>

Returns a new metric that is powered by this one, but which accepts updates of any type, and translates them to updates with the specified constant update value.

Example (Ignoring inputs with a constant value)

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

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

// Create a counter that normally expects a number increment
const requestCounter = Metric.counter("total_requests", {
  description: "Total number of requests processed"
})

// Create a version that always increments by 1, regardless of input
const simpleRequestCounter = Metric.withConstantInput(requestCounter, 1)

const program = Effect.gen(function*() {
  // These all increment the counter by 1, ignoring the input value
  yield* Metric.update(simpleRequestCounter, "any string")
  yield* Metric.update(simpleRequestCounter, { complex: "object" })
  yield* Metric.update(simpleRequestCounter, 999) // Still increments by 1

  const value = yield* Metric.value(simpleRequestCounter)
  return value // Counter state will show count: 3
})
Input
Source effect/Metric.ts:29067 lines
export const withConstantInput: {
  <Input>(input: Input): <State>(self: Metric<Input, State>) => Metric<unknown, State>
  <Input, State>(self: Metric<Input, State>, input: Input): Metric<unknown, State>
} = dual<
  <Input>(input: Input) => <State>(self: Metric<Input, State>) => Metric<unknown, State>,
  <Input, State>(self: Metric<Input, State>, input: Input) => Metric<unknown, State>
>(2, (self, input) => mapInput(self, () => input))