import { describe, expect, it } from '@effect/vitest'; import * as sentryCore from '@sentry/core'; import * as Context from 'effect/Context'; import { Duration, Effect, Layer, Metric } from 'effect'; import { TestClock } from 'effect/testing'; import { afterEach, beforeEach, vi } from 'vitest'; import { layerMetrics } from '../src/metrics'; describe('layerMetrics', () => { const mockCount = vi.fn(); const mockGauge = vi.fn(); const mockDistribution = vi.fn(); beforeEach(() => { vi.spyOn(sentryCore.metrics, 'count').mockImplementation(mockCount); vi.spyOn(sentryCore.metrics, 'gauge').mockImplementation(mockGauge); vi.spyOn(sentryCore.metrics, 'distribution').mockImplementation(mockDistribution); }); afterEach(() => { vi.clearAllMocks(); vi.restoreAllMocks(); }); it.effect('creates counter metrics', () => Effect.gen(function* () { const counter = Metric.counter('test_counter'); yield* Metric.update(counter, 1); yield* Metric.update(counter, 1); yield* Metric.update(counter, 5); const snapshot = Metric.snapshotUnsafe(Context.empty()); const counterMetric = snapshot.find(p => p.id === 'test_counter'); expect(counterMetric).toBeDefined(); }), ); it.effect('creates gauge metrics', () => Effect.gen(function* () { const gauge = Metric.gauge('test_gauge'); yield* Metric.update(gauge, 42); const snapshot = Metric.snapshotUnsafe(Context.empty()); const gaugeMetric = snapshot.find(p => p.id === 'test_gauge'); expect(gaugeMetric).toBeDefined(); }), ); it.effect('creates histogram metrics', () => Effect.gen(function* () { const histogram = Metric.histogram('test_histogram', { boundaries: Metric.linearBoundaries({ start: 0, width: 10, count: 10 }), }); yield* Metric.update(histogram, 5); yield* Metric.update(histogram, 15); yield* Metric.update(histogram, 25); const snapshot = Metric.snapshotUnsafe(Context.empty()); const histogramMetric = snapshot.find(p => p.id === 'test_histogram'); expect(histogramMetric).toBeDefined(); }), ); it.effect('creates summary metrics', () => Effect.gen(function* () { const summary = Metric.summary('test_summary', { maxAge: '1 minute', maxSize: 100, error: 0.01, quantiles: [0.5, 0.9, 0.99], }); yield* Metric.update(summary, 10); yield* Metric.update(summary, 20); yield* Metric.update(summary, 30); const snapshot = Metric.snapshotUnsafe(Context.empty()); const summaryMetric = snapshot.find(p => p.id === 'test_summary'); expect(summaryMetric).toBeDefined(); }), ); it.effect('creates frequency metrics', () => Effect.gen(function* () { const frequency = Metric.frequency('test_frequency'); yield* Metric.update(frequency, 'foo'); yield* Metric.update(frequency, 'bar'); yield* Metric.update(frequency, 'foo'); const snapshot = Metric.snapshotUnsafe(Context.empty()); const frequencyMetric = snapshot.find(p => p.id === 'test_frequency'); expect(frequencyMetric).toBeDefined(); }), ); it.effect('supports metrics with attributes', () => Effect.gen(function* () { const counter = Metric.counter('labeled_counter').pipe( Metric.withAttributes({ env: 'test', service: 'my-service' }), ); yield* Metric.update(counter, 1); const snapshot = Metric.snapshotUnsafe(Context.empty()); const labeledMetric = snapshot.find(p => p.id === 'labeled_counter'); expect(labeledMetric).toBeDefined(); const attrs = labeledMetric?.attributes ?? {}; expect(attrs['env']).toBe('test'); expect(attrs['service']).toBe('my-service'); }), ); it.effect('tracks Effect durations with histogram metric', () => Effect.gen(function* () { const histogram = Metric.histogram('operation_duration', { boundaries: Metric.linearBoundaries({ start: 10, width: 100, count: 10 }), }); yield* Metric.update(histogram, Duration.millis(50)); const snapshot = Metric.snapshotUnsafe(Context.empty()); const timerMetric = snapshot.find(p => p.id === 'operation_duration'); expect(timerMetric).toBeDefined(); }), ); it.effect('integrates with Effect.timed', () => Effect.gen(function* () { const [duration, result] = yield* Effect.timed(Effect.succeed('completed')); expect(result).toBe('completed'); expect(Duration.toMillis(duration)).toBeGreaterThanOrEqual(0); }), ); }); describe('layerMetrics flushing', () => { const mockCount = vi.fn(); const mockGauge = vi.fn(); const mockDistribution = vi.fn(); beforeEach(() => { vi.spyOn(sentryCore.metrics, 'count').mockImplementation(mockCount); vi.spyOn(sentryCore.metrics, 'gauge').mockImplementation(mockGauge); vi.spyOn(sentryCore.metrics, 'distribution').mockImplementation(mockDistribution); }); afterEach(() => { vi.clearAllMocks(); vi.restoreAllMocks(); }); const TestLayer = layerMetrics.pipe(Layer.provideMerge(TestClock.layer())); it.effect('sends counter metrics to Sentry', () => Effect.gen(function* () { const counter = Metric.counter('flush_test_counter'); yield* Metric.update(counter, 1); yield* Metric.update(counter, 4); yield* TestClock.adjust('10 seconds'); expect(mockCount).toHaveBeenCalledWith('flush_test_counter', 5, { attributes: {} }); }).pipe(Effect.provide(TestLayer)), ); it.effect('sends gauge metrics to Sentry', () => Effect.gen(function* () { const gauge = Metric.gauge('flush_test_gauge'); yield* Metric.update(gauge, 42); yield* TestClock.adjust('10 seconds'); expect(mockGauge).toHaveBeenCalledWith('flush_test_gauge', 42, { attributes: {} }); }).pipe(Effect.provide(TestLayer)), ); it.effect('sends histogram metrics to Sentry', () => Effect.gen(function* () { const histogram = Metric.histogram('flush_test_histogram', { boundaries: Metric.linearBoundaries({ start: 0, width: 10, count: 5 }), }); yield* Metric.update(histogram, 5); yield* Metric.update(histogram, 15); yield* TestClock.adjust('10 seconds'); expect(mockGauge).toHaveBeenCalledWith('flush_test_histogram.sum', expect.any(Number), { attributes: {} }); expect(mockGauge).toHaveBeenCalledWith('flush_test_histogram.count', expect.any(Number), { attributes: {} }); expect(mockGauge).toHaveBeenCalledWith('flush_test_histogram.min', expect.any(Number), { attributes: {} }); expect(mockGauge).toHaveBeenCalledWith('flush_test_histogram.max', expect.any(Number), { attributes: {} }); }).pipe(Effect.provide(TestLayer)), ); it.effect('sends summary metrics to Sentry', () => Effect.gen(function* () { const summary = Metric.summary('flush_test_summary', { maxAge: '1 minute', maxSize: 100, error: 0.01, quantiles: [0.5, 0.9, 0.99], }); yield* Metric.update(summary, 10); yield* Metric.update(summary, 20); yield* Metric.update(summary, 30); yield* TestClock.adjust('10 seconds'); expect(mockGauge).toHaveBeenCalledWith('flush_test_summary.sum', 60, { attributes: {} }); expect(mockGauge).toHaveBeenCalledWith('flush_test_summary.count', 3, { attributes: {} }); expect(mockGauge).toHaveBeenCalledWith('flush_test_summary.min', 10, { attributes: {} }); expect(mockGauge).toHaveBeenCalledWith('flush_test_summary.max', 30, { attributes: {} }); }).pipe(Effect.provide(TestLayer)), ); it.effect('sends frequency metrics to Sentry', () => Effect.gen(function* () { const frequency = Metric.frequency('flush_test_frequency'); yield* Metric.update(frequency, 'apple'); yield* Metric.update(frequency, 'banana'); yield* Metric.update(frequency, 'apple'); yield* TestClock.adjust('10 seconds'); expect(mockCount).toHaveBeenCalledWith('flush_test_frequency', 2, { attributes: { word: 'apple' } }); expect(mockCount).toHaveBeenCalledWith('flush_test_frequency', 1, { attributes: { word: 'banana' } }); }).pipe(Effect.provide(TestLayer)), ); it.effect('sends metrics with attributes to Sentry', () => Effect.gen(function* () { const gauge = Metric.gauge('flush_test_labeled_gauge').pipe( Metric.withAttributes({ env: 'production', region: 'us-east' }), ); yield* Metric.update(gauge, 100); yield* TestClock.adjust('10 seconds'); expect(mockGauge).toHaveBeenCalledWith('flush_test_labeled_gauge', 100, { attributes: { env: 'production', region: 'us-east' }, }); }).pipe(Effect.provide(TestLayer)), ); it.effect('sends counter delta values on subsequent flushes', () => Effect.gen(function* () { const counter = Metric.counter('flush_test_delta_counter'); yield* Metric.update(counter, 10); yield* TestClock.adjust('10 seconds'); mockCount.mockClear(); yield* Metric.update(counter, 5); yield* TestClock.adjust('10 seconds'); expect(mockCount).toHaveBeenCalledWith('flush_test_delta_counter', 5, { attributes: {} }); }).pipe(Effect.provide(TestLayer)), ); it.effect('does not send counter when delta is zero', () => Effect.gen(function* () { const counter = Metric.counter('flush_test_zero_delta'); yield* Metric.update(counter, 10); yield* TestClock.adjust('10 seconds'); mockCount.mockClear(); yield* TestClock.adjust('10 seconds'); expect(mockCount).not.toHaveBeenCalledWith('flush_test_zero_delta', 0, { attributes: {} }); }).pipe(Effect.provide(TestLayer)), ); });