OpenTelemetry Java "Failed to export spans" to OTLP (port 4318) in CI
The OpenTelemetry Java SDK BatchSpanProcessor tried to send spans over OTLP to localhost:4318 (HTTP) or 4317 (gRPC) and got connection refused, because no collector runs in the CI job. The log fills with export failures.
What this error means
The build log repeats "Failed to export spans. The request could not be executed. Full error message: Connection refused" from io.opentelemetry.exporter.otlp, usually while a Java test suite runs with the agent attached.
[BatchSpanProcessor_WorkerThread-1] ERROR io.opentelemetry.exporter.internal.grpc
Failed to export spans. The request could not be executed. Full error message:
Connection refused: localhost/127.0.0.1:4317Common causes
No collector endpoint in CI
The default OTLP endpoint targets localhost:4317/4318. With no collector service in the job, the exporter connection is refused on every batch.
The Java agent is attached during tests
-javaagent:opentelemetry-javaagent.jar auto-configures an OTLP exporter, so tests export even when you did not intend them to.
How to fix it
Disable the exporter for test runs
- Set
OTEL_TRACES_EXPORTER=none(and metrics/logs) so no OTLP connection is attempted. - Or set
OTEL_SDK_DISABLED=trueto turn the SDK off. - Detach the javaagent from the test JVM if it is not needed.
env:
OTEL_TRACES_EXPORTER: none
OTEL_METRICS_EXPORTER: none
OTEL_LOGS_EXPORTER: nonePoint at a collector service when export is required
If a test asserts on exported data, run a collector service and set the endpoint to it.
env:
OTEL_EXPORTER_OTLP_ENDPOINT: http://otelcol:4318How to prevent it
- Set OTEL_TRACES_EXPORTER=none in unit test jobs.
- Only attach the OTel javaagent where a collector exists.
- Assert on spans with an in-memory exporter, not a live endpoint.