|
| 1 | +import { MongoDBInstrumentation } from '@opentelemetry/instrumentation-mongodb'; |
| 2 | + |
| 3 | +import { mongoIntegration, instrumentMongo, _defaultDbStatementSerializer } from '../../../src/integrations/tracing/mongo'; |
| 4 | +import { INSTRUMENTED } from '../../../src/otel/instrument'; |
| 5 | + |
| 6 | +jest.mock('@opentelemetry/instrumentation-mongodb'); |
| 7 | + |
| 8 | +describe('Mongo', () => { |
| 9 | + beforeEach(() => { |
| 10 | + jest.clearAllMocks(); |
| 11 | + delete INSTRUMENTED.Mongo; |
| 12 | + |
| 13 | + (MongoDBInstrumentation as unknown as jest.SpyInstance).mockImplementation(() => { |
| 14 | + return { |
| 15 | + setTracerProvider: () => undefined, |
| 16 | + setMeterProvider: () => undefined, |
| 17 | + getConfig: () => ({}), |
| 18 | + setConfig: () => ({}), |
| 19 | + enable: () => undefined, |
| 20 | + }; |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + it('defaults are correct for instrumentMongo', () => { |
| 25 | + instrumentMongo(); |
| 26 | + |
| 27 | + expect(MongoDBInstrumentation).toHaveBeenCalledTimes(1); |
| 28 | + expect(MongoDBInstrumentation).toHaveBeenCalledWith({ |
| 29 | + dbStatementSerializer: expect.any(Function), |
| 30 | + responseHook: expect.any(Function), |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + it('defaults are correct for mongoIntegration', () => { |
| 35 | + mongoIntegration().setupOnce!(); |
| 36 | + |
| 37 | + expect(MongoDBInstrumentation).toHaveBeenCalledTimes(1); |
| 38 | + expect(MongoDBInstrumentation).toHaveBeenCalledWith({ |
| 39 | + responseHook: expect.any(Function), |
| 40 | + dbStatementSerializer: expect.any(Function) |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('_defaultDbStatementSerializer', () => { |
| 45 | + it('rewrites strings as ?', () => { |
| 46 | + const serialized = _defaultDbStatementSerializer({ |
| 47 | + find: 'foo' |
| 48 | + }); |
| 49 | + expect(JSON.parse(serialized).find).toBe('?'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('rewrites nested strings as ?', () => { |
| 53 | + const serialized = _defaultDbStatementSerializer({ |
| 54 | + find: { |
| 55 | + inner: 'foo' |
| 56 | + } |
| 57 | + }); |
| 58 | + expect(JSON.parse(serialized).find.inner).toBe('?'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('rewrites Buffer as ?', () => { |
| 62 | + const serialized = _defaultDbStatementSerializer({ |
| 63 | + find: Buffer.from('foo', 'utf8') |
| 64 | + }); |
| 65 | + expect(JSON.parse(serialized).find).toBe('?'); |
| 66 | + }); |
| 67 | + }); |
| 68 | +}); |
0 commit comments