Skip to content

Commit

Permalink
merged in upstream/main
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerHelmuth committed Jul 25, 2022
2 parents 3c4b398 + 9930104 commit 655af68
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 136 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### 💡 Enhancements 💡

- `ocb` now exits with an error if it fails to load the build configuration. (#5731)

### 🧰 Bug fixes 🧰

## v0.56.0 Beta
Expand Down
14 changes: 9 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ ALL_MODULES := $(shell find . -type f -name "go.mod" -exec dirname {} \; | sort
CMD?=
TOOLS_MOD_DIR := ./internal/tools

GOOS=$(shell $(GOCMD) env GOOS)
GOARCH=$(shell $(GOCMD) env GOARCH)
GH := $(shell which gh)

# TODO: Find a way to configure this in the generated code, currently no effect.
BUILD_INFO_IMPORT_PATH=go.opentelemetry.io/collector/internal/version
VERSION=$(shell git describe --always --match "v[0-9]*" HEAD)
Expand Down Expand Up @@ -207,6 +203,10 @@ otelcorecol:
genotelcorecol:
pushd cmd/builder/ && $(GOCMD) run ./ --skip-compilation --config ../otelcorecol/builder-config.yaml --output-path ../otelcorecol && popd

.PHONY: ocb
ocb:
$(MAKE) -C cmd/builder ocb

DEPENDABOT_PATH=".github/dependabot.yml"
.PHONY: internal-gendependabot
internal-gendependabot:
Expand Down Expand Up @@ -411,7 +411,11 @@ endif
echo "'gh' command not found, can't submit the PR on your behalf."; \
exit 1; \
fi
gh pr create --title "[chore] prepare release $(RELEASE_CANDIDATE)"
$(GH) pr create --title "[chore] prepare release $(RELEASE_CANDIDATE)"

.PHONY: clean
clean:
test -d bin && $(RM) bin/*

.PHONY: checklinks
checklinks:
Expand Down
4 changes: 4 additions & 0 deletions Makefile.Common
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ GO_ACC=go-acc
LINT=golangci-lint
IMPI=impi

GOOS := $(shell $(GOCMD) env GOOS)
GOARCH := $(shell $(GOCMD) env GOARCH)
GH := $(shell which gh)

.PHONY: test
test:
$(GOTEST) $(GOTEST_OPT) ./...
Expand Down
4 changes: 4 additions & 0 deletions cmd/builder/Makefile
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
include ../../Makefile.Common

.PHONY: ocb
ocb:
GO111MODULE=on CGO_ENABLED=0 $(GOCMD) build -trimpath -o ../../bin/ocb_$(GOOS)_$(GOARCH) .
17 changes: 10 additions & 7 deletions cmd/builder/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# OpenTelemetry Collector builder
# OpenTelemetry Collector Builder (ocb)
[![CI](https://github.com/open-telemetry/opentelemetry-collector-builder/actions/workflows/go.yaml/badge.svg)](https://github.com/open-telemetry/opentelemetry-collector-builder/actions/workflows/go.yaml?query=branch%3Amain)

This program generates a custom OpenTelemetry Collector binary based on a given configuration.
Expand All @@ -7,7 +7,7 @@ This program generates a custom OpenTelemetry Collector binary based on a given

```console
$ GO111MODULE=on go install go.opentelemetry.io/collector/cmd/builder@latest
$ cat > ~/.otelcol-builder.yaml <<EOF
$ cat > otelcol-builder.yaml <<EOF
exporters:
- gomod: "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/alibabacloudlogserviceexporter v0.40.0"
- import: go.opentelemetry.io/collector/exporter/loggingexporter
Expand All @@ -21,7 +21,7 @@ processors:
- import: go.opentelemetry.io/collector/processor/batchprocessor
gomod: go.opentelemetry.io/collector v0.40.0
EOF
$ builder --output-path=/tmp/dist
$ builder --config=otelcol-builder.yaml --output-path=/tmp/dist
$ cat > /tmp/otelcol.yaml <<EOF
receivers:
otlp:
Expand Down Expand Up @@ -51,23 +51,26 @@ $ /tmp/dist/otelcol-custom --config=/tmp/otelcol.yaml
## Installation

Download the binary for your respective platform under the ["Releases"](https://github.com/open-telemetry/opentelemetry-collector/releases/latest) page.
If install an official release build, the binary is named `ocb`, but if you installed by using `go install`, it will be called `builder`.

## Running

A configuration file isn't strictly required, but the final artifact won't be different than a regular OpenTelemetry Collector. You probably want to specify at least one module (extension, exporter, receiver, processor) to add to your distribution. You can specify them via a configuration file. When no `--config` flag is provided with the location for the configuration file, `${HOME}/.otelcol-builder.yaml` will be used, if available.
A build configuration file must be provided with the `--config` flag.
You will need to specify at least one module (extension, exporter, receiver, processor) to add to your distribution.
To build a default collector configuration, you can use [this](../otelcorecol/builder-config.yaml) build configuration.

```console
$ builder --config config.yaml
$ ocb --config=builder-config.yaml
```

Use `builder --help` to learn about which flags are available.
Use `ocb --help` to learn about which flags are available.

## Configuration

The configuration file is composed of two main parts: `dist` and module types. All `dist` options can be specified via command line flags:

```console
$ builder --name="my-otelcol"
$ ocb --config=config.yaml --name="my-otelcol"
```

The module types are specified at the top-level, and might be: `extensions`, `exporters`, `receivers` and `processors`. They all accept a list of components, and each component is required to have at least the `gomod` entry. When not specified, the `import` value is inferred from the `gomod`. When not specified, the `name` is inferred from the `import`.
Expand Down
43 changes: 25 additions & 18 deletions cmd/builder/internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,39 @@ var (
// Command is the main entrypoint for this application
func Command() (*cobra.Command, error) {
cmd := &cobra.Command{
Use: "builder",
Long: fmt.Sprintf("OpenTelemetry Collector distribution builder (%s)", version),
SilenceUsage: true, // Don't print usage on Run error.
SilenceErrors: true, // Don't print errors; main does it.
Use: "ocb",
Long: fmt.Sprintf("OpenTelemetry Collector Builder (%s)", version) + `
ocb generates a custom OpenTelemetry Collector binary using the
build configuration given by the "--config" argument.
`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if err := initConfig(); err != nil {
return err
}
if err := cfg.Validate(); err != nil {
cfg.Logger.Error("invalid configuration", zap.Error(err))
return err
return fmt.Errorf("invalid configuration: %w", err)
}

if err := cfg.ParseModules(); err != nil {
cfg.Logger.Error("invalid module configuration", zap.Error(err))
return err
return fmt.Errorf("invalid module configuration: %w", err)
}

return builder.GenerateAndCompile(cfg)
},
}

// the external config file
cmd.Flags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.otelcol-builder.yaml)")
cmd.Flags().StringVar(&cfgFile, "config", "", "build configuration file")

// A build configuration file is always required, and there's no
// default. We can relax this in future by embedding the default
// config that is used to build otelcorecol.
if err := cmd.MarkFlagRequired("config"); err != nil {
panic(err) // Only fails if the usage message is empty, which is a programmer error.
}

// the distribution parameters, which we accept as CLI flags as well
cmd.Flags().BoolVar(&cfg.SkipCompilation, "skip-compilation", false, "Whether builder should only generate go code with no compile of the collector (default false)")
Expand All @@ -75,34 +86,30 @@ func Command() (*cobra.Command, error) {
cmd.AddCommand(versionCommand())

if err := k.Load(posflag.Provider(cmd.Flags(), ".", k), nil); err != nil {
cfg.Logger.Error("failed to load command line arguments", zap.Error(err))
return nil, fmt.Errorf("failed to load command line arguments: %w", err)
}

return cmd, nil
}

func initConfig() error {
cfg.Logger.Info("OpenTelemetry Collector distribution builder", zap.String("version", version), zap.String("date", date))
// use the default path if there is no config file being specified
if cfgFile == "" {
cfgFile = "$HOME/.otelcol-builder.yaml"
}
cfg.Logger.Info("OpenTelemetry Collector Builder",
zap.String("version", version), zap.String("date", date))

// load the config file
if err := k.Load(file.Provider(cfgFile), yaml.Parser()); err != nil {
cfg.Logger.Error("failed to load config file", zap.String("config-file", cfgFile), zap.Error(err))
return fmt.Errorf("failed to load configuration file: %w", err)
}

// handle env variables
if err := k.Load(env.Provider("", ".", func(s string) string {
return strings.ReplaceAll(s, ".", "_")
}), nil); err != nil {
cfg.Logger.Error("failed to load env var", zap.Error(err))
return fmt.Errorf("failed to load environment variables: %w", err)
}

if err := k.UnmarshalWithConf("", &cfg, koanf.UnmarshalConf{Tag: "mapstructure"}); err != nil {
cfg.Logger.Error("failed to unmarshal config", zap.Error(err))
return err
return fmt.Errorf("failed to unmarshal configuration: %w", err)
}

cfg.Logger.Info("Using config file", zap.String("path", cfgFile))
Expand Down
4 changes: 2 additions & 2 deletions cmd/builder/internal/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ var (
func versionCommand() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Version of opentelemetry-collector-builder",
Long: "Prints the version of opentelemetry-collector-builder binary",
Short: "Version of ocb",
Long: "Prints the version of the ocb binary",
Run: func(cmd *cobra.Command, args []string) {
cmd.Println(fmt.Sprintf("%s version %s", cmd.Parent().Name(), version))
},
Expand Down
Loading

0 comments on commit 655af68

Please sign in to comment.