| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Package semconvkit is used to generate opentelemetry-go specific semantic |
| 5 |
// convention code. |
| 6 |
package main |
| 7 |
|
| 8 |
import ( |
| 9 |
"embed" |
| 10 |
"errors" |
| 11 |
"flag" |
| 12 |
"fmt" |
| 13 |
"go/ast" |
| 14 |
"go/token" |
| 15 |
"log/slog" |
| 16 |
"os" |
| 17 |
"path/filepath" |
| 18 |
"sort" |
| 19 |
"strings" |
| 20 |
"text/template" |
| 21 |
|
| 22 |
"github.com/Masterminds/semver" |
| 23 |
|
| 24 |
"go.opentelemetry.io/otel/internal/tools/semconvkit/decls" |
| 25 |
) |
| 26 |
|
| 27 |
var ( |
| 28 |
logLevel = flag.String("log-level", "", `Logging level ("debug", "info", "warn", "error")`) |
| 29 |
semconvPkg = flag.String("semconv", "./", "semconv package directory") |
| 30 |
tag = flag.String("tag", "", "OpenTelemetry tagged version") |
| 31 |
prev = flag.String("prev", "", "previous semconv version") |
| 32 |
|
| 33 |
//go:embed templates/*.tmpl |
| 34 |
rootFS embed.FS |
| 35 |
) |
| 36 |
|
| 37 |
func main() { |
| 38 |
flag.Parse() |
| 39 |
|
| 40 |
slog.SetDefault(newLogger(*logLevel)) |
| 41 |
|
| 42 |
if *tag == "" { |
| 43 |
slog.Error("invalid tag", "tag", *tag) |
| 44 |
os.Exit(1) |
| 45 |
} |
| 46 |
|
| 47 |
sc := &SemanticConventions{TagVer: *tag} |
| 48 |
|
| 49 |
out := filepath.Join(*semconvPkg, *tag) |
| 50 |
|
| 51 |
// Render all other files before the MIGRATION file. That file needs the |
| 52 |
// full package declaration so it can determine compatibility accurately. |
| 53 |
entries, err := rootFS.ReadDir("templates") |
| 54 |
if err != nil { |
| 55 |
slog.Error("error reading templates", "err", err) |
| 56 |
os.Exit(1) |
| 57 |
} |
| 58 |
|
| 59 |
for _, entry := range entries { |
| 60 |
if entry.Name() == "MIGRATION.md.tmpl" { |
| 61 |
continue |
| 62 |
} |
| 63 |
|
| 64 |
src := filepath.Join("templates", entry.Name()) |
| 65 |
err := render(src, out, sc) |
| 66 |
if err != nil { |
| 67 |
slog.Error("error rendering template", "err", err, "template", entry.Name()) |
| 68 |
os.Exit(1) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
prevPkg, err := prevVer(*semconvPkg, *tag, *prev) |
| 73 |
if err != nil { |
| 74 |
slog.Error("previous version not found, skipping migration", "err", err) |
| 75 |
os.Exit(1) |
| 76 |
} |
| 77 |
|
| 78 |
slog.Debug("previous version found", "prev", prevPkg) |
| 79 |
m, err := newMigration(out, filepath.Join(*semconvPkg, prevPkg)) |
| 80 |
if err != nil { |
| 81 |
slog.Error("error getting migration, skipping", "err", err) |
| 82 |
os.Exit(1) |
| 83 |
} |
| 84 |
|
| 85 |
if err := render("templates/MIGRATION.md.tmpl", out, m); err != nil { |
| 86 |
slog.Error("error rendering migration template", "err", err) |
| 87 |
os.Exit(1) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
func newLogger(lvlStr string) *slog.Logger { |
| 92 |
levelVar := new(slog.LevelVar) // Default value of info. |
| 93 |
opts := &slog.HandlerOptions{AddSource: true, Level: levelVar} |
| 94 |
h := slog.NewTextHandler(os.Stderr, opts) |
| 95 |
logger := slog.New(h) |
| 96 |
|
| 97 |
if lvlStr == "" { |
| 98 |
return logger |
| 99 |
} |
| 100 |
|
| 101 |
var level slog.Level |
| 102 |
if err := level.UnmarshalText([]byte(lvlStr)); err != nil { |
| 103 |
logger.Error("failed to parse log level", "error", err, "log-level", lvlStr) |
| 104 |
} else { |
| 105 |
levelVar.Set(level) |
| 106 |
} |
| 107 |
|
| 108 |
return logger |
| 109 |
} |
| 110 |
|
| 111 |
// render renders all templates to the dest directory using the data. |
| 112 |
func render(src, dest string, data any) error { |
| 113 |
tmpls, err := template.ParseFS(rootFS, src) |
| 114 |
if err != nil { |
| 115 |
return err |
| 116 |
} |
| 117 |
for _, tmpl := range tmpls.Templates() { |
| 118 |
slog.Debug("rendering template", "name", tmpl.Name()) |
| 119 |
target := filepath.Join(dest, strings.TrimSuffix(tmpl.Name(), ".tmpl")) |
| 120 |
wr, err := os.Create(target) |
| 121 |
if err != nil { |
| 122 |
return err |
| 123 |
} |
| 124 |
|
| 125 |
err = tmpl.Execute(wr, data) |
| 126 |
if err != nil { |
| 127 |
return err |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return nil |
| 132 |
} |
| 133 |
|
| 134 |
// prevVer returns the previous version of the semantic conventions package. |
| 135 |
// It will first check for hint within root and return that value if found. If |
| 136 |
// not found, it will find all directories in root with a version name and |
| 137 |
// return the version that is less than and closest to the curr version. |
| 138 |
func prevVer(root, cur, hint string) (string, error) { |
| 139 |
slog.Debug("prevVer", "root", root, "current", cur, "hint", hint) |
| 140 |
info, err := os.Stat(root) |
| 141 |
if err != nil { |
| 142 |
return "", fmt.Errorf("root directory %q not found: %w", root, err) |
| 143 |
} |
| 144 |
if !info.IsDir() { |
| 145 |
return "", fmt.Errorf("root %q is not a directory", root) |
| 146 |
} |
| 147 |
|
| 148 |
if hint != "" { |
| 149 |
sub := filepath.Join(root, hint) |
| 150 |
slog.Debug("looking for hint", "path", sub) |
| 151 |
info, err = os.Stat(sub) |
| 152 |
if err == nil && info.IsDir() { |
| 153 |
return hint, nil |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
v, err := semver.NewVersion(cur) |
| 158 |
if err != nil { |
| 159 |
return "", fmt.Errorf("invalid current version %q: %w", cur, err) |
| 160 |
} |
| 161 |
|
| 162 |
entries, err := os.ReadDir(root) |
| 163 |
if err != nil { |
| 164 |
return "", fmt.Errorf("error reading root %q: %w", root, err) |
| 165 |
} |
| 166 |
|
| 167 |
var prev *semver.Version |
| 168 |
for _, entry := range entries { |
| 169 |
slog.Debug("root entry", "name", entry.Name()) |
| 170 |
if !entry.IsDir() { |
| 171 |
continue |
| 172 |
} |
| 173 |
|
| 174 |
ver, err := semver.NewVersion(entry.Name()) |
| 175 |
if err != nil { |
| 176 |
slog.Debug("not a version dir", "name", entry.Name()) |
| 177 |
// Ignore errors for non-semver directories. |
| 178 |
continue |
| 179 |
} |
| 180 |
slog.Debug("found version dir", "prev", ver) |
| 181 |
if ver.LessThan(v) && (prev == nil || ver.GreaterThan(prev)) { |
| 182 |
slog.Debug("new previous version", "version", ver) |
| 183 |
prev = ver |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
if prev == nil { |
| 188 |
return "", errors.New("no previous version found") |
| 189 |
} |
| 190 |
return prev.Original(), nil |
| 191 |
} |
| 192 |
|
| 193 |
// SemanticConventions are information about the semantic conventions being |
| 194 |
// generated. |
| 195 |
type SemanticConventions struct { |
| 196 |
// TagVer is the tagged version (i.e. v.7.0 and not 1.7.0). |
| 197 |
TagVer string |
| 198 |
} |
| 199 |
|
| 200 |
func (sc SemanticConventions) SemVer() string { |
| 201 |
return strings.TrimPrefix(*tag, "v") |
| 202 |
} |
| 203 |
|
| 204 |
// Migration contains the details about the migration from the previous |
| 205 |
// semantic conventions to the current one. |
| 206 |
type Migration struct { |
| 207 |
CurVer string |
| 208 |
PrevVer string |
| 209 |
Removals []string |
| 210 |
Renames []Rename |
| 211 |
} |
| 212 |
|
| 213 |
// Remove is a semantic convention declaration that has been renamed. |
| 214 |
type Rename struct { |
| 215 |
Old, New string |
| 216 |
} |
| 217 |
|
| 218 |
func newMigration(cur, prev string) (*Migration, error) { |
| 219 |
cDecl, err := decls.GetNames(cur, parse) |
| 220 |
if err != nil { |
| 221 |
return nil, fmt.Errorf("error parsing current version %q: %w", cur, err) |
| 222 |
} |
| 223 |
|
| 224 |
pDecl, err := decls.GetNames(prev, parse) |
| 225 |
if err != nil { |
| 226 |
return nil, fmt.Errorf("error parsing previous version %q: %w", prev, err) |
| 227 |
} |
| 228 |
|
| 229 |
m := Migration{ |
| 230 |
CurVer: filepath.Base(cur), |
| 231 |
PrevVer: filepath.Base(prev), |
| 232 |
Removals: inAnotB(pDecl, cDecl), |
| 233 |
Renames: renames(pDecl, cDecl), |
| 234 |
} |
| 235 |
|
| 236 |
sort.Strings(m.Removals) |
| 237 |
sort.Slice(m.Renames, func(i, j int) bool { |
| 238 |
return m.Renames[i].Old < m.Renames[j].Old |
| 239 |
}) |
| 240 |
|
| 241 |
return &m, nil |
| 242 |
} |
| 243 |
|
| 244 |
func parse(d ast.Decl) []string { |
| 245 |
var out []string |
| 246 |
switch decl := d.(type) { |
| 247 |
case *ast.FuncDecl: |
| 248 |
out = []string{decl.Name.Name} |
| 249 |
case *ast.GenDecl: |
| 250 |
if decl.Tok == token.CONST || decl.Tok == token.VAR { |
| 251 |
for _, spec := range decl.Specs { |
| 252 |
if valueSpec, ok := spec.(*ast.ValueSpec); ok { |
| 253 |
for _, name := range valueSpec.Names { |
| 254 |
out = append(out, name.Name) |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
return out |
| 261 |
} |
| 262 |
|
| 263 |
// inAnotB returns the canonical names in a that are not in b. |
| 264 |
func inAnotB(a, b decls.Names) []string { |
| 265 |
var diff []string |
| 266 |
for key, name := range a { |
| 267 |
if _, ok := b[key]; !ok { |
| 268 |
diff = append(diff, string(name)) |
| 269 |
} |
| 270 |
} |
| 271 |
return diff |
| 272 |
} |
| 273 |
|
| 274 |
// renames returns the renames between the old and current names. |
| 275 |
func renames(old, current decls.Names) []Rename { |
| 276 |
var renames []Rename |
| 277 |
for key, name := range old { |
| 278 |
if otherName, ok := current[key]; ok && name != otherName { |
| 279 |
renames = append(renames, Rename{ |
| 280 |
Old: string(name), |
| 281 |
New: string(otherName), |
| 282 |
}) |
| 283 |
} |
| 284 |
} |
| 285 |
return renames |
| 286 |
} |