|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import { initTracing } from '@fxa/shared/otel'; |
| 6 | +import { initSentry } from '@fxa/shared/sentry-node'; |
| 7 | +import { initMonitoring } from './index'; |
| 8 | + |
| 9 | +jest.mock('@fxa/shared/otel', () => ({ initTracing: jest.fn() })); |
| 10 | +jest.mock('@fxa/shared/sentry-node', () => ({ initSentry: jest.fn() })); |
| 11 | + |
| 12 | +describe('shared-monitoring', () => { |
| 13 | + beforeEach(() => { |
| 14 | + jest.resetAllMocks(); |
| 15 | + jest.resetModules(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('initializes tracing and sentry when config contains them', () => { |
| 19 | + const log = { |
| 20 | + info: jest.fn(), |
| 21 | + warn: jest.fn(), |
| 22 | + error: jest.fn(), |
| 23 | + debug: jest.fn(), |
| 24 | + }; |
| 25 | + const opts = { |
| 26 | + log, |
| 27 | + config: { |
| 28 | + sentry: { dsn: 'https://example.com' }, |
| 29 | + tracing: { |
| 30 | + enabled: true, |
| 31 | + serviceName: 'test', |
| 32 | + batchSpanProcessor: true, |
| 33 | + clientName: 'test-client', |
| 34 | + corsUrls: 'http://localhost:\\d*/', |
| 35 | + filterPii: true, |
| 36 | + sampleRate: 1, |
| 37 | + batchProcessor: false, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }; |
| 41 | + |
| 42 | + initMonitoring(opts); |
| 43 | + |
| 44 | + expect(initTracing).toHaveBeenCalledWith(opts.config.tracing, log); |
| 45 | + expect(initSentry).toHaveBeenCalledWith(opts.config, log); |
| 46 | + }); |
| 47 | + |
| 48 | + it('does not call tracing or sentry when not configured', () => { |
| 49 | + const log = { |
| 50 | + warn: jest.fn(), |
| 51 | + error: jest.fn(), |
| 52 | + info: jest.fn(), |
| 53 | + debug: jest.fn(), |
| 54 | + }; |
| 55 | + const opts = { log, config: {} }; |
| 56 | + |
| 57 | + initMonitoring(opts); |
| 58 | + |
| 59 | + expect(initTracing).not.toHaveBeenCalled(); |
| 60 | + expect(initSentry).not.toHaveBeenCalled(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('warns and skips when initialized more than once', () => { |
| 64 | + const log = { |
| 65 | + warn: jest.fn(), |
| 66 | + error: jest.fn(), |
| 67 | + info: jest.fn(), |
| 68 | + debug: jest.fn(), |
| 69 | + }; |
| 70 | + const opts = { |
| 71 | + log, |
| 72 | + config: { |
| 73 | + sentry: { dsn: 'https://example.com' }, |
| 74 | + tracing: { |
| 75 | + enabled: true, |
| 76 | + serviceName: 'test', |
| 77 | + batchSpanProcessor: true, |
| 78 | + clientName: 'test-client', |
| 79 | + corsUrls: 'http://localhost:\\d*/', |
| 80 | + filterPii: true, |
| 81 | + sampleRate: 1, |
| 82 | + batchProcessor: false, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }; |
| 86 | + |
| 87 | + initMonitoring(opts); |
| 88 | + initMonitoring(opts); |
| 89 | + |
| 90 | + expect(log.warn).toHaveBeenCalledWith( |
| 91 | + 'monitoring', |
| 92 | + 'Monitoring can only be initialized once' |
| 93 | + ); |
| 94 | + }); |
| 95 | +}); |
0 commit comments