- Core 发行版是 Collector 的基础发行版,供 OTel 开发人员进行开发和测试。它包含一组基本的扩展、连接器、接收器、处理器和导出器。
- Contrib 发行版供非 OTel 开发人员进行实验和学习。它还扩展了 Core 发行版,并包含由第三方(包括供应商和个人社区成员)创建的组件,这些组件对整个 OpenTelemetry 社区非常有用。
不管 Core 还是 Contrib 都不应该成为你生产工作负载的一部分。仅仅使用 Core 本身太过简单,无法满足组织的需求(尽管它提供的组件都是必须的);虽然 Contrib 中提供的组件足够全面,然而并不是说每个组件都是你所需要的,太多冗余的组件显得过于臃肿,还增大的攻击面。
那如何选择你所需的发行版呢?答案就是构建自己的发行版。可以使用官方提供的名为 OpenTelemetry Collector Builder (OCB) 的工具来自定义附件。
安装 OCB
ocb 是个简单的 CLI,支持 Window/Linux/macOS 以及 x86 和 arm64 平台。我们可以从 release 页面[1] 下载 ocb 的 binary。
curl -o ocb -sL https://github.com/open-telemetry/opentelemetry-collector/releases/download/cmd%2Fbuilder%2Fv0.95.0/ocb_0.95.0_linux_amd64
chmod +x ocb
可以通过 ./ocb help 查看使用方法。
还可以通过 go install 来安装。
go install go.opentelemetry.io/collector/cmd/builder@latest
要构建自己的发行版,需要使用下面的命令,通过 --config 指定配置文件,也就是下面要介绍的构建清单。
./ocb --config=builder-config.yaml
如果不提供,会使用 默认的清单文件[2]。既然我们是要构建自己的发行版,那接来看看如何配置构建清单。
构建清单
构建清单是对 OpenTelemetry Collector 的描述,通常包含如下几个部分:
- dist:发行版的基本信息
- receivers:集成的 receiver[3] 列表
- exporters:集成的 exporter[4] 列表
- extensions:集成的 extension[5] 列表
- processors:集成的 processor[6] 列表
- connectors:集成的 receiver[7] 列表
从 GitHub 仓库中可以找到 core 发行版的构建清单[8],我们可以基于该版本进行裁剪(去掉 replace 的部分)。
dist:
module: go.opentelemetry.io/collector/cmd/otelcorecol
name: otelcorecol
description: Local OpenTelemetry Collector binary, testing only.
version: 0.95.0-dev
otelcol_version: 0.95.0
output_path: /tmp/dist
exporters:
- gomod: go.opentelemetry.io/collector/exporter/debugexporter v0.95.0
- gomod: go.opentelemetry.io/collector/exporter/loggingexporter v0.95.0
- gomod: go.opentelemetry.io/collector/exporter/otlpexporter v0.95.0
- gomod: go.opentelemetry.io/collector/exporter/otlphttpexporter v0.95.0
extensions:
- gomod: go.opentelemetry.io/collector/extension/ballastextension v0.95.0
- gomod: go.opentelemetry.io/collector/extension/memorylimiterextension v0.95.0
- gomod: go.opentelemetry.io/collector/extension/zpagesextension v0.95.0
processors:
- gomod: go.opentelemetry.io/collector/processor/batchprocessor v0.95.0
- gomod: go.opentelemetry.io/collector/processor/memorylimiterprocessor v0.95.0
connectors:
- gomod: go.opentelemetry.io/collector/connector/forwardconnector v0.95.0
现在为 开头提到的文章[9] 中可以找到更多的组件。
dist:
module: go.opentelemetry.io/collector/cmd/otelcorecol
name: otelcorecol
description: Local OpenTelemetry Collector binary, testing only.
version: 0.95.0-dev
otelcol_version: 0.95.0
output_path: /tmp/dist
receivers:
- gomod: go.opentelemetry.io/collector/receiver/otlpreceiver v0.95.0
exporters:
- gomod: go.opentelemetry.io/collector/exporter/debugexporter v0.95.0
- gomod: go.opentelemetry.io/collector/exporter/loggingexporter v0.95.0
- gomod: go.opentelemetry.io/collector/exporter/otlpexporter v0.95.0
- gomod: go.opentelemetry.io/collector/exporter/otlphttpexporter v0.95.0
- gomod: github.com/open-telemetry/opentelemetry-collector-contrib/exporter/lokiexporter v0.95.0
extensions:
- gomod: go.opentelemetry.io/collector/extension/ballastextension v0.95.0
- gomod: go.opentelemetry.io/collector/extension/memorylimiterextension v0.95.0
- gomod: go.opentelemetry.io/collector/extension/zpagesextension v0.95.0
processors:
- gomod: go.opentelemetry.io/collector/processor/batchprocessor v0.95.0
- gomod: go.opentelemetry.io/collector/processor/memorylimiterprocessor v0.95.0
- gomod: github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourceprocessor v0.95.0
connectors:
- gomod: go.opentelemetry.io/collector/connector/forwardconnector v0.95.0
这样就准备好了构建清单,执行下面的命令,然后在 dist.output_path 指定的目录中可以到找构建好的二进制文件。
./ocb --config=builder-config.yaml
构建镜像
通过上面的方法来构建 collector 的二进制文件,进一步再构建容器镜像。
FROM golang:1.21 as build
ARG OTEL_VERSION=0.95.0
WORKDIR /app
COPY . .
RUN go install go.opentelemetry.io/collector/cmd/builder@v${OTEL_VERSION}
RUN CGO_ENABLED=0 builder --config=builder-config.yaml
FROM gcr.io/distroless/base-debian11
COPY --from=build /tmp/dist/otelcorecol /
# 4317 - default OTLP receiver
# 55678 - opencensus (tracing) receiver
# 55679 - zpages
EXPOSE 4317/tcp 55678/tcp 55679/tcp
CMD ["--config", "/etc/otelcol-contrib/config.yaml"]
ENTRYPOINT ["/otelcorecol"]
我已经构建好了镜像 addozhang/opentelemetry-collector:0.95.0 ,在创建 CR OpenTelemetryCollector 的时候,可以将镜像修改为我们定制的镜像。
参考资料
[1] release 页面: https://github.com/open-telemetry/opentelemetry-collector/releases?q=builder
[2] 默认的清单文件: https://github.com/open-telemetry/opentelemetry-collector/blob/main/cmd/builder/internal/config/default.yaml
[3] receiver: https://opentelemetry.io/docs/collector/configuration/#receivers
[4] exporter: https://opentelemetry.io/docs/collector/configuration/#exporters
[5] extension: https://opentelemetry.io/docs/collector/configuration/#extensions
[6] processor: https://opentelemetry.io/docs/collector/configuration/#processors
[7] receiver: https://opentelemetry.io/docs/collector/configuration/#connectors
[8] core 发行版的构建清单: https://raw.githubusercontent.com/open-telemetry/opentelemetry-collector/main/cmd/otelcorecol/builder-config.yaml
[9] 开头提到的文章: https://mp.weixin.qq.com/s/DUUDYggjaVtFRbDF3rslhw/