Skip to content

Commit

Permalink
Deprecate featuregate.Registry.List in favor of Visit. (#7041)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Jan 27, 2023
1 parent b771d13 commit cf9c829
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
11 changes: 11 additions & 0 deletions .chloggen/addvisit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: deprecation

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: featuregate

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Deprecate Registry.List in favor of Registry.Visit.

# One or more tracking issues or pull requests related to the change
issues: [7041]
23 changes: 19 additions & 4 deletions featuregate/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package featuregate // import "go.opentelemetry.io/collector/featuregate"

import (
"fmt"
"sort"
"sync"

"go.uber.org/atomic"
Expand Down Expand Up @@ -153,12 +154,26 @@ func (r *Registry) Set(id string, enabled bool) error {
return nil
}

// List returns a slice of copies of all registered Gates.
func (r *Registry) List() []Gate {
var ret []Gate
// Visit visits the gates in lexicographical order, calling fn for each.
func (r *Registry) Visit(fn func(*Gate)) {
var gates []*Gate
r.gates.Range(func(key, value any) bool {
ret = append(ret, *(value.(*Gate)))
gates = append(gates, value.(*Gate))
return true
})
sort.Slice(gates, func(i, j int) bool {
return gates[i].ID() < gates[j].ID()
})
for i := range gates {
fn(gates[i])
}
}

// Deprecated: [v0.71.0] use Visit.
func (r *Registry) List() []Gate {
var ret []Gate
r.Visit(func(gate *Gate) {
ret = append(ret, *gate)
})
return ret
}
11 changes: 7 additions & 4 deletions featuregate/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ func TestGlobalRegistry(t *testing.T) {

func TestRegistry(t *testing.T) {
r := NewRegistry()
// Expect that no gates to visit.
r.Visit(func(gate *Gate) {
t.FailNow()
})

const id = "foo"

assert.Empty(t, r.List())

g, err := r.Register(id, StageBeta, WithRegisterDescription("Test Gate"))
assert.NoError(t, err)
assert.Len(t, r.List(), 1)
r.Visit(func(gate *Gate) {
assert.Equal(t, id, gate.ID())
})
assert.True(t, g.IsEnabled())

assert.NoError(t, r.Set(id, false))
Expand Down
17 changes: 8 additions & 9 deletions service/zpages.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,16 @@ func handleFeaturezRequest(w http.ResponseWriter, r *http.Request) {

func getFeaturesTableData() zpages.FeatureGateTableData {
data := zpages.FeatureGateTableData{}
for _, g := range featuregate.GlobalRegistry().List() {
featuregate.GlobalRegistry().Visit(func(gate *featuregate.Gate) {
data.Rows = append(data.Rows, zpages.FeatureGateTableRowData{
ID: g.ID(),
Enabled: g.IsEnabled(),
Description: g.Description(),
ReferenceURL: g.ReferenceURL(),
Stage: g.Stage().String(),
RemovalVersion: g.RemovalVersion(),
ID: gate.ID(),
Enabled: gate.IsEnabled(),
Description: gate.Description(),
ReferenceURL: gate.ReferenceURL(),
Stage: gate.Stage().String(),
RemovalVersion: gate.RemovalVersion(),
})
}

})
return data
}

Expand Down

0 comments on commit cf9c829

Please sign in to comment.