Debian Codegraph @

cmd/mdatagen/internal/status.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package internal // import "go.opentelemetry.io/collector/cmd/mdatagen/internal"
5
6 import (
7 "errors"
8 "fmt"
9 "slices"
10 "sort"
11 "time"
12
13 "go.opentelemetry.io/collector/component"
14 )
15
16 // distroURL returns the collection of distributions that can be referenced in the metadata.yaml files.
17 // The rules below apply to every distribution added to this list:
18 // - The distribution is open source and maintained by the OpenTelemetry project.
19 // - The link must point to a publicly accessible repository.
20 func distroURL(name string) string {
21 switch name {
22 case "core":
23 return "https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol"
24 case "contrib":
25 return "https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol-contrib"
26 case "k8s":
27 return "https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol-k8s"
28 case "otlp":
29 return "https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol-otlp"
30 default:
31 return ""
32 }
33 }
34
35 type Codeowners struct {
36 // Active codeowners
37 Active []string `mapstructure:"active"`
38 // Emeritus codeowners
39 Emeritus []string `mapstructure:"emeritus"`
40 // Whether new codeowners are being sought
41 SeekingNew bool `mapstructure:"seeking_new"`
42 }
43
44 type Status struct {
45 Stability StabilityMap `mapstructure:"stability"`
46 Distributions []string `mapstructure:"distributions"`
47 Class string `mapstructure:"class"`
48 Warnings []string `mapstructure:"warnings"`
49 Codeowners *Codeowners `mapstructure:"codeowners"`
50 UnsupportedPlatforms []string `mapstructure:"unsupported_platforms"`
51 Deprecation DeprecationMap `mapstructure:"deprecation"`
52 CodeCovComponentID string `mapstructure:"codecov_component_id"`
53 DisableCodeCov bool `mapstructure:"disable_codecov_badge"`
54 }
55
56 type DeprecationMap map[string]DeprecationInfo
57
58 type DeprecationInfo struct {
59 Date string `mapstructure:"date"`
60 Migration string `mapstructure:"migration"`
61 }
62
63 var validClasses = []string{
64 "cmd",
65 "connector",
66 "converter",
67 "exporter",
68 "extension",
69 "pkg",
70 "processor",
71 "provider",
72 "receiver",
73 "scraper",
74 }
75
76 var validStabilityKeys = []string{
77 "converter",
78 "extension",
79 "logs",
80 "logs_to_traces",
81 "logs_to_metrics",
82 "logs_to_logs",
83 "logs_to_profiles",
84 "metrics",
85 "metrics_to_traces",
86 "metrics_to_metrics",
87 "metrics_to_logs",
88 "metrics_to_profiles",
89 "profiles",
90 "profiles_to_profiles",
91 "profiles_to_traces",
92 "profiles_to_metrics",
93 "profiles_to_logs",
94 "provider",
95 "traces_to_traces",
96 "traces_to_metrics",
97 "traces_to_logs",
98 "traces_to_profiles",
99 "traces",
100 }
101
102 func (s *Status) SortedDistributions() []string {
103 sorted := s.Distributions
104 sort.Slice(sorted, func(i, j int) bool {
105 if s.Distributions[i] == "core" {
106 return true
107 }
108 if s.Distributions[i] == "contrib" {
109 return s.Distributions[j] != "core"
110 }
111 if s.Distributions[j] == "core" {
112 return false
113 }
114 if s.Distributions[j] == "contrib" {
115 return s.Distributions[i] == "core"
116 }
117 return s.Distributions[i] < s.Distributions[j]
118 })
119 return sorted
120 }
121
122 func (s *Status) Validate() error {
123 var errs error
124 if s == nil {
125 return errors.New("missing status")
126 }
127 if err := s.validateClass(); err != nil {
128 errs = errors.Join(errs, err)
129 }
130
131 if err := s.Stability.Validate(); err != nil {
132 errs = errors.Join(errs, err)
133 }
134 if err := s.Deprecation.Validate(s.Stability); err != nil {
135 errs = errors.Join(errs, err)
136 }
137 return errs
138 }
139
140 func (s *Status) validateClass() error {
141 if s.Class == "" {
142 return errors.New("missing class")
143 }
144 if !slices.Contains(validClasses, s.Class) {
145 return fmt.Errorf("invalid class: %v", s.Class)
146 }
147 return nil
148 }
149
150 type StabilityMap map[component.StabilityLevel][]string
151
152 func (ms StabilityMap) Validate() error {
153 var errs error
154 if len(ms) == 0 {
155 return errors.New("missing stability")
156 }
157 for stability, cmps := range ms {
158 if len(cmps) == 0 {
159 errs = errors.Join(errs, fmt.Errorf("missing component for stability: %v", stability))
160 }
161 for _, c := range cmps {
162 if !slices.Contains(validStabilityKeys, c) {
163 errs = errors.Join(errs, fmt.Errorf("invalid component: %v", c))
164 }
165 }
166 }
167 return errs
168 }
169
170 func (dm DeprecationMap) Validate(ms StabilityMap) error {
171 var errs error
172 for stability, cmps := range ms {
173 if stability != component.StabilityLevelDeprecated {
174 continue
175 }
176 for _, c := range cmps {
177 depInfo, found := dm[c]
178 if !found {
179 errs = errors.Join(errs, fmt.Errorf("deprecated component missing deprecation date and migration guide for %v", c))
180 continue
181 }
182 if depInfo.Migration == "" {
183 errs = errors.Join(errs, fmt.Errorf("deprecated component missing migration guide: %v", c))
184 }
185 if depInfo.Date == "" {
186 errs = errors.Join(errs, fmt.Errorf("deprecated component missing date in YYYY-MM-DD format: %v", c))
187 } else {
188 _, err := time.Parse("2006-01-02", depInfo.Date)
189 if err != nil {
190 errs = errors.Join(errs, fmt.Errorf("deprecated component missing valid date in YYYY-MM-DD format: %v", c))
191 }
192 }
193 }
194 }
195 return errs
196 }