http

package
v1.60.0 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2024 License: Apache-2.0, BSD-3-Clause, Apache-2.0 Imports: 18 Imported by: 142

Documentation

Overview

Package http provides functions to trace the net/http package (https://golang.org/pkg/net/http).

Example
package main

import (
	"net/http"

	httptrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http"
)

func main() {
	mux := httptrace.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello World!\n"))
	})
	http.ListenAndServe(":8080", mux)
}
Output:

Example (WithServiceName)
package main

import (
	"net/http"

	httptrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http"
)

func main() {
	mux := httptrace.NewServeMux(httptrace.WithServiceName("my-service"))
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello World!\n"))
	})
	http.ListenAndServe(":8080", mux)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func TraceAndServe added in v1.36.0

func TraceAndServe(h http.Handler, w http.ResponseWriter, r *http.Request, cfg *ServeConfig)

TraceAndServe serves the handler h using the given ResponseWriter and Request, applying tracing according to the specified config.

Example
package main

import (
	"net/http"

	httptrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http"
)

func main() {
	mux := http.NewServeMux()
	mux.Handle("/", traceMiddleware(mux, http.HandlerFunc(Index)))
	http.ListenAndServe(":8080", mux)
}

func Index(w http.ResponseWriter, _ *http.Request) {
	w.Write([]byte("Hello World!\n"))
}

func traceMiddleware(mux *http.ServeMux, next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		_, route := mux.Handler(r)
		resource := r.Method + " " + route
		httptrace.TraceAndServe(next, w, r, &httptrace.ServeConfig{
			Service:     "http.router",
			Resource:    resource,
			QueryParams: true,
		})
	})
}
Output:

func WrapClient added in v1.4.0

func WrapClient(c *http.Client, opts ...RoundTripperOption) *http.Client

WrapClient modifies the given client's transport to augment it with tracing and returns it.

Example

ExampleWrapClient provides an example of how to connect an incoming request span to an outgoing http call.

package main

import (
	"net/http"

	httptrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http"
)

func main() {
	mux := httptrace.NewServeMux()
	// Note that `WrapClient` modifies the passed in Client, so all other users of DefaultClient in this example will have a traced http Client
	c := httptrace.WrapClient(http.DefaultClient)
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		req, _ := http.NewRequestWithContext(r.Context(), http.MethodGet, "http://test.test", nil)
		resp, err := c.Do(req)
		if err != nil {
			w.Write([]byte(err.Error()))
			return
		}
		defer resp.Body.Close()
		w.Write([]byte(resp.Status))
	})
	http.ListenAndServe(":8080", mux)
}
Output:

func WrapHandler

func WrapHandler(h http.Handler, service, resource string, opts ...Option) http.Handler

WrapHandler wraps an http.Handler with tracing using the given service and resource. If the WithResourceNamer option is provided as part of opts, it will take precedence over the resource argument.

func WrapRoundTripper added in v1.2.0

func WrapRoundTripper(rt http.RoundTripper, opts ...RoundTripperOption) http.RoundTripper

WrapRoundTripper returns a new RoundTripper which traces all requests sent over the transport.

Types

type MuxOption

type MuxOption = Option

MuxOption has been deprecated in favor of Option.

func WithAnalytics added in v1.11.0

func WithAnalytics(on bool) MuxOption

WithAnalytics enables Trace Analytics for all started spans.

func WithAnalyticsRate added in v1.11.0

func WithAnalyticsRate(rate float64) MuxOption

WithAnalyticsRate sets the sampling rate for Trace Analytics events correlated to started spans.

func WithIgnoreRequest added in v1.32.0

func WithIgnoreRequest(f func(*http.Request) bool) MuxOption

WithIgnoreRequest holds the function to use for determining if the incoming HTTP request should not be traced.

func WithServiceName

func WithServiceName(name string) MuxOption

WithServiceName sets the given service name for the returned ServeMux.

type Option added in v1.13.0

type Option func(*config)

Option represents an option that can be passed to NewServeMux or WrapHandler.

func NoDebugStack added in v1.24.0

func NoDebugStack() Option

NoDebugStack prevents stack traces from being attached to spans finishing with an error. This is useful in situations where errors are frequent and performance is critical.

func WithHeaderTags added in v1.53.0

func WithHeaderTags(headers []string) Option

WithHeaderTags enables the integration to attach HTTP request headers as span tags. Warning: Using this feature can risk exposing sensitive data such as authorization tokens to Datadog. Special headers can not be sub-selected. E.g., an entire Cookie header would be transmitted, without the ability to choose specific Cookies.

func WithResourceNamer added in v1.39.0

func WithResourceNamer(namer func(req *http.Request) string) Option

WithResourceNamer populates the name of a resource based on a custom function.

func WithSpanOptions added in v1.13.0

func WithSpanOptions(opts ...ddtrace.StartSpanOption) Option

WithSpanOptions defines a set of additional ddtrace.StartSpanOption to be added to spans started by the integration.

type RoundTripperAfterFunc added in v1.2.0

type RoundTripperAfterFunc func(*http.Response, ddtrace.Span)

A RoundTripperAfterFunc can be used to modify a span after an http RoundTrip is made. It is possible for the http Response to be nil.

type RoundTripperBeforeFunc added in v1.2.0

type RoundTripperBeforeFunc func(*http.Request, ddtrace.Span)

A RoundTripperBeforeFunc can be used to modify a span before an http RoundTrip is made.

type RoundTripperOption added in v1.2.0

type RoundTripperOption func(*roundTripperConfig)

A RoundTripperOption represents an option that can be passed to WrapRoundTripper.

func RTWithAnalytics added in v1.11.0

func RTWithAnalytics(on bool) RoundTripperOption

RTWithAnalytics enables Trace Analytics for all started spans.

func RTWithAnalyticsRate added in v1.11.0

func RTWithAnalyticsRate(rate float64) RoundTripperOption

RTWithAnalyticsRate sets the sampling rate for Trace Analytics events correlated to started spans.

func RTWithErrorCheck added in v1.49.0

func RTWithErrorCheck(fn func(err error) bool) RoundTripperOption

RTWithErrorCheck specifies a function fn which determines whether the passed error should be marked as an error. The fn is called whenever an http operation finishes with an error

func RTWithIgnoreRequest added in v1.41.0

func RTWithIgnoreRequest(f func(*http.Request) bool) RoundTripperOption

RTWithIgnoreRequest holds the function to use for determining if the outgoing HTTP request should not be traced.

func RTWithPropagation added in v1.53.0

func RTWithPropagation(propagation bool) RoundTripperOption

RTWithPropagation enables/disables propagation for tracing headers. Disabling propagation will disconnect this trace from any downstream traces.

func RTWithResourceNamer added in v1.26.0

func RTWithResourceNamer(namer func(req *http.Request) string) RoundTripperOption

RTWithResourceNamer specifies a function which will be used to obtain the resource name for a given request.

func RTWithServiceName added in v1.20.0

func RTWithServiceName(name string) RoundTripperOption

RTWithServiceName sets the given service name for the RoundTripper.

func RTWithSpanNamer added in v1.50.0

func RTWithSpanNamer(namer func(req *http.Request) string) RoundTripperOption

RTWithSpanNamer specifies a function which will be used to obtain the span operation name for a given request.

func RTWithSpanOptions added in v1.35.0

func RTWithSpanOptions(opts ...ddtrace.StartSpanOption) RoundTripperOption

RTWithSpanOptions defines a set of additional ddtrace.StartSpanOption to be added to spans started by the integration.

func WithAfter added in v1.2.0

WithAfter adds a RoundTripperAfterFunc to the RoundTripper config.

func WithBefore added in v1.2.0

WithBefore adds a RoundTripperBeforeFunc to the RoundTripper config.

type ServeConfig added in v1.36.0

type ServeConfig struct {
	// Service specifies the service name to use. If left blank, the global service name
	// will be inherited.
	Service string
	// Resource optionally specifies the resource name for this request.
	Resource string
	// QueryParams should be true in order to append the URL query values to the  "http.url" tag.
	QueryParams bool
	// Route is the request matched route if any, or is empty otherwise
	Route string
	// RouteParams specifies framework-specific route parameters (e.g. for route /user/:id coming
	// in as /user/123 we'll have {"id": "123"}). This field is optional and is used for monitoring
	// by AppSec. It is only taken into account when AppSec is enabled.
	RouteParams map[string]string
	// FinishOpts specifies any options to be used when finishing the request span.
	FinishOpts []ddtrace.FinishOption
	// SpanOpts specifies any options to be applied to the request starting span.
	SpanOpts []ddtrace.StartSpanOption
}

ServeConfig specifies the tracing configuration when using TraceAndServe.

type ServeMux

type ServeMux struct {
	*http.ServeMux
	// contains filtered or unexported fields
}

ServeMux is an HTTP request multiplexer that traces all the incoming requests.

func NewServeMux

func NewServeMux(opts ...Option) *ServeMux

NewServeMux allocates and returns an http.ServeMux augmented with the global tracer.

func (*ServeMux) ServeHTTP

func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP dispatches the request to the handler whose pattern most closely matches the request URL. We only need to rewrite this function to be able to trace all the incoming requests to the underlying multiplexer

Jump to

Keyboard shortcuts

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