contrib/net/http

Those integrations are enabled by having the following import in the project’s orchestrion.tool.go file:

import (
	_ "github.com/DataDog/orchestrion"

	_ "github.com/DataDog/dd-trace-go/contrib/net/http/v2" // integration
	//...
)

github.com/DataDog/dd-trace-go/contrib/net/http/v2

HTTP stack implementation.

Transport.DD__tracer_internal

Join Point
Definition of http.Transport
Advice
Add new field DD__tracer_internal of type bool.

Transport.DD__tracer_internal=true

Advice
Replace the expression using the template:
{{- .AST.Type -}}{
	DD__tracer_internal: true,
	{{ range .AST.Elts }}{{ . }},
	{{ end }}
}

Transport.RoundTrip

Join Point
Function body
Advice
Introduce new declarations:
type ddAfterRoundTrip = func(*Response, error) (*Response, error)

//go:linkname __dd_httptrace_ObserveRoundTrip github.com/DataDog/dd-trace-go/contrib/net/http/v2/internal/orchestrion.ObserveRoundTrip
func __dd_httptrace_ObserveRoundTrip(*Request) (*Request, ddAfterRoundTrip, error)
Record link-time dependencies on:
Advice
Prepend statements produced by the following template:
{{- $t := .Function.Receiver -}}
{{- $req := .Function.Argument 0 -}}
{{- $res := .Function.Result 0 -}}
{{- $err := .Function.Result 1 -}}
if !{{ $t }}.DD__tracer_internal {
	var (
		__after__ ddAfterRoundTrip
		__err__   error
	)
	{{ $req }}, __after__, __err__ = __dd_httptrace_ObserveRoundTrip({{ $req }})
	if __err__ != nil {
		return nil, __err__
	}
	defer func(){
		{{ $res }}, {{ $err }} = __after__({{ $res }}, {{ $err }})
	}()
}

Get|Head|Post|PostForm

Join Point
All of
Advice
Replace the expression using the template:
// Using the following synthetic imports:
import (
	client "github.com/DataDog/dd-trace-go/contrib/net/http/v2/client"
)
{{- $ctx := .Function.ArgumentOfType "context.Context" -}}
{{- $req := .Function.ArgumentOfType "*net/http.Request" }}
{{- if $ctx -}}
	client.{{ .AST.Fun.Name }}(
		{{ $ctx }},
		{{ range .AST.Args }}{{ . }},
		{{ end }}
	)
{{- else if $req -}}
	client.{{ .AST.Fun.Name }}(
		{{ $req }}.Context(),
		{{ range .AST.Args }}{{ . }},
		{{ end }}
	)
{{- else -}}
	{{ . }}
{{- end -}}

Server.Serve

Join Point
Function body
  • Function declaration
Advice
Introduce new declarations:
//go:linkname __dd_contrib_net_http_internal_orchestrion_WrapHandler github.com/DataDog/dd-trace-go/contrib/net/http/v2/internal/orchestrion.WrapHandler
func __dd_contrib_net_http_internal_orchestrion_WrapHandler(Handler) Handler
Record link-time dependencies on:
Advice
Prepend statements produced by the following template:
{{- $srv := .Function.Receiver -}}
if {{ $srv }}.Handler == nil {
	{{ $srv }}.Handler = __dd_contrib_net_http_internal_orchestrion_WrapHandler(DefaultServeMux)
} else {
	{{ $srv }}.Handler = __dd_contrib_net_http_internal_orchestrion_WrapHandler({{ $srv }}.Handler)
}

github.com/DataDog/dd-trace-go/contrib/net/http/v2.Client

HTTP client implementation.

Transport.DD__tracer_internal

Join Point
Definition of http.Transport
Advice
Add new field DD__tracer_internal of type bool.

Transport.DD__tracer_internal=true

Advice
Replace the expression using the template:
{{- .AST.Type -}}{
	DD__tracer_internal: true,
	{{ range .AST.Elts }}{{ . }},
	{{ end }}
}

Transport.RoundTrip

Join Point
Function body
Advice
Introduce new declarations:
type ddAfterRoundTrip = func(*Response, error) (*Response, error)

//go:linkname __dd_httptrace_ObserveRoundTrip github.com/DataDog/dd-trace-go/contrib/net/http/v2/internal/orchestrion.ObserveRoundTrip
func __dd_httptrace_ObserveRoundTrip(*Request) (*Request, ddAfterRoundTrip, error)
Record link-time dependencies on:
Advice
Prepend statements produced by the following template:
{{- $t := .Function.Receiver -}}
{{- $req := .Function.Argument 0 -}}
{{- $res := .Function.Result 0 -}}
{{- $err := .Function.Result 1 -}}
if !{{ $t }}.DD__tracer_internal {
	var (
		__after__ ddAfterRoundTrip
		__err__   error
	)
	{{ $req }}, __after__, __err__ = __dd_httptrace_ObserveRoundTrip({{ $req }})
	if __err__ != nil {
		return nil, __err__
	}
	defer func(){
		{{ $res }}, {{ $err }} = __after__({{ $res }}, {{ $err }})
	}()
}

Get|Head|Post|PostForm

Join Point
All of
Advice
Replace the expression using the template:
// Using the following synthetic imports:
import (
	client "github.com/DataDog/dd-trace-go/contrib/net/http/v2/client"
)
{{- $ctx := .Function.ArgumentOfType "context.Context" -}}
{{- $req := .Function.ArgumentOfType "*net/http.Request" }}
{{- if $ctx -}}
	client.{{ .AST.Fun.Name }}(
		{{ $ctx }},
		{{ range .AST.Args }}{{ . }},
		{{ end }}
	)
{{- else if $req -}}
	client.{{ .AST.Fun.Name }}(
		{{ $req }}.Context(),
		{{ range .AST.Args }}{{ . }},
		{{ end }}
	)
{{- else -}}
	{{ . }}
{{- end -}}

github.com/DataDog/dd-trace-go/contrib/net/http/v2.Server

HTTP server implementation.

Server.Serve

Join Point
Function body
  • Function declaration
Advice
Introduce new declarations:
//go:linkname __dd_contrib_net_http_internal_orchestrion_WrapHandler github.com/DataDog/dd-trace-go/contrib/net/http/v2/internal/orchestrion.WrapHandler
func __dd_contrib_net_http_internal_orchestrion_WrapHandler(Handler) Handler
Record link-time dependencies on:
Advice
Prepend statements produced by the following template:
{{- $srv := .Function.Receiver -}}
if {{ $srv }}.Handler == nil {
	{{ $srv }}.Handler = __dd_contrib_net_http_internal_orchestrion_WrapHandler(DefaultServeMux)
} else {
	{{ $srv }}.Handler = __dd_contrib_net_http_internal_orchestrion_WrapHandler({{ $srv }}.Handler)
}