-
Notifications
You must be signed in to change notification settings - Fork 900
feat: add global error handler #1514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7c2e783
feat: add logging error handler
mwear 2d5d2da
feat: add global error handler
mwear b058bb7
feat: use global error handler in collector exporter
mwear 4bde8c6
feat: use global error handler in collector exporter (proto over http)
mwear 3653178
feat: use global error handler in collector exporter (grpc)
mwear 1c2453d
feat: more paranoia
mwear 51b4c26
chore: lint fix
mwear 3594b78
chore: remove logging
mwear bac4fa4
feat: use global error handler in jaeger exporter
mwear 166d0c5
feat: use global error handler in prometheus exporter
mwear 43945ad
feat: use global error handler in zipkin exporter
mwear c6c4db8
refactor: make CollectorExporterError a subclass of Error
mwear ed1d3eb
refactor: use JSON.stringify to format log messages
mwear da941ad
fix: do not use globalErrorHandler in collector exporter subclasses
mwear b66df44
fix: remove redundant usage of global error handler
mwear 9890c3b
refactor: log error instead of reject in PushController
mwear f9820f3
refactor: log error instead of reject in MultiSpanProcessor#forceFlush
mwear d5c5337
feat: add additional globalErrorHandler tests
mwear File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.