package request

import "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp/internal/request"

Index

Types

type BodyWrapper

type BodyWrapper struct {
	io.ReadCloser
	OnRead func(n int64) // must not be nil
	// contains filtered or unexported fields
}

BodyWrapper wraps a http.Request.Body (an io.ReadCloser) to track the number of bytes read and the last error.

func NewBodyWrapper

func NewBodyWrapper(body io.ReadCloser, onRead func(int64)) *BodyWrapper

NewBodyWrapper creates a new BodyWrapper.

The onRead attribute is a callback that will be called every time the data is read, with the number of bytes being read.

func (*BodyWrapper) BytesRead

func (w *BodyWrapper) BytesRead() int64

BytesRead returns the number of bytes read up to this point.

func (*BodyWrapper) Close

func (w *BodyWrapper) Close() error

Closes closes the io.ReadCloser.

func (*BodyWrapper) Error

func (w *BodyWrapper) Error() error

Error returns the last error.

func (*BodyWrapper) Read

func (w *BodyWrapper) Read(b []byte) (int, error)

Read reads the data from the io.ReadCloser, and stores the number of bytes read and the error.

type RespWriterWrapper

type RespWriterWrapper struct {
	http.ResponseWriter
	OnWrite func(n int64) // must not be nil
	// contains filtered or unexported fields
}

RespWriterWrapper wraps a http.ResponseWriter in order to track the number of bytes written, the last error, and to catch the first written statusCode. TODO: The wrapped http.ResponseWriter doesn't implement any of the optional types (http.Hijacker, http.Pusher, http.CloseNotifier, etc) that may be useful when using it in real life situations.

func NewRespWriterWrapper

func NewRespWriterWrapper(w http.ResponseWriter, onWrite func(int64)) *RespWriterWrapper

NewRespWriterWrapper creates a new RespWriterWrapper.

The onWrite attribute is a callback that will be called every time the data is written, with the number of bytes that were written.

func (*RespWriterWrapper) BytesWritten

func (w *RespWriterWrapper) BytesWritten() int64

BytesWritten returns the number of bytes written.

func (*RespWriterWrapper) Error

func (w *RespWriterWrapper) Error() error

Error returns the last error.

func (*RespWriterWrapper) Flush

func (w *RespWriterWrapper) Flush()

Flush implements http.Flusher.

func (*RespWriterWrapper) StatusCode

func (w *RespWriterWrapper) StatusCode() int

BytesWritten returns the HTTP status code that was sent.

func (*RespWriterWrapper) Write

func (w *RespWriterWrapper) Write(p []byte) (int, error)

Write writes the bytes array into the [ResponseWriter], and tracks the number of bytes written and last error.

func (*RespWriterWrapper) WriteHeader

func (w *RespWriterWrapper) WriteHeader(statusCode int)

WriteHeader persists initial statusCode for span attribution. All calls to WriteHeader will be propagated to the underlying ResponseWriter and will persist the statusCode from the first call. Blocking consecutive calls to WriteHeader alters expected behavior and will remove warning logs from net/http where developers will notice incorrect handler implementations.

Source Files

body_wrapper.go gen.go resp_writer_wrapper.go

Version
v0.60.0 (latest)
Published
Mar 6, 2025
Platform
linux/amd64
Imports
3 packages
Last checked
1 month ago

Tools for package owners.