Go Agent 数据采集
前置条件
- 安装 light-agent(登录 Lighthouse 平台后点击「数据采集」菜单查看安装步骤,或点此查看)
快速开始
- 下载 otel-go-instrumentation。
- 运行目标应用程序。
- 以 root 权限运行 otel-go-instrumentation :
- 启动命令
sudo OTEL_GO_AUTO_TARGET_EXE=/home/bin/service_executable OTEL_SERVICE_NAME=my_service OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318 ./otel-go-instrumentation
tip
/home/bin/service_executable
目标应用程序my_service
替换为实际的服务名称http://localhost:4318
替换为实际的数据上报地址
使用 Docker Compose 部署 OTel 探针
要检测容器化应用程序,请按照以下步骤操作:
-
创建或编辑 docker-compose.yaml 文件。确保添加 Docker 网络、共享卷和应用程序的服务。
-
编辑 docker-compose 文件以添加检测的新服务:
go-auto:
image: otel/autoinstrumentation-go
privileged: true
pid: "host"
environment:
- OTEL_EXPORTER_OTLP_ENDPOINT=http://0.0.0.0:54318
- OTEL_GO_AUTO_TARGET_EXE=<location_of_target_application_binary>
- OTEL_SERVICE_NAME=<name_of_your_application>
- OTEL_PROPAGATORS=tracecontext,baggage
volumes:
- <shared_volume_of_application>
- /proc:/host/proc
- 运行
docker compose up
。
使用 Kubernetes Operator 部署 OTel 探针
前置条件
- 需要安装好 light-agent,具体安装步骤查看 Lighthouse 平台【数据采集】
- 区分 light-agent 是否安装在 Kubernetes 集群中
- 如果 light-agent 安装在 Kubernetes 集群中,在安装 light-agent 的时候,已经将镜像导入到 Kubernetes 可以访问的仓库
- 如果 light-agent 安装在 Kubernetes 集群外,需要将 kube-state-metrics 的镜像导入到 Kubernetes 可以访问的仓库。具体步骤查看 Lighthouse 平台【数据采集】-> 【Kubernetes】-> 推送镜像到 Kubernetes 的镜像仓库
- 需要安装好 k8s-operator ,具体安装步骤查看 Kubenetes 安装 k8s-operator
1. 启用 opentelemetry-operator 的 Go 的自动注入
kubectl patch deployment -n opentelemetry-operator-system opentelemetry-operator-controller-manager --patch-file operator-patch-go.yaml
operator-patch-go.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: opentelemetry-operator-controller-manager
namespace: opentelemetry-operator-system
spec:
template:
spec:
containers:
- name: manager
args:
- --enable-go-instrumentation=true
#确认opentelemetry-operator-controller-manager 的容器参数中包含 --enable-go-instrumentation=true
kubectl get pods -n opentelemetry-operator-system -o jsonpath='{range .items[?(@.metadata.name=="opentelemetry-operator-controller-manager-58c68ff5c6-plkzn")]}{.spec.containers[?(@.name=="manager")}{.args}{end}'
2. 添加 Go 的 Automatic Instrumentation
kubectl apply -f automatic-instrumentation-go.yaml
automatic-instrumentation-go.yaml
apiVersion: opentelemetry.io/v1alpha1
kind: Instrumentation
metadata:
name: auto-instr
namespace: default
spec:
exporter:
endpoint: http://light-agent.light-agent.svc.cluster.local:54318 #仅对.net 语言生效 根据实际情况修改
propagators:
- tracecontext
- baggage
- b3
sampler:
type: parentbased_traceidratio
argument: "1"
go:
image: ghcr.io/open-telemetry/opentelemetry-go-instrumentation/autoinstrumentation-go:v0.17.0-alpha
env:
- name: OTEL_INSTRUMENTATION_KAFKA_ENABLED
value: "false"
- name: OTEL_INSTRUMENTATION_REDISCALA_ENABLED
value: "false"
- name: OTEL_BSP_MAX_EXPORT_BATCH_SIZE
value: "512"
- name: OTEL_EXPORTER_OTLP_COMPRESSION
value: "gzip"
- name: OTEL_BSP_SCHEDULE_DELAY
value: "5000"
- name: OTEL_EXPORTER_OTLP_PROTOCOL
value: "http/protobuf"
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: "http://light-agent.light-agent.svc.cluster.local:54318" # 关键配置 数据上报地址 根据实际情况修改
3. 启动 Go Demo
kubectl apply -f go-demo.yaml
go-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: weather-service
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: weather-service
template:
metadata:
labels:
app: weather-service
annotations:
instrumentation.opentelemetry.io/inject-go: "true" # 关键配置 是否开启自动检测 根据实际情况修改
instrumentation.opentelemetry.io/otel-go-auto-target-exe: "/app/weather-service" # 关键配置 需要检测的二进制文件路径 根据实际情况修改
spec:
containers:
- name: weather-service
image: 192.168.2.99/obs_dev/weather-service:latest #根据实际情况修改
imagePullPolicy: IfNotPresent
env:
- name: OTEL_SERVICE_NAME
value: "weather-service" # 根据实际情况修改
ports:
- containerPort: 7090
securityContext:
capabilities:
add:
- SYS_PTRACE
privileged: true
runAsUser: 0
配置
有关更多环境变量,请参阅此处 。