2018-06-28 16:40:04 +00:00
|
|
|
package tracer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-01-16 16:08:06 +00:00
|
|
|
"io"
|
2018-06-28 16:40:04 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2019-01-16 16:08:06 +00:00
|
|
|
"os"
|
2018-06-28 16:40:04 +00:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2019-07-18 19:36:05 +00:00
|
|
|
|
|
|
|
"gopkg.in/DataDog/dd-trace-go.v1/internal/version"
|
2018-06-28 16:40:04 +00:00
|
|
|
)
|
|
|
|
|
2019-01-16 16:08:06 +00:00
|
|
|
var (
|
|
|
|
// We copy the transport to avoid using the default one, as it might be
|
|
|
|
// augmented with tracing and we don't want these calls to be recorded.
|
|
|
|
// See https://golang.org/pkg/net/http/#DefaultTransport .
|
|
|
|
defaultRoundTripper = &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
DialContext: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
DualStack: true,
|
|
|
|
}).DialContext,
|
|
|
|
MaxIdleConns: 100,
|
|
|
|
IdleConnTimeout: 90 * time.Second,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
|
|
}
|
|
|
|
)
|
2018-06-28 16:40:04 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
defaultHostname = "localhost"
|
|
|
|
defaultPort = "8126"
|
|
|
|
defaultAddress = defaultHostname + ":" + defaultPort
|
|
|
|
defaultHTTPTimeout = time.Second // defines the current timeout before giving up with the send process
|
|
|
|
traceCountHeader = "X-Datadog-Trace-Count" // header containing the number of traces in the payload
|
|
|
|
)
|
|
|
|
|
2019-01-16 16:08:06 +00:00
|
|
|
// transport is an interface for span submission to the agent.
|
2018-06-28 16:40:04 +00:00
|
|
|
type transport interface {
|
2019-01-16 16:08:06 +00:00
|
|
|
// send sends the payload p to the agent using the transport set up.
|
|
|
|
// It returns a non-nil response body when no error occurred.
|
|
|
|
send(p *payload) (body io.ReadCloser, err error)
|
2018-06-28 16:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newTransport returns a new Transport implementation that sends traces to a
|
2019-01-16 16:08:06 +00:00
|
|
|
// trace agent running on the given hostname and port, using a given
|
|
|
|
// http.RoundTripper. If the zero values for hostname and port are provided,
|
|
|
|
// the default values will be used ("localhost" for hostname, and "8126" for
|
|
|
|
// port). If roundTripper is nil, a default is used.
|
2018-06-28 16:40:04 +00:00
|
|
|
//
|
|
|
|
// In general, using this method is only necessary if you have a trace agent
|
2019-01-16 16:08:06 +00:00
|
|
|
// running on a non-default port, if it's located on another machine, or when
|
|
|
|
// otherwise needing to customize the transport layer, for instance when using
|
|
|
|
// a unix domain socket.
|
|
|
|
func newTransport(addr string, roundTripper http.RoundTripper) transport {
|
|
|
|
if roundTripper == nil {
|
|
|
|
roundTripper = defaultRoundTripper
|
|
|
|
}
|
|
|
|
return newHTTPTransport(addr, roundTripper)
|
2018-06-28 16:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newDefaultTransport return a default transport for this tracing client
|
|
|
|
func newDefaultTransport() transport {
|
2019-01-16 16:08:06 +00:00
|
|
|
return newHTTPTransport(defaultAddress, defaultRoundTripper)
|
2018-06-28 16:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type httpTransport struct {
|
|
|
|
traceURL string // the delivery URL for traces
|
|
|
|
client *http.Client // the HTTP client used in the POST
|
|
|
|
headers map[string]string // the Transport headers
|
|
|
|
}
|
|
|
|
|
|
|
|
// newHTTPTransport returns an httpTransport for the given endpoint
|
2019-01-16 16:08:06 +00:00
|
|
|
func newHTTPTransport(addr string, roundTripper http.RoundTripper) *httpTransport {
|
2018-06-28 16:40:04 +00:00
|
|
|
// initialize the default EncoderPool with Encoder headers
|
|
|
|
defaultHeaders := map[string]string{
|
|
|
|
"Datadog-Meta-Lang": "go",
|
|
|
|
"Datadog-Meta-Lang-Version": strings.TrimPrefix(runtime.Version(), "go"),
|
|
|
|
"Datadog-Meta-Lang-Interpreter": runtime.Compiler + "-" + runtime.GOARCH + "-" + runtime.GOOS,
|
2019-07-18 19:36:05 +00:00
|
|
|
"Datadog-Meta-Tracer-Version": version.Tag,
|
2018-06-28 16:40:04 +00:00
|
|
|
"Content-Type": "application/msgpack",
|
|
|
|
}
|
|
|
|
return &httpTransport{
|
2019-01-16 16:08:06 +00:00
|
|
|
traceURL: fmt.Sprintf("http://%s/v0.4/traces", resolveAddr(addr)),
|
2018-06-28 16:40:04 +00:00
|
|
|
client: &http.Client{
|
2019-01-16 16:08:06 +00:00
|
|
|
Transport: roundTripper,
|
|
|
|
Timeout: defaultHTTPTimeout,
|
2018-06-28 16:40:04 +00:00
|
|
|
},
|
|
|
|
headers: defaultHeaders,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 16:08:06 +00:00
|
|
|
func (t *httpTransport) send(p *payload) (body io.ReadCloser, err error) {
|
2018-06-28 16:40:04 +00:00
|
|
|
// prepare the client and send the payload
|
|
|
|
req, err := http.NewRequest("POST", t.traceURL, p)
|
|
|
|
if err != nil {
|
2019-01-16 16:08:06 +00:00
|
|
|
return nil, fmt.Errorf("cannot create http request: %v", err)
|
2018-06-28 16:40:04 +00:00
|
|
|
}
|
|
|
|
for header, value := range t.headers {
|
|
|
|
req.Header.Set(header, value)
|
|
|
|
}
|
|
|
|
req.Header.Set(traceCountHeader, strconv.Itoa(p.itemCount()))
|
|
|
|
req.Header.Set("Content-Length", strconv.Itoa(p.size()))
|
|
|
|
response, err := t.client.Do(req)
|
|
|
|
if err != nil {
|
2019-01-16 16:08:06 +00:00
|
|
|
return nil, err
|
2018-06-28 16:40:04 +00:00
|
|
|
}
|
|
|
|
if code := response.StatusCode; code >= 400 {
|
|
|
|
// error, check the body for context information and
|
|
|
|
// return a nice error.
|
|
|
|
msg := make([]byte, 1000)
|
|
|
|
n, _ := response.Body.Read(msg)
|
2019-01-16 16:08:06 +00:00
|
|
|
response.Body.Close()
|
2018-06-28 16:40:04 +00:00
|
|
|
txt := http.StatusText(code)
|
|
|
|
if n > 0 {
|
2019-01-16 16:08:06 +00:00
|
|
|
return nil, fmt.Errorf("%s (Status: %s)", msg[:n], txt)
|
2018-06-28 16:40:04 +00:00
|
|
|
}
|
2019-01-16 16:08:06 +00:00
|
|
|
return nil, fmt.Errorf("%s", txt)
|
2018-06-28 16:40:04 +00:00
|
|
|
}
|
2019-01-16 16:08:06 +00:00
|
|
|
return response.Body, nil
|
2018-06-28 16:40:04 +00:00
|
|
|
}
|
2018-11-19 15:40:03 +00:00
|
|
|
|
|
|
|
// resolveAddr resolves the given agent address and fills in any missing host
|
2019-01-16 16:08:06 +00:00
|
|
|
// and port using the defaults. Some environment variable settings will
|
|
|
|
// take precedence over configuration.
|
2018-11-19 15:40:03 +00:00
|
|
|
func resolveAddr(addr string) string {
|
|
|
|
host, port, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
// no port in addr
|
|
|
|
host = addr
|
|
|
|
}
|
|
|
|
if host == "" {
|
|
|
|
host = defaultHostname
|
|
|
|
}
|
|
|
|
if port == "" {
|
|
|
|
port = defaultPort
|
|
|
|
}
|
2019-01-16 16:08:06 +00:00
|
|
|
if v := os.Getenv("DD_AGENT_HOST"); v != "" {
|
|
|
|
host = v
|
|
|
|
}
|
|
|
|
if v := os.Getenv("DD_TRACE_AGENT_PORT"); v != "" {
|
|
|
|
port = v
|
|
|
|
}
|
2018-11-19 15:40:03 +00:00
|
|
|
return fmt.Sprintf("%s:%s", host, port)
|
|
|
|
}
|