| 1 |
# OpenTelemetry Collector Builder (ocb) |
| 2 |
|
| 3 |
This program generates a custom OpenTelemetry Collector binary based on a given configuration. |
| 4 |
|
| 5 |
## TL;DR |
| 6 |
|
| 7 |
```console |
| 8 |
$ go install go.opentelemetry.io/collector/cmd/[email protected] |
| 9 |
$ cat > otelcol-builder.yaml <<EOF |
| 10 |
dist: |
| 11 |
name: otelcol-custom |
| 12 |
description: Local OpenTelemetry Collector binary |
| 13 |
output_path: /tmp/dist |
| 14 |
exporters: |
| 15 |
- gomod: github.com/open-telemetry/opentelemetry-collector-contrib/exporter/alibabacloudlogserviceexporter v0.129.0 |
| 16 |
- gomod: go.opentelemetry.io/collector/exporter/debugexporter v0.129.0 |
| 17 |
|
| 18 |
receivers: |
| 19 |
- gomod: go.opentelemetry.io/collector/receiver/otlpreceiver v0.129.0 |
| 20 |
|
| 21 |
processors: |
| 22 |
- gomod: go.opentelemetry.io/collector/processor/batchprocessor v0.129.0 |
| 23 |
|
| 24 |
providers: |
| 25 |
- gomod: go.opentelemetry.io/collector/confmap/provider/envprovider v1.35.0 |
| 26 |
- gomod: go.opentelemetry.io/collector/confmap/provider/fileprovider v1.35.0 |
| 27 |
- gomod: go.opentelemetry.io/collector/confmap/provider/httpprovider v1.35.0 |
| 28 |
- gomod: go.opentelemetry.io/collector/confmap/provider/httpsprovider v1.35.0 |
| 29 |
- gomod: go.opentelemetry.io/collector/confmap/provider/yamlprovider v1.35.0 |
| 30 |
EOF |
| 31 |
$ builder --config=otelcol-builder.yaml |
| 32 |
$ cat > /tmp/otelcol.yaml <<EOF |
| 33 |
receivers: |
| 34 |
otlp: |
| 35 |
protocols: |
| 36 |
grpc: |
| 37 |
endpoint: localhost:4317 |
| 38 |
|
| 39 |
exporters: |
| 40 |
debug: |
| 41 |
|
| 42 |
service: |
| 43 |
pipelines: |
| 44 |
traces: |
| 45 |
receivers: |
| 46 |
- otlp |
| 47 |
exporters: |
| 48 |
- debug |
| 49 |
EOF |
| 50 |
$ /tmp/dist/otelcol-custom --config=/tmp/otelcol.yaml |
| 51 |
``` |
| 52 |
|
| 53 |
## Installation |
| 54 |
|
| 55 |
There are three supported ways to install the builder: |
| 56 |
1. Via official release Docker images (recommended) |
| 57 |
2. Via official release binaries (recommended) |
| 58 |
3. Through `go install` (not recommended) |
| 59 |
|
| 60 |
### Official release Docker image |
| 61 |
|
| 62 |
You will find the official docker images at [DockerHub](https://hub.docker.com/r/otel/opentelemetry-collector-builder). |
| 63 |
|
| 64 |
Pull the image via tagged version number (e.g. `0.129.0`) or 'latest'. You may also specify platform, although Docker will handle this automatically as it is a multi-platform build. |
| 65 |
|
| 66 |
```console |
| 67 |
docker pull otel/opentelemetry-collector-builder:latest |
| 68 |
``` |
| 69 |
|
| 70 |
The included builder configuration file/manifest should be replaced by mounting a file from your local filesystem to the docker container; the default location is `/build/builder-config.yaml`. If you mount a file at a different location inside the container, your `builder.config.yaml` must be specified as a command line argument to ocb. Additionally, the output folder must also be mounted from your local system to the docker container. This output directory must be specified in your `builder-config.yaml` file as it cannot be set via the command-line arguments. |
| 71 |
|
| 72 |
Assuming you are running this image in your working directory, have a `builder-config.yaml` file located in this folder, the `dist.output_path` item inside your `builder-config.yaml` is set to `./otelcol-dev`, and you wish to output the binary/go module files to a folder named `output`, the command would look as follows: |
| 73 |
|
| 74 |
```console |
| 75 |
docker run -v "$(pwd)/builder-config.yaml:/build/builder-config.yaml" -v "$(pwd)/output:/build/otelcol-dev" otel/opentelemetry-collector-builder:latest --config=/build/builder-config.yaml |
| 76 |
``` |
| 77 |
|
| 78 |
Please note that a `--config` flag must be passed to specify your custom manifest.yaml/builder-config.yaml file regardless of where you mount it inside the container, otherwise a default config is used that cannot be changed. |
| 79 |
|
| 80 |
Additional arguments may be passed to ocb on the command line as specified below, but if you wish to do this, you must make sure to pass the `--config` argument, as this is specified as an additional `CMD`, not an entrypoint. |
| 81 |
|
| 82 |
### Official release binaries |
| 83 |
|
| 84 |
This is the recommended installation method for the binary. Download the binary for your respective platform from the ["Releases"](https://github.com/open-telemetry/opentelemetry-collector-releases/releases?q=cmd/builder) page. |
| 85 |
|
| 86 |
### `go install` |
| 87 |
|
| 88 |
You need to have a `go` compiler in your PATH. Run the following command to install the latest version: |
| 89 |
|
| 90 |
```console |
| 91 |
go install go.opentelemetry.io/collector/cmd/builder@latest |
| 92 |
``` |
| 93 |
|
| 94 |
If installing through this method the binary will be called `builder`. |
| 95 |
|
| 96 |
In order to successfully generate and build a collector using ocb, you must use [compatible Go version](../../README.md#compatibility). |
| 97 |
|
| 98 |
## Running |
| 99 |
|
| 100 |
A build configuration file must be provided with the `--config` flag. |
| 101 |
You will need to specify at least one module (extension, exporter, receiver, processor) to add to your distribution. |
| 102 |
To build a default collector configuration, you can use [this](../otelcorecol/builder-config.yaml) build configuration. |
| 103 |
|
| 104 |
```console |
| 105 |
ocb --config=builder-config.yaml |
| 106 |
``` |
| 107 |
|
| 108 |
Use `ocb --help` to learn about which flags are available. |
| 109 |
|
| 110 |
## Debug |
| 111 |
|
| 112 |
### Debug symbols |
| 113 |
|
| 114 |
By default, the LDflags are set to `-s -w`, which strips debugging symbols to produce a smaller OpenTelemetry Collector binary. To retain debugging symbols and DWARF debugging data in the binary, override the LDflags as shown: |
| 115 |
|
| 116 |
```console |
| 117 |
ocb --ldflags="" --config=builder-config.yaml. |
| 118 |
``` |
| 119 |
|
| 120 |
### Debugging with Delve |
| 121 |
|
| 122 |
To ensure the code being executed matches the written code exactly, debugging symbols must be preserved, and compiler inlining and optimizations disabled. You can achieve this in two ways: |
| 123 |
|
| 124 |
1. Set the configuration property `debug_compilation` to true. |
| 125 |
2. Manually override the ldflags and gcflags `ocb --ldflags="" --gcflags="all=-N -l" --config=builder-config.yaml.` |
| 126 |
|
| 127 |
Then install `go-delve` and run OpenTelemetry Collector with `dlv` command as the following example: |
| 128 |
|
| 129 |
```bash |
| 130 |
# go install github.com/go-delve/delve/cmd/dlv@latest |
| 131 |
# ~/go/bin/dlv --listen=:2345 --headless=true --api-version=2 --accept-multiclient --log exec .otel-collector-binary -- --config otel-collector-config.yaml |
| 132 |
``` |
| 133 |
|
| 134 |
Finally, load the OpenTelemetry Collector as a project in the IDE, configure debug for Go |
| 135 |
|
| 136 |
## Configuration |
| 137 |
|
| 138 |
The configuration file is composed of two main parts: `dist` and module types. All `dist` options can be specified via command line flags: |
| 139 |
|
| 140 |
```console |
| 141 |
ocb --config=config.yaml |
| 142 |
``` |
| 143 |
|
| 144 |
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`. |
| 145 |
|
| 146 |
The `import` might specify a more specific path than what is specified in the `gomod`. For instance, your Go module might be `gitlab.com/myorg/myrepo` and the `import` might be `gitlab.com/myorg/myrepo/myexporter`. |
| 147 |
|
| 148 |
The `name` will typically be omitted, except when multiple components have the same name. In such case, set a unique name for each module. |
| 149 |
|
| 150 |
Optionally, a list of `go mod` replace entries can be provided, in case custom overrides are needed. This is typically necessary when a processor or some of its transitive dependencies have dependency problems. |
| 151 |
|
| 152 |
```yaml |
| 153 |
dist: |
| 154 |
module: github.com/open-telemetry/opentelemetry-collector # the module name for the new distribution, following Go mod conventions. Optional, but recommended. |
| 155 |
name: otelcol-custom # the binary name. Optional. |
| 156 |
description: "Custom OpenTelemetry Collector distribution" # a long name for the application. Optional. |
| 157 |
output_path: /tmp/otelcol-distributionNNN # the path to write the output (sources and binary). Optional. |
| 158 |
version: "1.0.0" # the version for your custom OpenTelemetry Collector. Optional. |
| 159 |
go: "/usr/bin/go" # which Go binary to use to compile the generated sources. Optional. |
| 160 |
debug_compilation: false # enabling this causes the builder to keep the debug symbols in the resulting binary. Optional. |
| 161 |
exporters: |
| 162 |
- gomod: "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/alibabacloudlogserviceexporter v0.129.0" # the Go module for the component. Required. |
| 163 |
import: "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/alibabacloudlogserviceexporter" # the import path for the component. Optional. |
| 164 |
name: "alibabacloudlogserviceexporter" # package name to use in the generated sources. Optional. |
| 165 |
path: "./alibabacloudlogserviceexporter" # in case a local version should be used for the module, the path relative to the current dir, or a full path can be specified. Optional. |
| 166 |
replaces: |
| 167 |
# a list of "replaces" directives that will be part of the resulting go.mod |
| 168 |
- github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.128.0 |
| 169 |
``` |
| 170 |
|
| 171 |
The builder also allows setting the scheme to use as the default URI scheme via `conf_resolver.default_uri_scheme`: |
| 172 |
|
| 173 |
```yaml |
| 174 |
conf_resolver: |
| 175 |
default_uri_scheme: "env" |
| 176 |
``` |
| 177 |
|
| 178 |
This tells the builder to produce a Collector that uses the `env` scheme when expanding configuration that does not |
| 179 |
provide a scheme, such as `${HOST}` (instead of doing `${env:HOST}`). |
| 180 |
|
| 181 |
## Steps |
| 182 |
|
| 183 |
The builder has 3 steps: |
| 184 |
|
| 185 |
* Generate: generates the golang source code |
| 186 |
* Get modules: generates the go.mod file based on the imported modules in the generated golang source code |
| 187 |
* Compilation: builds the OpenTelemetry Collector executable |
| 188 |
|
| 189 |
Each step can be skipped independently: `--skip-generate`, `--skip-get-modules` and `--skip-compilation`. |
| 190 |
|
| 191 |
For instance, a code generation step could execute |
| 192 |
|
| 193 |
```console |
| 194 |
ocb --skip-compilation --config=config.yaml |
| 195 |
``` |
| 196 |
|
| 197 |
then commit the code in a git repo. A CI can sync the code and execute |
| 198 |
|
| 199 |
```console |
| 200 |
ocb --skip-generate --skip-get-modules --config=config.yaml |
| 201 |
``` |
| 202 |
|
| 203 |
to only execute the compilation step. |
| 204 |
|
| 205 |
### Strict versioning checks |
| 206 |
|
| 207 |
The builder checks the relevant `go.mod` |
| 208 |
file for the following things after `go get`ing all components and calling |
| 209 |
`go mod tidy`: |
| 210 |
|
| 211 |
1. The `dist::otelcol_version` field in the build configuration must have |
| 212 |
matching major and minor versions as the core library version calculated by |
| 213 |
the Go toolchain, considering all components. A mismatch could happen, for |
| 214 |
example, when the builder or one of the components depends on a newer release |
| 215 |
of the core collector library. |
| 216 |
2. For each component in the build configuration, the major and minor versions |
| 217 |
included in the `gomod` module specifier must match the one calculated by |
| 218 |
the Go toolchain, considering all components. A mismatch could |
| 219 |
happen, for example, when the enclosing Go module uses a newer |
| 220 |
release of the core collector library. |
| 221 |
|
| 222 |
The `--skip-strict-versioning` flag disables these versioning checks. |
| 223 |
This flag is available temporarily and |
| 224 |
**will be removed in a future minor version**. |
| 225 |
|
| 226 |
### Cgo disabled by default |
| 227 |
|
| 228 |
By default, the OpenTelemetry Collector binary is built with `CGO_ENABLED=0` in accordance with |
| 229 |
how the official OpenTelemetry Collector releases are built. This can be overridden by adding |
| 230 |
the following configuration option to the `dist` section of the builder configuration file: |
| 231 |
|
| 232 |
```yaml |
| 233 |
dist: |
| 234 |
cgo_enabled: true |
| 235 |
``` |