/** * @since 1.0.0 */ import type * as Chunk from "@effect/data/Chunk" import type * as Duration from "@effect/data/Duration" import type { LazyArg } from "@effect/data/Function" import type * as HashSet from "@effect/data/HashSet" import type { Pipeable } from "@effect/data/Pipeable" import type * as Effect from "@effect/io/Effect" import * as fiberRuntime from "@effect/io/internal/fiberRuntime" import * as internal from "@effect/io/internal/metric" import type * as MetricBoundaries from "@effect/io/MetricBoundaries" import type * as MetricKey from "@effect/io/MetricKey" import type * as MetricKeyType from "@effect/io/MetricKeyType" import type * as MetricLabel from "@effect/io/MetricLabel" import type * as MetricPair from "@effect/io/MetricPair" import type * as MetricRegistry from "@effect/io/MetricRegistry" import type * as MetricState from "@effect/io/MetricState" /** * @since 1.0.0 * @category symbols */ export const MetricTypeId: unique symbol = internal.MetricTypeId /** * @since 1.0.0 * @category symbols */ export type MetricTypeId = typeof MetricTypeId /** * A `Metric` represents a concurrent metric which accepts * updates of type `In` and are aggregated to a stateful value of type `Out`. * * For example, a counter metric would have type `Metric`, * representing the fact that the metric can be updated with numbers (the amount * to increment or decrement the counter by), and the state of the counter is a * number. * * There are five primitive metric types supported by Effect: * * - Counters * - Frequencies * - Gauges * - Histograms * - Summaries * * @since 1.0.0 * @category models */ export interface Metric extends Metric.Variance, Pipeable { /** * The type of the underlying primitive metric. For example, this could be * `MetricKeyType.Counter` or `MetricKeyType.Gauge`. */ readonly keyType: Type readonly unsafeUpdate: (input: In, extraTags: HashSet.HashSet) => void readonly unsafeValue: (extraTags: HashSet.HashSet) => Out /** */ (effect: Effect.Effect): Effect.Effect } /** * @since 1.0.0 * @category models */ export interface MetricApply { ( keyType: Type, unsafeUpdate: (input: In, extraTags: HashSet.HashSet) => void, unsafeValue: (extraTags: HashSet.HashSet) => Out ): Metric } /** * @since 1.0.0 */ export declare namespace Metric { /** * @since 1.0.0 * @category models */ export interface Counter extends Metric {} /** * @since 1.0.0 * @category models */ export interface Gauge extends Metric {} /** * @since 1.0.0 * @category models */ export interface Frequency extends Metric {} /** * @since 1.0.0 * @category models */ export interface Histogram extends Metric {} /** * @since 1.0.0 * @category models */ export interface Summary extends Metric {} /** * @since 1.0.0 * @category models */ export interface Variance { readonly [MetricTypeId]: { readonly _Type: (_: Type) => Type readonly _In: (_: In) => void readonly _Out: (_: never) => Out } } } /** * @since 1.0.0 * @category globals */ export const globalMetricRegistry: MetricRegistry.MetricRegistry = internal.globalMetricRegistry /** * @since 1.0.0 * @category constructors */ export const make: MetricApply = internal.make /** * 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. * * @since 1.0.0 * @category mapping */ export const mapInput: { (f: (input: In2) => In): (self: Metric) => Metric (self: Metric, f: (input: In2) => In): Metric } = internal.mapInput /** * A counter, which can be incremented by numbers. * * @since 1.0.0 * @category constructors */ export const counter: (name: string, description?: string) => Metric.Counter = internal.counter /** * A string histogram metric, which keeps track of the counts of different * strings. * * @since 1.0.0 * @category constructors */ export const frequency: (name: string, description?: string) => Metric.Frequency = internal.frequency /** * 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. * * @since 1.0.0 * @category constructors */ export const withConstantInput: { (input: In): (self: Metric) => Metric (self: Metric, input: In): Metric } = internal.withConstantInput /** * @since 1.0.0 * @category constructors */ export const fromMetricKey: >( key: MetricKey.MetricKey ) => Metric, MetricKeyType.MetricKeyType.OutType> = internal.fromMetricKey /** * A gauge, which can be set to a value. * * @since 1.0.0 * @category constructors */ export const gauge: (name: string, description?: string) => Metric.Gauge = internal.gauge /** * A numeric histogram metric, which keeps track of the count of numbers that * fall in bins with the specified boundaries. * * @since 1.0.0 * @category constructors */ export const histogram: ( name: string, boundaries: MetricBoundaries.MetricBoundaries, description?: string ) => Metric = internal.histogram /** * @since 1.0.0 * @category aspects */ export const increment: (self: Metric.Counter) => Effect.Effect = internal.increment /** * @since 1.0.0 * @category aspects */ export const incrementBy: { (amount: number): (self: Metric.Counter) => Effect.Effect (self: Metric.Counter, amount: number): Effect.Effect } = internal.incrementBy /** * Returns a new metric that is powered by this one, but which outputs a new * state type, determined by transforming the state type of this metric by the * specified function. * * @since 1.0.0 * @category mapping */ export const map: { (f: (out: Out) => Out2): (self: Metric) => Metric (self: Metric, f: (out: Out) => Out2): Metric } = internal.map /** * @since 1.0.0 * @category mapping */ export const mapType: { (f: (type: Type) => Type2): (self: Metric) => Metric (self: Metric, f: (type: Type) => Type2): Metric } = internal.mapType /** * @since 1.0.0 * @category aspects */ export const set: { (value: In): (self: Metric.Gauge) => Effect.Effect (self: Metric.Gauge, value: In): Effect.Effect } = internal.set /** * Captures a snapshot of all metrics recorded by the application. * * @since 1.0.0 * @category getters */ export const snapshot: Effect.Effect> = internal.snapshot /** * Creates a metric that ignores input and produces constant output. * * @since 1.0.0 * @category constructors */ export const succeed: (out: Out) => Metric = internal.succeed /** * Creates a metric that ignores input and produces constant output. * * @since 1.0.0 * @category constructors */ export const sync: (evaluate: LazyArg) => Metric = internal.sync /** * @since 1.0.0 * @category constructors */ export const summary: ( options: { readonly name: string readonly maxAge: Duration.DurationInput readonly maxSize: number readonly error: number readonly quantiles: Chunk.Chunk readonly description?: string } ) => Metric.Summary = internal.summary /** * @since 1.0.0 * @category constructors */ export const summaryTimestamp: ( options: { readonly name: string readonly maxAge: Duration.DurationInput readonly maxSize: number readonly error: number readonly quantiles: Chunk.Chunk readonly description?: string } ) => Metric.Summary = internal.summaryTimestamp /** * Returns a new metric, which is identical in every way to this one, except * the specified tags have been added to the tags of this metric. * * @since 1.0.0 * @category utils */ export const tagged: { (key: string, value: string): (self: Metric) => Metric (self: Metric, key: string, value: string): Metric } = internal.tagged /** * Returns a new metric, which is identical in every way to this one, except * dynamic tags are added based on the update values. Note that the metric * returned by this method does not return any useful information, due to the * dynamic nature of the added tags. * * @since 1.0.0 * @category utils */ export const taggedWithLabelsInput: { ( f: (input: In) => Iterable ): (self: Metric) => Metric ( self: Metric, f: (input: In) => Iterable ): Metric } = internal.taggedWithLabelsInput /** * Returns a new metric, which is identical in every way to this one, except * the specified tags have been added to the tags of this metric. * * @since 1.0.0 * @category utils */ export const taggedWithLabels: { (extraTags: Iterable): (self: Metric) => Metric (self: Metric, extraTags: Iterable): Metric } = internal.taggedWithLabels /** * Creates a timer metric, based on a histogram, which keeps track of * durations in milliseconds. The unit of time will automatically be added to * the metric as a tag (i.e. `"time_unit: milliseconds"`). * * @since 1.0.0 * @category constructors */ export const timer: ( name: string ) => Metric = internal.timer /** * Creates a timer metric, based on a histogram created from the provided * boundaries, which keeps track of durations in milliseconds. The unit of time * will automatically be added to the metric as a tag (i.e. * `"time_unit: milliseconds"`). * * @since 1.0.0 * @category constructors */ export const timerWithBoundaries: ( name: string, boundaries: Chunk.Chunk ) => Metric = internal.timerWithBoundaries /** * Returns an aspect that will update this metric with the specified constant * value every time the aspect is applied to an effect, regardless of whether * that effect fails or succeeds. * * @since 1.0.0 * @category aspects */ export const trackAll: { ( input: In ): (self: Metric) => (effect: Effect.Effect) => Effect.Effect ( self: Metric, input: In ): (effect: Effect.Effect) => Effect.Effect } = internal.trackAll /** * Returns an aspect that will update this metric with the defects of the * effects that it is applied to. * * @since 1.0.0 * @category aspects */ export const trackDefect: { (metric: Metric): (self: Effect.Effect) => Effect.Effect (self: Effect.Effect, metric: Metric): Effect.Effect } = internal.trackDefect /** * Returns an aspect that will update this metric with the result of applying * the specified function to the defect throwables of the effects that the * aspect is applied to. * * @since 1.0.0 * @category aspects */ export const trackDefectWith: { ( metric: Metric, f: (defect: unknown) => In ): (self: Effect.Effect) => Effect.Effect ( self: Effect.Effect, metric: Metric, f: (defect: unknown) => In ): Effect.Effect } = internal.trackDefectWith /** * Returns an aspect that will update this metric with the duration that the * effect takes to execute. To call this method, the input type of the metric * must be `Duration`. * * @since 1.0.0 * @category aspects */ export const trackDuration: { ( metric: Metric ): (self: Effect.Effect) => Effect.Effect ( self: Effect.Effect, metric: Metric ): Effect.Effect } = internal.trackDuration /** * Returns an aspect that will update this metric with the duration that the * effect takes to execute. To call this method, you must supply a function * that can convert the `Duration` to the input type of this metric. * * @since 1.0.0 * @category aspects */ export const trackDurationWith: { ( metric: Metric, f: (duration: Duration.Duration) => In ): (effect: Effect.Effect) => Effect.Effect ( self: Effect.Effect, metric: Metric, f: (duration: Duration.Duration) => In ): Effect.Effect } = internal.trackDurationWith /** * Returns an aspect that will update this metric with the failure value of * the effects that it is applied to. * * @since 1.0.0 * @category aspects */ export const trackError: { ( metric: Metric ): (self: Effect.Effect) => Effect.Effect ( self: Effect.Effect, metric: Metric ): Effect.Effect } = internal.trackError /** * Returns an aspect that will update this metric with the result of applying * the specified function to the error value of the effects that the aspect is * applied to. * * @since 1.0.0 * @category aspects */ export const trackErrorWith: { ( metric: Metric, f: (error: In2) => In ): (effect: Effect.Effect) => Effect.Effect ( self: Effect.Effect, metric: Metric, f: (error: In2) => In ): Effect.Effect } = internal.trackErrorWith /** * Returns an aspect that will update this metric with the success value of * the effects that it is applied to. * * @since 1.0.0 * @category aspects */ export const trackSuccess: { ( metric: Metric ): (self: Effect.Effect) => Effect.Effect ( self: Effect.Effect, metric: Metric ): Effect.Effect } = internal.trackSuccess /** * Returns an aspect that will update this metric with the result of applying * the specified function to the success value of the effects that the aspect is * applied to. * * @since 1.0.0 * @category aspects */ export const trackSuccessWith: { ( metric: Metric, f: (value: In2) => In ): (self: Effect.Effect) => Effect.Effect ( self: Effect.Effect, metric: Metric, f: (value: In2) => In ): Effect.Effect } = internal.trackSuccessWith /** * Updates the metric with the specified update message. For example, if the * metric were a counter, the update would increment the method by the * provided amount. * * @since 1.0.0 * @category utils */ export const update: { (input: In): (self: Metric) => Effect.Effect (self: Metric, input: In): Effect.Effect } = internal.update /** * Retrieves a snapshot of the value of the metric at this moment in time. * * @since 1.0.0 * @category getters */ export const value: (self: Metric) => Effect.Effect = internal.value /** * @since 1.0.0 * @category utils */ export const withNow: (self: Metric) => Metric = internal.withNow /** * @since 1.0.0 * @category zipping */ export const zip: { ( that: Metric ): ( self: Metric ) => Metric ( self: Metric, that: Metric ): Metric } = internal.zip /** * Unsafely captures a snapshot of all metrics recorded by the application. * * @since 1.0.0 * @category unsafe */ export const unsafeSnapshot: (_: void) => HashSet.HashSet = internal.unsafeSnapshot /** * @since 1.0.0 * @category metrics */ export const fiberStarted: Metric.Counter = fiberRuntime.fiberStarted /** * @since 1.0.0 * @category metrics */ export const fiberSuccesses: Metric.Counter = fiberRuntime.fiberSuccesses /** * @since 1.0.0 * @category metrics */ export const fiberFailures: Metric.Counter = fiberRuntime.fiberFailures /** * @since 1.0.0 * @category metrics */ export const fiberLifetimes: Metric = fiberRuntime.fiberLifetimes /** * @since 1.0.0 * @category metrics */ export const fiberActive: Metric.Counter = fiberRuntime.fiberActive