-
Notifications
You must be signed in to change notification settings - Fork 44
Detect blocking sync operations during function executions #558 #582
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
4 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { AzureFunctionsRpcMessages as rpc } from './../../azure-functions-language-worker-protobuf/src/rpc'; | ||
| import LogCategory = rpc.RpcLog.RpcLogCategory; | ||
| import LogLevel = rpc.RpcLog.Level; | ||
| import blockedAt = require('blocked-at'); | ||
|
|
||
| export function startBlockedMonitor( | ||
| channel: { log: (log: rpc.IRpcLog) => void }, | ||
| threshold = 500, | ||
| intreval = 10000 | ||
| ): NodeJS.Timer { | ||
| function logBlockedWarning(message: string) { | ||
| channel.log({ | ||
| message, | ||
| level: LogLevel.Warning, | ||
| logCategory: LogCategory.System, | ||
| }); | ||
| } | ||
|
|
||
| logBlockedWarning( | ||
| `Monitoring for blocking code is turned on, with a threshold of ${threshold} ms. This will have a negative impact on performance. Adjust "AZURE_FUNCTIONS_NODE_BLOCK_LOG" to turn it off. ` + | ||
| 'IMPORTANT NOTE: The stack traces are only an approximation and you should analyze all synchronous operations' | ||
| ); | ||
|
|
||
| let blockedHistory: { time: string; duration: number; stack: string[] }[] = []; | ||
|
|
||
| //threshold - minimum miliseconds of blockage to report. | ||
| //other parameters are default, more details on https://github.com/naugtur/blocked-at. | ||
| blockedAt( | ||
| (ms, stack) => { | ||
| const date = new Date(); | ||
| blockedHistory.push({ time: date.toISOString(), duration: ms, stack: stack }); | ||
| }, | ||
| { threshold: threshold } | ||
| ); | ||
|
|
||
| // Log blockedHistory if it's not empty each 10 seconds | ||
| return setInterval(() => { | ||
ejizba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (blockedHistory.length > 0) { | ||
| logBlockedWarning(`Blocking code monitoring history: ${JSON.stringify(blockedHistory)}`); | ||
| blockedHistory = []; | ||
| } | ||
| }, intreval); | ||
| } | ||
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,6 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| export function isEnvironmentVariableSet(val: string | boolean | number | undefined | null): boolean { | ||
| return !/^(false|0)?$/i.test(val === undefined || val === null ? '' : String(val)); | ||
| } |
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,44 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { expect } from 'chai'; | ||
| import 'mocha'; | ||
| import { AzureFunctionsRpcMessages as rpc } from './../azure-functions-language-worker-protobuf/src/rpc'; | ||
| import { startBlockedMonitor } from './../src/utils/blockedMonitor'; | ||
| import LogLevel = rpc.RpcLog.Level; | ||
|
|
||
| describe('Event loop blocking operation monitor', () => { | ||
| it('startBlockMonitor logs warning', async () => { | ||
| console.log('start ' + new Date().getSeconds() + ':' + new Date().getMilliseconds()); | ||
| let timer: NodeJS.Timer | null = null; | ||
| let isTimerDestroyed = false; | ||
| const logFun = function (log: rpc.IRpcLog): void { | ||
| expect(log.level).to.equal(LogLevel.Warning); | ||
| if (log.message && log.message.startsWith('Blocking code monitoring history')) { | ||
| if (timer) { | ||
| clearInterval(timer); | ||
| isTimerDestroyed = true; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| timer = startBlockedMonitor({ log: logFun }, 100, 100); | ||
| await new Promise((resolve) => { | ||
| //Adding new event to event loop to start monitoring | ||
| setTimeout(() => { | ||
| resolve(true); | ||
| }, 1); | ||
| }); | ||
| const end = Date.now() + 500; | ||
| while (Date.now() < end) {} // blocking code | ||
|
|
||
| await new Promise((resolve) => { | ||
| //assert | ||
| setTimeout(() => { | ||
| if (isTimerDestroyed) { | ||
| resolve(true); | ||
| } | ||
| }, 500); | ||
| }); | ||
| }); | ||
| }); |
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.