#6484·k6

OpenTelemetry output: shutdown cancels an in-flight periodic export and logs a spurious "failed to upload metrics: context canceled"

Author: jmsundarCreated Sep 22, 2026Updated Sep 25, 2026
Labelstriage

Brief summary

With the OpenTelemetry output, a run that ends while a periodic metrics export is in flight logs a spurious export failure:

level=info msg="2026/09/22 15:55:51 failed to upload metrics: Post \"https://<otlp-endpoint>/v1/metrics\": context canceled"

No metrics are lost. The line reads like a real export failure, though, and tooling that watches k6's output for export failures can't tell it apart from one.

Cause. Output.StopWithTestError (internal/output/opentelemetry/output.go) calls meterProvider.Shutdown(context.Background()) directly. In go.opentelemetry.io/otel/sdk/metric v1.44.0, PeriodicReader.Shutdown first calls r.cancel() to stop the run loop. That cancels the context of any collectAndExport the ticker started, and the run loop passes the resulting error to otel.Handle. The default handler prints it through Go's log package, which k6 surfaces at info. Shutdown then collects and exports once more under its own context. The exporter's default temporality is cumulative, so that final export carries everything the cancelled one would have.

Suggested fix. Call meterProvider.ForceFlush before Shutdown. PeriodicReader.ForceFlush goes through the run loop's flushCh, so it waits for an in-flight ticker export to finish instead of cancelling it, and then resets the ticker. When Shutdown runs, nothing is in flight to cancel. The cost is one extra export at the end of the run.

go
o.periodicFlusher.Stop()

ctx := context.Background()
if err := o.meterProvider.ForceFlush(ctx); err != nil {
	o.logger.WithError(err).Error("can't flush OpenTelemetry metric provider")
}
if err := o.meterProvider.Shutdown(ctx); err != nil {
	o.logger.WithError(err).Error("can't shutdown OpenTelemetry metric provider")
}

k6 version

v2.2.0

OS

macOS 26 (darwin/arm64)

Docker version and image (if applicable)

No response

Steps to reproduce the problem

  1. Point the OpenTelemetry output at a remote OTLP/HTTP endpoint (network latency widens the window):
    bash
    K6_OUT=opentelemetry \
    K6_OTEL_EXPORTER_PROTOCOL=http/protobuf \
    K6_OTEL_HTTP_EXPORTER_ENDPOINT=<otlp-host>:443 \
    k6 run script.js
  2. Use any short script that ends on its own. The line appears only on runs that end while a 10-second periodic export is still in flight, so repeat a few times, or shorten K6_OTEL_EXPORT_INTERVAL to hit it more often.

Expected behaviour

A clean shutdown logs no export failure. An error is logged only when metrics were actually not delivered, which the final export's own can't shutdown OpenTelemetry metric provider error already covers.

Actual behaviour

An intermittent failed to upload metrics: ... context canceled line at the end of the run, followed by no shutdown error. The backend received every sample: the counters at the end of the run match k6's end-of-test summary exactly.