Skip to content

Commit 3b54e81

Browse files
committed
finished exposure writer work
Signed-off-by: Eliott Bouhana <[email protected]>
1 parent eb73e8c commit 3b54e81

File tree

2 files changed

+47
-27
lines changed

2 files changed

+47
-27
lines changed

openfeature/doc.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,47 @@
191191
// Configuration updates are acknowledged back to Remote Config with appropriate
192192
// status codes (acknowledged for success, error for validation failures).
193193
//
194+
// # Configuration
195+
//
196+
// The provider can be configured using ProviderConfig when creating a new instance:
197+
//
198+
// config := openfeature.ProviderConfig{
199+
// ExposureFlushInterval: 5 * time.Second, // Optional: defaults to 1 second
200+
// }
201+
// provider, err := openfeature.NewDatadogProvider(config)
202+
//
203+
// Configuration Options:
204+
//
205+
// - ExposureFlushInterval: Duration between automatic flushes of exposure events
206+
// to the Datadog agent. Defaults to 1 second if not specified. Exposure events
207+
// track which feature flags are evaluated and by which users, providing visibility
208+
// into feature flag usage. Set to 0 to disable automatic flushing (not recommended).
209+
//
210+
// # Environment Variables
211+
//
212+
// The provider requires the following environment variable to be set:
213+
//
214+
// - DD_EXPERIMENTAL_FLAGGING_PROVIDER_ENABLED: Must be set to "true" to enable
215+
// the OpenFeature provider. This is a safety flag to ensure the feature is
216+
// intentionally activated. If not set or set to false, NewDatadogProvider()
217+
// will return a NoopProvider instead of the actual Datadog provider.
218+
//
219+
// Example:
220+
//
221+
// export DD_EXPERIMENTAL_FLAGGING_PROVIDER_ENABLED=true
222+
//
223+
// Standard Datadog environment variables also apply:
224+
//
225+
// - DD_AGENT_HOST: Datadog agent host (default: localhost)
226+
// - DD_TRACE_AGENT_PORT: Datadog agent port (default: 8126)
227+
// - DD_SERVICE: Service name for tagging
228+
// - DD_ENV: Environment name (e.g., production, staging)
229+
// - DD_VERSION: Application version
230+
//
194231
// # Prerequisites
195232
//
196233
// Before creating the provider, ensure that:
234+
// - DD_EXPERIMENTAL_FLAGGING_PROVIDER_ENABLED environment variable is set to "true"
197235
// - The Datadog tracer is started (tracer.Start()) OR
198236
// - Remote Config client is properly configured
199237
//

openfeature/exposure.go

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ type exposureSubject struct {
7474

7575
// exposureContext represents service context metadata for the exposure payload
7676
type exposureContext struct {
77-
ServiceName string `json:"service_name"`
77+
ServiceName string `json:"service"`
7878
Version string `json:"version,omitempty"`
7979
Env string `json:"env,omitempty"`
8080
}
@@ -85,17 +85,10 @@ type exposurePayload struct {
8585
Exposures []exposureEvent `json:"exposures"`
8686
}
8787

88-
type bufferKey struct {
89-
flagKey string
90-
allocationKey string
91-
variantKey string
92-
subjectID string
93-
}
94-
9588
// exposureWriter manages buffering and flushing of exposure events to the Datadog Agent
9689
type exposureWriter struct {
9790
mu sync.Mutex
98-
buffer map[bufferKey]exposureEvent // Deduplicate by composite key
91+
buffer []exposureEvent // Buffer for exposure events
9992
flushInterval time.Duration
10093
httpClient *http.Client
10194
agentURL *url.URL
@@ -143,7 +136,7 @@ func newExposureWriter(config ProviderConfig) *exposureWriter {
143136
}
144137

145138
return &exposureWriter{
146-
buffer: make(map[bufferKey]exposureEvent),
139+
buffer: make([]exposureEvent, 0),
147140
flushInterval: flushInterval,
148141
httpClient: httpClient,
149142
agentURL: agentURL,
@@ -167,7 +160,7 @@ func (w *exposureWriter) start() {
167160
}()
168161
}
169162

170-
// append adds an exposure event to the buffer with deduplication
163+
// append adds an exposure event to the buffer
171164
func (w *exposureWriter) append(event exposureEvent) {
172165
w.mu.Lock()
173166
defer w.mu.Unlock()
@@ -176,17 +169,9 @@ func (w *exposureWriter) append(event exposureEvent) {
176169
return
177170
}
178171

179-
// Create composite key for deduplication
180-
// Deduplicate by flag, allocation, variant, and subject.id
181-
key := bufferKey{
182-
flagKey: event.Flag.Key,
183-
allocationKey: event.Allocation.Key,
184-
variantKey: event.Variant.Key,
185-
subjectID: event.Subject.ID,
186-
}
187-
188-
// Store event (will overwrite if duplicate)
189-
w.buffer[key] = event
172+
// Append event to buffer
173+
// Each exposure event is tracked individually to maintain accurate analytics
174+
w.buffer = append(w.buffer, event)
190175
}
191176

192177
// flush sends all buffered exposure events to the agent
@@ -198,11 +183,8 @@ func (w *exposureWriter) flush() {
198183
}
199184

200185
// Move buffer to local variable and create new buffer
201-
events := make([]exposureEvent, 0, len(w.buffer))
202-
for _, event := range w.buffer {
203-
events = append(events, event)
204-
}
205-
w.buffer = make(map[bufferKey]exposureEvent)
186+
events := w.buffer
187+
w.buffer = make([]exposureEvent, 0)
206188
w.mu.Unlock()
207189

208190
// Build payload

0 commit comments

Comments
 (0)