Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions ddtrace/tracer/remote_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func (t *tracer) startDynamicInstrumentationRCSubscriptions() error {
t.dynInstSubscriptions.mu.Lock()
defer t.dynInstSubscriptions.mu.Unlock()

if t.dynInstSubscriptions.ldSubscriptionToken != 0 || t.dynInstSubscriptions.symDBSubscriptionToken != 0 {
if !t.dynInstSubscriptions.ldSubscriptionToken.IsZero() || !t.dynInstSubscriptions.symDBSubscriptionToken.IsZero() {
return errors.New("programming error: dynamic instrumentation RC subscriptions already started")
}

Expand Down Expand Up @@ -400,19 +400,19 @@ func (t *tracer) startDynamicInstrumentationRCSubscriptions() error {
func (t *tracer) stopDynamicInstrumentationRCSubscriptions() error {
t.dynInstSubscriptions.mu.Lock()
defer t.dynInstSubscriptions.mu.Unlock()
if t.dynInstSubscriptions.ldSubscriptionToken != 0 {
if !t.dynInstSubscriptions.ldSubscriptionToken.IsZero() {
err := remoteconfig.Unsubscribe(t.dynInstSubscriptions.ldSubscriptionToken)
if err != nil {
return err
}
t.dynInstSubscriptions.ldSubscriptionToken = 0
t.dynInstSubscriptions.ldSubscriptionToken = remoteconfig.SubscriptionToken{}
}
if t.dynInstSubscriptions.symDBSubscriptionToken != 0 {
if !t.dynInstSubscriptions.symDBSubscriptionToken.IsZero() {
err := remoteconfig.Unsubscribe(t.dynInstSubscriptions.symDBSubscriptionToken)
if err != nil {
return err
}
t.dynInstSubscriptions.symDBSubscriptionToken = 0
t.dynInstSubscriptions.symDBSubscriptionToken = remoteconfig.SubscriptionToken{}
}
return nil
}
8 changes: 7 additions & 1 deletion ddtrace/tracer/remote_config_errortracking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"testing"

rc "github.com/DataDog/datadog-agent/pkg/remoteconfig/state"

"github.com/DataDog/dd-trace-go/v2/internal/log"
internalffe "github.com/DataDog/dd-trace-go/v2/internal/openfeature"
"github.com/DataDog/dd-trace-go/v2/internal/remoteconfig"
Expand Down Expand Up @@ -58,6 +60,10 @@ func TestRemoteConfigDisabledOpenFeatureSubscriptionLogsLocallyWithoutReporting(
}

func TestRemoteConfigSubscriptionErrorsReportedToTelemetry(t *testing.T) {
require.NoError(t, remoteconfig.Start(remoteconfig.DefaultClientConfig()))
staleToken, err := remoteconfig.Subscribe("STALE_TEST_PRODUCT", func(remoteconfig.ProductUpdate) map[string]rc.ApplyStatus { return nil })
require.NoError(t, err)
remoteconfig.Stop()
t.Setenv("DD_REMOTE_CONFIGURATION_ENABLED", "false")
t.Cleanup(remoteconfig.Reset)

Expand All @@ -79,7 +85,7 @@ func TestRemoteConfigSubscriptionErrorsReportedToTelemetry(t *testing.T) {
// enable path left both real tokens at zero because the subscriptions
// never started.
tr.dynInstSubscriptions.mu.Lock()
tr.dynInstSubscriptions.ldSubscriptionToken = 1
tr.dynInstSubscriptions.ldSubscriptionToken = staleToken
tr.dynInstSubscriptions.mu.Unlock()
disabled := false
tr.handleDynamicInstrumentationEnabledRC(&disabled)
Expand Down
34 changes: 34 additions & 0 deletions internal/openfeature/evaluator_capabilities.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2025 Datadog, Inc.

package openfeature

import (
"slices"

"github.com/DataDog/dd-trace-go/v2/internal/remoteconfig"
)

// DependentFlagVariantKeyV1 identifies prerequisite variant-key evaluation support.
const DependentFlagVariantKeyV1 = "dependent-flag-variant-key-v1"

// DependentFlagsVariantKeyQueryParameter requests prerequisite variant keys in UFC responses.
const DependentFlagsVariantKeyQueryParameter = "cap_dependent-flags-variant-key"

// DependentFlagsVariantKeyQueryVersion is the supported query parameter version.
const DependentFlagsVariantKeyQueryVersion = "v1"

// EvaluatorCapabilities reports features supported by the Go evaluator and exposure hooks.
// The tracer needs this report before the provider exists, so it lives in the
// lightweight internal package.
func EvaluatorCapabilities() []string { return []string{DependentFlagVariantKeyV1} }

func remoteConfigCapabilities(evaluatorCapabilities []string) []remoteconfig.Capability {
capabilities := []remoteconfig.Capability{remoteconfig.FFEFlagEvaluation}
if slices.Contains(evaluatorCapabilities, DependentFlagVariantKeyV1) {
capabilities = append(capabilities, remoteconfig.FFEFlagDependentFlagVariantKeyV1)
}
return capabilities
}
34 changes: 34 additions & 0 deletions internal/openfeature/evaluator_capabilities_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2025 Datadog, Inc.

package openfeature

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/DataDog/dd-trace-go/v2/internal/remoteconfig"
)

func TestEvaluatorCapabilities(t *testing.T) {
assert.Equal(t, []string{DependentFlagVariantKeyV1}, EvaluatorCapabilities())
assert.Equal(t, remoteconfig.Capability(50), remoteconfig.FFEFlagDependentFlagVariantKeyV1)
for _, test := range []struct {
name string
capabilities []string
expected []remoteconfig.Capability
}{
{"absent", nil, []remoteconfig.Capability{46}},
{"empty", []string{}, []remoteconfig.Capability{46}},
{"unknown", []string{"unknown-v1"}, []remoteconfig.Capability{46}},
{"supported", []string{DependentFlagVariantKeyV1}, []remoteconfig.Capability{46, 50}},
{"mixed", []string{"unknown-v1", DependentFlagVariantKeyV1}, []remoteconfig.Capability{46, 50}},
} {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expected, remoteConfigCapabilities(test.capabilities))
})
}
}
161 changes: 130 additions & 31 deletions internal/openfeature/rc_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package openfeature

import (
"bytes"
"errors"
"fmt"
"sync"

Expand All @@ -30,13 +31,22 @@ const (
// apply statuses. This matches the signature of DatadogProvider.rcCallback.
type Callback func(update remoteconfig.ProductUpdate) map[string]rc.ApplyStatus

// ProviderToken identifies one provider's ownership of either a tracer-owned
// callback attachment or a provider-owned slow-path subscription.
type ProviderToken uint64

// rcState bridges the tracer's early RC subscription (during tracer.Start)
// with the late-created DatadogProvider (during NewDatadogProvider).
var rcState struct {
sync.Mutex
subscribed bool
callback Callback
buffered remoteconfig.ProductUpdate // latest snapshot; RC sends full state each time
subscribed bool
callback Callback
callbackToken ProviderToken
buffered remoteconfig.ProductUpdate // pending changes received while no provider is attached
delivered remoteconfig.ProductUpdate // last acknowledged state, retained for replacement providers
providerSubscription remoteconfig.SubscriptionToken
providerToken ProviderToken
nextProviderToken ProviderToken
}

// SubscribeRC subscribes to the FFE_FLAGS RC product using a forwarding
Expand All @@ -55,14 +65,17 @@ func SubscribeRC() error {
log.Debug("openfeature: RC subscription for %s was lost (tracer restart?), re-subscribing", FFEProductName)
rcState.subscribed = false
rcState.callback = nil
rcState.callbackToken = 0
rcState.buffered = nil
rcState.delivered = nil
}

if has, _ := remoteconfig.HasProduct(FFEProductName); has {
log.Debug("openfeature: RC product %s already subscribed via provider, skipping tracer subscription", FFEProductName)
return nil
}

if _, err := remoteconfig.Subscribe(FFEProductName, forwardingCallback, remoteconfig.FFEFlagEvaluation); err != nil {
if _, err := remoteconfig.Subscribe(FFEProductName, forwardingCallback, remoteConfigCapabilities(EvaluatorCapabilities())...); err != nil {
return err
}

Expand All @@ -72,22 +85,25 @@ func SubscribeRC() error {
}

// forwardingCallback is the RC callback registered by SubscribeRC. If a
// provider callback is attached, it forwards directly. Otherwise, it buffers
// the latest update for replay when the provider attaches.
// provider callback is attached, it forwards directly and records only the
// paths the provider acknowledged. Otherwise, it buffers changes for replay
// when the provider attaches.
func forwardingCallback(update remoteconfig.ProductUpdate) map[string]rc.ApplyStatus {
rcState.Lock()
defer rcState.Unlock()

if rcState.callback != nil {
return rcState.callback(update)
statuses := rcState.callback(update)
applyAcknowledgedUpdateLocked(update, statuses)
return statuses
}

// Buffer the latest update for replay.
cpy := make(remoteconfig.ProductUpdate, len(update))
for k, v := range update {
cpy[k] = bytes.Clone(v)
if rcState.buffered == nil {
rcState.buffered = make(remoteconfig.ProductUpdate, len(update))
}
for path, data := range update {
rcState.buffered[path] = bytes.Clone(data)
}
rcState.buffered = cpy

// Acknowledge all paths so RC doesn't consider them errored.
statuses := make(map[string]rc.ApplyStatus, len(update))
Expand All @@ -97,60 +113,143 @@ func forwardingCallback(update remoteconfig.ProductUpdate) map[string]rc.ApplySt
return statuses
}

// applyAcknowledgedUpdateLocked advances the replayable state only for paths
// which the provider accepted. The caller holds rcState.
func applyAcknowledgedUpdateLocked(update remoteconfig.ProductUpdate, statuses map[string]rc.ApplyStatus) {
for path, data := range update {
if statuses[path].State != rc.ApplyStateAcknowledged {
continue
}
if data == nil {
delete(rcState.delivered, path)
continue
}
if rcState.delivered == nil {
rcState.delivered = make(remoteconfig.ProductUpdate)
}
rcState.delivered[path] = bytes.Clone(data)
}
}

// AttachCallback wires the given callback to the global RC subscription.
// If SubscribeRC() was called (i.e. the tracer subscribed), it replays any
// buffered config and returns true. Otherwise returns false, meaning the
// caller should fall back to its own RC subscription.
func AttachCallback(cb Callback) bool {
// buffered config and returns an ownership token with true. Otherwise it
// returns a zero token with false, meaning the caller should fall back to its
// own RC subscription.
func AttachCallback(cb Callback) (ProviderToken, bool) {
rcState.Lock()
defer rcState.Unlock()

if !rcState.subscribed {
return false
return 0, false
}

if rcState.callback != nil {
log.Warn("openfeature: callback already attached, multiple providers are not supported") //errtrack:ignore caller initialized multiple providers
return false
return 0, false
}

token := nextProviderTokenLocked()
rcState.callback = cb
rcState.callbackToken = token

if len(rcState.delivered) > 0 {
cb(rcState.delivered)
containLoggerPanic(func() {
log.Debug("openfeature: replayed last acknowledged RC config to replacement provider")
})
}
if rcState.buffered != nil {
Comment thread
typotter marked this conversation as resolved.
Resolved
log.Debug("openfeature: replaying buffered RC config to provider")
// Apply statuses are intentionally discarded: the RC client already received
// ApplyStateAcknowledged from forwardingCallback when this update was buffered.
// This call only initializes the provider's in-memory state.
cb(rcState.buffered)
// The RC client already received ApplyStateAcknowledged when this update was
// buffered. The replay statuses only determine which paths are safe to retain.
statuses := cb(rcState.buffered)
applyAcknowledgedUpdateLocked(rcState.buffered, statuses)
rcState.buffered = nil
containLoggerPanic(func() {
log.Debug("openfeature: replayed buffered RC config to provider")
})
}

return true
return token, true
}

// containLoggerPanic keeps an application logger from interrupting ownership
// transitions after the callback state has already changed.
func containLoggerPanic(logf func()) {
defer func() { _ = recover() }()
logf()
}

// SubscribeProvider attempts to subscribe a provider callback to RC.
// SubscribeProvider attempts to subscribe a provider callback to RC. A
// provider-owned slow-path subscription returns its ownership token; the
// tracer-owned fast path returns a zero token until AttachCallback succeeds.
// It holds the subscription mutex to prevent races with SubscribeRC.
func SubscribeProvider(cb remoteconfig.ProductCallback) (tracerOwnsSubscription bool, err error) {
func SubscribeProvider(cb remoteconfig.ProductCallback) (tracerOwnsSubscription bool, token ProviderToken, err error) {
rcState.Lock()
defer rcState.Unlock()

if rcState.subscribed {
return true, nil
return true, 0, nil
}

// Slow path: tracer didn't subscribe, so we need to start RC and subscribe ourselves.
if err := remoteconfig.Start(remoteconfig.DefaultClientConfig()); err != nil {
return false, fmt.Errorf("failed to start Remote Config: %w", err)
return false, 0, fmt.Errorf("failed to start Remote Config: %w", err)
}

if has, _ := remoteconfig.HasProduct(FFEProductName); has {
return false, fmt.Errorf("RC product %s already subscribed", FFEProductName)
return false, 0, fmt.Errorf("RC product %s already subscribed", FFEProductName)
}

if _, err := remoteconfig.Subscribe(FFEProductName, cb, remoteconfig.FFEFlagEvaluation); err != nil {
return false, err
capabilities := remoteConfigCapabilities(EvaluatorCapabilities())
subscription, err := remoteconfig.Subscribe(FFEProductName, cb, capabilities...)
if err != nil {
return false, 0, err
}
token = nextProviderTokenLocked()
rcState.providerSubscription = subscription
rcState.providerToken = token

log.Debug("openfeature: provider subscribed to RC product %s", FFEProductName)
return false, nil
return false, token, nil
}

// nextProviderTokenLocked returns a non-zero token. The caller holds rcState.
func nextProviderTokenLocked() ProviderToken {
rcState.nextProviderToken++
if rcState.nextProviderToken == 0 {
rcState.nextProviderToken++
}
return rcState.nextProviderToken
}

// UnsubscribeProvider releases only the provider ownership represented by
// token. Fast-path ownership detaches the matching callback while retaining
// the tracer subscription; slow-path ownership removes its subscription and
// exact capability set. Stale tokens are harmless.
func UnsubscribeProvider(token ProviderToken) error {
rcState.Lock()
defer rcState.Unlock()

if token == 0 {
return nil
}
if rcState.callbackToken == token {
rcState.callback = nil
rcState.callbackToken = 0
return nil
}
if rcState.providerToken != token {
return nil
}
subscription := rcState.providerSubscription
rcState.providerSubscription = remoteconfig.SubscriptionToken{}
rcState.providerToken = 0
if err := remoteconfig.Unsubscribe(subscription); err != nil {
Comment thread
typotter marked this conversation as resolved.
Resolved
Comment thread
typotter marked this conversation as resolved.
Resolved
if errors.Is(err, remoteconfig.ErrClientNotStarted) {
return nil
}
return err
}
return nil
}
Loading
Loading