transport

package
v1.4.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 4, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package transport provides HTTP transport middleware for logging, recording, and other cross-cutting concerns.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTransport

func NewTransport(tracer trace.Tracer, next http.RoundTripper) http.RoundTripper

NewTransport returns an http.RoundTripper that creates an OpenTelemetry client span for every HTTP request, including OAuth token refresh calls and redirect hops. It wraps next, falling back to http.DefaultTransport when next is nil.

Use this with client.Client.Transport to ensure all HTTP calls — including those made by golang.org/x/oauth2 during token refresh — are traced:

httpClient.Transport = transport.NewTransport(tracer, httpClient.Transport)

func WithSkipTokenInjection added in v1.4.2

func WithSkipTokenInjection(ctx context.Context) context.Context

WithSkipTokenInjection returns a copy of ctx that instructs TokenTransport to skip Authorization header injection for the request carrying this context. Use this when deliberately omitting credentials — for example when building a redirect hop to a different host where credential leakage must be prevented.

Types

type HeadersTransport added in v1.4.2

type HeadersTransport struct {
	http.RoundTripper
	// contains filtered or unexported fields
}

HeadersTransport is an http.RoundTripper middleware that injects a fixed set of headers (and an optional User-Agent) into every outbound request. It is installed once during client construction and then immutable, so no locking is required.

func NewHeaders added in v1.4.2

func NewHeaders(parent http.RoundTripper, ua string, headers map[string]string) *HeadersTransport

NewHeaders wraps parent in a HeadersTransport that sets ua as the User-Agent (when non-empty) and merges headers into every request. A header with an empty value is deleted rather than set. If parent is nil, http.DefaultTransport is used. The headers map is copied defensively so later mutations by the caller have no effect on the transport.

func (*HeadersTransport) RoundTrip added in v1.4.2

func (t *HeadersTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper. The request is cloned before any header is mutated so the original is never modified.

type Logging

type Logging struct {
	http.RoundTripper
	// contains filtered or unexported fields
}

Logging is an http.RoundTripper middleware that logs requests and responses.

func NewLogging

func NewLogging(w io.Writer, parent http.RoundTripper, verbose bool) *Logging

NewLogging creates a logging middleware that wraps parent. Every request and response is written to w. When verbose is true the request and response bodies are also written. If parent is nil, http.DefaultTransport is used.

func (*Logging) Payload

func (t *Logging) Payload(v interface{})

Payload writes v to the log as indented JSON.

func (*Logging) RoundTrip

func (t *Logging) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

type RateLimitTransport added in v1.4.2

type RateLimitTransport struct {
	http.RoundTripper
	// contains filtered or unexported fields
}

RateLimitTransport is an http.RoundTripper middleware that enforces a maximum request rate (requests per second). It sleeps before forwarding each request when necessary, and respects context cancellation during the sleep.

func NewRateLimit added in v1.4.2

func NewRateLimit(parent http.RoundTripper, rate float32) *RateLimitTransport

NewRateLimit wraps parent in a RateLimitTransport that allows at most rate requests per second. A rate of 0 disables throttling. If parent is nil, http.DefaultTransport is used.

func (*RateLimitTransport) RoundTrip added in v1.4.2

func (t *RateLimitTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper. When a rate limit is configured each call reserves its send-slot under the lock (advancing t.ts immediately) and then sleeps outside the lock until that slot arrives. Reserving the slot before releasing the lock ensures that concurrent callers are assigned distinct, strictly ordered slots rather than all sleeping for the same duration and proceeding together.

type Recorder

type Recorder struct {
	http.RoundTripper
	// contains filtered or unexported fields
}

Recorder is an http.RoundTripper middleware that captures the status code and response headers of the most recent response. It is safe for concurrent use; each RoundTrip call overwrites the previously recorded values.

func NewRecorder

func NewRecorder(parent http.RoundTripper) *Recorder

NewRecorder wraps parent in a Recorder. If parent is nil, http.DefaultTransport is used.

func (*Recorder) Header

func (r *Recorder) Header() http.Header

Header returns a copy of the response headers recorded from the most recent response, or nil if no response has been received yet.

func (*Recorder) Reset

func (r *Recorder) Reset()

Reset clears the recorded status code and headers.

func (*Recorder) RoundTrip

func (r *Recorder) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper, recording the response status and headers before returning the response to the caller.

func (*Recorder) StatusCode

func (r *Recorder) StatusCode() int

StatusCode returns the HTTP status code recorded from the most recent response, or 0 if no response has been received yet.

type TokenTransport added in v1.4.2

type TokenTransport struct {
	http.RoundTripper
	// contains filtered or unexported fields
}

TokenTransport is an http.RoundTripper middleware that injects an Authorization header into every outbound request. The token value is fetched lazily via a callback on each request, so it always reflects the most recently obtained access token.

func NewToken added in v1.4.2

func NewToken(parent http.RoundTripper, token func() string) *TokenTransport

NewToken wraps parent in a TokenTransport. On each request, token() is called; if it returns a non-empty string it is set as the Authorization header (the callback is responsible for including the scheme prefix, e.g. "Bearer <value>"). If parent is nil, http.DefaultTransport is used.

func (*TokenTransport) RoundTrip added in v1.4.2

func (t *TokenTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper. If the token callback returns a non-empty string AND the request does not already carry an Authorization header AND the request context does not carry a WithSkipTokenInjection signal, the request is cloned and its Authorization header is set before being forwarded to the parent transport.

Priority (highest to lowest):

  1. WithSkipTokenInjection in context — injection skipped entirely
  2. Pre-existing Authorization header (e.g. set via OptToken) — preserved
  3. Global token callback — injected when neither of the above applies

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL