http

package module
v2.0.0-rc.4 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2025 License: Apache-2.0, BSD-3-Clause Imports: 12 Imported by: 5

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 "github.com/DataDog/dd-trace-go/contrib/net/http/v2"
	"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
)

func main() {
	tracer.Start()
	defer tracer.Stop()

	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 "github.com/DataDog/dd-trace-go/contrib/net/http/v2"
	"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
)

func main() {
	tracer.Start()
	defer tracer.Stop()

	mux := httptrace.NewServeMux(httptrace.WithService("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

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 "github.com/DataDog/dd-trace-go/contrib/net/http/v2"
	"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
)

func main() {
	tracer.Start()
	defer tracer.Stop()

	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

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 "github.com/DataDog/dd-trace-go/contrib/net/http/v2"
	"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
)

func main() {
	tracer.Start()
	defer tracer.Stop()

	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

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

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

Types

type HandlerOptionFn

type HandlerOptionFn = internal.HandlerOptionFn

HandlerOptionFn represents options applicable to NewServeMux and WrapHandler.

func NoDebugStack

func NoDebugStack() HandlerOptionFn

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

func WithHeaderTags(headers []string) HandlerOptionFn

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 WithStatusCheck

func WithStatusCheck(fn func(statusCode int) bool) HandlerOptionFn

WithStatusCheck sets a span to be an error if the passed function returns true for a given status code.

type Option

type Option = internal.Option

Option describes options for http.ServeMux.

type OptionFn

type OptionFn = internal.OptionFn

OptionFn represents options applicable to NewServeMux and WrapHandler.

func WithAnalytics

func WithAnalytics(on bool) OptionFn

WithAnalytics enables Trace Analytics for all started spans.

func WithAnalyticsRate

func WithAnalyticsRate(rate float64) OptionFn

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

func WithIgnoreRequest

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

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

func WithResourceNamer

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

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

func WithService

func WithService(name string) OptionFn

WithService sets the given service name for the returned ServeMux.

func WithSpanOptions

func WithSpanOptions(opts ...tracer.StartSpanOption) OptionFn

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

type RoundTripperAfterFunc

type RoundTripperAfterFunc = internal.RoundTripperAfterFunc

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

type RoundTripperBeforeFunc = internal.RoundTripperBeforeFunc

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

type RoundTripperOption

type RoundTripperOption = internal.RoundTripperOption

RoundTripperOption describes options for http.RoundTripper.

type RoundTripperOptionFn

type RoundTripperOptionFn = internal.RoundTripperOptionFn

RoundTripperOptionFn represents options applicable to WrapClient and WrapRoundTripper.

func WithAfter

WithAfter adds a RoundTripperAfterFunc to the RoundTripper config.

func WithBefore

WithBefore adds a RoundTripperBeforeFunc to the RoundTripper config.

func WithErrorCheck

func WithErrorCheck(fn func(err error) bool) RoundTripperOptionFn

WithErrorCheck 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 WithPropagation

func WithPropagation(propagation bool) RoundTripperOptionFn

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

func WithSpanNamer

func WithSpanNamer(namer func(req *http.Request) string) RoundTripperOptionFn

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

type ServeConfig

type ServeConfig = httptrace.ServeConfig

ServeConfig specifies the tracing configuration when using TraceAndServe.

type ServeMux

type ServeMux = wrap.ServeMux

func NewServeMux

func NewServeMux(opts ...Option) *ServeMux

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

Directories

Path Synopsis
Package client provides context.Context-aware alternatives to the short-hand request functions http.Get, http.Head, http.Post, and http.PostForm.
Package client provides context.Context-aware alternatives to the short-hand request functions http.Get, http.Head, http.Post, and http.PostForm.
internal
make_responsewriter
This program generates wrapper implementations of http.ResponseWriter that also satisfy http.Flusher, http.Pusher, http.CloseNotifier and http.Hijacker, based on whether or not the passed in http.ResponseWriter also satisfies them.
This program generates wrapper implementations of http.ResponseWriter that also satisfy http.Flusher, http.Pusher, http.CloseNotifier and http.Hijacker, based on whether or not the passed in http.ResponseWriter also satisfies them.

Jump to

Keyboard shortcuts

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