httprouter

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

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

Go to latest
Published: Dec 19, 2024 License: Apache-2.0, BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package httprouter provides functions to trace the julienschmidt/httprouter package (https://github.com/julienschmidt/httprouter).

Example
package main

import (
	"fmt"
	"log"
	"net/http"

	httptrace "github.com/DataDog/dd-trace-go/contrib/julienschmidt/httprouter/v2"
	"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"

	"github.com/julienschmidt/httprouter"
)

func Index(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
	fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, _ *http.Request, ps httprouter.Params) {
	fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}

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

	router := httptrace.New()
	router.GET("/", Index)
	router.GET("/hello/:name", Hello)

	log.Fatal(http.ListenAndServe(":8080", router))
}
Output:

Example (WithServiceName)
package main

import (
	"fmt"
	"log"
	"net/http"

	httptrace "github.com/DataDog/dd-trace-go/contrib/julienschmidt/httprouter/v2"
	"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"

	"github.com/julienschmidt/httprouter"
)

func Index(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
	fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, _ *http.Request, ps httprouter.Params) {
	fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}

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

	router := httptrace.New(httptrace.WithService("http.router"))
	router.GET("/", Index)
	router.GET("/hello/:name", Hello)

	log.Fatal(http.ListenAndServe(":8080", router))
}
Output:

Example (WithSpanOpts)
package main

import (
	"fmt"
	"log"
	"net/http"

	httptrace "github.com/DataDog/dd-trace-go/contrib/julienschmidt/httprouter/v2"
	"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
	"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"

	"github.com/julienschmidt/httprouter"
)

func Index(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
	fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, _ *http.Request, ps httprouter.Params) {
	fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}

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

	router := httptrace.New(
		httptrace.WithService("http.router"),
		httptrace.WithSpanOptions(
			tracer.Tag(ext.ManualKeep, true),
		),
	)

	router.GET("/", Index)
	router.GET("/hello/:name", Hello)

	log.Fatal(http.ListenAndServe(":8080", router))
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var WithAnalytics = tracing.WithAnalytics

WithAnalytics enables Trace Analytics for all started spans.

View Source
var WithAnalyticsRate = tracing.WithAnalyticsRate

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

View Source
var WithHeaderTags = tracing.WithHeaderTags

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.

View Source
var WithService = tracing.WithService

WithService sets the given service name for the returned router.

View Source
var WithSpanOptions = tracing.WithSpanOptions

WithSpanOptions applies the given set of options to the span started by the router.

Functions

This section is empty.

Types

type Router

type Router struct {
	*httprouter.Router
	// contains filtered or unexported fields
}

Router is a traced version of httprouter.Router.

func New

func New(opts ...RouterOption) *Router

New returns a new router augmented with tracing.

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP implements http.Handler.

type RouterOption

type RouterOption = tracing.Option

RouterOption represents an option that can be passed to New.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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