nethttp

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2020 License: Apache-2.0 Imports: 17 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Middleware

func Middleware(h http.Handler, options ...MWOption) http.Handler

func MiddlewareFunc

func MiddlewareFunc(h http.HandlerFunc, options ...MWOption) http.Handler

func PatchHttpDefaultClient

func PatchHttpDefaultClient(options ...Option)

Patches the default http client with the instrumented transport

Types

type ClientOption

type ClientOption func(*clientOptions)

ClientOption controls the behavior of TraceRequest.

func ClientSpanObserver

func ClientSpanObserver(f func(span opentracing.Span, r *http.Request)) ClientOption

ClientSpanObserver returns a ClientOption that observes the span for the client-side span.

func ClientTrace

func ClientTrace(enabled bool) ClientOption

ClientTrace returns a ClientOption that turns on or off extra instrumentation via httptrace.WithClientTrace.

func ComponentName

func ComponentName(componentName string) ClientOption

ComponentName returns a ClientOption that sets the component name for the client-side span.

func InjectSpanContext

func InjectSpanContext(enabled bool) ClientOption

InjectSpanContext returns a ClientOption that turns on or off injection of the Span context in the request HTTP headers. If this option is not used, the default behaviour is to inject the span context.

func OperationName

func OperationName(operationName string) ClientOption

OperationName returns a ClientOption that sets the operation name for the client-side span.

type MWOption

type MWOption func(*mwOptions)

MWOption controls the behavior of the Middleware.

func MWComponentName

func MWComponentName(componentName string) MWOption

MWComponentName returns a MWOption that sets the component name for the server-side span.

func MWPayloadInstrumentation

func MWPayloadInstrumentation() MWOption

Enable payload instrumentation

func MWSpanFilter

func MWSpanFilter(f func(r *http.Request) bool) MWOption

MWSpanFilter returns a MWOption that filters requests from creating a span for the server-side span. Span won't be created if it returns false.

func MWSpanObserver

func MWSpanObserver(f func(span opentracing.Span, r *http.Request)) MWOption

MWSpanObserver returns a MWOption that observe the span for the server-side span.

func MWURLTagFunc

func MWURLTagFunc(f func(u *url.URL) string) MWOption

MWURLTagFunc returns a MWOption that uses given function f to set the span's http.url tag. Can be used to change the default http.url tag, eg to redact sensitive information.

func OperationNameFunc

func OperationNameFunc(f func(r *http.Request) string) MWOption

OperationNameFunc returns a MWOption that uses given function f to generate operation name for each server-side span.

type Option

type Option func(*Transport)

func WithPayloadInstrumentation

func WithPayloadInstrumentation() Option

Enables the payload instrumentation in the transport

func WithStacktrace added in v0.1.13

func WithStacktrace() Option

Enables stacktrace

type Tracer

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

Tracer holds tracing details for one HTTP request.

func TraceRequest

func TraceRequest(tr opentracing.Tracer, req *http.Request, options ...ClientOption) (*http.Request, *Tracer)

TraceRequest adds a ClientTracer to req, tracing the request and all requests caused due to redirects. When tracing requests this way you must also use Transport.

Example:

func AskGoogle(ctx context.Context) error {
	client := &http.Client{Transport: &nethttp.Transport{}}
	req, err := http.NewRequest("GET", "http://google.com", nil)
	if err != nil {
		return err
	}
	req = req.WithContext(ctx) // extend existing trace, if any

	req, ht := nethttp.TraceRequest(tracer, req)
	defer ht.Finish()

	res, err := client.Do(req)
	if err != nil {
		return err
	}
	res.Body.Close()
	return nil
}

func TracerFromRequest

func TracerFromRequest(req *http.Request) *Tracer

TracerFromRequest retrieves the Tracer from the request. If the request does not have a Tracer it will return nil.

func (*Tracer) Finish

func (h *Tracer) Finish()

Finish finishes the span of the traced request.

func (*Tracer) Span

func (h *Tracer) Span() opentracing.Span

Span returns the root span of the traced request. This function should only be called after the request has been executed.

type Transport

type Transport struct {
	// The actual RoundTripper to use for the request. A nil
	// RoundTripper defaults to http.DefaultTransport.
	http.RoundTripper

	// Enable payload instrumentation
	PayloadInstrumentation bool

	// Enable stacktrace
	Stacktrace bool
}

Transport wraps a RoundTripper. If a request is being traced with Tracer, Transport will inject the current span into the headers, and set HTTP related tags on the span.

func (*Transport) RoundTrip

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

Jump to

Keyboard shortcuts

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