Skip to content

Commit 53f1991

Browse files
authored
feat(auth): Implement Actionable Errors logging for HTTP requests (#14265)
This PR introduces structured, OTel-compliant telemetry logging for actionable errors within the `google-cloud-go/auth` HTTP transport wrapper (`otelAttributeTransport`). Key implementations include: * **Safe Response Cloning:** Adds a `cloneResponse` mechanism that buffers up to 1MB of the HTTP response body to parse error payloads (like quota limits or permission errors) and uses `io.MultiReader` to transparently stitch the stream back together. This ensures the downstream generated client can still read the `http.Response.Body` without hitting stream exhaustion or OOM errors. * **Inline Transport Logging:** Introduces `readResponseError` to extract semantic attributes (e.g., `error.type`, `ErrorInfo` metadata) from the cloned response. The structured log is emitted inline via `slog` directly within the `RoundTrip`'s `onError` hook, ensuring it properly correlates with low-level T4 network spans. * **Feature Gated:** The entire logging execution is strictly gated behind the `gax.IsFeatureEnabled("LOGGING")` experimental flag to guarantee zero allocations when disabled. * **Testing:** Updates `httptransport_otel_test.go` to validate error mappings, OTel semantic conventions, and structured JSON payload formats with high coverage.
1 parent efe2e3a commit 53f1991

4 files changed

Lines changed: 882 additions & 60 deletions

File tree

auth/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
go.opentelemetry.io/otel/trace v1.42.0
1616
golang.org/x/net v0.52.0
1717
golang.org/x/time v0.15.0
18+
google.golang.org/api v0.272.0
1819
google.golang.org/genproto/googleapis/rpc v0.0.0-20260401001100-f93e5f3e9f0f
1920
google.golang.org/grpc v1.79.3
2021
google.golang.org/protobuf v1.36.11
@@ -33,5 +34,4 @@ require (
3334
golang.org/x/sync v0.20.0 // indirect
3435
golang.org/x/sys v0.42.0 // indirect
3536
golang.org/x/text v0.35.0 // indirect
36-
google.golang.org/api v0.272.0 // indirect
3737
)

auth/grpctransport/grpctransport.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,14 +471,19 @@ func addOpenTelemetryStatsHandler(dialOpts []grpc.DialOption, opts *Options, end
471471
}
472472
var staticAttrs []attribute.KeyValue
473473
var scopedLogger *slog.Logger
474+
475+
if gax.IsFeatureEnabled("LOGGING") && opts.Logger != nil {
476+
scopedLogger = opts.Logger
477+
}
478+
474479
if opts.InternalOptions != nil {
475480
staticAttrs = transport.StaticTelemetryAttributes(opts.InternalOptions.TelemetryAttributes)
476-
if gax.IsFeatureEnabled("LOGGING") && opts.Logger != nil {
481+
if scopedLogger != nil {
477482
var staticLogAttrs []any
478483
for _, attr := range staticAttrs {
479484
staticLogAttrs = append(staticLogAttrs, slog.String(string(attr.Key), attr.Value.AsString()))
480485
}
481-
scopedLogger = opts.Logger.With(staticLogAttrs...)
486+
scopedLogger = scopedLogger.With(staticLogAttrs...)
482487
}
483488
}
484489
var otelOpts []otelgrpc.Option

0 commit comments

Comments
 (0)