diff --git a/dev-packages/e2e-tests/test-applications/effect-3-browser/public/index.html b/dev-packages/e2e-tests/test-applications/effect-3-browser/public/index.html
index 19d5c3d99a2f..e0ea20a49f1e 100644
--- a/dev-packages/e2e-tests/test-applications/effect-3-browser/public/index.html
+++ b/dev-packages/e2e-tests/test-applications/effect-3-browser/public/index.html
@@ -38,6 +38,12 @@
Log Tests
+
+
Navigation Test
Navigation Link
diff --git a/dev-packages/e2e-tests/test-applications/effect-3-browser/src/index.js b/dev-packages/e2e-tests/test-applications/effect-3-browser/src/index.js
index 9e288d79d174..1e41ea8c7e72 100644
--- a/dev-packages/e2e-tests/test-applications/effect-3-browser/src/index.js
+++ b/dev-packages/e2e-tests/test-applications/effect-3-browser/src/index.js
@@ -2,9 +2,10 @@
import * as Sentry from '@sentry/effect';
import * as Logger from 'effect/Logger';
import * as Layer from 'effect/Layer';
-import * as Runtime from 'effect/Runtime';
+import * as ManagedRuntime from 'effect/ManagedRuntime';
import * as LogLevel from 'effect/LogLevel';
import * as Effect from 'effect/Effect';
+import * as Metric from 'effect/Metric';
const LogLevelLayer = Logger.minimumLogLevel(LogLevel.Debug);
const AppLayer = Layer.mergeAll(
@@ -18,12 +19,20 @@ const AppLayer = Layer.mergeAll(
}),
Sentry.layerTracer,
Sentry.layerLogger,
+ Sentry.layerMetrics,
LogLevelLayer,
);
-const runtime = Layer.toRuntime(AppLayer).pipe(Effect.scoped, Effect.runSync);
+const ordersPlaced = Metric.counter('orders_placed').pipe(Metric.tagged('shop', 'e2e'));
+const queueDepth = Metric.gauge('queue_depth');
-const runEffect = fn => Runtime.runPromise(runtime)(fn());
+// ManagedRuntime keeps the layer scope open, so the `Sentry.layerMetrics` fiber keeps running
+const runtime = ManagedRuntime.make(AppLayer);
+
+// Builds the layer at page load, so Sentry is initialized before the first click
+Effect.runSync(runtime.runtimeEffect);
+
+const runEffect = fn => runtime.runPromise(fn());
document.getElementById('exception-button')?.addEventListener('click', () => {
throw new Error('I am an error!');
@@ -82,6 +91,17 @@ document.getElementById('log-context-button')?.addEventListener('click', async (
if (el) el.textContent = 'Log with context sent!';
});
+document.getElementById('metrics-button')?.addEventListener('click', async () => {
+ await runEffect(() =>
+ Effect.gen(function* () {
+ yield* Metric.update(ordersPlaced, 1);
+ yield* Metric.update(queueDepth, 7);
+ }),
+ );
+ const el = document.getElementById('metrics-result');
+ if (el) el.textContent = 'Metrics updated!';
+});
+
document.getElementById('navigation-link')?.addEventListener('click', () => {
document.getElementById('navigation-target')?.scrollIntoView({ behavior: 'smooth' });
});
diff --git a/dev-packages/e2e-tests/test-applications/effect-3-browser/tests/metrics.test.ts b/dev-packages/e2e-tests/test-applications/effect-3-browser/tests/metrics.test.ts
new file mode 100644
index 000000000000..b8ae536264c4
--- /dev/null
+++ b/dev-packages/e2e-tests/test-applications/effect-3-browser/tests/metrics.test.ts
@@ -0,0 +1,30 @@
+import { expect, test } from '@playwright/test';
+import { waitForEnvelopeItem } from '@sentry-internal/test-utils';
+import type { SerializedMetricContainer } from '@sentry/core';
+
+test('sends Effect metrics', async ({ page }) => {
+ const metricContainerPromise = waitForEnvelopeItem('effect-3-browser', envelopeItem => {
+ return (
+ envelopeItem[0].type === 'trace_metric' &&
+ (envelopeItem[1] as SerializedMetricContainer).items.some(metric => metric.name === 'orders_placed')
+ );
+ });
+
+ await page.goto('/');
+ await page.locator('id=metrics-button').click();
+
+ await expect(page.locator('id=metrics-result')).toHaveText('Metrics updated!');
+
+ const metrics = ((await metricContainerPromise)[1] as SerializedMetricContainer).items;
+ expect(metrics).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ name: 'orders_placed',
+ type: 'counter',
+ value: 1,
+ attributes: expect.objectContaining({ shop: { value: 'e2e', type: 'string' } }),
+ }),
+ expect.objectContaining({ name: 'queue_depth', type: 'gauge', value: 7 }),
+ ]),
+ );
+});
diff --git a/dev-packages/e2e-tests/test-applications/effect-3-node/src/app.ts b/dev-packages/e2e-tests/test-applications/effect-3-node/src/app.ts
index 013e2328fe56..ab88e213b63c 100644
--- a/dev-packages/e2e-tests/test-applications/effect-3-node/src/app.ts
+++ b/dev-packages/e2e-tests/test-applications/effect-3-node/src/app.ts
@@ -6,6 +6,8 @@ import * as Cause from 'effect/Cause';
import * as Layer from 'effect/Layer';
import * as Logger from 'effect/Logger';
import * as LogLevel from 'effect/LogLevel';
+import * as Metric from 'effect/Metric';
+import * as MetricBoundaries from 'effect/MetricBoundaries';
import * as Tracer from 'effect/Tracer';
import { createServer } from 'http';
@@ -19,8 +21,17 @@ const SentryLayer = Layer.mergeAll(
}),
Sentry.layerTracer,
Sentry.layerLogger,
+ Sentry.layerMetrics,
);
+const ordersPlaced = Metric.counter('orders_placed').pipe(Metric.tagged('shop', 'e2e'));
+const queueDepth = Metric.gauge('queue_depth');
+const requestDuration = Metric.histogram(
+ 'request_duration_ms',
+ MetricBoundaries.linear({ start: 0, width: 50, count: 10 }),
+);
+const cacheLookups = Metric.frequency('cache_lookups');
+
const router = HttpRouter.empty.pipe(
HttpRouter.get('/test-success', HttpServerResponse.json({ version: 'v1' })),
@@ -110,6 +121,27 @@ const router = HttpRouter.empty.pipe(
}),
),
+ HttpRouter.get(
+ '/test-metric-counter',
+ Effect.gen(function* () {
+ yield* Metric.update(ordersPlaced, 1);
+ return yield* HttpServerResponse.json({ status: 'ok' });
+ }),
+ ),
+
+ HttpRouter.get(
+ '/test-metrics',
+ Effect.gen(function* () {
+ yield* Metric.update(queueDepth, 7);
+ yield* Metric.update(requestDuration, 120);
+ yield* Metric.update(requestDuration, 80);
+ yield* Metric.update(cacheLookups, 'hit');
+ yield* Metric.update(cacheLookups, 'hit');
+ yield* Metric.update(cacheLookups, 'miss');
+ return yield* HttpServerResponse.json({ status: 'ok' });
+ }),
+ ),
+
HttpRouter.catchAllCause(cause => {
const error = Cause.squash(cause);
Sentry.captureException(error);
diff --git a/dev-packages/e2e-tests/test-applications/effect-3-node/tests/metrics.test.ts b/dev-packages/e2e-tests/test-applications/effect-3-node/tests/metrics.test.ts
new file mode 100644
index 000000000000..10c64fb0b707
--- /dev/null
+++ b/dev-packages/e2e-tests/test-applications/effect-3-node/tests/metrics.test.ts
@@ -0,0 +1,68 @@
+import { expect, test } from '@playwright/test';
+import { waitForEnvelopeItem, waitForMetric } from '@sentry-internal/test-utils';
+import type { SerializedMetricContainer } from '@sentry/core';
+
+test('sends the increase of an Effect counter since the last flush', async ({ baseURL }) => {
+ // `Sentry.layerMetrics` reads the Effect metrics every 10 seconds, and this test waits for two reads.
+ test.slow();
+
+ const firstMetricPromise = waitForMetric('effect-3-node', metric => metric.name === 'orders_placed');
+
+ await fetch(`${baseURL}/test-metric-counter`);
+
+ expect(await firstMetricPromise).toMatchObject({
+ type: 'counter',
+ value: 1,
+ attributes: { shop: { value: 'e2e', type: 'string' } },
+ });
+
+ const secondMetricPromise = waitForMetric('effect-3-node', metric => metric.name === 'orders_placed');
+
+ await fetch(`${baseURL}/test-metric-counter`);
+
+ expect(await secondMetricPromise).toMatchObject({ type: 'counter', value: 1 });
+});
+
+test('sends Effect gauges, histograms and frequencies', async ({ baseURL }) => {
+ const metricContainerPromise = waitForEnvelopeItem('effect-3-node', envelopeItem => {
+ return (
+ envelopeItem[0].type === 'trace_metric' &&
+ (envelopeItem[1] as SerializedMetricContainer).items.some(metric => metric.name === 'queue_depth')
+ );
+ });
+
+ await fetch(`${baseURL}/test-metrics`);
+
+ const metrics = ((await metricContainerPromise)[1] as SerializedMetricContainer).items;
+ expect(metrics).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({ name: 'queue_depth', type: 'gauge', value: 7 }),
+ expect.objectContaining({ name: 'request_duration_ms.sum', type: 'gauge', value: 200 }),
+ expect.objectContaining({ name: 'request_duration_ms.count', type: 'gauge', value: 2 }),
+ expect.objectContaining({ name: 'request_duration_ms.min', type: 'gauge', value: 80 }),
+ expect.objectContaining({ name: 'request_duration_ms.max', type: 'gauge', value: 120 }),
+ expect.objectContaining({
+ name: 'cache_lookups',
+ type: 'counter',
+ value: 2,
+ attributes: expect.objectContaining({ word: { value: 'hit', type: 'string' } }),
+ }),
+ expect.objectContaining({
+ name: 'cache_lookups',
+ type: 'counter',
+ value: 1,
+ attributes: expect.objectContaining({ word: { value: 'miss', type: 'string' } }),
+ }),
+ ]),
+ );
+});
+
+test('sends the Effect runtime metrics, which Effect v3 records by default', async ({ baseURL }) => {
+ const metricPromise = waitForMetric('effect-3-node', metric => metric.name === 'effect_fiber_started');
+
+ await fetch(`${baseURL}/test-success`);
+
+ const metric = await metricPromise;
+ expect(metric.type).toBe('counter');
+ expect(metric.value).toBeGreaterThan(0);
+});
diff --git a/dev-packages/e2e-tests/test-applications/effect-4-browser/public/index.html b/dev-packages/e2e-tests/test-applications/effect-4-browser/public/index.html
index 19d5c3d99a2f..e0ea20a49f1e 100644
--- a/dev-packages/e2e-tests/test-applications/effect-4-browser/public/index.html
+++ b/dev-packages/e2e-tests/test-applications/effect-4-browser/public/index.html
@@ -38,6 +38,12 @@ Log Tests
+
+
Navigation Test
Navigation Link
diff --git a/dev-packages/e2e-tests/test-applications/effect-4-browser/src/index.js b/dev-packages/e2e-tests/test-applications/effect-4-browser/src/index.js
index 814657a980c0..2282a32a6911 100644
--- a/dev-packages/e2e-tests/test-applications/effect-4-browser/src/index.js
+++ b/dev-packages/e2e-tests/test-applications/effect-4-browser/src/index.js
@@ -2,6 +2,7 @@
import * as Sentry from '@sentry/effect';
import * as Layer from 'effect/Layer';
import * as ManagedRuntime from 'effect/ManagedRuntime';
+import * as Metric from 'effect/Metric';
import * as References from 'effect/References';
import * as Effect from 'effect/Effect';
@@ -16,9 +17,13 @@ const AppLayer = Layer.mergeAll(
}),
Sentry.layerLogger,
Sentry.layerTracer,
+ Sentry.layerMetrics,
Layer.succeed(References.MinimumLogLevel, 'Debug'),
);
+const ordersPlaced = Metric.counter('orders_placed', { attributes: { shop: 'e2e' } });
+const queueDepth = Metric.gauge('queue_depth');
+
// v4 pattern: ManagedRuntime creates a long-lived runtime from the layer
const runtime = ManagedRuntime.make(AppLayer);
@@ -84,6 +89,17 @@ document.getElementById('log-context-button')?.addEventListener('click', async (
if (el) el.textContent = 'Log with context sent!';
});
+document.getElementById('metrics-button')?.addEventListener('click', async () => {
+ await runEffect(() =>
+ Effect.gen(function* () {
+ yield* Metric.update(ordersPlaced, 1);
+ yield* Metric.update(queueDepth, 7);
+ }),
+ );
+ const el = document.getElementById('metrics-result');
+ if (el) el.textContent = 'Metrics updated!';
+});
+
document.getElementById('navigation-link')?.addEventListener('click', () => {
document.getElementById('navigation-target')?.scrollIntoView({ behavior: 'smooth' });
});
diff --git a/dev-packages/e2e-tests/test-applications/effect-4-browser/tests/metrics.test.ts b/dev-packages/e2e-tests/test-applications/effect-4-browser/tests/metrics.test.ts
new file mode 100644
index 000000000000..9e96f3e61ed7
--- /dev/null
+++ b/dev-packages/e2e-tests/test-applications/effect-4-browser/tests/metrics.test.ts
@@ -0,0 +1,30 @@
+import { expect, test } from '@playwright/test';
+import { waitForEnvelopeItem } from '@sentry-internal/test-utils';
+import type { SerializedMetricContainer } from '@sentry/core';
+
+test('sends Effect metrics', async ({ page }) => {
+ const metricContainerPromise = waitForEnvelopeItem('effect-4-browser', envelopeItem => {
+ return (
+ envelopeItem[0].type === 'trace_metric' &&
+ (envelopeItem[1] as SerializedMetricContainer).items.some(metric => metric.name === 'orders_placed')
+ );
+ });
+
+ await page.goto('/');
+ await page.locator('id=metrics-button').click();
+
+ await expect(page.locator('id=metrics-result')).toHaveText('Metrics updated!');
+
+ const metrics = ((await metricContainerPromise)[1] as SerializedMetricContainer).items;
+ expect(metrics).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ name: 'orders_placed',
+ type: 'counter',
+ value: 1,
+ attributes: expect.objectContaining({ shop: { value: 'e2e', type: 'string' } }),
+ }),
+ expect.objectContaining({ name: 'queue_depth', type: 'gauge', value: 7 }),
+ ]),
+ );
+});
diff --git a/dev-packages/e2e-tests/test-applications/effect-4-node/src/app.ts b/dev-packages/e2e-tests/test-applications/effect-4-node/src/app.ts
index 0d86544264ae..4b29171798da 100644
--- a/dev-packages/e2e-tests/test-applications/effect-4-node/src/app.ts
+++ b/dev-packages/e2e-tests/test-applications/effect-4-node/src/app.ts
@@ -4,7 +4,9 @@ import * as Effect from 'effect/Effect';
import * as Cause from 'effect/Cause';
import * as Data from 'effect/Data';
import * as ErrorReporter from 'effect/ErrorReporter';
+import * as Fiber from 'effect/Fiber';
import * as Layer from 'effect/Layer';
+import * as Metric from 'effect/Metric';
import * as Tracer from 'effect/Tracer';
import * as References from 'effect/References';
import { HttpRouter, HttpServerResponse } from 'effect/http';
@@ -20,9 +22,17 @@ const SentryLayer = Layer.mergeAll(
}),
Sentry.layerLogger,
Sentry.layerTracer,
+ Sentry.layerMetrics,
Layer.succeed(References.MinimumLogLevel, 'Debug'),
);
+const ordersPlaced = Metric.counter('orders_placed', { attributes: { shop: 'e2e' } });
+const queueDepth = Metric.gauge('queue_depth');
+const requestDuration = Metric.histogram('request_duration_ms', {
+ boundaries: Metric.linearBoundaries({ start: 0, width: 50, count: 10 }),
+});
+const cacheLookups = Metric.frequency('cache_lookups');
+
class NotFoundError extends Data.TaggedError('NotFoundError')<{ readonly id: string }> {
readonly [ErrorReporter.ignore] = true;
}
@@ -216,6 +226,39 @@ const Routes = Layer.mergeAll(
return yield* HttpServerResponse.json({ message: 'Log with context sent' });
}),
),
+
+ HttpRouter.add(
+ 'GET',
+ '/test-metric-counter',
+ Effect.gen(function* () {
+ yield* Metric.update(ordersPlaced, 1);
+ return yield* HttpServerResponse.json({ status: 'ok' });
+ }),
+ ),
+
+ HttpRouter.add(
+ 'GET',
+ '/test-metrics',
+ Effect.gen(function* () {
+ yield* Metric.update(queueDepth, 7);
+ yield* Metric.update(requestDuration, 120);
+ yield* Metric.update(requestDuration, 80);
+ yield* Metric.update(cacheLookups, 'hit');
+ yield* Metric.update(cacheLookups, 'hit');
+ yield* Metric.update(cacheLookups, 'miss');
+ return yield* HttpServerResponse.json({ status: 'ok' });
+ }),
+ ),
+
+ HttpRouter.add(
+ 'GET',
+ '/test-runtime-metrics',
+ Effect.gen(function* () {
+ const fiber = yield* Effect.forkChild(Effect.sleep('10 millis'));
+ yield* Fiber.join(fiber);
+ return yield* HttpServerResponse.json({ status: 'ok' });
+ }).pipe(Metric.enableRuntimeMetrics),
+ ),
);
const HttpLayer = HttpRouter.serve(Routes).pipe(
diff --git a/dev-packages/e2e-tests/test-applications/effect-4-node/tests/metrics.test.ts b/dev-packages/e2e-tests/test-applications/effect-4-node/tests/metrics.test.ts
new file mode 100644
index 000000000000..2e41af02982f
--- /dev/null
+++ b/dev-packages/e2e-tests/test-applications/effect-4-node/tests/metrics.test.ts
@@ -0,0 +1,66 @@
+import { expect, test } from '@playwright/test';
+import { waitForEnvelopeItem, waitForMetric } from '@sentry-internal/test-utils';
+import type { SerializedMetricContainer } from '@sentry/core';
+
+test('sends the increase of an Effect counter since the last flush', async ({ baseURL }) => {
+ // `Sentry.layerMetrics` reads the Effect metrics every 10 seconds, and this test waits for two reads.
+ test.slow();
+
+ const firstMetricPromise = waitForMetric('effect-4-node', metric => metric.name === 'orders_placed');
+
+ await fetch(`${baseURL}/test-metric-counter`);
+
+ expect(await firstMetricPromise).toMatchObject({
+ type: 'counter',
+ value: 1,
+ attributes: { shop: { value: 'e2e', type: 'string' } },
+ });
+
+ const secondMetricPromise = waitForMetric('effect-4-node', metric => metric.name === 'orders_placed');
+
+ await fetch(`${baseURL}/test-metric-counter`);
+
+ expect(await secondMetricPromise).toMatchObject({ type: 'counter', value: 1 });
+});
+
+test('sends Effect gauges, histograms and frequencies', async ({ baseURL }) => {
+ const metricContainerPromise = waitForEnvelopeItem('effect-4-node', envelopeItem => {
+ return (
+ envelopeItem[0].type === 'trace_metric' &&
+ (envelopeItem[1] as SerializedMetricContainer).items.some(metric => metric.name === 'queue_depth')
+ );
+ });
+
+ await fetch(`${baseURL}/test-metrics`);
+
+ const metrics = ((await metricContainerPromise)[1] as SerializedMetricContainer).items;
+ expect(metrics).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({ name: 'queue_depth', type: 'gauge', value: 7 }),
+ expect.objectContaining({ name: 'request_duration_ms.sum', type: 'gauge', value: 200 }),
+ expect.objectContaining({ name: 'request_duration_ms.count', type: 'gauge', value: 2 }),
+ expect.objectContaining({ name: 'request_duration_ms.min', type: 'gauge', value: 80 }),
+ expect.objectContaining({ name: 'request_duration_ms.max', type: 'gauge', value: 120 }),
+ expect.objectContaining({
+ name: 'cache_lookups',
+ type: 'counter',
+ value: 2,
+ attributes: expect.objectContaining({ word: { value: 'hit', type: 'string' } }),
+ }),
+ expect.objectContaining({
+ name: 'cache_lookups',
+ type: 'counter',
+ value: 1,
+ attributes: expect.objectContaining({ word: { value: 'miss', type: 'string' } }),
+ }),
+ ]),
+ );
+});
+
+test('sends Effect runtime metrics when they are enabled', async ({ baseURL }) => {
+ const metricPromise = waitForMetric('effect-4-node', metric => metric.name === 'child_fibers_started');
+
+ await fetch(`${baseURL}/test-runtime-metrics`);
+
+ expect(await metricPromise).toMatchObject({ type: 'counter', value: 1 });
+});