|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | +import { |
| 3 | + _INTERNAL_flushLogsBuffer, |
| 4 | + _INTERNAL_getLogBuffer, |
| 5 | + captureLog, |
| 6 | + logAttributeToSerializedLogAttribute, |
| 7 | +} from '../../../src/logs'; |
| 8 | +import { TestClient, getDefaultTestClientOptions } from '../../mocks/client'; |
| 9 | +import * as loggerModule from '../../../src/utils-hoist/logger'; |
| 10 | +import { Scope } from '../../../src'; |
| 11 | + |
| 12 | +const PUBLIC_DSN = 'https://username@domain/123'; |
| 13 | + |
| 14 | +describe('logAttributeToSerializedLogAttribute', () => { |
| 15 | + it('serializes number values', () => { |
| 16 | + const result = logAttributeToSerializedLogAttribute('count', 42); |
| 17 | + expect(result).toEqual({ |
| 18 | + key: 'count', |
| 19 | + value: { doubleValue: 42 }, |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + it('serializes boolean values', () => { |
| 24 | + const result = logAttributeToSerializedLogAttribute('enabled', true); |
| 25 | + expect(result).toEqual({ |
| 26 | + key: 'enabled', |
| 27 | + value: { boolValue: true }, |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + it('serializes string values', () => { |
| 32 | + const result = logAttributeToSerializedLogAttribute('username', 'john_doe'); |
| 33 | + expect(result).toEqual({ |
| 34 | + key: 'username', |
| 35 | + value: { stringValue: 'john_doe' }, |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + it('serializes object values as JSON strings', () => { |
| 40 | + const obj = { name: 'John', age: 30 }; |
| 41 | + const result = logAttributeToSerializedLogAttribute('user', obj); |
| 42 | + expect(result).toEqual({ |
| 43 | + key: 'user', |
| 44 | + value: { stringValue: JSON.stringify(obj) }, |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('serializes array values as JSON strings', () => { |
| 49 | + const array = [1, 2, 3, 'test']; |
| 50 | + const result = logAttributeToSerializedLogAttribute('items', array); |
| 51 | + expect(result).toEqual({ |
| 52 | + key: 'items', |
| 53 | + value: { stringValue: JSON.stringify(array) }, |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it('serializes undefined values as empty strings', () => { |
| 58 | + const result = logAttributeToSerializedLogAttribute('missing', undefined); |
| 59 | + expect(result).toEqual({ |
| 60 | + key: 'missing', |
| 61 | + value: { stringValue: '' }, |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('serializes null values as JSON strings', () => { |
| 66 | + const result = logAttributeToSerializedLogAttribute('empty', null); |
| 67 | + expect(result).toEqual({ |
| 68 | + key: 'empty', |
| 69 | + value: { stringValue: 'null' }, |
| 70 | + }); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe('captureLog', () => { |
| 75 | + it('captures and sends logs', () => { |
| 76 | + const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, _experiments: { enableLogs: true } }); |
| 77 | + const client = new TestClient(options); |
| 78 | + |
| 79 | + captureLog({ level: 'info', message: 'test log message' }, undefined, client); |
| 80 | + expect(_INTERNAL_getLogBuffer(client)).toHaveLength(1); |
| 81 | + expect(_INTERNAL_getLogBuffer(client)?.[0]).toEqual( |
| 82 | + expect.objectContaining({ |
| 83 | + severityText: 'info', |
| 84 | + body: { |
| 85 | + stringValue: 'test log message', |
| 86 | + }, |
| 87 | + timeUnixNano: expect.any(String), |
| 88 | + }), |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it('does not capture logs when enableLogs experiment is not enabled', () => { |
| 93 | + const logWarnSpy = vi.spyOn(loggerModule.logger, 'warn').mockImplementation(() => undefined); |
| 94 | + const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN }); |
| 95 | + const client = new TestClient(options); |
| 96 | + |
| 97 | + captureLog({ level: 'info', message: 'test log message' }, undefined, client); |
| 98 | + |
| 99 | + expect(logWarnSpy).toHaveBeenCalledWith('logging option not enabled, log will not be captured.'); |
| 100 | + expect(_INTERNAL_getLogBuffer(client)).toBeUndefined(); |
| 101 | + |
| 102 | + logWarnSpy.mockRestore(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('includes trace context when available', () => { |
| 106 | + const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, _experiments: { enableLogs: true } }); |
| 107 | + const client = new TestClient(options); |
| 108 | + const scope = new Scope(); |
| 109 | + scope.setPropagationContext({ |
| 110 | + traceId: '3d9355f71e9c444b81161599adac6e29', |
| 111 | + sampleRand: 1, |
| 112 | + }); |
| 113 | + |
| 114 | + captureLog({ level: 'error', message: 'test log with trace' }, scope, client); |
| 115 | + |
| 116 | + expect(_INTERNAL_getLogBuffer(client)?.[0]).toEqual( |
| 117 | + expect.objectContaining({ |
| 118 | + traceId: '3d9355f71e9c444b81161599adac6e29', |
| 119 | + }), |
| 120 | + ); |
| 121 | + }); |
| 122 | + |
| 123 | + it('includes release and environment in log attributes when available', () => { |
| 124 | + const options = getDefaultTestClientOptions({ |
| 125 | + dsn: PUBLIC_DSN, |
| 126 | + _experiments: { enableLogs: true }, |
| 127 | + release: '1.0.0', |
| 128 | + environment: 'test', |
| 129 | + }); |
| 130 | + const client = new TestClient(options); |
| 131 | + |
| 132 | + captureLog({ level: 'info', message: 'test log with metadata' }, undefined, client); |
| 133 | + |
| 134 | + const logAttributes = _INTERNAL_getLogBuffer(client)?.[0]?.attributes; |
| 135 | + expect(logAttributes).toEqual( |
| 136 | + expect.arrayContaining([ |
| 137 | + expect.objectContaining({ key: 'release', value: { stringValue: '1.0.0' } }), |
| 138 | + expect.objectContaining({ key: 'environment', value: { stringValue: 'test' } }), |
| 139 | + ]), |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + it('includes custom attributes in log', () => { |
| 144 | + const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, _experiments: { enableLogs: true } }); |
| 145 | + const client = new TestClient(options); |
| 146 | + |
| 147 | + captureLog( |
| 148 | + { |
| 149 | + level: 'info', |
| 150 | + message: 'test log with custom attributes', |
| 151 | + attributes: { userId: '123', component: 'auth' }, |
| 152 | + }, |
| 153 | + undefined, |
| 154 | + client, |
| 155 | + ); |
| 156 | + |
| 157 | + const logAttributes = _INTERNAL_getLogBuffer(client)?.[0]?.attributes; |
| 158 | + expect(logAttributes).toEqual( |
| 159 | + expect.arrayContaining([ |
| 160 | + expect.objectContaining({ key: 'userId', value: { stringValue: '123' } }), |
| 161 | + expect.objectContaining({ key: 'component', value: { stringValue: 'auth' } }), |
| 162 | + ]), |
| 163 | + ); |
| 164 | + }); |
| 165 | + |
| 166 | + it('flushes logs buffer when it reaches max size', () => { |
| 167 | + const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, _experiments: { enableLogs: true } }); |
| 168 | + const client = new TestClient(options); |
| 169 | + |
| 170 | + // Fill the buffer to max size (100 is the MAX_LOG_BUFFER_SIZE constant in client.ts) |
| 171 | + for (let i = 0; i < 100; i++) { |
| 172 | + captureLog({ level: 'info', message: `log message ${i}` }, undefined, client); |
| 173 | + } |
| 174 | + |
| 175 | + expect(_INTERNAL_getLogBuffer(client)).toHaveLength(100); |
| 176 | + |
| 177 | + // Add one more to trigger flush |
| 178 | + captureLog({ level: 'info', message: 'trigger flush' }, undefined, client); |
| 179 | + |
| 180 | + expect(_INTERNAL_getLogBuffer(client)).toEqual([]); |
| 181 | + }); |
| 182 | + |
| 183 | + it('does not flush logs buffer when it is empty', () => { |
| 184 | + const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, _experiments: { enableLogs: true } }); |
| 185 | + const client = new TestClient(options); |
| 186 | + const mockSendEnvelope = vi.spyOn(client as any, 'sendEnvelope').mockImplementation(() => {}); |
| 187 | + _INTERNAL_flushLogsBuffer(client); |
| 188 | + expect(mockSendEnvelope).not.toHaveBeenCalled(); |
| 189 | + }); |
| 190 | +}); |
0 commit comments