Skip to content

Commit 4390c45

Browse files
dmitryaxXinRanZhAWS
authored andcommitted
[chore] [cmd/mdatagen] Use enum for stability levels (open-telemetry#31530)
It'll allow us to apply comparisons to the stability levels
1 parent 53cbbb0 commit 4390c45

File tree

21 files changed

+120
-53
lines changed

21 files changed

+120
-53
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: 'breaking'
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: 'cmd/mdatagen'
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Use enum for stability levels in the Metadata struct"
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [31530]
14+
15+
# If your change doesn't affect end users or the exported elements of any package,
16+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
17+
# Optional: The change log or logs in which this entry should be included.
18+
# e.g. '[user]' or '[user, api]'
19+
# Include 'user' if the change is relevant to end users.
20+
# Include 'api' if there is a change to a library API.
21+
# Default: '[user]'
22+
change_logs: [api]

cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_status.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/loader_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
"github.com/stretchr/testify/require"
10+
"go.opentelemetry.io/collector/component"
1011
"go.opentelemetry.io/collector/pdata/pcommon"
1112
"go.opentelemetry.io/collector/pdata/pmetric"
1213
)
@@ -24,10 +25,10 @@ func TestLoadMetadata(t *testing.T) {
2425
SemConvVersion: "1.9.0",
2526
Status: &Status{
2627
Class: "receiver",
27-
Stability: map[string][]string{
28-
"development": {"logs"},
29-
"beta": {"traces"},
30-
"stable": {"metrics"},
28+
Stability: map[component.StabilityLevel][]string{
29+
component.StabilityLevelDevelopment: {"logs"},
30+
component.StabilityLevelBeta: {"traces"},
31+
component.StabilityLevelStable: {"metrics"},
3132
},
3233
Distributions: []string{},
3334
Codeowners: &Codeowners{

cmd/mdatagen/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ func templatize(tmplFile string, md metadata) *template.Template {
165165
}
166166
return result
167167
},
168-
"casesTitle": cases.Title(language.English).String,
168+
"casesTitle": cases.Title(language.English).String,
169+
"toLowerCase": strings.ToLower,
169170
"toCamelCase": func(s string) string {
170171
caser := cases.Title(language.English).String
171172
parts := strings.Split(s, "_")

cmd/mdatagen/main_test.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212

1313
"github.com/stretchr/testify/require"
14+
"go.opentelemetry.io/collector/component"
1415
)
1516

1617
func TestRunContents(t *testing.T) {
@@ -129,7 +130,7 @@ func TestInlineReplace(t *testing.T) {
129130
outputFile string
130131
componentClass string
131132
warnings []string
132-
stability map[string][]string
133+
stability map[component.StabilityLevel][]string
133134
distros []string
134135
codeowners *Codeowners
135136
}{
@@ -258,8 +259,11 @@ Some warning there.
258259
Some info about a component
259260
`,
260261
outputFile: "readme_with_multiple_signals.md",
261-
stability: map[string][]string{"beta": {"metrics"}, "alpha": {"logs"}},
262-
distros: []string{"contrib"},
262+
stability: map[component.StabilityLevel][]string{
263+
component.StabilityLevelBeta: {"metrics"},
264+
component.StabilityLevelAlpha: {"logs"},
265+
},
266+
distros: []string{"contrib"},
263267
},
264268
{
265269
name: "readme with cmd class",
@@ -270,15 +274,18 @@ Some info about a component
270274
271275
Some info about a component
272276
`,
273-
outputFile: "readme_with_cmd_class.md",
274-
stability: map[string][]string{"beta": {"metrics"}, "alpha": {"logs"}},
277+
outputFile: "readme_with_cmd_class.md",
278+
stability: map[component.StabilityLevel][]string{
279+
component.StabilityLevelBeta: {"metrics"},
280+
component.StabilityLevelAlpha: {"logs"},
281+
},
275282
componentClass: "cmd",
276283
distros: []string{},
277284
},
278285
}
279286
for _, tt := range tests {
280287
t.Run(tt.name, func(t *testing.T) {
281-
stability := map[string][]string{"beta": {"metrics"}}
288+
stability := map[component.StabilityLevel][]string{component.StabilityLevelBeta: {"metrics"}}
282289
if len(tt.stability) > 0 {
283290
stability = tt.stability
284291
}
@@ -327,7 +334,9 @@ func TestGenerateStatusMetadata(t *testing.T) {
327334
md: metadata{
328335
Type: "foo",
329336
Status: &Status{
330-
Stability: map[string][]string{"beta": {"metrics"}},
337+
Stability: map[component.StabilityLevel][]string{
338+
component.StabilityLevelBeta: {"metrics"},
339+
},
331340
Distributions: []string{"contrib"},
332341
Class: "receiver",
333342
},
@@ -364,7 +373,9 @@ func Tracer(settings component.TelemetrySettings) trace.Tracer {
364373
md: metadata{
365374
Type: "foo",
366375
Status: &Status{
367-
Stability: map[string][]string{"alpha": {"metrics"}},
376+
Stability: map[component.StabilityLevel][]string{
377+
component.StabilityLevelAlpha: {"metrics"},
378+
},
368379
Distributions: []string{"contrib"},
369380
Class: "receiver",
370381
},

cmd/mdatagen/statusdata.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
package main
55

66
import (
7+
"errors"
78
"sort"
9+
"strings"
10+
11+
"go.opentelemetry.io/collector/component"
12+
"go.opentelemetry.io/collector/confmap"
813
)
914

1015
// distros is a collection of distributions that can be referenced in the metadata.yaml files.
@@ -32,13 +37,15 @@ type Codeowners struct {
3237
SeekingNew bool `mapstructure:"seeking_new"`
3338
}
3439

40+
type StabilityMap map[component.StabilityLevel][]string
41+
3542
type Status struct {
36-
Stability map[string][]string `mapstructure:"stability"`
37-
Distributions []string `mapstructure:"distributions"`
38-
Class string `mapstructure:"class"`
39-
Warnings []string `mapstructure:"warnings"`
40-
Codeowners *Codeowners `mapstructure:"codeowners"`
41-
UnsupportedPlatforms []string `mapstructure:"unsupported_platforms"`
43+
Stability StabilityMap `mapstructure:"stability"`
44+
Distributions []string `mapstructure:"distributions"`
45+
Class string `mapstructure:"class"`
46+
Warnings []string `mapstructure:"warnings"`
47+
Codeowners *Codeowners `mapstructure:"codeowners"`
48+
UnsupportedPlatforms []string `mapstructure:"unsupported_platforms"`
4249
}
4350

4451
func (s *Status) SortedDistributions() []string {
@@ -60,3 +67,31 @@ func (s *Status) SortedDistributions() []string {
6067
})
6168
return sorted
6269
}
70+
71+
func (ms *StabilityMap) Unmarshal(parser *confmap.Conf) error {
72+
*ms = make(StabilityMap)
73+
raw := make(map[string][]string)
74+
err := parser.Unmarshal(&raw)
75+
if err != nil {
76+
return err
77+
}
78+
for k, v := range raw {
79+
switch strings.ToLower(k) {
80+
case strings.ToLower(component.StabilityLevelUnmaintained.String()):
81+
(*ms)[component.StabilityLevelUnmaintained] = v
82+
case strings.ToLower(component.StabilityLevelDeprecated.String()):
83+
(*ms)[component.StabilityLevelDeprecated] = v
84+
case strings.ToLower(component.StabilityLevelDevelopment.String()):
85+
(*ms)[component.StabilityLevelDevelopment] = v
86+
case strings.ToLower(component.StabilityLevelAlpha.String()):
87+
(*ms)[component.StabilityLevelAlpha] = v
88+
case strings.ToLower(component.StabilityLevelBeta.String()):
89+
(*ms)[component.StabilityLevelBeta] = v
90+
case strings.ToLower(component.StabilityLevelStable.String()):
91+
(*ms)[component.StabilityLevelStable] = v
92+
default:
93+
return errors.New("invalid stability level: " + k)
94+
}
95+
}
96+
return nil
97+
}

cmd/mdatagen/templates/readme.md.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
{{- if ne $class "connector" }}
88
{{- $idx := 0 }}
99
{{- range $stability, $value := .Status.Stability }}
10-
| {{ if not $idx }}Stability{{ else }} {{ end }} | [{{ $stability }}]{{ if ne $class "extension" }}: {{ stringsJoin $value ", " }} {{ end }} |
10+
| {{ if not $idx }}Stability{{ else }} {{ end }} | [{{ toLowerCase $stability.String }}]{{ if ne $class "extension" }}: {{ stringsJoin $value ", " }} {{ end }} |
1111
{{- $idx = inc $idx }}
1212
{{- end }}
1313
{{- end}}
@@ -29,7 +29,7 @@
2929
{{- end }}
3030
{{- end }}
3131
{{range $stability, $val := .Status.Stability}}
32-
[{{ $stability }}]: https://github.com/open-telemetry/opentelemetry-collector#{{ $stability }}
32+
[{{ toLowerCase $stability.String }}]: https://github.com/open-telemetry/opentelemetry-collector#{{ toLowerCase $stability.String }}
3333
{{- end }}
3434
{{- range .Status.SortedDistributions }}
3535
[{{.}}]: {{ distroURL . }}
@@ -43,7 +43,7 @@
4343
{{- range $stability, $pipelines := .Status.Stability }}
4444
{{- range $pipeline := $pipelines }}
4545
{{- $parts := stringsSplit $pipeline "_to_" }}
46-
| {{index $parts 0}} | {{index $parts 1}} | [{{$stability}}] |
46+
| {{index $parts 0}} | {{index $parts 1}} | [{{ toLowerCase $stability.String }}] |
4747
{{- end }}
4848
{{- end }}
4949

cmd/mdatagen/templates/status.go.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var (
1717
const (
1818
{{- range $stability, $signals := .Status.Stability }}
1919
{{- range $signal := $signals }}
20-
{{ toCamelCase $signal }}Stability = component.StabilityLevel{{ casesTitle $stability }}
20+
{{ toCamelCase $signal }}Stability = component.StabilityLevel{{ $stability.String }}
2121
{{- end }}
2222
{{- end }}
2323
)

cmd/mdatagen/validate.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,7 @@ func (s *Status) validateStability() error {
8989
return errors.New("missing stability")
9090
}
9191
for stability, component := range s.Stability {
92-
if stability != "development" && stability != "alpha" && stability != "beta" && stability != "stable" && stability != "deprecated" && stability != "unmaintained" {
93-
errs = multierr.Append(errs, fmt.Errorf("invalid stability: %v", stability))
94-
}
95-
if component == nil {
92+
if len(component) == 0 {
9693
errs = multierr.Append(errs, fmt.Errorf("missing component for stability: %v", stability))
9794
}
9895
for _, c := range component {

cmd/mdatagen/validate_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ func TestValidate(t *testing.T) {
4040
},
4141
{
4242
name: "testdata/invalid_stability.yaml",
43-
wantErr: "invalid stability: incorrectstability",
43+
wantErr: "1 error(s) decoding:\n\n* error decoding 'status.stability': invalid stability level: incorrectstability",
4444
},
4545
{
4646
name: "testdata/no_stability_component.yaml",
47-
wantErr: "missing component for stability: beta",
47+
wantErr: "missing component for stability: Beta",
4848
},
4949
{
5050
name: "testdata/invalid_stability_component.yaml",

0 commit comments

Comments
 (0)