Skip to content

Commit 429e2f8

Browse files
committed
finished exposure writer work
Signed-off-by: Eliott Bouhana <[email protected]>
1 parent 79a81b3 commit 429e2f8

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
w := &exposureWriter{
146-
buffer: make(map[bufferKey]exposureEvent),
139+
buffer: make([]exposureEvent, 0),
147140
flushInterval: flushInterval,
148141
httpClient: httpClient,
149142
agentURL: agentURL,
@@ -172,7 +165,7 @@ func (w *exposureWriter) start() {
172165
}()
173166
}
174167

175-
// append adds an exposure event to the buffer with deduplication
168+
// append adds an exposure event to the buffer
176169
func (w *exposureWriter) append(event exposureEvent) {
177170
w.mu.Lock()
178171
defer w.mu.Unlock()
@@ -181,17 +174,9 @@ func (w *exposureWriter) append(event exposureEvent) {
181174
return
182175
}
183176

184-
// Create composite key for deduplication
185-
// Deduplicate by flag, allocation, variant, and subject.id
186-
key := bufferKey{
187-
flagKey: event.Flag.Key,
188-
allocationKey: event.Allocation.Key,
189-
variantKey: event.Variant.Key,
190-
subjectID: event.Subject.ID,
191-
}
192-
193-
// Store event (will overwrite if duplicate)
194-
w.buffer[key] = event
177+
// Append event to buffer
178+
// Each exposure event is tracked individually to maintain accurate analytics
179+
w.buffer = append(w.buffer, event)
195180
}
196181

197182
// flush sends all buffered exposure events to the agent
@@ -203,11 +188,8 @@ func (w *exposureWriter) flush() {
203188
}
204189

205190
// Move buffer to local variable and create new buffer
206-
events := make([]exposureEvent, 0, len(w.buffer))
207-
for _, event := range w.buffer {
208-
events = append(events, event)
209-
}
210-
w.buffer = make(map[bufferKey]exposureEvent)
191+
events := w.buffer
192+
w.buffer = make([]exposureEvent, 0)
211193
w.mu.Unlock()
212194

213195
// Build payload

0 commit comments

Comments
 (0)