|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { expect } from 'chai'; |
| 19 | +import { LoggerProvider } from '@opentelemetry/sdk-logs'; |
| 20 | +import { Telemetry } from './public-types'; |
| 21 | +import { Logger, LogRecord, SeverityNumber } from '@opentelemetry/api-logs'; |
| 22 | +import { captureError, flush } from './api'; |
| 23 | + |
| 24 | +const emittedLogs: LogRecord[] = []; |
| 25 | + |
| 26 | +const fakeLoggerProvider = { |
| 27 | + getLogger: (): Logger => { |
| 28 | + return { |
| 29 | + emit: (logRecord: LogRecord) => { |
| 30 | + emittedLogs.push(logRecord); |
| 31 | + } |
| 32 | + }; |
| 33 | + }, |
| 34 | + forceFlush: () => { |
| 35 | + emittedLogs.length = 0; |
| 36 | + return Promise.resolve(); |
| 37 | + }, |
| 38 | + shutdown: () => Promise.resolve() |
| 39 | +} as unknown as LoggerProvider; |
| 40 | + |
| 41 | +const fakeTelemetry: Telemetry = { |
| 42 | + app: { |
| 43 | + name: 'DEFAULT', |
| 44 | + automaticDataCollectionEnabled: true, |
| 45 | + options: { |
| 46 | + projectId: 'my-project', |
| 47 | + appId: 'my-appid' |
| 48 | + } |
| 49 | + }, |
| 50 | + loggerProvider: fakeLoggerProvider |
| 51 | +}; |
| 52 | + |
| 53 | +describe('Top level API', () => { |
| 54 | + beforeEach(() => { |
| 55 | + // Clear the logs before each test. |
| 56 | + emittedLogs.length = 0; |
| 57 | + }); |
| 58 | + |
| 59 | + describe('captureError()', () => { |
| 60 | + it('should capture an Error object correctly', () => { |
| 61 | + const error = new Error('This is a test error'); |
| 62 | + error.stack = '...stack trace...'; |
| 63 | + error.name = 'TestError'; |
| 64 | + |
| 65 | + captureError(fakeTelemetry, error); |
| 66 | + |
| 67 | + expect(emittedLogs.length).to.equal(1); |
| 68 | + const log = emittedLogs[0]; |
| 69 | + expect(log.severityNumber).to.equal(SeverityNumber.ERROR); |
| 70 | + expect(log.body).to.equal('This is a test error'); |
| 71 | + expect(log.attributes).to.deep.equal({ |
| 72 | + 'error.type': 'TestError', |
| 73 | + 'error.stack': '...stack trace...' |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should handle an Error object with no stack trace', () => { |
| 78 | + const error = new Error('error with no stack'); |
| 79 | + error.stack = undefined; |
| 80 | + |
| 81 | + captureError(fakeTelemetry, error); |
| 82 | + |
| 83 | + expect(emittedLogs.length).to.equal(1); |
| 84 | + const log = emittedLogs[0]; |
| 85 | + expect(log.severityNumber).to.equal(SeverityNumber.ERROR); |
| 86 | + expect(log.body).to.equal('error with no stack'); |
| 87 | + expect(log.attributes).to.deep.equal({ |
| 88 | + 'error.type': 'Error', |
| 89 | + 'error.stack': 'No stack trace available' |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should capture a string error correctly', () => { |
| 94 | + captureError(fakeTelemetry, 'a string error'); |
| 95 | + |
| 96 | + expect(emittedLogs.length).to.equal(1); |
| 97 | + const log = emittedLogs[0]; |
| 98 | + expect(log.severityNumber).to.equal(SeverityNumber.ERROR); |
| 99 | + expect(log.body).to.equal('a string error'); |
| 100 | + expect(log.attributes).to.be.undefined; |
| 101 | + }); |
| 102 | + |
| 103 | + it('should capture an unknown error type correctly', () => { |
| 104 | + captureError(fakeTelemetry, 12345); |
| 105 | + |
| 106 | + expect(emittedLogs.length).to.equal(1); |
| 107 | + const log = emittedLogs[0]; |
| 108 | + expect(log.severityNumber).to.equal(SeverityNumber.ERROR); |
| 109 | + expect(log.body).to.equal('Unknown error type: number'); |
| 110 | + expect(log.attributes).to.be.undefined; |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('flush()', () => { |
| 115 | + it('should flush logs correctly', async () => { |
| 116 | + captureError(fakeTelemetry, 'error1'); |
| 117 | + captureError(fakeTelemetry, 'error2'); |
| 118 | + |
| 119 | + expect(emittedLogs.length).to.equal(2); |
| 120 | + |
| 121 | + await flush(fakeTelemetry); |
| 122 | + |
| 123 | + expect(emittedLogs.length).to.equal(0); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments