Skip to content
Latchkey

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.

text
[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:4317

Common 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

  1. Set OTEL_TRACES_EXPORTER=none (and metrics/logs) so no OTLP connection is attempted.
  2. Or set OTEL_SDK_DISABLED=true to turn the SDK off.
  3. Detach the javaagent from the test JVM if it is not needed.
.github/workflows/ci.yml
env:
  OTEL_TRACES_EXPORTER: none
  OTEL_METRICS_EXPORTER: none
  OTEL_LOGS_EXPORTER: none

Point 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.

.github/workflows/ci.yml
env:
  OTEL_EXPORTER_OTLP_ENDPOINT: http://otelcol:4318

How 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.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →