---
title: Record Feature Flag Exposures in Your Own Data Store
description: >-
  Use the Datadog ExposureHook to record feature flag exposures in your own
  logs, data warehouse, or analytics pipeline, and optionally stop sending
  exposures to Datadog.
breadcrumbs: >-
  Docs > Feature Flags > Feature Flags Guides > Record Feature Flag Exposures in
  Your Own Data Store
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# Record Feature Flag Exposures in Your Own Data Store

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

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

{% /callout %}

## Overview{% #overview %}

The Datadog Feature Flags SDKs send *exposures* to Datadog: records of which subject received which variant of a flag, and when. You can also record these exposures in your own system, such as your logs, a data warehouse, or an analytics pipeline. You can do this instead of sending exposures to Datadog, or in addition to it.

Each supported SDK includes an `ExposureHook`, an [OpenFeature hook](https://openfeature.dev/docs/reference/concepts/hooks) that calls your code after every flag evaluation. The hook tells you whether the evaluation is an exposure, and whether it is new. It uses the same decision that the SDK uses when it sends exposures to Datadog, so your records match what Datadog records.

## Supported SDKs{% #supported-sdks %}

| SDK                                                                                                   | Minimum version                                |
| ----------------------------------------------------------------------------------------------------- | ---------------------------------------------- |
| [Java](https://docs.datadoghq.com/feature_flags/server/java.md)                                       | `dd-java-agent` and `dd-openfeature` 1.68.0    |
| [Go](https://docs.datadoghq.com/feature_flags/server/go.md)                                           | `dd-trace-go` 2.12.0                           |
| [Node.js](https://docs.datadoghq.com/feature_flags/server/nodejs.md)                                  | `dd-trace` 6.24.0 (6.x) or 5.135.0 (5.x)       |
| [Ruby](https://docs.datadoghq.com/feature_flags/server/ruby.md)                                       | `datadog` 2.49.0, with `openfeature-sdk` 0.5.1 |
| [Browser](https://docs.datadoghq.com/feature_flags/client/javascript.md) (JavaScript, React, Angular) | `@datadog/openfeature-browser` 2.3.0           |

## How the SDK identifies exposures{% #how-the-sdk-identifies-exposures %}

An evaluation is an exposure when it succeeds and selects an allocation that logs exposures, such as an experiment or a holdout. A failed evaluation is not an exposure, including an evaluation that a later hook marks as failed.

The SDK deduplicates exposures for each flag and subject. It records an exposure again only when the subject's allocation, variant, or split changes. A repeated evaluation with the same result is a *cache hit*.

## Record exposures with ExposureHook{% #record-exposures-with-exposurehook %}

Create an `ExposureHook` with a callback and register it with OpenFeature. The callback runs for every completed evaluation, including evaluations that error, evaluations that are not exposures, and repeats. Use `shouldSend()` to keep only new exposures.

The callback runs on the evaluation thread, inside your request. Do not write to your data store or do other blocking I/O in the callback. Copy the values you need to a background queue, and write them to your data store from that queue. An error in the callback does not affect the flag evaluation.

{% tab title="Java" %}

```java
import com.datadog.openfeature.ExposureHook;
import dev.openfeature.sdk.OpenFeatureAPI;

BlockingQueue<ExposureRecord> exposures = new LinkedBlockingQueue<>(10_000);

OpenFeatureAPI.getInstance().addHooks(new ExposureHook(evaluation -> {
    if (!evaluation.shouldSend()) {
        return;
    }
    // ExposureRecord is your application's own type.
    exposures.offer(new ExposureRecord(
        Instant.now(),
        evaluation.getTargetingKey(),
        evaluation.getFlagKey(),
        evaluation.getAllocationKey(),
        evaluation.getVariant(),
        evaluation.getFeatures()));
}));
// A background thread takes records from `exposures` and writes them to your data store.
```

{% alert level="info" %}
The example requires version 1.69.0 or later. In version 1.68.x, import `datadog.trace.api.openfeature.ExposureHook`, read the flag key, variant, and targeting key from `getDetails()` and `getContext()`, and read the allocation key with `getDetails().getFlagMetadata().getString("allocationKey")`.
{% /alert %}

Evaluate flags through an OpenFeature client. If you call the Datadog provider directly, the SDK records no exposure.
{% /tab %}

{% tab title="Go" %}

```go
import ddopenfeature "github.com/DataDog/dd-trace-go/v2/openfeature"

exposures := make(chan ExposureRecord, 10000)

client.AddHooks(ddopenfeature.NewExposureHook(func(e ddopenfeature.ExposureEvaluation) {
    if !e.ShouldSend() {
        return
    }
    allocation, _ := e.AllocationKey()
    // ExposureRecord is your application's own type.
    record := ExposureRecord{
        Timestamp:  time.Now(),
        Subject:    e.Context.TargetingKey(),
        Flag:       e.Details.FlagKey,
        Allocation: allocation,
        Variant:    e.Details.Variant,
        Features:   e.Features(),
    }
    select {
    case exposures <- record:
    default:
        // The queue is full. Drop the record instead of blocking the evaluation.
    }
}))
// A background goroutine reads from `exposures` and writes the records to your data store.
```

Create the hook once and reuse it. The callback can run concurrently, so make it safe to call from more than one goroutine.

Do not change `e.Details.Value`. For object flags, the value can share data with the provider. Copy it before you change it or process it in the background.
{% /tab %}

{% tab title="Node.js" %}

```javascript
const tracer = require('dd-trace').init()
const { OpenFeature } = require('@openfeature/server-sdk')
const { ExposureHook } = require('dd-trace/openfeature/exposure-hook')

const exposures = []

OpenFeature.addHooks(
  new ExposureHook((evaluation) => {
    if (!evaluation.shouldSend()) return
    exposures.push({
      timestamp: new Date().toISOString(),
      subject: evaluation.targetingKey,
      flag: evaluation.flagKey,
      allocation: evaluation.allocation,
      variant: evaluation.variant,
      features: evaluation.getFeatures(),
    })
  })
)

await OpenFeature.setProviderAndWait(tracer.openfeature)
// A background task writes the records in `exposures` to your data store.
```

{% /tab %}

{% tab title="Ruby" %}

```ruby
require 'datadog'
require 'open_feature/sdk'
require 'datadog/open_feature/provider'

exposure_hook = Datadog::OpenFeature::ExposureHook.new do |evaluation|
  next unless evaluation.should_send?

  # ExposureQueue is your application's own queue.
  ExposureQueue.push(
    timestamp: Time.now.utc,
    subject: evaluation.targeting_key,
    flag: evaluation.flag_key,
    allocation: evaluation.allocation_key,
    variant: evaluation.variant,
    features: evaluation.features,
  )
end

OpenFeature::SDK.configure do |config|
  config.set_provider(Datadog::OpenFeature::Provider.new)
  config.hooks << exposure_hook
end
# A background thread takes records from ExposureQueue and writes them to your data store.
```

Register the hook on the OpenFeature API, a client, or a single evaluation. Evaluate flags through an OpenFeature client. A direct call to a `fetch_*_value` method of the provider runs no hooks, so the SDK records no exposure.
{% /tab %}

{% tab title="Browser" %}

```javascript
import { ExposureHook } from '@datadog/openfeature-browser'
import { OpenFeature } from '@openfeature/web-sdk'

const exposures = []

OpenFeature.addHooks(
  new ExposureHook((evaluation) => {
    if (!evaluation.shouldSend()) return
    exposures.push({
      timestamp: new Date().toISOString(),
      subject: evaluation.targetingKey,
      flag: evaluation.flagKey,
      allocation: evaluation.allocation,
      variant: evaluation.variant,
      features: evaluation.getFeatures(),
    })
  })
)
// Send the records in `exposures` to your own endpoint in batches, outside the callback.
```

The browser SDK keeps its exposure cache across page loads. When a page reload gives a subject the same result, the evaluation is a cache hit.
{% /tab %}

### What the callback receives{% #what-the-callback-receives %}

| Member         | Java, Node.js, and browser                                                                                                                                                                                           | Go                                      | Ruby                                                                                                     | Description                                                                                                                       |
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------- | -------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| Is exposure    | `isExposure()`                                                                                                                                                                                                       | `IsExposure()`                          | `exposure?`                                                                                              | `true` when the evaluation succeeded and selected an allocation that logs exposures. Repeats are included.                        |
| Is cache hit   | `isCacheHit()`                                                                                                                                                                                                       | `IsCacheHit()`                          | `cache_hit?`                                                                                             | `true` when the subject already received the same allocation, variant, and split for this flag.                                   |
| Should send    | `shouldSend()`                                                                                                                                                                                                       | `ShouldSend()`                          | `should_send?`                                                                                           | `true` for a new or changed exposure: an exposure that is not a cache hit. These are the exposures that the SDK sends to Datadog. |
| Split features | `getFeatures()`                                                                                                                                                                                                      | `Features()`                            | `features`                                                                                               | The split features attached to the selected split, such as the holdout fields. Empty when the split has none.                     |
| Evaluation     | Java: `getFlagKey()`, `getVariant()`, `getAllocationKey()`, `getTargetingKey()`, `getDetails()`, and `getContext()`Node.js and browser: `flagKey`, `variant`, `allocation`, `targetingKey`, `details`, and `context` | `Details`, `Context`, `AllocationKey()` | `flag_key`, `variant`, `allocation_key`, `targeting_key`, `evaluation_details`, and `evaluation_context` | The flag key, variant, allocation key, flag metadata, and evaluation context, including the targeting key.                        |

In Java, `getFlagKey()`, `getVariant()`, `getAllocationKey()`, and `getTargetingKey()` require version 1.69.0 or later.

If you use `isExposure()` instead of `shouldSend()`, deduplicate the records yourself.

## Split features{% #split-features %}

Split features are values that Datadog attaches to a split of a flag. The hook returns them as a read-only map. Values are strings, numbers, or Booleans.

The same values are also in the evaluation's flag metadata, under keys that start with `__dd_feature.` followed by the feature key, such as `__dd_feature.holdout.key`. A hook that uses only the OpenFeature API can read them there. Read them in the `finally` stage of the hook and skip evaluations that have an error code.

{% alert level="warning" %}
The `__dd_feature.<key>` entries are the only flag metadata intended for your code. Other flag metadata keys that start with `__dd_` are internal to the SDK and can change without notice.
{% /alert %}

### Holdout fields{% #holdout-fields %}

When a flag is part of a [holdout](https://docs.datadoghq.com/experiments/holdouts.md), the selected split carries these split features:

| Feature                                      | Value                                                                                                                                                                                   |
| -------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `holdout.key`                                | The holdout key.                                                                                                                                                                        |
| `holdout.assignment_group`                   | `status_quo` or `all_shipped`.                                                                                                                                                          |
| `holdout.should_include_in_holdout_analysis` | `true` when the exposure counts in the primary holdout analysis. `false` before a [covered experiment](https://docs.datadoghq.com/experiments/holdouts.md) ships a non-control variant. |

## Fields to record{% #fields-to-record %}

To analyze exposures in Datadog Experiments with an [Exposure SQL Model](https://docs.datadoghq.com/experiments/concepts/exposure_sql.md), record at least these fields for each exposure:

| Field                                                                                   | Source                                                                                                                                                            |
| --------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Timestamp                                                                               | The time of the evaluation.                                                                                                                                       |
| Subject                                                                                 | The targeting key of the evaluation context.                                                                                                                      |
| Flag key                                                                                | The evaluation details.                                                                                                                                           |
| Allocation key                                                                          | Java: `getAllocationKey()`, or the `allocationKey` flag metadata key in 1.68.x. Go: `AllocationKey()`. Node.js and browser: `allocation`. Ruby: `allocation_key`. |
| Variant                                                                                 | The evaluation details.                                                                                                                                           |
| `holdout.key`, `holdout.assignment_group`, `holdout.should_include_in_holdout_analysis` | The split features, for flags in a holdout.                                                                                                                       |

To map these columns for a holdout, see [Analyze holdouts with your own exposure data](https://docs.datadoghq.com/experiments/holdouts.md#analyze-holdouts-with-your-own-exposure-data).

## Stop sending data to Datadog{% #stop-sending-data-to-datadog %}

You can stop sending exposures to Datadog and still record them yourself. The exposure cache and `ExposureHook` keep working, and `shouldSend()` gives the same results.

### Server SDKs{% #server-sdks %}

Set this environment variable on your application before it starts:

```bash
DD_FEATURE_FLAGS_EXPOSURES_DATADOG_LOGGING_ENABLED=false
```

In Ruby, you can also set `c.feature_flags.exposures_datadog_logging_enabled = false` in a `Datadog.configure` block.

This setting stops only the exposures sent to Datadog. It does not stop the aggregated flag evaluation events. To stop those events too, set:

```bash
DD_FLAGGING_EVALUATION_COUNTS_ENABLED=false
```

### Browser SDK{% #browser-sdk %}

Set `trackExposures` to `false` when you create the provider:

```javascript
const provider = new DatadogProvider({
  // ...
  trackExposures: false,
})
```

`trackExposures` replaces the deprecated `enableExposureLogging` option. When both are set, `trackExposures` takes precedence. To also stop the aggregated flag evaluation telemetry, set `enableFlagEvaluationTracking` to `false`.

## Limitations{% #limitations %}

- **OpenFeature MultiProvider**:
  - Java: Use OpenFeature SDK 1.23.0 or later. With an earlier SDK, a Datadog provider inside a `MultiProvider` sends no exposures to Datadog, and `shouldSend()` is `true` for every exposure.
  - Go and browser: Under a `MultiProvider`, the SDK records no exposures and does not call the `ExposureHook` callback. Flag evaluation is not affected. Register the Datadog provider directly with OpenFeature. In the browser, you can also wrap it in `DatadogDevtools`, the provider wrapper for the [Datadog Browser SDK developer extension](https://docs.datadoghq.com/feature_flags/browser_developer_extension.md).
- **Ruby**:
  - Under a composed provider, such as `OpenFeature::MetaProvider`, the SDK records no exposures. `ExposureHook` skips the callback and logs one warning. Register the Datadog provider directly with OpenFeature.
  - With an `openfeature-sdk` release earlier than 0.5.1, flags evaluate and no hooks run. The SDK records no exposures and does not call the `ExposureHook` callback.
- **Java agent version**: Split features require `dd-java-agent` 1.68.0 or later. With an earlier agent, `getFeatures()` is empty.

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

Additional helpful documentation, links, and articles:

- [Feature Flags and Experiments](https://docs.datadoghq.com/feature_flags/concepts/experiments.md)
- [Exposure SQL Models](https://docs.datadoghq.com/experiments/concepts/exposure_sql.md)
- [Analyze holdouts with your own exposure data](https://docs.datadoghq.com/experiments/holdouts.md#analyze-holdouts-with-your-own-exposure-data)
