Skip to content

feat(storage): add RetryBackoff spans for retry backoffs - #20715

Merged
sahusneha2004 merged 5 commits into
googleapis:mainfrom
sahusneha2004:feat/otel-retry-backoff-spans
Oct 23, 2026
Merged

sahusneha2004 merged 5 commits into
googleapis:mainfrom
sahusneha2004:feat/otel-retry-backoff-spans

Conversation

@sahusneha2004

@sahusneha2004 sahusneha2004 commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Adds OpenTelemetry tracing for the retry backoff sleeps in run() (HTTP and gRPC transports).

For each backoff the client records a RetryBackoff child span of the operation span, covering the sleep. The span is recorded once the sleep ends, with its start time set to when the sleep began (trace.WithTimestamp), so it is not visible while the backoff is still in progress — a deliberate trade-off that keeps the hot path free of span bookkeeping when the operation span is not recording.

A backoff cut short by context cancellation or deadline is still recorded, so the trace shows the time spent waiting before the operation gave up. The backoff span itself is left with an unset status: the cancellation/deadline is the operation's error and is recorded on the operation span by endSpan.

Nothing is recorded unless GO_STORAGE_DEV_OTEL_TRACING=true and the operation span is recording, so there is no change for users with tracing disabled. Once the dev flag is removed each retry will add one span to the trace; a TODO on recordRetryBackoff notes that opt-in/sampling should be decided at that point.

Tests: TestRecordRetryBackoffNoParentSpan, TestRunRetryBackoffTracing (table-driven, including a backoff interrupted by the context), TestRunRetryBackoffTracingDisabled.

@sahusneha2004
sahusneha2004 requested review from a team as code owners October 17, 2026 11:10
@product-auto-label product-auto-label Bot added the api: storage Issues related to the Cloud Storage API. label Oct 17, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds OpenTelemetry tracing for retry backoffs in the storage client by recording a 'RetryBackoff' child span with the attempt number during backoff sleep. It also includes comprehensive unit tests for this new behavior. The reviewer suggested an improvement to record the context error and set the span status to error if the context is cancelled or times out during the backoff, which enhances observability.

Comment thread storage/trace.go Resolved
Adds OpenTelemetry tracing for the retry backoff sleeps in run() (HTTP and
gRPC transports).

For each backoff the client records a RetryBackoff child span of the
operation span, covering the sleep. The span is recorded once the sleep
ends, with its start time set to when the sleep began, so it is not
visible while the backoff is still in progress.

A backoff cut short by context cancellation or deadline is still recorded,
so the trace shows the time spent waiting before the operation gave up.

Nothing is recorded unless GO_STORAGE_DEV_OTEL_TRACING=true and the
operation span is recording, so there is no change for users with tracing
disabled. Once the flag is removed each retry will add one span; a TODO
notes that this should be revisited then.

Tests: TestRecordRetryBackoffNoParentSpan, TestRunRetryBackoffTracing
(table-driven, including a backoff interrupted by the context),
TestRunRetryBackoffTracingDisabled.
@sahusneha2004
sahusneha2004 force-pushed the feat/otel-retry-backoff-spans branch from 57317b8 to 214f4a5 Compare October 17, 2026 21:22
Comment thread storage/trace.go Resolved
Comment thread storage/trace_test.go Resolved

@cpriti-os cpriti-os left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. One simplification and one assertion to add.

Comment thread storage/trace.go Outdated
if !span.IsRecording() {
return
}
_, backoffSpan := tracer().Start(ctx, appendPackageName("RetryBackoff"), append(getCommonTraceOptions(), trace.WithTimestamp(start))...)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

startSpan already does this composition:

_, span := startSpan(ctx, "RetryBackoff", trace.WithTimestamp(start))
span.End()

Bonus: it also attaches the gcp.resource.destination.* attributes from ctx, which every other span in the trace has and this one currently doesn't.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — switched back to startSpan(ctx, "RetryBackoff", trace.WithTimestamp(start)) so the backoff span also includes the gcp.resource.destination.* attributes from ctx.

Comment thread storage/trace_test.go
Comment on lines +457 to +464
name: "context done during backoff",
// gax picks a random pause in [1ns, Initial], so a huge Initial makes
// a pause shorter than the timeout practically impossible.
retry: &retryConfig{backoff: &gax.Backoff{Initial: 1000 * time.Hour, Max: 1000 * time.Hour}},
errs: []error{retryableErr},
timeout: 50 * time.Millisecond,
wantErr: true,
wantBackoffs: 1,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the "context done during backoff" case, could you also assert the span's duration is ≥ the timeout? That's what actually proves the comment in invoke.go ("recorded even if ctx ended").

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — updated checkRetryBackoffs to assert s.EndTime.Sub(s.StartTime) >= tc.timeout.

Comment thread storage/trace.go Outdated
// RetryBackoff child span of the span in ctx. It is a no-op unless dev tracing
// is enabled and the span in ctx is recording.
//
// TODO: When the dev tracing flag is removed, decide whether backoff spans

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NitL remove this todo? I think we want the span on all retries?

Comment thread storage/trace_test.go Outdated

// findSpans returns the spans in spans named with the package-qualified form
// of name.
func findSpans(spans tracetest.SpanStubs, name string) tracetest.SpanStubs {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the helper functions at the end of the test file, this and the function below

@sahusneha2004
sahusneha2004 enabled auto-merge (squash) October 23, 2026 15:29
@sahusneha2004
sahusneha2004 merged commit edca405 into googleapis:main Oct 23, 2026
17 checks passed
sahusneha2004 pushed a commit that referenced this pull request Nov 16, 2026
🤖 I have created a release *beep* *boop*
---


##
[1.71.0](storage/v1.70.0...storage/v1.71.0)
(2026-11-14)


### Features

* **storage:** Add default buffer pool for gRPC parallel uploads
([#20719](#20719))
([69a9ad3](69a9ad3))
* **storage:** Add error.type and gcp.errors.domain to operation spans
([#20705](#20705))
([50cd4a3](50cd4a3))
* **storage:** Add gcp.client.retry.attempt_number to gRPC attempt spans
([#20716](#20716))
([5528a50](5528a50))
* **storage:** Add gcp.storage.payload.offset/size_bytes to attempt
spans
([#20757](#20757))
([56721d1](56721d1))
* **storage:** Add gcp.storage.upload.id attribute for resumable uploads
([#20687](#20687))
([1259592](1259592))
* **storage:** Add gcp.storage.uri trace attribute to object and bucket
operation spans
([#20606](#20606))
([dfdddde](dfdddde))
* **storage:** Add OpenTelemetry tracing for auth credential refreshes
([#20677](#20677))
([3ec4bb2](3ec4bb2))
* **storage:** Add RetryBackoff spans for retry backoffs
([#20715](#20715))
([edca405](edca405))
* **storage:** Add rpc.system.name attribute to trace spans
([#20655](#20655))
([7d238ad](7d238ad))
* **storage:** Add Storage.UploadChunk spans for resumable uploads
([#20750](#20750))
([22c9ad5](22c9ad5))
* **storage:** Align metric names, units, and base labels
([#20796](#20796))
([9e83e4c](9e83e4c))
* **storage:** Expose Unfinalized on ReaderObjectAttrs
([#20703](#20703))
([c2aabb1](c2aabb1))
* **storage:** Record read and write mode trace attributes
([#20714](#20714))
([f41be16](f41be16))


### Bug Fixes

* Adds additional validation for REST RPC requests
([#20753](#20753))
([7827181](7827181))
* Correct 'seperated' and 'paramters' typos in comments
([#20828](#20828))
([51c6cf6](51c6cf6))
* **storage:** Apply read stall timeout to initial stream open in
MultiRangeDownloader
([#20840](#20840))
([1ddfb07](1ddfb07))
* **storage:** Do not discard seen bytes on 206 Partial Content during
reader reopen
([#20766](#20766))
([aa773a1](aa773a1))
* **storage:** Fix WaitGroup data race in MultiRangeDownloader callbacks
([#20827](#20827))
([b6b9f0b](b6b9f0b))
* **storage:** Prevent spurious cancelled status on completed gRPC reads
([#20409](#20409))
([4749772](4749772))
* **storage:** Unbreak storage benchmark module
([#20893](#20893))
([51e96b4](51e96b4))
* **storage:** Use URL-safe base64 for the feature tracking header
([#20684](#20684))
([50fd4eb](50fd4eb))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: storage Issues related to the Cloud Storage API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants