forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add global error handler (open-telemetry#1514)
- Loading branch information
Showing
29 changed files
with
582 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/opentelemetry-core/src/common/global-error-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Exception } from '@opentelemetry/api'; | ||
import { loggingErrorHandler } from './logging-error-handler'; | ||
import { ErrorHandler } from './types'; | ||
|
||
/** The global error handler delegate */ | ||
let delegateHandler = loggingErrorHandler(); | ||
|
||
/** | ||
* Set the global error handler | ||
* @param {ErrorHandler} handler | ||
*/ | ||
export function setGlobalErrorHandler(handler: ErrorHandler) { | ||
delegateHandler = handler; | ||
} | ||
|
||
/** | ||
* Return the global error handler | ||
* @param {Exception} ex | ||
*/ | ||
export const globalErrorHandler = (ex: Exception) => { | ||
try { | ||
delegateHandler(ex); | ||
} catch {} // eslint-disable-line no-empty | ||
}; |
66 changes: 66 additions & 0 deletions
66
packages/opentelemetry-core/src/common/logging-error-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Logger, Exception } from '@opentelemetry/api'; | ||
import { ConsoleLogger } from './ConsoleLogger'; | ||
import { ErrorHandler, LogLevel } from './types'; | ||
|
||
/** | ||
* Returns a function that logs an error using the provided logger, or a | ||
* console logger if one was not provided. | ||
* @param {Logger} logger | ||
*/ | ||
export function loggingErrorHandler(logger?: Logger): ErrorHandler { | ||
logger = logger ?? new ConsoleLogger(LogLevel.ERROR); | ||
return (ex: Exception) => { | ||
logger!.error(stringifyException(ex)); | ||
}; | ||
} | ||
|
||
/** | ||
* Converts an exception into a string representation | ||
* @param {Exception} ex | ||
*/ | ||
function stringifyException(ex: Exception | string): string { | ||
if (typeof ex === 'string') { | ||
return ex; | ||
} else { | ||
return JSON.stringify(flattenException(ex)); | ||
} | ||
} | ||
|
||
/** | ||
* Flattens an exception into key-value pairs by traversing the prototype chain | ||
* and coercing values to strings. Duplicate properties will not be overwritten; | ||
* the first insert wins. | ||
*/ | ||
function flattenException(ex: Exception): Record<string, string> { | ||
const result = {} as Record<string, string>; | ||
let current = ex; | ||
|
||
while (current !== null) { | ||
Object.getOwnPropertyNames(current).forEach(propertyName => { | ||
if (result[propertyName]) return; | ||
const value = current[propertyName as keyof typeof current]; | ||
if (value) { | ||
result[propertyName] = String(value); | ||
} | ||
}); | ||
current = Object.getPrototypeOf(current); | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
packages/opentelemetry-core/test/common/global-error-handler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as assert from 'assert'; | ||
import * as sinon from 'sinon'; | ||
import { globalErrorHandler, setGlobalErrorHandler } from '../../src'; | ||
import { Exception } from '@opentelemetry/api'; | ||
|
||
describe('globalErrorHandler', () => { | ||
let defaultHandler: sinon.SinonSpy; | ||
|
||
beforeEach(() => { | ||
defaultHandler = sinon.spy(); | ||
setGlobalErrorHandler(defaultHandler); | ||
}); | ||
|
||
it('receives errors', () => { | ||
const err = new Error('this is bad'); | ||
globalErrorHandler(err); | ||
sinon.assert.calledOnceWithExactly(defaultHandler, err); | ||
}); | ||
|
||
it('replaces delegate when handler is updated', () => { | ||
const err = new Error('this is bad'); | ||
const newHandler = sinon.spy(); | ||
setGlobalErrorHandler(newHandler); | ||
|
||
globalErrorHandler(err); | ||
|
||
sinon.assert.calledOnceWithExactly(newHandler, err); | ||
sinon.assert.notCalled(defaultHandler); | ||
}); | ||
|
||
it('catches exceptions thrown in handler', () => { | ||
setGlobalErrorHandler((ex: Exception) => { | ||
throw new Error('bad things'); | ||
}); | ||
|
||
assert.doesNotThrow(() => { | ||
globalErrorHandler('an error'); | ||
}); | ||
}); | ||
}); |
75 changes: 75 additions & 0 deletions
75
packages/opentelemetry-core/test/common/logging-error-handler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as assert from 'assert'; | ||
import * as sinon from 'sinon'; | ||
import { ErrorHandler, loggingErrorHandler } from '../../src'; | ||
|
||
describe('loggingErrorHandler', () => { | ||
let handler: ErrorHandler; | ||
const errorStub = sinon.fake(); | ||
|
||
beforeEach(() => { | ||
handler = loggingErrorHandler({ | ||
debug: sinon.fake(), | ||
info: sinon.fake(), | ||
warn: sinon.fake(), | ||
error: errorStub, | ||
}); | ||
}); | ||
|
||
it('logs from string', () => { | ||
const err = 'not found'; | ||
handler(err); | ||
assert.ok(errorStub.calledOnceWith(err)); | ||
}); | ||
|
||
it('logs from an object', () => { | ||
const err = { | ||
name: 'NotFoundError', | ||
message: 'not found', | ||
randomString: 'random value', | ||
randomNumber: 42, | ||
randomArray: [1, 2, 3], | ||
randomObject: { a: 'a' }, | ||
stack: 'a stack', | ||
}; | ||
|
||
handler(err); | ||
|
||
const [result] = errorStub.lastCall.args; | ||
|
||
assert.ok(result.includes(err.name)); | ||
assert.ok(result.includes(err.message)); | ||
assert.ok(result.includes(err.randomString)); | ||
assert.ok(result.includes(err.randomNumber)); | ||
assert.ok(result.includes(err.randomArray)); | ||
assert.ok(result.includes(err.randomObject)); | ||
assert.ok(result.includes(JSON.stringify(err.stack))); | ||
}); | ||
|
||
it('logs from an error', () => { | ||
const err = new Error('this is bad'); | ||
|
||
handler(err); | ||
|
||
const [result] = errorStub.lastCall.args; | ||
|
||
assert.ok(result.includes(err.name)); | ||
assert.ok(result.includes(err.message)); | ||
assert.ok(result.includes(JSON.stringify(err.stack))); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.