Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

Commit

Permalink
fix: do not back-off on intake req errors in Lambda env (#180)
Browse files Browse the repository at this point in the history
This ensures that the APM agent instrumentation in a Lambda
function will not cause a `null` response from the user's handler
if the agent cannot talk to the extension.

Note that the spec at elastic/apm#613 related to this might
yet change.

Refs: elastic/apm-agent-nodejs#1831
Refs: elastic/apm#613
  • Loading branch information
trentm committed Mar 10, 2022
1 parent 0073c43 commit 59a9d93
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# elastic-apm-http-client changelog

## Unreleased

- Fix an issue when running in a Lambda function, where a missing or erroring
APM Lambda extension could result in apmclient back-off such that (a) the
end-of-lambda-invocation signaling (`?flushed=true`) would not happen and
(b) premature "beforeExit" event could result in the Lambda Runtime
responding `null` before the Lambda function could respond
(https://github.com/elastic/apm-agent-nodejs/issues/1831).

## v11.0.0

- Add support for coordinating data flushing in an AWS Lambda environment. The
Expand Down
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -781,9 +781,18 @@ Client.prototype._destroy = function (err, cb) {
// Return the appropriate backoff delay (in milliseconds) before a next possible
// request to APM server.
// Spec: https://github.com/elastic/apm/blob/main/specs/agents/transport.md#transport-errors
//
// In a Lambda environment, a backoff delay can be harmful: The backoff
// setTimeout is unref'd, to not hold the process open. A subsequent Lambda
// function invocation during that timer will result in no active handles and
// a process "beforeExit" event. That event is interpreted by the Lambda Runtime
// as "the Lambda function callback was never called", and it terminates the
// function and responds with `null`. The solution is to never backoff in a
// Lambda environment -- we expect and assume the Lambda extension is working,
// and pass responsibility for backoff to the extension.
Client.prototype._getBackoffDelay = function (isErr) {
let reconnectCount = this._backoffReconnectCount
if (isErr) {
if (isErr && !isLambdaExecutionEnvironment) {
this._backoffReconnectCount++
} else {
this._backoffReconnectCount = 0
Expand Down

0 comments on commit 59a9d93

Please sign in to comment.