Spring Boot OTEL Starter无法向Otel Collector发送数据求助
问题:Spring Boot 3.4.0集成OpenTelemetry发送数据到Otel Collector报错Broken pipe
基于Spring Boot 3.4.0搭建基础应用,集成opentelemetry-spring-boot-starter与Otel Collector后,向本地Otel Collector发送指标和日志时出现如下错误:
2024-12-02T13:45:37.578+01:00 ERROR 41207 --- [alhost:4318/...] i.o.exporter.internal.http.HttpExporter : Failed to export logs. The request could not be executed. Full error message: Broken pipe java.net.SocketException: Broken pipe at java.base/sun.nio.ch.SocketDispatcher.write0(Native Method) ~[na:na] at java.base/sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:62) ~[na:na] at java.base/sun.nio.ch.NioSocketImpl.tryWrite(NioSocketImpl.java:394) ~[na:na] (...) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) ~[okhttp-4.12.0.jar:na] at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201) ~[okhttp-4.12.0.jar:na] at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517) ~[okhttp-4.12.0.jar:na] at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144) ~[na:na] at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642) ~[na:na] at java.base/java.lang.Thread.run(Thread.java:1583) ~[na:na] Suppressed: java.io.IOException: unexpected end of stream on http://localhost:4318/... at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.kt:210) ~[okhttp-4.12.0.jar:na] at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.kt:110) ~[okhttp-4.12.0.jar:na] at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:93) ~[okhttp-4.12.0.jar:na] ... 14 common frames omitted Caused by: java.io.EOFException: \n not found: limit=0 content=… at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:335) at okhttp3.internal.http1.HeadersReader.readLine(HeadersReader.kt:29) at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.kt:180) ... 16 common frames omitted 2024-12-02T13:45:37.582+01:00 DEBUG 41207 --- [_WorkerThread-1] i.o.s.l.e.BatchLogRecordProcessor$Worker : Exporter failed
相关配置信息
Gradle依赖
implementation("org.springframework.boot:spring-boot-starter-actuator") implementation("io.opentelemetry.instrumentation:opentelemetry-spring-boot-starter")
application.yml配置
management: endpoints.web.exposure.include: - mappings - health - info - metrics endpoint: mappings.enabled: true info.enabled: true health.enabled: true metrics.enabled: true tracing: sampling: probability: 1.0 otel: resource.attributes: deployment.environment: dev service: name: my-app namespace: my-namespace
Otel Collector配置
receivers: otlp: protocols: http: exporters: debug: verbosity: detailed service: pipelines: traces: receivers: [otlp] exporters: [debug] metrics: receivers: [otlp] exporters: [debug] logs: receivers: [otlp] exporters: [debug]
Otel Docker日志
2024-12-02T12:43:45.877Z info service@v0.114.0/service.go:166 Setting up own telemetry... 2024-12-02T12:43:45.877Z info telemetry/metrics.go:70 Serving metrics {"address": "localhost:8888", "metrics level": "Normal"} 2024-12-02T12:43:45.877Z info builders/builders.go:26 Development component. May change in the future. {"kind": "exporter", "data_type": "traces", "name": "debug"} 2024-12-02T12:43:45.877Z info builders/builders.go:26 Development component. May change in the future. {"kind": "exporter", "data_type": "logs", "name": "debug"} 2024-12-02T12:43:45.877Z info builders/builders.go:26 Development component. May change in the future. {"kind": "exporter", "data_type": "metrics", "name": "debug"} 2024-12-02T12:43:45.878Z info service@v0.114.0/service.go:238 Starting otelcol-contrib... {"Version": "0.114.0", "NumCPU": 6} 2024-12-02T12:43:45.878Z info extensions/extensions.go:39 Starting extensions... 2024-12-02T12:43:45.878Z info otlpreceiver@v0.114.0/otlp.go:169 Starting HTTP server {"kind": "receiver", "name": "otlp", "data_type": "metrics", "endpoint": "localhost:4318"} 2024-12-02T12:43:45.878Z info service@v0.114.0/service.go:261 Everything is ready. Begin running and processing data.
解决方案
修正Otel Collector监听地址
从Docker日志可见,Collector的OTLP HTTP接收器绑定的是localhost:4318,仅允许容器内部访问。若Spring Boot应用在宿主机运行,需修改Collector配置让其监听所有网卡:receivers: otlp: protocols: http: endpoint: 0.0.0.0:4318同时启动Docker容器时必须映射端口:
docker run -p 4318:4318 otel/opentelemetry-collector-contrib:0.114.0添加Spring Boot OTLP导出配置
当前application.yml未指定OTLP导出地址,默认配置可能与Collector不兼容。需补充导出配置:otel: exporter: otlp: endpoint: http://localhost:4318/v1/logs protocol: http/protobuf resource.attributes: deployment.environment: dev service.name: my-app service.namespace: my-namespace若Spring Boot应用也在Docker容器内,需将
localhost替换为Docker网关地址(通常是172.17.0.1)或Collector容器名称。修正资源属性格式
OpenTelemetry规范要求资源属性为扁平结构,原配置中嵌套的service属性无法被正确识别,需改为扁平写法(如上段配置所示)。
内容的提问来源于stack exchange,提问作者Maciej Bodus
相关产品推荐
相关产品推荐

