|
| 1 | +package otlpbuild |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "crypto/sha1" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "io/fs" |
| 10 | + "os" |
| 11 | + "os/exec" |
| 12 | + "path/filepath" |
| 13 | + "regexp" |
| 14 | + "sort" |
| 15 | + "strings" |
| 16 | +) |
| 17 | + |
| 18 | +type Config struct { |
| 19 | + // SrcDir is the path to opentelemetry directory, typically inside a copy of |
| 20 | + // the opentelemetry-proto repository. |
| 21 | + SrcDir string |
| 22 | + // TmpDir is the path to a temporary directory that will be used to build a |
| 23 | + // version of the OTLP proto files. |
| 24 | + TmpDir string |
| 25 | + // DstDir is the path to the directory where the built OTLP Go files will be |
| 26 | + // stored. |
| 27 | + DstDir string |
| 28 | + // PackagePrefix is the prefix to use for the Go package names. |
| 29 | + PackagePrefix string |
| 30 | +} |
| 31 | + |
| 32 | +// Build builds the OTLP Go bindings and uses the base name of the DstDir as a |
| 33 | +// namespace to allow importing multiple versions of the same proto files into |
| 34 | +// the same program. |
| 35 | +func Build(ctx context.Context, c Config) error { |
| 36 | + // derive srcDir |
| 37 | + srcDir, err := filepath.Abs(filepath.Join(c.TmpDir, "src")) |
| 38 | + if err != nil { |
| 39 | + return fmt.Errorf("get absolute path: %w", err) |
| 40 | + } |
| 41 | + |
| 42 | + // derive namespace directory |
| 43 | + namespace := filepath.Base(c.DstDir) |
| 44 | + namespaceDir := filepath.Join(srcDir, namespace) |
| 45 | + |
| 46 | + // derive dstDir |
| 47 | + dstDir, err := filepath.Abs(c.DstDir) |
| 48 | + if err != nil { |
| 49 | + return fmt.Errorf("get absolute path: %w", err) |
| 50 | + } |
| 51 | + |
| 52 | + // copy srcDir to nameSpaceDir |
| 53 | + if err := os.CopyFS(filepath.Join(namespaceDir, "opentelemetry"), os.DirFS(c.SrcDir)); err != nil { |
| 54 | + return fmt.Errorf("copy source directory: %w", err) |
| 55 | + } |
| 56 | + |
| 57 | + // find proto files |
| 58 | + protoFiles, err := findProtoFiles(ctx, namespaceDir) |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("find proto files: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + // rewrite proto files |
| 64 | + for _, protoFile := range protoFiles { |
| 65 | + if err := rewriteProtoFile(protoFile, namespace, c.PackagePrefix); err != nil { |
| 66 | + return fmt.Errorf("rewrite proto file: %w", err) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // compile proto files |
| 71 | + if err := os.MkdirAll(dstDir, 0o755); err != nil { |
| 72 | + return fmt.Errorf("create destination directory: %w", err) |
| 73 | + } |
| 74 | + if err := compileProtoFiles(ctx, c.TmpDir, srcDir, namespace, dstDir, protoFiles); err != nil { |
| 75 | + return fmt.Errorf("compile proto files: %w", err) |
| 76 | + } |
| 77 | + |
| 78 | + return nil |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +func findProtoFiles(ctx context.Context, protoRootDir string) ([]string, error) { |
| 83 | + if _, err := os.Stat(protoRootDir); err != nil { |
| 84 | + if errors.Is(err, os.ErrNotExist) { |
| 85 | + return nil, nil |
| 86 | + } |
| 87 | + return nil, fmt.Errorf("find proto files: %w", err) |
| 88 | + } |
| 89 | + |
| 90 | + var protoFiles []string |
| 91 | + walkErr := filepath.WalkDir(protoRootDir, func(path string, d fs.DirEntry, err error) error { |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + select { |
| 97 | + case <-ctx.Done(): |
| 98 | + return ctx.Err() |
| 99 | + default: |
| 100 | + } |
| 101 | + |
| 102 | + if d.IsDir() { |
| 103 | + return nil |
| 104 | + } |
| 105 | + if filepath.Ext(d.Name()) == ".proto" { |
| 106 | + protoFiles = append(protoFiles, path) |
| 107 | + } |
| 108 | + return nil |
| 109 | + }) |
| 110 | + if walkErr != nil { |
| 111 | + return nil, fmt.Errorf("find proto files: %w", walkErr) |
| 112 | + } |
| 113 | + |
| 114 | + sort.Strings(protoFiles) |
| 115 | + return protoFiles, nil |
| 116 | + |
| 117 | +} |
| 118 | + |
| 119 | +func rewriteProtoFile(protoFile, namespace, pkgPrefix string) error { |
| 120 | + content, err := os.ReadFile(protoFile) |
| 121 | + if err != nil { |
| 122 | + return fmt.Errorf("read proto file: %w", err) |
| 123 | + } |
| 124 | + content = rewriteProtoFileData(content, namespace, pkgPrefix) |
| 125 | + if err := os.WriteFile(protoFile, content, 0o644); err != nil { |
| 126 | + return fmt.Errorf("write proto file: %w", err) |
| 127 | + } |
| 128 | + return nil |
| 129 | +} |
| 130 | + |
| 131 | +var packageRe = regexp.MustCompile(`(?m)^package\s+(opentelemetry\.proto\.)`) |
| 132 | +var importRe = regexp.MustCompile(`(?m)^import\s+"(opentelemetry/proto)`) |
| 133 | +var goPackageRe = regexp.MustCompile(`(?m)^option go_package = "go\.opentelemetry\.io/proto/otlp`) |
| 134 | + |
| 135 | +func rewriteProtoFileData(data []byte, namespace, pkgPrefix string) []byte { |
| 136 | + pkgNamespace := namespaceHash(namespace) |
| 137 | + data = packageRe.ReplaceAll(data, []byte(`package `+pkgNamespace+`.$1`)) |
| 138 | + data = importRe.ReplaceAll(data, []byte(`import "`+namespace+`/$1`)) |
| 139 | + data = goPackageRe.ReplaceAll(data, []byte(`option go_package = "`+pkgPrefix+`/`+namespace+"/opentelemetry/proto")) |
| 140 | + return data |
| 141 | +} |
| 142 | + |
| 143 | +func namespaceHash(namespace string) string { |
| 144 | + hash := sha1.Sum([]byte(namespace)) |
| 145 | + encoded := make([]byte, len(hash)*2) |
| 146 | + for i, b := range hash { |
| 147 | + encoded[i*2] = 'a' + (b >> 4) |
| 148 | + encoded[i*2+1] = 'a' + (b & 0x0f) |
| 149 | + } |
| 150 | + return string(encoded) |
| 151 | +} |
| 152 | + |
| 153 | +func compileProtoFiles(ctx context.Context, tmpDir, protoDir, namespace, dstDir string, protoFiles []string) error { |
| 154 | + uid := os.Getuid() |
| 155 | + |
| 156 | + absTmpDir, err := filepath.Abs(tmpDir) |
| 157 | + if err != nil { |
| 158 | + return fmt.Errorf("get absolute temporary directory: %w", err) |
| 159 | + } |
| 160 | + |
| 161 | + tmpDstDir := filepath.Join(absTmpDir, "dst") |
| 162 | + if err := os.MkdirAll(tmpDstDir, 0o755); err != nil { |
| 163 | + return fmt.Errorf("create destination directory: %w", err) |
| 164 | + } |
| 165 | + |
| 166 | + cmdArgs := []string{ |
| 167 | + "docker", |
| 168 | + "run", |
| 169 | + "--rm", |
| 170 | + "-u", fmt.Sprintf("%d", uid), |
| 171 | + "-v", fmt.Sprintf("%s:%s", absTmpDir, absTmpDir), |
| 172 | + "-w", absTmpDir, |
| 173 | + "otel/build-protobuf:0.9.0", |
| 174 | + "--proto_path=" + protoDir, |
| 175 | + "--go_opt=paths=source_relative", |
| 176 | + "--go_out=" + tmpDstDir, |
| 177 | + } |
| 178 | + cmdArgs = append(cmdArgs, protoFiles...) |
| 179 | + |
| 180 | + var buf bytes.Buffer |
| 181 | + cmd := exec.CommandContext(ctx, cmdArgs[0], cmdArgs[1:]...) |
| 182 | + cmd.Dir = absTmpDir |
| 183 | + cmd.Stdout = &buf |
| 184 | + cmd.Stderr = &buf |
| 185 | + if err := cmd.Run(); err != nil { |
| 186 | + return fmt.Errorf("%s: %s: %s", strings.Join(cmdArgs, " "), err, buf.String()) |
| 187 | + } |
| 188 | + |
| 189 | + // copy to dstDir |
| 190 | + if err := os.RemoveAll(dstDir); err != nil { |
| 191 | + return fmt.Errorf("remove destination directory: %w", err) |
| 192 | + } |
| 193 | + if err := os.CopyFS(dstDir, os.DirFS(filepath.Join(tmpDstDir, namespace))); err != nil { |
| 194 | + return fmt.Errorf("copy tmp dst to final dst directory: %w", err) |
| 195 | + } |
| 196 | + |
| 197 | + return nil |
| 198 | +} |
0 commit comments