In complex and highly concurrent applications, managing various interconnected components can be quite challenging. Ensuring that everything runs smoothly and avoiding application downtime becomes crucial in such setups.
Now, let’s imagine we have a sophisticated infrastructure with numerous services. These services are replicated and distributed across our servers. However, we often lack insight into what’s happening across these services, including error rates, response times, and service uptime. This lack of visibility can make it challenging to identify and address issues effectively. This is where Effect Metrics comes into play; it allows us to capture and analyze various metrics, providing valuable data for later investigation.
Effect Metrics offers support for five different types of metrics:
Metric
Description
Counter
Counters are used to track values that increase over time, such as request counts. They help us keep tabs on how many times a specific event or action has occurred.
Gauge
Gauges represent a single numerical value that can fluctuate up and down over time. They are often used to monitor metrics like memory usage, which can vary continuously.
Histogram
Histograms are useful for tracking the distribution of observed values across different buckets. They are commonly used for metrics like request latencies, allowing us to understand how response times are distributed.
Summary
Summaries provide insight into a sliding window of a time series and offer metrics for specific percentiles of the time series, often referred to as quantiles. This is particularly helpful for understanding latency-related metrics, such as request response times.
Frequency
Frequency metrics count the occurrences of distinct string values. They are useful when you want to keep track of how often different events or conditions are happening in your application.
Counter
In the world of metrics, a Counter is a metric that represents a single numerical value that can be both incremented and decremented over time. Think of it like a tally that keeps track of changes, such as the number of a particular type of request received by your application, whether it’s increasing or decreasing.
Unlike some other types of metrics (like gauges), where we’re interested in the value at a specific moment, with counters, we care about the cumulative value over time. This means it provides a running total of changes, which can go up and down, reflecting the dynamic nature of certain metrics.
Some typical use cases for counters include:
Request Counts: Monitoring the number of incoming requests to your server.
Completed Tasks: Keeping track of how many tasks or processes have been successfully completed.
Error Counts: Counting the occurrences of errors in your application.
How to Create a Counter
To create a counter, you can use the Metric.counter constructor.
Example (Creating a Counter)
Once created, the counter can accept an effect that returns a number, which will increment or decrement the counter.
Represents a Counter metric that tracks cumulative numerical values over time.
Counters can be incremented and decremented and provide a running total of changes.
@param ― name - The name of the counter metric.
@param ―
options - An optional configuration for the counter:
description - A description of the counter.
bigint - Indicates if the counter uses 'bigint' data type.
incremental - Set to 'true' for a counter that only increases. With this configuration, Effect ensures that non-incremental updates have no impact on the counter, making it exclusively suitable for counting upwards.
@example
import { Metric } from "effect"
const numberCounter = Metric.counter("count", {
description: "A number counter"
});
Creates an Effect that succeeds with the provided value.
Use this function to represent a successful computation that yields a value of type A.
The effect does not fail and does not require any environmental context.
@example
import { Effect } from "effect"
// Creating an effect that succeeds with the number 42
const success = Effect.succeed(42)
Creates an Effect that succeeds with the provided value.
Use this function to represent a successful computation that yields a value of type A.
The effect does not fail and does not require any environmental context.
@example
import { Effect } from "effect"
// Creating an effect that succeeds with the number 42
const success = Effect.succeed(42)
Creates an Effect that succeeds with the provided value.
Use this function to represent a successful computation that yields a value of type A.
The effect does not fail and does not require any environmental context.
@example
import { Effect } from "effect"
// Creating an effect that succeeds with the number 42
const success = Effect.succeed(42)
Retrieves a snapshot of the value of the metric at this moment in time.
@since ― 2.0.0
value(
constrequestCount:Metric.Metric.Counter<number>
requestCount)
15
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Executes an effect and returns a Promise that resolves with the result.
Use runPromise when working with asynchronous effects and you need to integrate with code that uses Promises.
If the effect fails, the returned Promise will be rejected with the error.
@example
import { Effect } from "effect"
// Execute an effect and handle the result with a Promise
Effect.runPromise(Effect.succeed(1)).then(console.log) // Output: 1
// Execute a failing effect and handle the rejection
Effect.runPromise(Effect.fail("my error")).catch((error) => {
console.error("Effect failed with error:", error)
})
Attaches callbacks for the resolution and/or rejection of the Promise.
@param ― onfulfilled The callback to execute when the Promise is resolved.
@param ― onrejected The callback to execute when the Promise is rejected.
@returns ― A Promise for the completion of which ever callback is executed.
then(
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Represents a Counter metric that tracks cumulative numerical values over time.
Counters can be incremented and decremented and provide a running total of changes.
@param ― name - The name of the counter metric.
@param ―
options - An optional configuration for the counter:
description - A description of the counter.
bigint - Indicates if the counter uses 'bigint' data type.
incremental - Set to 'true' for a counter that only increases. With this configuration, Effect ensures that non-incremental updates have no impact on the counter, making it exclusively suitable for counting upwards.
@example
import { Metric } from "effect"
const numberCounter = Metric.counter("count", {
description: "A number counter"
});
Represents a Counter metric that tracks cumulative numerical values over time.
Counters can be incremented and decremented and provide a running total of changes.
@param ― name - The name of the counter metric.
@param ―
options - An optional configuration for the counter:
description - A description of the counter.
bigint - Indicates if the counter uses 'bigint' data type.
incremental - Set to 'true' for a counter that only increases. With this configuration, Effect ensures that non-incremental updates have no impact on the counter, making it exclusively suitable for counting upwards.
@example
import { Metric } from "effect"
const numberCounter = Metric.counter("count", {
description: "A number counter"
});
Represents a Counter metric that tracks cumulative numerical values over time.
Counters can be incremented and decremented and provide a running total of changes.
@param ― name - The name of the counter metric.
@param ―
options - An optional configuration for the counter:
description - A description of the counter.
bigint - Indicates if the counter uses 'bigint' data type.
incremental - Set to 'true' for a counter that only increases. With this configuration, Effect ensures that non-incremental updates have no impact on the counter, making it exclusively suitable for counting upwards.
@example
import { Metric } from "effect"
const numberCounter = Metric.counter("count", {
description: "A number counter"
});
Creates an Effect that succeeds with the provided value.
Use this function to represent a successful computation that yields a value of type A.
The effect does not fail and does not require any environmental context.
@example
import { Effect } from "effect"
// Creating an effect that succeeds with the number 42
const success = Effect.succeed(42)
Creates an Effect that succeeds with the provided value.
Use this function to represent a successful computation that yields a value of type A.
The effect does not fail and does not require any environmental context.
@example
import { Effect } from "effect"
// Creating an effect that succeeds with the number 42
const success = Effect.succeed(42)
Creates an Effect that succeeds with the provided value.
Use this function to represent a successful computation that yields a value of type A.
The effect does not fail and does not require any environmental context.
@example
import { Effect } from "effect"
// Creating an effect that succeeds with the number 42
const success = Effect.succeed(42)
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Executes an effect and returns a Promise that resolves with the result.
Use runPromise when working with asynchronous effects and you need to integrate with code that uses Promises.
If the effect fails, the returned Promise will be rejected with the error.
@example
import { Effect } from "effect"
// Execute an effect and handle the result with a Promise
Effect.runPromise(Effect.succeed(1)).then(console.log) // Output: 1
// Execute a failing effect and handle the rejection
Effect.runPromise(Effect.fail("my error")).catch((error) => {
console.error("Effect failed with error:", error)
})
Attaches callbacks for the resolution and/or rejection of the Promise.
@param ― onfulfilled The callback to execute when the Promise is resolved.
@param ― onrejected The callback to execute when the Promise is rejected.
@returns ― A Promise for the completion of which ever callback is executed.
then(
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
In this configuration, the counter only accepts positive values. Any attempts to decrement will have no effect, ensuring the counter strictly counts upwards.
Counters With Constant Input
You can configure a counter to always increment by a fixed value each time it is invoked.
Represents a Counter metric that tracks cumulative numerical values over time.
Counters can be incremented and decremented and provide a running total of changes.
@param ― name - The name of the counter metric.
@param ―
options - An optional configuration for the counter:
description - A description of the counter.
bigint - Indicates if the counter uses 'bigint' data type.
incremental - Set to 'true' for a counter that only increases. With this configuration, Effect ensures that non-incremental updates have no impact on the counter, making it exclusively suitable for counting upwards.
@example
import { Metric } from "effect"
const numberCounter = Metric.counter("count", {
description: "A number counter"
});
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 ― 2.0.0
withConstantInput(1) // Automatically increments by 1
Creates an Effect that succeeds with the provided value.
Use this function to represent a successful computation that yields a value of type A.
The effect does not fail and does not require any environmental context.
@example
import { Effect } from "effect"
// Creating an effect that succeeds with the number 42
const success = Effect.succeed(42)
Creates an Effect that succeeds with the provided value.
Use this function to represent a successful computation that yields a value of type A.
The effect does not fail and does not require any environmental context.
@example
import { Effect } from "effect"
// Creating an effect that succeeds with the number 42
const success = Effect.succeed(42)
Creates an Effect that succeeds with the provided value.
Use this function to represent a successful computation that yields a value of type A.
The effect does not fail and does not require any environmental context.
@example
import { Effect } from "effect"
// Creating an effect that succeeds with the number 42
const success = Effect.succeed(42)
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Executes an effect and returns a Promise that resolves with the result.
Use runPromise when working with asynchronous effects and you need to integrate with code that uses Promises.
If the effect fails, the returned Promise will be rejected with the error.
@example
import { Effect } from "effect"
// Execute an effect and handle the result with a Promise
Effect.runPromise(Effect.succeed(1)).then(console.log) // Output: 1
// Execute a failing effect and handle the rejection
Effect.runPromise(Effect.fail("my error")).catch((error) => {
console.error("Effect failed with error:", error)
})
Attaches callbacks for the resolution and/or rejection of the Promise.
@param ― onfulfilled The callback to execute when the Promise is resolved.
@param ― onrejected The callback to execute when the Promise is rejected.
@returns ― A Promise for the completion of which ever callback is executed.
then(
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
In the world of metrics, a Gauge is a metric that represents a single numerical value that can be set or adjusted. Think of it as a dynamic variable that can change over time. One common use case for a gauge is to monitor something like the current memory usage of your application.
Unlike counters, where we’re interested in cumulative values over time, with gauges, our focus is on the current value at a specific point in time.
Gauges are the best choice when you want to monitor values that can both increase and decrease, and you’re not interested in tracking their rates of change. In other words, gauges help us measure things that have a specific value at a particular moment.
Some typical use cases for gauges include:
Memory Usage: Keeping an eye on how much memory your application is using right now.
Queue Size: Monitoring the current size of a queue where tasks are waiting to be processed.
In-Progress Request Counts: Tracking the number of requests currently being handled by your server.
Temperature: Measuring the current temperature, which can fluctuate up and down.
How to Create a Gauge
To create a gauge, you can use the Metric.gauge constructor.
Represents a Gauge metric that tracks and reports a single numerical value at a specific moment.
Gauges are suitable for metrics that represent instantaneous values, such as memory usage or CPU load.
@param ― name - The name of the gauge metric.
@param ―
options - An optional configuration for the gauge:
description - A description of the gauge metric.
bigint - Indicates if the counter uses 'bigint' data type.
@example
import { Metric } from "effect"
const numberGauge = Metric.gauge("memory_usage", {
description: "A gauge for memory usage"
});
const bigintGauge = Metric.gauge("cpu_load", {
description: "A gauge for CPU load",
bigint: true
});
@since ― 2.0.0
gauge("memory_usage", {
4
// Optional
5
description?: string |undefined
description: "A gauge for memory usage"
6
})
Once created, a gauge can be updated by passing an effect that produces the value you want to set for the gauge.
Represents a Gauge metric that tracks and reports a single numerical value at a specific moment.
Gauges are suitable for metrics that represent instantaneous values, such as memory usage or CPU load.
@param ― name - The name of the gauge metric.
@param ―
options - An optional configuration for the gauge:
description - A description of the gauge metric.
bigint - Indicates if the counter uses 'bigint' data type.
@example
import { Metric } from "effect"
const numberGauge = Metric.gauge("memory_usage", {
description: "A gauge for memory usage"
});
const bigintGauge = Metric.gauge("cpu_load", {
description: "A gauge for CPU load",
bigint: true
});
Returns the next integer value in the specified range from the
pseudo-random number generator.
@since ― 2.0.0
nextIntBetween(-10, 10)
10
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Retrieves a snapshot of the value of the metric at this moment in time.
@since ― 2.0.0
value(
consttemperature:Metric.Metric.Gauge<number>
temperature)
24
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Executes an effect and returns a Promise that resolves with the result.
Use runPromise when working with asynchronous effects and you need to integrate with code that uses Promises.
If the effect fails, the returned Promise will be rejected with the error.
@example
import { Effect } from "effect"
// Execute an effect and handle the result with a Promise
Effect.runPromise(Effect.succeed(1)).then(console.log) // Output: 1
// Execute a failing effect and handle the rejection
Effect.runPromise(Effect.fail("my error")).catch((error) => {
console.error("Effect failed with error:", error)
})
Attaches callbacks for the resolution and/or rejection of the Promise.
@param ― onfulfilled The callback to execute when the Promise is resolved.
@param ― onrejected The callback to execute when the Promise is rejected.
@returns ― A Promise for the completion of which ever callback is executed.
then(
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Represents a Gauge metric that tracks and reports a single numerical value at a specific moment.
Gauges are suitable for metrics that represent instantaneous values, such as memory usage or CPU load.
@param ― name - The name of the gauge metric.
@param ―
options - An optional configuration for the gauge:
description - A description of the gauge metric.
bigint - Indicates if the counter uses 'bigint' data type.
@example
import { Metric } from "effect"
const numberGauge = Metric.gauge("memory_usage", {
description: "A gauge for memory usage"
});
const bigintGauge = Metric.gauge("cpu_load", {
description: "A gauge for CPU load",
bigint: true
});
Represents a Gauge metric that tracks and reports a single numerical value at a specific moment.
Gauges are suitable for metrics that represent instantaneous values, such as memory usage or CPU load.
@param ― name - The name of the gauge metric.
@param ―
options - An optional configuration for the gauge:
description - A description of the gauge metric.
bigint - Indicates if the counter uses 'bigint' data type.
@example
import { Metric } from "effect"
const numberGauge = Metric.gauge("memory_usage", {
description: "A gauge for memory usage"
});
const bigintGauge = Metric.gauge("cpu_load", {
description: "A gauge for CPU load",
bigint: true
});
@since ― 2.0.0
gauge("cpu_load", {
9
description?: string |undefined
description: "A gauge for CPU load",
10
bigint: true
bigint: true
11
})
Histogram
A Histogram is a metric used to analyze how numerical values are distributed over time. Instead of focusing on individual data points, a histogram groups values into predefined ranges, called buckets, and tracks how many values fall into each range.
When a value is recorded, it gets assigned to one of the histogram’s buckets based on its range. Each bucket has an upper boundary, and the count for that bucket is increased if the value is less than or equal to its boundary. Once recorded, the individual value is discarded, and the focus shifts to how many values have fallen into each bucket.
Histograms also track:
Total Count: The number of values that have been observed.
Sum: The sum of all the observed values.
Min: The smallest observed value.
Max: The largest observed value.
Histograms are especially useful for calculating percentiles, which can help you estimate specific points in a dataset by analyzing how many values are in each bucket.
This concept is inspired by Prometheus, a well-known monitoring and alerting toolkit.
Histograms are particularly useful in performance analysis and system monitoring. By examining how response times, latencies, or other metrics are distributed, you can gain valuable insights into your system’s behavior. This data helps you identify outliers, performance bottlenecks, or trends that may require optimization.
Common use cases for histograms include:
Percentile Estimation: Histograms allow you to approximate percentiles of observed values, like the 95th percentile of response times.
Known Ranges: If you can estimate the range of values in advance, histograms can organize the data into predefined buckets for better analysis.
Performance Metrics: Use histograms to track metrics like request latencies, memory usage, or throughput over time.
Aggregation: Histograms can be aggregated across multiple instances, making them ideal for distributed systems where you need to collect data from different sources.
Example (Histogram With Linear Buckets)
In this example, we define a histogram with linear buckets, where the values range from 0 to 100 in increments of 10. Additionally, we include a final bucket for values greater than 100, referred to as the “Infinity” bucket. This configuration is useful for tracking numeric values, like request latencies, within specific ranges.
The program generates random numbers between 1 and 120, records them in the histogram, and then prints the histogram’s state, showing the count of values that fall into each bucket.
1
import {
import Effect
@since ― 2.0.0
@since ― 2.0.0
@since ― 2.0.0
Effect,
import Metric
Metric,
import MetricBoundaries
MetricBoundaries,
import Random
Random } from"effect"
2
3
// Define a histogram to track request latencies, with linear buckets
Represents a Histogram metric that records observations in specified value boundaries.
Histogram metrics are useful for measuring the distribution of values within a range.
@param ― name - The name of the histogram metric.
@param ― boundaries - The boundaries for defining the histogram's value ranges.
@param ― description - A description of the histogram metric.
@example
import { Metric, MetricBoundaries } from "effect"
const latencyHistogram = Metric.histogram("latency_histogram",
MetricBoundaries.linear({ start: 0, width: 10, count: 11 }),
"Measures the distribution of request latency."
);
@since ― 2.0.0
histogram(
5
"request_latency",
6
// Buckets from 0-100, with an extra Infinity bucket
7
import MetricBoundaries
MetricBoundaries.
constlinear: (options: {
readonlystart:number;
readonlywidth:number;
readonlycount:number;
}) =>MetricBoundaries.MetricBoundaries
A helper method to create histogram bucket boundaries for a histogram
with linear increasing values.
constrepeatN: (n:number) => <A, E, R>(self:Effect.Effect<A, E, R>) =>Effect.Effect<A, E, R> (+1overload)
The repeatN function returns a new effect that repeats the specified effect a
given number of times or until the first failure. The repeats are in addition
to the initial execution, so Effect.repeatN(action, 1) executes action once
initially and then repeats it one additional time if it succeeds.
@example
import { Effect, Console } from "effect"
const action = Console.log("success")
const program = Effect.repeatN(action, 2)
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Executes an effect and returns a Promise that resolves with the result.
Use runPromise when working with asynchronous effects and you need to integrate with code that uses Promises.
If the effect fails, the returned Promise will be rejected with the error.
@example
import { Effect } from "effect"
// Execute an effect and handle the result with a Promise
Effect.runPromise(Effect.succeed(1)).then(console.log) // Output: 1
// Execute a failing effect and handle the rejection
Effect.runPromise(Effect.fail("my error")).catch((error) => {
console.error("Effect failed with error:", error)
})
@since ― 2.0.0
runPromise(
constprogram:Effect.Effect<void, never, never>
program)
22
/*
23
Example Output:
24
HistogramState {
25
buckets: [
26
[ 0, 0 ], // No values in the 0-10 range
27
[ 10, 7 ], // 7 values in the 10-20 range
28
[ 20, 11 ], // 11 values in the 20-30 range
29
[ 30, 20 ], // 20 values in the 30-40 range
30
[ 40, 27 ], // and so on...
31
[ 50, 38 ],
32
[ 60, 53 ],
33
[ 70, 64 ],
34
[ 80, 73 ],
35
[ 90, 84 ],
36
[ Infinity, 100 ] // All 100 values have been recorded
37
],
38
count: 100, // Total count of observed values
39
min: 1, // Smallest observed value
40
max: 119, // Largest observed value
41
sum: 5980, // Sum of all observed values
42
...
43
}
44
*/
Timer Metric
In this example, we demonstrate how to use a timer metric to track the duration of specific workflows. The timer captures how long certain tasks take to execute, storing this information in a histogram, which provides insights into the distribution of these durations.
We generate random values to simulate varying wait times, record the durations in the timer, and then print out the histogram’s state.
Example (Tracking Workflow Durations with a Timer Metric)
1
import {
import Metric
Metric,
import Array
Array,
import Random
Random,
import Effect
@since ― 2.0.0
@since ― 2.0.0
@since ― 2.0.0
Effect } from"effect"
2
3
// Create a timer metric with predefined boundaries from 1 to 10
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").
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.
constrepeatN: (n:number) => <A, E, R>(self:Effect.Effect<A, E, R>) =>Effect.Effect<A, E, R> (+1overload)
The repeatN function returns a new effect that repeats the specified effect a
given number of times or until the first failure. The repeats are in addition
to the initial execution, so Effect.repeatN(action, 1) executes action once
initially and then repeats it one additional time if it succeeds.
@example
import { Effect, Console } from "effect"
const action = Console.log("success")
const program = Effect.repeatN(action, 2)
Effect.runPromise(program)
@since ― 2.0.0
repeatN(99))
17
18
// Retrieve and print the current state of the timer histogram
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Executes an effect and returns a Promise that resolves with the result.
Use runPromise when working with asynchronous effects and you need to integrate with code that uses Promises.
If the effect fails, the returned Promise will be rejected with the error.
@example
import { Effect } from "effect"
// Execute an effect and handle the result with a Promise
Effect.runPromise(Effect.succeed(1)).then(console.log) // Output: 1
// Execute a failing effect and handle the rejection
Effect.runPromise(Effect.fail("my error")).catch((error) => {
console.error("Effect failed with error:", error)
})
@since ― 2.0.0
runPromise(
constprogram:Effect.Effect<void, never, never>
program)
24
/*
25
Example Output:
26
HistogramState {
27
buckets: [
28
[ 1, 3 ], // 3 tasks completed in <= 1 ms
29
[ 2, 13 ], // 13 tasks completed in <= 2 ms
30
[ 3, 17 ], // and so on...
31
[ 4, 26 ],
32
[ 5, 35 ],
33
[ 6, 43 ],
34
[ 7, 53 ],
35
[ 8, 56 ],
36
[ 9, 65 ],
37
[ 10, 72 ],
38
[ Infinity, 100 ] // All 100 tasks have completed
39
],
40
count: 100, // Total number of tasks observed
41
min: 0.25797, // Shortest task duration in milliseconds
42
max: 12.25421, // Longest task duration in milliseconds
43
sum: 683.0266810000002, // Total time spent across all tasks
44
...
45
}
46
*/
Summary
A Summary is a metric that gives insights into a series of data points by calculating specific percentiles. Percentiles help us understand how data is distributed. For instance, if you’re tracking response times for requests over the past hour, you may want to examine key percentiles such as the 50th, 90th, 95th, or 99th to better understand your system’s performance.
Summaries are similar to histograms in that they observe number values, but with a different approach. Instead of immediately sorting values into buckets and discarding them, a summary holds onto the observed values in memory. However, to avoid storing too much data, summaries use two parameters:
maxAge: The maximum age a value can have before it’s discarded.
maxSize: The maximum number of values stored in the summary.
This creates a sliding window of recent values, so the summary always represents a fixed number of the most recent observations.
Summaries are commonly used to calculate quantiles over this sliding window. A quantile is a number between 0 and 1 that represents the percentage of values less than or equal to a certain threshold. For example, a quantile of 0.5 (or 50th percentile) is the median value, while 0.95 (or 95th percentile) would represent the value below which 95% of the observed data falls.
Quantiles are helpful for monitoring important performance metrics, such as latency, and for ensuring that your system meets performance goals (like Service Level Agreements, or SLAs).
The Effect Metrics API also allows you to configure summaries with an error margin. This margin introduces a range of acceptable values for quantiles, improving the accuracy of the result.
Summaries are particularly useful in cases where:
The range of values you’re observing is not known or estimated in advance, making histograms less practical.
You don’t need to aggregate data across multiple instances or average results. Summaries calculate their results on the application side, meaning they focus on the specific instance where they are used.
Example (Creating and Using a Summary)
In this example, we will create a summary to track response times. The summary will:
Hold up to 100 samples.
Discard samples older than 1 day.
Have a 3% error margin when calculating quantiles.
Report the 10%, 50%, and 90% quantiles, which help track response time distributions.
We’ll apply the summary to an effect that generates random integers, simulating response times.
Creates a Summary metric that records observations and calculates quantiles.
Summary metrics provide statistical information about a set of values, including quantiles.
@param ―
options - An object with configuration options for the Summary metric:
name - The name of the Summary metric.
maxAge - The maximum age of observations to retain.
maxSize - The maximum number of observations to keep.
error - The error percentage when calculating quantiles.
quantiles - An Chunk of quantiles to calculate (e.g., [0.5, 0.9]).
description - An optional description of the Summary metric.
@example
import { Metric, Chunk } from "effect"
const responseTimesSummary = Metric.summary({
name: "response_times_summary",
maxAge: "60 seconds", // Retain observations for 60 seconds.
maxSize: 1000, // Keep a maximum of 1000 observations.
error: 0.01, // Allow a 1% error when calculating quantiles.
quantiles: [0.5, 0.9, 0.99], // Calculate 50th, 90th, and 99th percentiles.
description: "Measures the distribution of response times."
});
@since ― 2.0.0
summary({
5
name: string
name: "response_time_summary", // Name of the summary metric
6
maxAge: DurationInput
maxAge: "1 day", // Maximum sample age
7
maxSize: number
maxSize: 100, // Maximum number of samples to retain
8
error: number
error: 0.03, // Error margin for quantile calculation
constrepeatN: (n:number) => <A, E, R>(self:Effect.Effect<A, E, R>) =>Effect.Effect<A, E, R> (+1overload)
The repeatN function returns a new effect that repeats the specified effect a
given number of times or until the first failure. The repeats are in addition
to the initial execution, so Effect.repeatN(action, 1) executes action once
initially and then repeats it one additional time if it succeeds.
@example
import { Effect, Console } from "effect"
const action = Console.log("success")
const program = Effect.repeatN(action, 2)
Effect.runPromise(program)
@since ― 2.0.0
repeatN(99)
18
)
19
20
// Retrieve and log the current state of the summary
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Executes an effect and returns a Promise that resolves with the result.
Use runPromise when working with asynchronous effects and you need to integrate with code that uses Promises.
If the effect fails, the returned Promise will be rejected with the error.
@example
import { Effect } from "effect"
// Execute an effect and handle the result with a Promise
Effect.runPromise(Effect.succeed(1)).then(console.log) // Output: 1
// Execute a failing effect and handle the rejection
Effect.runPromise(Effect.fail("my error")).catch((error) => {
console.error("Effect failed with error:", error)
})
@since ― 2.0.0
runPromise(
constprogram:Effect.Effect<void, never, never>
program)
26
/*
27
Example Output:
28
SummaryState {
29
error: 0.03, // Error margin used for quantile calculation
Frequencies are metrics that help count the occurrences of specific values. Think of them as a set of counters, each associated with a unique value. When new values are observed, the frequency metric automatically creates new counters for those values.
Frequencies are particularly useful for tracking how often distinct string values occur. Some example use cases include:
Counting the number of invocations for each service in an application, where each service has a logical name.
Monitoring how frequently different types of failures occur.
Example (Tracking Error Occurrences)
In this example, we’ll create a Frequency to observe how often different error codes occur. This can be applied to effects that return a string value:
constrepeatN: (n:number) => <A, E, R>(self:Effect.Effect<A, E, R>) =>Effect.Effect<A, E, R> (+1overload)
The repeatN function returns a new effect that repeats the specified effect a
given number of times or until the first failure. The repeats are in addition
to the initial execution, so Effect.repeatN(action, 1) executes action once
initially and then repeats it one additional time if it succeeds.
@example
import { Effect, Console } from "effect"
const action = Console.log("success")
const program = Effect.repeatN(action, 2)
Effect.runPromise(program)
@since ― 2.0.0
repeatN(99))
17
18
// Retrieve and log the current state of the summary
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Executes an effect and returns a Promise that resolves with the result.
Use runPromise when working with asynchronous effects and you need to integrate with code that uses Promises.
If the effect fails, the returned Promise will be rejected with the error.
@example
import { Effect } from "effect"
// Execute an effect and handle the result with a Promise
Effect.runPromise(Effect.succeed(1)).then(console.log) // Output: 1
// Execute a failing effect and handle the rejection
Effect.runPromise(Effect.fail("my error")).catch((error) => {
console.error("Effect failed with error:", error)
})
@since ― 2.0.0
runPromise(
constprogram:Effect.Effect<void, never, never>
program)
24
/*
25
Example Output:
26
FrequencyState {
27
occurrences: Map(9) {
28
'Error-7' => 12,
29
'Error-2' => 12,
30
'Error-4' => 14,
31
'Error-1' => 14,
32
'Error-9' => 8,
33
'Error-6' => 11,
34
'Error-5' => 9,
35
'Error-3' => 14,
36
'Error-8' => 6
37
},
38
...
39
}
40
*/
Tagging Metrics
Tags are key-value pairs you can add to metrics to provide additional context. They help categorize and filter metrics, making it easier to analyze specific aspects of your application’s performance or behavior.
When creating metrics, you can add tags to them. Tags are key-value pairs that provide additional context, helping in categorizing and filtering metrics. This makes it easier to analyze and monitor specific aspects of your application.
Tagging a Specific Metric
You can tag individual metrics using the Metric.tagged function.
This allows you to add specific tags to a single metric, providing detailed context without applying tags globally.
Represents a Counter metric that tracks cumulative numerical values over time.
Counters can be incremented and decremented and provide a running total of changes.
@param ― name - The name of the counter metric.
@param ―
options - An optional configuration for the counter:
description - A description of the counter.
bigint - Indicates if the counter uses 'bigint' data type.
incremental - Set to 'true' for a counter that only increases. With this configuration, Effect ensures that non-incremental updates have no impact on the counter, making it exclusively suitable for counting upwards.
@example
import { Metric } from "effect"
const numberCounter = Metric.counter("count", {
description: "A number counter"
});
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 ― 2.0.0
tagged("environment", "production")
7
)
Here, the request_count metric is tagged with "environment": "production", allowing you to filter or analyze metrics by this tag later.
Tagging Multiple Metrics
You can use Effect.tagMetrics to apply tags to all metrics within the same context. This is useful when you want to apply common tags, like the environment (e.g., “production” or “development”), across multiple metrics.
Represents a Counter metric that tracks cumulative numerical values over time.
Counters can be incremented and decremented and provide a running total of changes.
@param ― name - The name of the counter metric.
@param ―
options - An optional configuration for the counter:
description - A description of the counter.
bigint - Indicates if the counter uses 'bigint' data type.
incremental - Set to 'true' for a counter that only increases. With this configuration, Effect ensures that non-incremental updates have no impact on the counter, making it exclusively suitable for counting upwards.
@example
import { Metric } from "effect"
const numberCounter = Metric.counter("count", {
description: "A number counter"
});
Represents a Counter metric that tracks cumulative numerical values over time.
Counters can be incremented and decremented and provide a running total of changes.
@param ― name - The name of the counter metric.
@param ―
options - An optional configuration for the counter:
description - A description of the counter.
bigint - Indicates if the counter uses 'bigint' data type.
incremental - Set to 'true' for a counter that only increases. With this configuration, Effect ensures that non-incremental updates have no impact on the counter, making it exclusively suitable for counting upwards.
@example
import { Metric } from "effect"
const numberCounter = Metric.counter("count", {
description: "A number counter"
});
Creates an Effect that succeeds with the provided value.
Use this function to represent a successful computation that yields a value of type A.
The effect does not fail and does not require any environmental context.
@example
import { Effect } from "effect"
// Creating an effect that succeeds with the number 42
const success = Effect.succeed(42)
consttagMetrics: (key:string, value:string) => <A, E, R>(effect:Effect.Effect<A, E, R>) =>Effect.Effect<A, E, R> (+3overloads)
Tags each metric in this effect with the specific tag.
@since ― 2.0.0
tagMetrics("environment", "production"))
If you only want to apply tags within a specific scope, you can use Effect.tagMetricsScoped. This limits the tag application to metrics within that scope, allowing for more precise tagging control.