Skip to content

Commit

Permalink
add preregisteredWords options to frequency metric key type (#2377)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored and mikearnaldi committed Apr 16, 2024
1 parent aa4a3b5 commit d50a652
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 11 deletions.
16 changes: 16 additions & 0 deletions .changeset/breezy-countries-warn.md
@@ -0,0 +1,16 @@
---
"effect": minor
---

add preregisteredWords option to frequency metric key type

You can use this to register a list of words to pre-populate the value of the
metric.

```ts
import { Metric } from "effect";

const counts = Metric.frequency("counts", {
preregisteredWords: ["a", "b", "c"],
}).register();
```
11 changes: 9 additions & 2 deletions packages/effect/src/Metric.ts
Expand Up @@ -208,12 +208,19 @@ export const counter: {
* @example
* import * as Metric from "effect/Metric"
*
* const errorFrequency = Metric.frequency("error_frequency", "Counts the occurrences of errors.");
* const errorFrequency = Metric.frequency("error_frequency", {
* description: "Counts the occurrences of errors."
* });
*
* @since 2.0.0
* @category constructors
*/
export const frequency: (name: string, description?: string) => Metric.Frequency<string> = internal.frequency
export const frequency: (
name: string,
options?:
| { readonly description?: string | undefined; readonly preregisteredWords?: ReadonlyArray<string> | undefined }
| undefined
) => Metric.Frequency<string> = internal.frequency

/**
* Returns a new metric that is powered by this one, but which accepts updates
Expand Down
10 changes: 9 additions & 1 deletion packages/effect/src/MetricKey.ts
Expand Up @@ -132,7 +132,15 @@ export const counter: {
* @since 2.0.0
* @category constructors
*/
export const frequency: (name: string, description?: string) => MetricKey.Frequency = internal.frequency
export const frequency: (
name: string,
options?:
| {
readonly description?: string | undefined
readonly preregisteredWords?: ReadonlyArray<string> | undefined
}
| undefined
) => MetricKey.Frequency = internal.frequency

/**
* Creates a metric key for a gauge, with the specified name.
Expand Down
7 changes: 6 additions & 1 deletion packages/effect/src/MetricKeyType.ts
Expand Up @@ -113,6 +113,7 @@ export declare namespace MetricKeyType {
*/
export type Frequency = MetricKeyType<string, MetricState.MetricState.Frequency> & {
readonly [FrequencyKeyTypeTypeId]: FrequencyKeyTypeTypeId
readonly preregisteredWords: ReadonlyArray<string>
}

/**
Expand Down Expand Up @@ -193,7 +194,11 @@ export const counter: <A extends number | bigint>() => MetricKeyType.Counter<A>
* @since 2.0.0
* @category constructors
*/
export const frequency: MetricKeyType.Frequency = internal.frequency
export const frequency: (
options?: {
readonly preregisteredWords?: ReadonlyArray<string> | undefined
} | undefined
) => MetricKeyType.Frequency = internal.frequency

/**
* @since 2.0.0
Expand Down
6 changes: 4 additions & 2 deletions packages/effect/src/internal/metric.ts
Expand Up @@ -98,8 +98,10 @@ export const counter: {
} = (name, options) => fromMetricKey(metricKey.counter(name, options as any)) as any

/** @internal */
export const frequency = (name: string, description?: string): Metric.Metric.Frequency<string> =>
fromMetricKey(metricKey.frequency(name, description))
export const frequency = (name: string, options?: {
readonly description?: string | undefined
readonly preregisteredWords?: ReadonlyArray<string> | undefined
}): Metric.Metric.Frequency<string> => fromMetricKey(metricKey.frequency(name, options))

/** @internal */
export const withConstantInput = dual<
Expand Down
5 changes: 4 additions & 1 deletion packages/effect/src/internal/metric/hook.ts
Expand Up @@ -78,8 +78,11 @@ export const counter = <A extends (number | bigint)>(
}

/** @internal */
export const frequency = (_key: MetricKey.MetricKey.Frequency): MetricHook.MetricHook.Frequency => {
export const frequency = (key: MetricKey.MetricKey.Frequency): MetricHook.MetricHook.Frequency => {
const values = new Map<string, number>()
for (const word of key.keyType.preregisteredWords) {
values.set(word, 0)
}
const update = (word: string) => {
const slotCount = values.get(word) ?? 0
values.set(word, slotCount + 1)
Expand Down
7 changes: 5 additions & 2 deletions packages/effect/src/internal/metric/key.ts
Expand Up @@ -83,8 +83,11 @@ export const counter: {
)

/** @internal */
export const frequency = (name: string, description?: string): MetricKey.MetricKey.Frequency =>
new MetricKeyImpl(name, metricKeyType.frequency, Option.fromNullable(description))
export const frequency = (name: string, options?: {
readonly description?: string | undefined
readonly preregisteredWords?: ReadonlyArray<string> | undefined
}): MetricKey.MetricKey.Frequency =>
new MetricKeyImpl(name, metricKeyType.frequency(options), Option.fromNullable(options?.description))

/** @internal */
export const gauge: {
Expand Down
7 changes: 5 additions & 2 deletions packages/effect/src/internal/metric/keyType.ts
Expand Up @@ -86,7 +86,8 @@ const FrequencyKeyTypeHash = Hash.string(FrequencyKeyTypeSymbolKey)
/** @internal */
class FrequencyKeyType implements MetricKeyType.MetricKeyType.Frequency {
readonly [MetricKeyTypeTypeId] = metricKeyTypeVariance
readonly [FrequencyKeyTypeTypeId]: MetricKeyType.FrequencyKeyTypeTypeId = FrequencyKeyTypeTypeId;
readonly [FrequencyKeyTypeTypeId]: MetricKeyType.FrequencyKeyTypeTypeId = FrequencyKeyTypeTypeId
constructor(readonly preregisteredWords: ReadonlyArray<string>) {}
[Hash.symbol](): number {
return FrequencyKeyTypeHash
}
Expand Down Expand Up @@ -192,7 +193,9 @@ export const counter: <A extends number | bigint>(options?: {
* @since 2.0.0
* @category constructors
*/
export const frequency: MetricKeyType.MetricKeyType.Frequency = new FrequencyKeyType()
export const frequency = (options?: {
readonly preregisteredWords?: ReadonlyArray<string> | undefined
}): MetricKeyType.MetricKeyType.Frequency => new FrequencyKeyType(options?.preregisteredWords ?? [])

/**
* @since 2.0.0
Expand Down

0 comments on commit d50a652

Please sign in to comment.