-
Notifications
You must be signed in to change notification settings - Fork 331
Add flag evaluation metrics via OTel counter and OpenFeature Hook #11040
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
Open
typotter
wants to merge
8
commits into
master
Choose a base branch
from
typo/evaluations-logging
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
04ae0fb
Add flag evaluation metrics via OTel counter and OpenFeature Hook
typotter c5467fb
Use own SdkMeterProvider with OTLP HTTP exporter for eval metrics
typotter 1816d30
Address code review feedback for eval metrics
typotter 3d789f0
Fix NoClassDefFoundError when OTel SDK absent from classpath
typotter 69c5529
Move FlagEvalHook construction inside try/catch block
typotter 18c0441
Add README for dd-openfeature with eval metrics setup
typotter da92198
Use ConfigHelper.env() instead of System.getenv()
typotter 340f25c
Address PR review feedback from manuel-alvarez-alvarez
typotter 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
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,79 @@ | ||
| # dd-openfeature | ||
|
|
||
| Datadog OpenFeature Provider for Java. Implements the [OpenFeature](https://openfeature.dev/) `FeatureProvider` interface for Datadog's Feature Flags and Experimentation (FFE) product. | ||
|
|
||
| Published as `com.datadoghq:dd-openfeature` on Maven Central. | ||
|
|
||
| ## Setup | ||
|
|
||
| ```xml | ||
| <dependency> | ||
| <groupId>com.datadoghq</groupId> | ||
| <artifactId>dd-openfeature</artifactId> | ||
| <version>${dd-openfeature.version}</version> | ||
| </dependency> | ||
| ``` | ||
|
|
||
| The OpenFeature SDK (`dev.openfeature:sdk`) is included as a transitive dependency. | ||
|
|
||
| ### Evaluation metrics (optional) | ||
|
|
||
| To enable evaluation metrics (`feature_flag.evaluations` counter), add the OpenTelemetry SDK dependencies: | ||
|
|
||
| ```xml | ||
| <dependency> | ||
| <groupId>io.opentelemetry</groupId> | ||
| <artifactId>opentelemetry-sdk-metrics</artifactId> | ||
| <version>1.47.0</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.opentelemetry</groupId> | ||
| <artifactId>opentelemetry-exporter-otlp</artifactId> | ||
| <version>1.47.0</version> | ||
| </dependency> | ||
| ``` | ||
|
|
||
| Any OpenTelemetry API 1.x version is compatible. If these dependencies are absent, the provider operates normally without metrics. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```java | ||
| import datadog.trace.api.openfeature.Provider; | ||
| import dev.openfeature.sdk.OpenFeatureAPI; | ||
| import dev.openfeature.sdk.Client; | ||
|
|
||
| OpenFeatureAPI api = OpenFeatureAPI.getInstance(); | ||
| api.setProviderAndWait(new Provider()); | ||
| Client client = api.getClient(); | ||
|
|
||
| boolean enabled = client.getBooleanValue("my-feature", false, | ||
| new MutableContext("user-123")); | ||
| ``` | ||
|
|
||
| ## Evaluation metrics | ||
|
|
||
| When the OTel SDK dependencies are on the classpath, the provider records a `feature_flag.evaluations` counter via OTLP HTTP/protobuf. Metrics are exported every 10 seconds to the Datadog Agent's OTLP receiver. | ||
|
|
||
| ### Configuration | ||
|
|
||
| | Environment variable | Description | Default | | ||
| |---|---|---| | ||
| | `OTEL_EXPORTER_OTLP_METRICS_ENDPOINT` | Signal-specific OTLP endpoint (used as-is) | — | | ||
| | `OTEL_EXPORTER_OTLP_ENDPOINT` | Generic OTLP endpoint (`/v1/metrics` appended) | — | | ||
| | (none set) | Default endpoint | `http://localhost:4318/v1/metrics` | | ||
|
|
||
| ### Metric attributes | ||
|
|
||
| | Attribute | Description | | ||
| |---|---| | ||
| | `feature_flag.key` | Flag key | | ||
| | `feature_flag.result.variant` | Resolved variant key | | ||
| | `feature_flag.result.reason` | Evaluation reason (lowercased) | | ||
| | `error.type` | Error code (lowercased, only on error) | | ||
| | `feature_flag.result.allocation_key` | Allocation key (when present) | | ||
|
|
||
| ## Requirements | ||
|
|
||
| - Java 11+ | ||
| - Datadog Agent with Remote Configuration enabled | ||
| - `DD_EXPERIMENTAL_FLAGGING_PROVIDER_ENABLED=true` |
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
41 changes: 41 additions & 0 deletions
41
...agging/feature-flagging-api/src/main/java/datadog/trace/api/openfeature/FlagEvalHook.java
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,41 @@ | ||
| package datadog.trace.api.openfeature; | ||
|
|
||
| import dev.openfeature.sdk.ErrorCode; | ||
| import dev.openfeature.sdk.FlagEvaluationDetails; | ||
| import dev.openfeature.sdk.Hook; | ||
| import dev.openfeature.sdk.HookContext; | ||
| import dev.openfeature.sdk.ImmutableMetadata; | ||
| import java.util.Map; | ||
|
|
||
| class FlagEvalHook implements Hook<Object> { | ||
|
|
||
| private final FlagEvalMetrics metrics; | ||
|
|
||
| FlagEvalHook(FlagEvalMetrics metrics) { | ||
| this.metrics = metrics; | ||
| } | ||
|
|
||
| @Override | ||
| public void finallyAfter( | ||
| HookContext<Object> ctx, FlagEvaluationDetails<Object> details, Map<String, Object> hints) { | ||
| if (metrics == null || details == null) { | ||
| return; | ||
| } | ||
| try { | ||
| String flagKey = details.getFlagKey(); | ||
| String variant = details.getVariant(); | ||
| String reason = details.getReason(); | ||
| ErrorCode errorCode = details.getErrorCode(); | ||
|
|
||
| String allocationKey = null; | ||
| ImmutableMetadata metadata = details.getFlagMetadata(); | ||
| if (metadata != null) { | ||
| allocationKey = metadata.getString("allocationKey"); | ||
| } | ||
|
|
||
| metrics.record(flagKey, variant, reason, errorCode, allocationKey); | ||
| } catch (Exception e) { | ||
| // Never let metrics recording break flag evaluation | ||
| } | ||
| } | ||
| } |
131 changes: 131 additions & 0 deletions
131
...ing/feature-flagging-api/src/main/java/datadog/trace/api/openfeature/FlagEvalMetrics.java
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,131 @@ | ||
| package datadog.trace.api.openfeature; | ||
|
|
||
| import datadog.trace.config.inversion.ConfigHelper; | ||
| import dev.openfeature.sdk.ErrorCode; | ||
| import io.opentelemetry.api.common.AttributeKey; | ||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.api.common.AttributesBuilder; | ||
| import io.opentelemetry.api.metrics.LongCounter; | ||
| import io.opentelemetry.api.metrics.Meter; | ||
| import io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporter; | ||
| import io.opentelemetry.sdk.metrics.SdkMeterProvider; | ||
| import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader; | ||
| import java.io.Closeable; | ||
| import java.time.Duration; | ||
|
|
||
| class FlagEvalMetrics implements Closeable { | ||
|
|
||
| private static final String METER_NAME = "ddtrace.openfeature"; | ||
| private static final String METRIC_NAME = "feature_flag.evaluations"; | ||
| private static final String METRIC_UNIT = "{evaluation}"; | ||
| private static final String METRIC_DESC = "Number of feature flag evaluations"; | ||
| private static final Duration EXPORT_INTERVAL = Duration.ofSeconds(10); | ||
|
|
||
| private static final String DEFAULT_ENDPOINT = "http://localhost:4318/v1/metrics"; | ||
| // Signal-specific env var (used as-is, must include /v1/metrics path) | ||
| private static final String ENDPOINT_ENV = "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT"; | ||
| // Generic env var fallback (base URL, /v1/metrics is appended) | ||
| private static final String ENDPOINT_GENERIC_ENV = "OTEL_EXPORTER_OTLP_ENDPOINT"; | ||
|
|
||
| private static final AttributeKey<String> ATTR_FLAG_KEY = | ||
| AttributeKey.stringKey("feature_flag.key"); | ||
| private static final AttributeKey<String> ATTR_VARIANT = | ||
| AttributeKey.stringKey("feature_flag.result.variant"); | ||
| private static final AttributeKey<String> ATTR_REASON = | ||
| AttributeKey.stringKey("feature_flag.result.reason"); | ||
| private static final AttributeKey<String> ATTR_ERROR_TYPE = AttributeKey.stringKey("error.type"); | ||
| private static final AttributeKey<String> ATTR_ALLOCATION_KEY = | ||
| AttributeKey.stringKey("feature_flag.result.allocation_key"); | ||
|
|
||
| private volatile LongCounter counter; | ||
| // Typed as Closeable to avoid loading SdkMeterProvider at class-load time | ||
| // when the OTel SDK is absent from the classpath | ||
| private volatile java.io.Closeable meterProvider; | ||
|
|
||
| FlagEvalMetrics() { | ||
| try { | ||
| String endpoint = ConfigHelper.env(ENDPOINT_ENV); | ||
| if (endpoint == null || endpoint.isEmpty()) { | ||
| String base = ConfigHelper.env(ENDPOINT_GENERIC_ENV); | ||
| if (base != null && !base.isEmpty()) { | ||
| endpoint = base.endsWith("/") ? base + "v1/metrics" : base + "/v1/metrics"; | ||
| } else { | ||
| endpoint = DEFAULT_ENDPOINT; | ||
| } | ||
| } | ||
|
|
||
| OtlpHttpMetricExporter exporter = | ||
| OtlpHttpMetricExporter.builder().setEndpoint(endpoint).build(); | ||
|
|
||
| PeriodicMetricReader reader = | ||
| PeriodicMetricReader.builder(exporter).setInterval(EXPORT_INTERVAL).build(); | ||
|
|
||
| SdkMeterProvider sdkMeterProvider = | ||
| SdkMeterProvider.builder().registerMetricReader(reader).build(); | ||
| meterProvider = sdkMeterProvider; | ||
|
|
||
| Meter meter = sdkMeterProvider.meterBuilder(METER_NAME).build(); | ||
| counter = | ||
| meter | ||
| .counterBuilder(METRIC_NAME) | ||
| .setUnit(METRIC_UNIT) | ||
| .setDescription(METRIC_DESC) | ||
| .build(); | ||
| } catch (NoClassDefFoundError | Exception e) { | ||
| // OTel SDK not on classpath or initialization failed — counter stays null (no-op) | ||
| counter = null; | ||
| meterProvider = null; | ||
| } | ||
| } | ||
|
|
||
| /** Package-private constructor for testing with a mock counter. */ | ||
| FlagEvalMetrics(LongCounter counter) { | ||
| this.counter = counter; | ||
| this.meterProvider = null; | ||
| } | ||
|
|
||
| void record( | ||
| String flagKey, String variant, String reason, ErrorCode errorCode, String allocationKey) { | ||
| LongCounter c = counter; | ||
| if (c == null) { | ||
| return; | ||
| } | ||
| try { | ||
| AttributesBuilder builder = | ||
| Attributes.builder() | ||
| .put(ATTR_FLAG_KEY, flagKey) | ||
| .put(ATTR_VARIANT, variant != null ? variant : "") | ||
| .put(ATTR_REASON, reason != null ? reason.toLowerCase() : "unknown"); | ||
|
|
||
| if (errorCode != null) { | ||
| builder.put(ATTR_ERROR_TYPE, errorCode.name().toLowerCase()); | ||
| } | ||
|
|
||
| if (allocationKey != null && !allocationKey.isEmpty()) { | ||
| builder.put(ATTR_ALLOCATION_KEY, allocationKey); | ||
| } | ||
|
|
||
| c.add(1, builder.build()); | ||
| } catch (Exception e) { | ||
| // Never let metrics recording break flag evaluation | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| shutdown(); | ||
| } | ||
|
|
||
| void shutdown() { | ||
| counter = null; | ||
| java.io.Closeable mp = meterProvider; | ||
| if (mp != null) { | ||
| meterProvider = null; | ||
| try { | ||
| mp.close(); | ||
| } catch (Exception e) { | ||
| // Ignore shutdown errors | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be better to just let the error flow to the
Providerclass since it's already capturing the exception?