| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package featuregate // import "go.opentelemetry.io/collector/featuregate" |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"net/url" |
| 10 |
"regexp" |
| 11 |
"sort" |
| 12 |
"sync" |
| 13 |
"sync/atomic" |
| 14 |
|
| 15 |
"github.com/hashicorp/go-version" |
| 16 |
) |
| 17 |
|
| 18 |
var ( |
| 19 |
globalRegistry = NewRegistry() |
| 20 |
|
| 21 |
// idRegexp is used to validate the ID of a Gate. |
| 22 |
// IDs' characters must be alphanumeric or dots. |
| 23 |
idRegexp = regexp.MustCompile(`^[0-9a-zA-Z.]*$`) |
| 24 |
) |
| 25 |
|
| 26 |
// ErrAlreadyRegistered is returned when adding a Gate that is already registered. |
| 27 |
var ErrAlreadyRegistered = errors.New("gate is already registered") |
| 28 |
|
| 29 |
// GlobalRegistry returns the global Registry. |
| 30 |
func GlobalRegistry() *Registry { |
| 31 |
return globalRegistry |
| 32 |
} |
| 33 |
|
| 34 |
type Registry struct { |
| 35 |
gates sync.Map |
| 36 |
} |
| 37 |
|
| 38 |
// NewRegistry returns a new empty Registry. |
| 39 |
func NewRegistry() *Registry { |
| 40 |
return &Registry{} |
| 41 |
} |
| 42 |
|
| 43 |
// RegisterOption allows to configure additional information about a Gate during registration. |
| 44 |
type RegisterOption interface { |
| 45 |
apply(g *Gate) error |
| 46 |
} |
| 47 |
|
| 48 |
type registerOptionFunc func(g *Gate) error |
| 49 |
|
| 50 |
func (ro registerOptionFunc) apply(g *Gate) error { |
| 51 |
return ro(g) |
| 52 |
} |
| 53 |
|
| 54 |
// WithRegisterDescription adds description for the Gate. |
| 55 |
func WithRegisterDescription(description string) RegisterOption { |
| 56 |
return registerOptionFunc(func(g *Gate) error { |
| 57 |
g.description = description |
| 58 |
return nil |
| 59 |
}) |
| 60 |
} |
| 61 |
|
| 62 |
// WithRegisterReferenceURL adds a URL that has all the contextual information about the Gate. |
| 63 |
// referenceURL must be a valid URL as defined by `net/url.Parse`. |
| 64 |
func WithRegisterReferenceURL(referenceURL string) RegisterOption { |
| 65 |
return registerOptionFunc(func(g *Gate) error { |
| 66 |
if _, err := url.Parse(referenceURL); err != nil { |
| 67 |
return fmt.Errorf("WithRegisterReferenceURL: invalid reference URL %q: %w", referenceURL, err) |
| 68 |
} |
| 69 |
|
| 70 |
g.referenceURL = referenceURL |
| 71 |
return nil |
| 72 |
}) |
| 73 |
} |
| 74 |
|
| 75 |
// WithRegisterFromVersion is used to set the Gate "FromVersion". |
| 76 |
// The "FromVersion" contains the Collector release when a feature is introduced. |
| 77 |
// fromVersion must be a valid version string: it may start with 'v' and must be in the format Major.Minor.Patch[-PreRelease]. |
| 78 |
// PreRelease is optional and may have dashes, tildes and ASCII alphanumeric characters. |
| 79 |
func WithRegisterFromVersion(fromVersion string) RegisterOption { |
| 80 |
return registerOptionFunc(func(g *Gate) error { |
| 81 |
from, err := version.NewVersion(fromVersion) |
| 82 |
if err != nil { |
| 83 |
return fmt.Errorf("WithRegisterFromVersion: invalid version %q: %w", fromVersion, err) |
| 84 |
} |
| 85 |
|
| 86 |
g.fromVersion = from |
| 87 |
return nil |
| 88 |
}) |
| 89 |
} |
| 90 |
|
| 91 |
// WithRegisterToVersion is used to set the Gate "ToVersion". |
| 92 |
// The "ToVersion", if not empty, contains the last Collector release in which you can still use a feature gate. |
| 93 |
// If the feature stage is either "Deprecated" or "Stable", the "ToVersion" is the Collector release when the feature is removed. |
| 94 |
// toVersion must be a valid version string: it may start with 'v' and must be in the format Major.Minor.Patch[-PreRelease]. |
| 95 |
// PreRelease is optional and may have dashes, tildes and ASCII alphanumeric characters. |
| 96 |
func WithRegisterToVersion(toVersion string) RegisterOption { |
| 97 |
return registerOptionFunc(func(g *Gate) error { |
| 98 |
to, err := version.NewVersion(toVersion) |
| 99 |
if err != nil { |
| 100 |
return fmt.Errorf("WithRegisterToVersion: invalid version %q: %w", toVersion, err) |
| 101 |
} |
| 102 |
|
| 103 |
g.toVersion = to |
| 104 |
return nil |
| 105 |
}) |
| 106 |
} |
| 107 |
|
| 108 |
// MustRegister like Register but panics if an invalid ID or gate options are provided. |
| 109 |
func (r *Registry) MustRegister(id string, stage Stage, opts ...RegisterOption) *Gate { |
| 110 |
g, err := r.Register(id, stage, opts...) |
| 111 |
if err != nil { |
| 112 |
panic(err) |
| 113 |
} |
| 114 |
return g |
| 115 |
} |
| 116 |
|
| 117 |
func validateID(id string) error { |
| 118 |
if id == "" { |
| 119 |
return errors.New("empty ID") |
| 120 |
} |
| 121 |
|
| 122 |
if !idRegexp.MatchString(id) { |
| 123 |
return errors.New("invalid character(s) in ID") |
| 124 |
} |
| 125 |
return nil |
| 126 |
} |
| 127 |
|
| 128 |
// Register a Gate and return it. The returned Gate can be used to check if is enabled or not. |
| 129 |
// id must be an ASCII alphanumeric nonempty string. Dots are allowed for namespacing. |
| 130 |
func (r *Registry) Register(id string, stage Stage, opts ...RegisterOption) (*Gate, error) { |
| 131 |
if err := validateID(id); err != nil { |
| 132 |
return nil, fmt.Errorf("invalid ID %q: %w", id, err) |
| 133 |
} |
| 134 |
|
| 135 |
g := &Gate{ |
| 136 |
id: id, |
| 137 |
stage: stage, |
| 138 |
} |
| 139 |
for _, opt := range opts { |
| 140 |
err := opt.apply(g) |
| 141 |
if err != nil { |
| 142 |
return nil, fmt.Errorf("failed to apply option: %w", err) |
| 143 |
} |
| 144 |
} |
| 145 |
switch g.stage { |
| 146 |
case StageAlpha, StageDeprecated: |
| 147 |
g.enabled = &atomic.Bool{} |
| 148 |
case StageBeta, StageStable: |
| 149 |
enabled := &atomic.Bool{} |
| 150 |
enabled.Store(true) |
| 151 |
g.enabled = enabled |
| 152 |
default: |
| 153 |
return nil, fmt.Errorf("unknown stage value %q for gate %q", stage, id) |
| 154 |
} |
| 155 |
if (g.stage == StageStable || g.stage == StageDeprecated) && g.toVersion == nil { |
| 156 |
return nil, fmt.Errorf("no removal version set for %v gate %q", g.stage.String(), id) |
| 157 |
} |
| 158 |
|
| 159 |
if g.fromVersion != nil && g.toVersion != nil && g.toVersion.LessThan(g.fromVersion) { |
| 160 |
return nil, fmt.Errorf("toVersion %q is before fromVersion %q", g.toVersion, g.fromVersion) |
| 161 |
} |
| 162 |
|
| 163 |
if _, loaded := r.gates.LoadOrStore(id, g); loaded { |
| 164 |
return nil, fmt.Errorf("failed to register %q: %w", id, ErrAlreadyRegistered) |
| 165 |
} |
| 166 |
return g, nil |
| 167 |
} |
| 168 |
|
| 169 |
// Set the enabled valued for a Gate identified by the given id. |
| 170 |
func (r *Registry) Set(id string, enabled bool) error { |
| 171 |
v, ok := r.gates.Load(id) |
| 172 |
if !ok { |
| 173 |
validGates := []string{} |
| 174 |
r.VisitAll(func(g *Gate) { |
| 175 |
validGates = append(validGates, g.ID()) |
| 176 |
}) |
| 177 |
return fmt.Errorf("no such feature gate %q. valid gates: %v", id, validGates) |
| 178 |
} |
| 179 |
g := v.(*Gate) |
| 180 |
|
| 181 |
switch g.stage { |
| 182 |
case StageStable: |
| 183 |
if !enabled { |
| 184 |
return fmt.Errorf("feature gate %q is stable, can not be disabled", id) |
| 185 |
} |
| 186 |
fmt.Printf("Feature gate %q is stable and already enabled. It will be removed in version %v and continued use of the gate after version %v will result in an error.\n", id, g.toVersion, g.toVersion) |
| 187 |
case StageDeprecated: |
| 188 |
if enabled { |
| 189 |
return fmt.Errorf("feature gate %q is deprecated, can not be enabled", id) |
| 190 |
} |
| 191 |
fmt.Printf("Feature gate %q is deprecated and already disabled. It will be removed in version %v and continued use of the gate after version %v will result in an error.\n", id, g.toVersion, g.toVersion) |
| 192 |
default: |
| 193 |
g.enabled.Store(enabled) |
| 194 |
} |
| 195 |
return nil |
| 196 |
} |
| 197 |
|
| 198 |
// VisitAll visits all the gates in lexicographical order, calling fn for each. |
| 199 |
func (r *Registry) VisitAll(fn func(*Gate)) { |
| 200 |
var gates []*Gate |
| 201 |
r.gates.Range(func(_, value any) bool { |
| 202 |
gates = append(gates, value.(*Gate)) |
| 203 |
return true |
| 204 |
}) |
| 205 |
sort.Slice(gates, func(i, j int) bool { |
| 206 |
return gates[i].ID() < gates[j].ID() |
| 207 |
}) |
| 208 |
for i := range gates { |
| 209 |
fn(gates[i]) |
| 210 |
} |
| 211 |
} |