Skip to content

Commit

Permalink
failed on unregistered featuregate (open-telemetry#5660)
Browse files Browse the repository at this point in the history
* failed on unregistered featuregate
* add test for existing/noexisting gates
* change command to use Apply for now

Signed-off-by: Ziqi Zhao <[email protected]>
  • Loading branch information
fatsheep9146 authored Jul 12, 2022
1 parent 6133c82 commit 1b32ae0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- `component.WithTracesProcessor` -> `component.WithTracesProcessorAndStabilityLevel`
- `component.WithMetricsProcessor` -> `component.WithMetricsProcessorAndStabilityLevel`
- `component.WithLogsProcessor` -> `component.WithLogsProcessorAndStabilityLevel`
- Deprecate `Registry.Apply` in `service.featuregate` (#5660)

### 💡 Enhancements 💡

Expand Down
16 changes: 16 additions & 0 deletions service/featuregate/gates.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Registry struct {

// Apply a configuration in the form of a map of Gate identifiers to boolean values.
// Sets only those values provided in the map, other gate values are not changed.
// Deprecated: [v0.56.0] Use MustApply instead.
func (r *Registry) Apply(cfg map[string]bool) {
r.mu.Lock()
defer r.mu.Unlock()
Expand All @@ -57,6 +58,21 @@ func (r *Registry) Apply(cfg map[string]bool) {
}
}

// MustApply a configuration in the form of a map of Gate identifiers to boolean values.
// Sets only those values provided in the map, other gate values are not changed.
func (r *Registry) MustApply(cfg map[string]bool) {
r.mu.Lock()
defer r.mu.Unlock()
for id, val := range cfg {
if g, ok := r.gates[id]; ok {
g.Enabled = val
r.gates[g.ID] = g
} else {
panic(fmt.Sprintf("feature gate %s is unregistered", id))
}
}
}

// IsEnabled returns true if a registered feature gate is enabled and false otherwise.
func (r *Registry) IsEnabled(id string) bool {
r.mu.RLock()
Expand Down
44 changes: 44 additions & 0 deletions service/featuregate/gates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,47 @@ func TestRegistry(t *testing.T) {
r.MustRegister(gate)
})
}

func TestRegistryWithMustApply(t *testing.T) {
r := Registry{gates: map[string]Gate{}}
gate := Gate{
ID: "foo",
Description: "Test Gate",
Enabled: true,
}
assert.NoError(t, r.Register(gate))

tests := []struct {
name string
gate string
enabled bool
shouldError bool
}{
{
name: "existing_gate",
gate: "foo",
enabled: false,
shouldError: false,
},
{
name: "none_existing_gate",
gate: "bar",
enabled: false,
shouldError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.shouldError {
assert.Panics(t, func() {
r.MustApply(map[string]bool{tt.gate: tt.enabled})
})
} else {
r.MustApply(map[string]bool{tt.gate: tt.enabled})
assert.Equal(t, tt.enabled, r.IsEnabled(tt.gate))
}

})
}
}

0 comments on commit 1b32ae0

Please sign in to comment.