xhttp

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2022 License: Apache-2.0 Imports: 17 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetRelativeURL

func GetRelativeURL(request *http.Request, endpoint string) url.URL

GetRelativeURL takes a path component of URL and constructs a new URL using the host and port from the request combined the provided path.

func NewRequestLogger

func NewRequestLogger(handler http.Handler, prefix string, additionalEntries AdditionalLogExtractor, granularity time.Duration, packageLogger string, opts ...RequestLoggerOption) http.Handler

NewRequestLogger create a new RequestLogger handler, requests are chained to the supplied handler. The log includes the clock time to handle the request, with specified granularity (e.g. time.Millisecond). The generated Log lines are in the format <prefix>:<HTTP Method>:<ClientCertSubjectCN>:<Path>:<RemoteIP>:<RemotePort>:<StatusCode>:<HTTP Version>:<Response Body Size>:<Request Duration>:<Additional Fields> skippath parameter allows to specify a list of paths to not log.

func NewRequestMetrics

func NewRequestMetrics(h http.Handler) http.Handler

NewRequestMetrics creates a wrapper handler to produce metrics for each request

func NewRequestProfiler

func NewRequestProfiler(delegate h.Handler, dir string, allow AllowProfiling, createdCallback ProfileCreated) (h.Handler, error)

NewRequestProfiler wraps the supplied delegate and can enable cpu or memory profiling of a specific request based on the presence of ?profile.cpu or ?profile.mem in the request query string the allow function if supplied, is given the chance decide if the given request should be profiled, this allows users of this middleware to decide what if any access policy they want WRT to generating profiles. if allow is nil, all indicated requests will be profiled.

Note that go doesn't allow for concurrent profiles, so all requests that are to be profiled are serialized. [unprofiled requests are unaffected]

if dir is "" then a temp dir is created to contain the profiler outputfiles otherwise the indicated directory is used.

details of each generated profile are passed to the supplied createdCallback function

Types

type AdditionalLogExtractor

type AdditionalLogExtractor func(resp *ResponseCapture, req *http.Request) []string

AdditionalLogExtractor allows for a user of RequestLogger to extract and have additional fields recorded in the log

type AllowProfiling

type AllowProfiling func(types ProfileType, r *h.Request) bool

AllowProfiling defines a callback function that is called to see if requested profile(s) are allowed for this request [this allows you to implement auth or some other kind of finer grained profiling permissions]

type LoggerSkipPath added in v0.9.0

type LoggerSkipPath struct {
	Path  string `json:"path,omitempty" yaml:"path,omitempty"`
	Agent string `json:"agent,omitempty" yaml:"agent,omitempty"`
}

LoggerSkipPath allows to skip a log for specified Path and Agent

type ProfileCreated

type ProfileCreated func(t ProfileType, r *h.Request, f string)

ProfileCreated defines a callback geneated when a new profile has been created You can supplied a custom callback, or use the prebuilt logging callback

func LogProfile

func LogProfile() ProfileCreated

LogProfile returns a ProfileCreated callback function that writes the details of the generated profile to the supplied logger

type ProfileType

type ProfileType int

ProfileType indicates the type of profile created

const (
	// ProfileCPU indicates a CPU Usage profile
	ProfileCPU ProfileType = 1
	// ProfileMem indicates a memory usage profile
	ProfileMem ProfileType = 2
)

func (ProfileType) String

func (p ProfileType) String() string

type RequestLogger

type RequestLogger struct {
	// contains filtered or unexported fields
}

RequestLogger is a http.Handler that logs requests and forwards them on down the chain.

func (*RequestLogger) ServeHTTP

func (l *RequestLogger) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface. We wrap the call to the real handler to collect info about the response, and then write out the log line

type RequestLoggerOption added in v0.9.0

type RequestLoggerOption option

RequestLoggerOption is an option that can be passed to New().

func WithLoggerSkipPaths added in v0.9.0

func WithLoggerSkipPaths(value []LoggerSkipPath) RequestLoggerOption

WithLoggerSkipPaths is an Option allows to skip logs on path/agent match

type ResponseCapture

type ResponseCapture struct {
	// contains filtered or unexported fields
}

ResponseCapture is a net/http.ResponseWriter that delegates everything to the contained delegate, but captures the status code and number of bytes written

func NewResponseCapture

func NewResponseCapture(w http.ResponseWriter) *ResponseCapture

NewResponseCapture returns a new ResponseCapture instance that delegates writes to the supplied ResponseWriter

func (*ResponseCapture) BodySize

func (r *ResponseCapture) BodySize() uint64

BodySize returns in bytes the total number of bytes written to the response body so far.

func (*ResponseCapture) Flush added in v0.5.0

func (r *ResponseCapture) Flush()

Flush sends any buffered data to the client.

func (*ResponseCapture) Header

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

Header returns the underlying writers Header instance

func (*ResponseCapture) StatusCode

func (r *ResponseCapture) StatusCode() int

StatusCode returns the http status set by the handler.

func (*ResponseCapture) Write

func (r *ResponseCapture) Write(data []byte) (int, error)

Write the supplied data to the response (tracking the number of bytes written as we go)

func (*ResponseCapture) WriteHeader

func (r *ResponseCapture) WriteHeader(sc int)

WriteHeader sets the HTTP status code of the response

Directories

Path Synopsis
Package authz provides an implemention of http authorization where specific URI (or URI's and their children) are allowed access by a set of roles the caller can supply a way to map from a request to a role name.
Package authz provides an implemention of http authorization where specific URI (or URI's and their children) are allowed access by a set of roles the caller can supply a way to map from a request to a role name.
Package identity extracts the callers contextual identity information from the HTTP/TLS requests and exposes them for access via the generalized go context model.
Package identity extracts the callers contextual identity information from the HTTP/TLS requests and exposes them for access via the generalized go context model.
Package marshal provides some common handlers for encoding or decoding json
Package marshal provides some common handlers for encoding or decoding json

Jump to

Keyboard shortcuts

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