httptransport

package
v0.0.0-...-be548ea Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CtxLogger

type CtxLogger interface {
	Printf(context.Context, string, ...any)
}

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

func (*CurlRT) RoundTrip

func (c *CurlRT) RoundTrip(req *http.Request) (*http.Response, error)

type IDCtxLogger

type IDCtxLogger struct{ L Logger }

func (IDCtxLogger) Printf

func (i IDCtxLogger) Printf(ctx context.Context, format string, args ...any)

type Logger

type Logger interface{ Printf(string, ...any) }

Logger is a minimal printf-style logger used when no context is required.

Directories

Path Synopsis
example
invoke command
stream command

Jump to

Keyboard shortcuts

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