Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cmd/mdatagen]: Add feature gates support to metadata-schema.yaml #11466

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
4 changes: 4 additions & 0 deletions cmd/mdatagen/internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@
toGenerate[filepath.Join(tmplDir, "documentation.md.tmpl")] = filepath.Join(ymlDir, "documentation.md")
}

if len(md.FeatureGates) != 0 {
toGenerate[filepath.Join(tmplDir, "feature_gates.go.tmpl")] = filepath.Join(ymlDir, "generated_feature_gates.go")
}

Check warning on line 133 in cmd/mdatagen/internal/command.go

View check run for this annotation

Codecov / codecov/patch

cmd/mdatagen/internal/command.go#L132-L133

Added lines #L132 - L133 were not covered by tests

for tmpl, dst := range toGenerate {
if err = generateFile(tmpl, dst, md, "metadata"); err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions cmd/mdatagen/internal/embedded_templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestEnsureTemplatesLoaded(t *testing.T) {
templateFiles = map[string]struct{}{
path.Join(rootDir, "component_test.go.tmpl"): {},
path.Join(rootDir, "documentation.md.tmpl"): {},
path.Join(rootDir, "feature_gates.go.tmpl"): {},
path.Join(rootDir, "metrics.go.tmpl"): {},
path.Join(rootDir, "metrics_test.go.tmpl"): {},
path.Join(rootDir, "resource.go.tmpl"): {},
Expand Down
77 changes: 77 additions & 0 deletions cmd/mdatagen/internal/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"strconv"
"strings"

"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/filter"
"go.opentelemetry.io/collector/pdata/pcommon"
)
Expand Down Expand Up @@ -41,6 +42,8 @@
ShortFolderName string `mapstructure:"-"`
// Tests is the set of tests generated with the component
Tests Tests `mapstructure:"tests"`
// FeatureGates that can be used for the component.
FeatureGates map[featureGateName]featureGate `mapstructure:"feature_gates"`
}

func (md *Metadata) Validate() error {
Expand Down Expand Up @@ -157,6 +160,70 @@
return errs
}

var (
// idRegexp is used to validate the ID of a Gate.
// IDs' characters must be alphanumeric or dots.
idRegexp = regexp.MustCompile(`^[0-9a-zA-Z\.]*$`)
versionRegexp = regexp.MustCompile(`^v(\d+)\.(\d+)\.(\d+)$`)
referenceURLRegexp = regexp.MustCompile(`^(https?:\/\/)?([a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+)(\/[^\s]*)?$`)
validStages = map[string]bool{
"StageAlpha": true,
"StageBeta": true,
"StageStable": true,
"StageDeprecated": true,
}
)

type featureGate struct {
// Required.
ID string `mapstructure:"id"`
// Description describes the purpose of the attribute.
Description string `mapstructure:"description"`
// Stage current stage at which the feature gate is in the development lifecyle
Stage string `mapstructure:"stage"`
// ReferenceURL can optionally give the url of the feature_gate
ReferenceURL string `mapstructure:"reference_url"`
// FromVersion optional field which gives the release version from which the gate has been given the current stage
FromVersion string `mapstructure:"from_version"`
// ToVersion optional field which gives the release version till which the gate the gate had the given lifecycle stage
ToVersion string `mapstructure:"to_version"`
// FeatureGateName name of the feature gate
FeatureGateName featureGateName `mapstructure:"-"`
}

func validateFeatureGate(parser *confmap.Conf) error {
var err []error
if !parser.IsSet("id") {
err = append(err, errors.New("missing required field: `id`"))
} else if !idRegexp.MatchString(fmt.Sprintf("%v", parser.Get("id"))) {
err = append(err, fmt.Errorf("invalid character(s) in ID"))
}

if !parser.IsSet("stage") {
err = append(err, errors.New("missing required field: `stage`"))
} else if _, ok := validStages[fmt.Sprintf("%v", parser.Get("stage"))]; !ok {
err = append(err, fmt.Errorf("invalid stage"))
}

if parser.IsSet("from_version") && !versionRegexp.MatchString(fmt.Sprintf("%v", parser.Get("from_version"))) {
err = append(err, fmt.Errorf("invalid character(s) in from_version"))
}
if parser.IsSet("to_version") && !versionRegexp.MatchString(fmt.Sprintf("%v", parser.Get("to_version"))) {
err = append(err, fmt.Errorf("invalid character(s) in to_version"))
}

Check warning on line 213 in cmd/mdatagen/internal/metadata.go

View check run for this annotation

Codecov / codecov/patch

cmd/mdatagen/internal/metadata.go#L212-L213

Added lines #L212 - L213 were not covered by tests
if parser.IsSet("reference_url") && !referenceURLRegexp.MatchString(fmt.Sprintf("%v", parser.Get("reference_url"))) {
err = append(err, fmt.Errorf("invalid character(s) in reference_url"))
}
return errors.Join(err...)
}

func (f *featureGate) Unmarshal(parser *confmap.Conf) error {
if err := validateFeatureGate(parser); err != nil {
return err
}
return parser.Unmarshal(f)

Check warning on line 224 in cmd/mdatagen/internal/metadata.go

View check run for this annotation

Codecov / codecov/patch

cmd/mdatagen/internal/metadata.go#L220-L224

Added lines #L220 - L224 were not covered by tests
}

type AttributeName string

func (mn AttributeName) Render() (string, error) {
Expand All @@ -167,6 +234,16 @@
return FormatIdentifier(string(mn), false)
}

type featureGateName string

func (mn featureGateName) Render() (string, error) {
return FormatIdentifier(string(mn), true)

Check warning on line 240 in cmd/mdatagen/internal/metadata.go

View check run for this annotation

Codecov / codecov/patch

cmd/mdatagen/internal/metadata.go#L239-L240

Added lines #L239 - L240 were not covered by tests
}

func (mn featureGateName) RenderUnexported() (string, error) {
return FormatIdentifier(string(mn), false)

Check warning on line 244 in cmd/mdatagen/internal/metadata.go

View check run for this annotation

Codecov / codecov/patch

cmd/mdatagen/internal/metadata.go#L243-L244

Added lines #L243 - L244 were not covered by tests
}

// ValueType defines an attribute value type.
type ValueType struct {
// ValueType is type of the attribute value.
Expand Down
68 changes: 68 additions & 0 deletions cmd/mdatagen/internal/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/confmap"
)

func TestValidate(t *testing.T) {
Expand Down Expand Up @@ -142,6 +144,72 @@ func TestValidateMetricDuplicates(t *testing.T) {
}
}

func TestFeatureGateValidate(t *testing.T) {
tests := []struct {
name string
input map[string]interface{}
wantErr string
}{
{
name: "missing id",
input: map[string]interface{}{
"stage": "StageAlpha",
},
wantErr: "missing required field: `id`",
},
{
name: "invalid id",
input: map[string]interface{}{
"id": "invalid@id",
"stage": "StageAlpha",
},
wantErr: "invalid character(s) in ID",
},
{
name: "missing stage",
input: map[string]interface{}{
"id": "valid.id",
},
wantErr: "missing required field: `stage`",
},
{
name: "invalid stage",
input: map[string]interface{}{
"id": "valid.id",
"stage": "InvalidStage",
},
wantErr: "invalid stage",
},
{
name: "invalid from_version",
input: map[string]interface{}{
"id": "valid.id",
"stage": "StageAlpha",
"from_version": "v1.2.a",
},
wantErr: "invalid character(s) in from_version",
},
{
name: "invalid reference_url",
input: map[string]interface{}{
"id": "valid.id",
"stage": "StageAlpha",
"reference_url": "invalid-url",
},
wantErr: "invalid character(s) in reference_url",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parser := confmap.NewFromStringMap(tt.input)
err := validateFeatureGate(parser)
require.Error(t, err)
require.EqualError(t, err, tt.wantErr)
})
}
}

func contains(r string, rs []string) bool {
for _, s := range rs {
if s == r {
Expand Down
20 changes: 20 additions & 0 deletions cmd/mdatagen/internal/sampleprocessor/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,23 @@ resource_attributes:
enabled: true
warnings:
if_enabled: This resource_attribute is deprecated and will be removed soon.

feature_gates:
useOTTLBridge:
id: useOTTLBridgeGate
stage: StageAlpha
description: When enabled, filterlog will convert filterlog configuration to OTTL and use filterottl evaluation
from_version: v1.0.0
reference_url: https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/18642
to_version: v2.0.0

allowFileDeletion:
id: allowFileDeletionGate
stage: StageStable
description: When enabled, allows usage of the `delete_after_read` setting.
from_version: v1.5.0
reference_url: https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/16314

allowFileCreation:
id: allowFileCreationGate
stage: StageStable
22 changes: 22 additions & 0 deletions cmd/mdatagen/internal/templates/feature_gates.go.tmpl
narcis96 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Code generated by mdatagen. DO NOT EDIT.

package {{ .Package }}

import (
"go.opentelemetry.io/collector/featuregate"
)

func init() {
{{- range $name, $fg := .FeatureGates }}
{
var opts []featuregate.RegisterOption
opts = append(opts
{{- if $fg.FromVersion }}, featuregate.WithRegisterFromVersion("{{ $fg.FromVersion }}") {{- end }}
{{- if $fg.Description }}, featuregate.WithRegisterDescription("{{ $fg.Description }}") {{- end }}
{{- if $fg.ReferenceURL }}, featuregate.WithRegisterReferenceURL("{{ $fg.ReferenceURL }}") {{- end }}
{{- if $fg.ToVersion }}, featuregate.WithRegisterToVersion("{{ $fg.ToVersion }}") {{- end }})
_ = featuregate.GlobalRegistry().MustRegister("{{ $fg.ID }}", featuregate.{{ $fg.Stage }}, opts...)
}
{{- end }}

}
17 changes: 17 additions & 0 deletions cmd/mdatagen/metadata-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,20 @@ telemetry:
# Optional: array of attributes that were defined in the attributes section that are emitted by this metric.
# Note: Only the following attribute types are supported: <string|int|double|bool>
attributes: [string]

#Optional: Gate is an immutable object that is owned by the Registry and represents an individual feature that
# may be enabled or disabled based on the lifecycle state of the feature and CLI flags specified by the user.
feature_gates:
<feature_gate.name>:
#Required: id of the feature gate
id:
#Optional: description of the gate
description:
#Required: current stage at which the feature gate is in the development lifecyle
stage:
#Optional: link to the issue where the gate has been discussed
reference_url:
#Optional: the release version from which the gate has been given the current stage
from_version:
#Optional: the release version till which the gate had the given lifecycle stage
to_version:
Loading