---
title: Early Flake Detection
description: >-
  Detect flakiness before it impacts your default branch using Early Flake
  Detection.
breadcrumbs: >-
  Docs > Test Optimization in Datadog > Working with Flaky Tests > Early Flake
  Detection
---

# Early Flake Detection

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site). ().
{% /alert %}

{% /callout %}

## Overview{% #overview %}

Early Flake Detection is Datadog's test flakiness solution that enhances code quality by identifying [flaky tests](https://docs.datadoghq.com/glossary/#flaky-test) early in the development cycle. For more information about flaky tests, see [Flaky Test Management](https://docs.datadoghq.com/tests/flaky_test_management).

By running newly added tests multiple times, Datadog can detect flakiness before these tests are merged into the default branch. A study shows that up to [75% of flaky tests](https://2020.splashcon.org/details/splash-2020-oopsla/78/A-Large-Scale-Longitudinal-Study-of-Flaky-Tests) can be identified with this approach.

{% dl %}

{% dt %}
Known Tests
{% /dt %}

{% dd %}
Datadog's backend stores unique tests for a given test service. Before a test session runs, the Datadog library fetches the list of these known tests.
{% /dd %}

{% dt %}
Detection of New Tests
{% /dt %}

{% dd %}
If a test is not in the list of known tests, it is considered **new** and is automatically retried up to ten times.
{% /dd %}

{% dt %}
Flakiness Identification
{% /dt %}

{% dd %}
Running a test multiple times helps uncover issues like race conditions, which may cause the test to pass and fail intermittently. If any of the test attempts fail, the test is automatically tagged as flaky.
{% /dd %}

{% /dl %}

Running a test multiple times increases the likelihood of exposing random conditions that cause flakiness. Early Flake Detection helps ensure that only stable, reliable tests are integrated into the default branch:

{% image
   source="https://datadog-docs.imgix.net/images/continuous_integration/early_flake_detection_commit_new_test_explanation_new.2ff48edbe8b00b34cac6c776c24b6dd9.png?auto=format"
   alt="How Early Flake Detection works in your commits" /%}

You can choose to block the merge of the feature branch with a [PR Gate](https://docs.datadoghq.com/pr_gates/). For more information, see the [PR Gates documentation](https://docs.datadoghq.com/pr_gates/setup).

## Setup{% #setup %}

Before implementing Early Flake Detection, you must configure [Test Optimization](https://docs.datadoghq.com/tests) for your development environment. If you are reporting data through the Datadog Agent, use v6.40 or 7.40 and later.

### Configuration{% #configuration %}

After you have set up your Datadog library for Test Optimization, you can configure Early Flake Detection from the [Test Optimization Settings page](https://app.datadoghq.com/ci/settings/test-optimization).

{% image
   source="https://datadog-docs.imgix.net/images/continuous_integration/early_flake_detection_test_settings.157316744c3fcb86e6091cca3c8b1098.png?auto=format"
   alt="Early flake Detection in Test Service Settings." /%}

1. Navigate to [**Software Delivery** > **Test Optimization** > **Settings**](https://app.datadoghq.com/ci/settings/test-optimization).
1. Click **Configure** on the Early Flake Detection column for a test service.
1. Click the toggle to enable Early Flake Detection.

## Compatibility{% #compatibility %}

{% tab title="JavaScript/TypeScript" %}
`dd-trace-js>=5.23.0`

The test framework compatibility is the same as [Test Optimization Compatibility](https://docs.datadoghq.com/tests/setup/javascript/?tab=cloudciprovideragentless#compatibility), with the exception of `playwright`, which is only supported from `>=1.38.0`.
{% /tab %}

{% tab title="Java" %}
`dd-trace-java>=1.34.0`

The test framework compatibility is the same as [Test Optimization Compatibility](https://docs.datadoghq.com/tests/setup/java/#compatibility), with the exception of `Scala Weaver`.
{% /tab %}

{% tab title=".NET" %}
`dd-trace-dotnet>=2.51.0`
{% /tab %}

{% tab title="Python" %}
`dd-trace-py >= 3.0.0` (`pytest >= 7.2.0`)
{% /tab %}

{% tab title="Ruby" %}
`datadog-ci-rb>=1.5.0`
{% /tab %}

{% tab title="Go" %}
`orchestrion >= 0.9.4 + dd-trace-go >= 1.69.1`
{% /tab %}

{% tab title="Swift" %}
`dd-sdk-swift-testing>=2.5.2`
{% /tab %}

{% alert level="danger" %}
Older tracer versions limit the number of known tests fetched to 500k. If you need to fetch more than 500k known tests, update to the latest tracer version.
{% /alert %}

## Explore results in the Test Optimization Explorer{% #explore-results-in-the-test-optimization-explorer %}

You can use the following facets to query sessions that run Early Flake Detection and new tests in the [Test Optimization Explorer](https://docs.datadoghq.com/tests/explorer/).

- **Test Session**: Test sessions running Early Flake Detection have the `@test.early_flake.enabled` tag set to `true`.
- **New Tests**: New tests have the `@test.is_new` tag set to `true`, and retries for this test have the `@test.is_retry` tag set to `true`.

## Troubleshooting{% #troubleshooting %}

If you suspect there are issues with Early Flake Detection, navigate to the [Test Optimization Settings page](https://app.datadoghq.com/ci/settings/test-optimization), look for your repository, and click **Configure**. Disable Early Flake Detection by clicking on the toggle.

### A new test is not being retried{% #a-new-test-is-not-being-retried %}

This could be caused by a couple of reasons:

- This test has ran previously.
- This test is slower than five minutes. There is a mechanism not to run Early Flake Detection on tests that are too slow, since retrying these tests could cause significant delays in CI pipelines.

Finally, older tracer versions limit the number of known tests fetched to 500k. If your repository has more than 500k known tests, no tests will be identified as new. To prevent this, update to the latest tracer version.

### A test was retried that is not new{% #a-test-was-retried-that-is-not-new %}

If a test hasn't been active for more than 14 days, it might be re-identified as new.

If the Datadog library can't fetch the full list of known tests, the Datadog library may retry tests that are not new. There is a mechanism to prevent this error from slowing down the CI pipeline, but if it happens, contact [Datadog Support](https://docs.datadoghq.com/help/).

## Further Reading{% #further-reading %}

- [Learn about Test Optimization](https://docs.datadoghq.com/tests)
- [Learn about Flaky Test Management](https://docs.datadoghq.com/tests/flaky_test_management)
- [Learn about PR Gates](https://docs.datadoghq.com/pr_gates)
