Documentation
¶
Overview ¶
Package httptransport provides a configurable cURL-style logging RoundTripper for HTTP-based ChatModel clients. It logs the real outbound HTTP request (as a cURL command) and the inbound HTTP response. Streaming responses (SSE or NDJSON) can be logged chunk-by-chunk without breaking the stream.
Quick usage:
client := &http.Client{Transport: httptransport.NewCurlRT(
http.DefaultTransport,
httptransport.WithLogger(log.Default()),
// Or pass a context-aware logger to extract request IDs:
httptransport.WithCtxLogger(httptransport.IDCtxLogger{L: log.Default()}),
// Security controls:
httptransport.WithPrintAuth(false), // mask Authorization
httptransport.WithMaskHeaders([]string{"X-API-KEY"}), // mask custom headers
// Streaming controls:
httptransport.WithStreamLogging(true),
httptransport.WithMaxStreamLogBytes(8192),
)}
cm, _ := openai.NewChatModel(ctx, &openai.ChatModelConfig{ HTTPClient: client, ... })
Notes:
- WithCtxLogger is preferred when you carry a request/log ID in context.
- WithPrintAuth controls whether the Authorization header is printed.
- WithMaskHeaders and WithMaskFunc allow masking arbitrary headers.
- When stream logging is enabled, headers are logged once, and chunks are emitted as they are read. With a plain Logger, a capped summary is printed on Close(); with a CtxLogger, each chunk is logged directly.
Index ¶
- type CtxLogger
- type CurlOption
- func WithCtxLogger(l CtxLogger) CurlOption
- func WithLogger(l Logger) CurlOption
- func WithMaskFunc(f func(name, value string) string) CurlOption
- func WithMaskHeaders(names []string) CurlOption
- func WithMaxStreamLogBytes(n int) CurlOption
- func WithPrintAuth(b bool) CurlOption
- func WithStreamContentTypeFilter(f func(ct string) bool) CurlOption
- func WithStreamLogging(enabled bool) CurlOption
- type CurlRT
- type IDCtxLogger
- type Logger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CtxLogger ¶
CtxLogger is a context-aware logger; use this to inject request IDs or structured logging derived from the HTTP request context.
type CurlOption ¶
type CurlOption func(*CurlRT)
CurlOption configures CurlRT behavior.
func WithCtxLogger ¶
func WithCtxLogger(l CtxLogger) CurlOption
WithCtxLogger sets a context-aware logger for request/response/chunk logs.
func WithLogger ¶
func WithLogger(l Logger) CurlOption
WithLogger sets a simple printf-style logger.
func WithMaskFunc ¶
func WithMaskFunc(f func(name, value string) string) CurlOption
WithMaskFunc provides a custom masking function for header values.
func WithMaskHeaders ¶
func WithMaskHeaders(names []string) CurlOption
WithMaskHeaders masks specified header names (case-insensitive) in logs.
func WithMaxStreamLogBytes ¶
func WithMaxStreamLogBytes(n int) CurlOption
WithMaxStreamLogBytes caps stream summary size when using a plain Logger.
func WithPrintAuth ¶
func WithPrintAuth(b bool) CurlOption
WithPrintAuth controls whether the Authorization header value is printed.
func WithStreamContentTypeFilter ¶
func WithStreamContentTypeFilter(f func(ct string) bool) CurlOption
WithStreamContentTypeFilter sets a filter to detect streaming responses.
func WithStreamLogging ¶
func WithStreamLogging(enabled bool) CurlOption
WithStreamLogging enables logging for streaming responses (SSE/NDJSON).
type CurlRT ¶
type CurlRT struct {
// contains filtered or unexported fields
}
CurlRT is an http.RoundTripper that logs requests/responses in cURL style. Configure it with the CurlOption helpers via NewCurlRT.
func NewCurlRT ¶
func NewCurlRT(base http.RoundTripper, opts ...CurlOption) *CurlRT
type IDCtxLogger ¶
type IDCtxLogger struct{ L Logger }