Skip to content

Disable flushing metrics in thread #120

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 1 commit into from
Mar 29, 2021
Merged

Conversation

tianchu
Copy link
Collaborator

@tianchu tianchu commented Mar 29, 2021

What does this PR do?

Disable flushing_in_thread of threadstats by default. Previously, for synchronous metric submission (without piggybacking on the forwarder and logs), the threadstats is configured to flush the metrics both at the end of the invocation (blocking) and every 10s in a background thread (non-blocking). This PR disables the periodic background flushing.

This change is a soft breaking change, though it shouldn't result in any data loss, it does delay synchronous metric submission for long-running Lambda functions (duration > 10s). In the worst case, metrics submitted at the beginning of an invocation could be delayed for showing up in Datadog for 15 min (the max Lambda duration allowed).

For affected functions, the behavior could be re-enabled by setting env var DD_FLUSH_IN_THREAD to true. However, given the problematic nature of this flushing behavior, I would recommend submitting metrics through the new Datadog Lambda extension instead.

Datadog Forwarder relies on the datadog-lambda to flush metrics, so the datadog-lambda version needs to be bumped to get the issue fixed for the Datadog Forwarder DataDog/datadog-serverless-functions#436

Also fixed the broken and flaky integration tests.

Motivation

A bug has been recently identified (see sample logs below). If there happen to be a background flushing in progress, the end-of-invocation flushing will be skipped (because there is already an in-flight flushing). However, if the background flushing doesn't finish before the AWS freezing the execution environment, it will resume at the beginning of the next invocation. There are two problems with it. First, if AWS decides to destroy the current execution environment, this request will never complete. Second, if the next invocation only happens a few min later (e.g., a cronjob), the request will fail due to connection timeout.

In short, the background thread flushing isn't suitable for Lambda and should be disabled.

# current invocation
Flushing metrics in thread
Flush #8 sending 640 distributions
A flush is already in progress. Skipping this one.
datadog_lambda_wrapper _after() done
REPORT RequestId: d0deabde-936b-467c-aba6-7d4cebdf25c0	Duration: 843.65 ms	Billed Duration: 844 ms	Memory Size: 1024 MB	Max Memory Used: 146 MB	
END RequestId: d0deabde-936b-467c-aba6-7d4cebdf25c0

# next invocation (5 mins later)
START RequestId: faa32e6f-8cd4-4384-aac1-2a5698344e54 Version: $LATEST
Could not request POST https://api.datadoghq.com/api/v1/distribution_points: ('Connection aborted.', timeout('The write operation timed out')). Please check the network connection or try again later.

Testing Guidelines

Additional Notes

Types of Changes

  • Bug fix
  • New feature
  • Breaking change
  • Misc (docs, refactoring, dependency upgrade, etc.)

Check all that apply

  • This PR's description is comprehensive
  • This PR contains breaking changes that are documented in the description
  • This PR introduces new APIs or parameters that are documented and unlikely to change in the foreseeable future
  • This PR impacts documentation, and it has been updated (or a ticket has been logged)
  • This PR's changes are covered by the automated tests
  • This PR collects user input/sensitive content into Datadog
  • This PR passes the integration tests (ask a Datadog member to run the tests)

@tianchu tianchu requested a review from a team as a code owner March 29, 2021 15:58
@tianchu tianchu force-pushed the tian.chu/disable-flush-in-thread branch 5 times, most recently from ed017fa to 64dda84 Compare March 29, 2021 19:50
@tianchu tianchu mentioned this pull request Mar 29, 2021
11 tasks
Copy link
Contributor

@nhinsch nhinsch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I would just suggest we de-emphasize the setting more, either be removing it completely, renaming it, and/or hiding it, to avoid confusion, since it is discouraged/potentially broken to flush metrics in the background.

@tianchu tianchu force-pushed the tian.chu/disable-flush-in-thread branch from 64dda84 to f558fe2 Compare March 29, 2021 20:34
@tianchu tianchu force-pushed the tian.chu/disable-flush-in-thread branch from f558fe2 to aaad49b Compare March 29, 2021 20:35
@tianchu tianchu merged commit f471299 into main Mar 29, 2021
@tianchu tianchu deleted the tian.chu/disable-flush-in-thread branch March 29, 2021 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants