Add DD support (#1596)

This commit is contained in:
Yamil Asusta
2018-03-09 16:08:57 -04:00
committed by John Belamaric
parent 95342dfaad
commit 87790dd47c
214 changed files with 69817 additions and 18 deletions

View File

@@ -16,8 +16,8 @@ The simplest form is just:
trace [ENDPOINT-TYPE] [ENDPOINT]
~~~
* **ENDPOINT-TYPE** is the type of tracing destination. Currently only `zipkin` is supported
and that is what it defaults to.
* **ENDPOINT-TYPE** is the type of tracing destination. Currently only `zipkin` and `datadog` are supported.
Defaults to `zipkin`.
* **ENDPOINT** is the tracing destination, and defaults to `localhost:9411`. For Zipkin, if
ENDPOINT does not begin with `http`, then it will be transformed to `http://ENDPOINT/api/v1/spans`.
@@ -69,6 +69,12 @@ the standard Zipkin URL you can do something like:
trace http://tracinghost:9411/zipkin/api/v1/spans
~~~
Using DataDog:
~~~
trace datadog localhost:8125
~~~
Trace one query every 10000 queries, rename the service, and enable same span:
~~~

View File

@@ -36,7 +36,7 @@ func setup(c *caddy.Controller) error {
func traceParse(c *caddy.Controller) (*trace, error) {
var (
tr = &trace{Endpoint: defEP, EndpointType: defEpType, every: 1, serviceName: defServiceName}
tr = &trace{every: 1, serviceName: defServiceName}
err error
)
@@ -47,12 +47,12 @@ func traceParse(c *caddy.Controller) (*trace, error) {
args := c.RemainingArgs()
switch len(args) {
case 0:
tr.Endpoint, err = normalizeEndpoint(tr.EndpointType, defEP)
tr.EndpointType, tr.Endpoint, err = normalizeEndpoint(defEpType, "")
case 1:
tr.Endpoint, err = normalizeEndpoint(defEpType, args[0])
tr.EndpointType, tr.Endpoint, err = normalizeEndpoint(defEpType, args[0])
case 2:
tr.EndpointType = strings.ToLower(args[0])
tr.Endpoint, err = normalizeEndpoint(tr.EndpointType, args[1])
epType := strings.ToLower(args[0])
tr.EndpointType, tr.Endpoint, err = normalizeEndpoint(epType, args[1])
default:
err = c.ArgErr()
}
@@ -94,20 +94,30 @@ func traceParse(c *caddy.Controller) (*trace, error) {
return tr, err
}
func normalizeEndpoint(epType, ep string) (string, error) {
switch epType {
case "zipkin":
func normalizeEndpoint(epType, ep string) (string, string, error) {
if _, ok := supportedProviders[epType]; !ok {
return "", "", fmt.Errorf("tracing endpoint type '%s' is not supported", epType)
}
if ep == "" {
ep = supportedProviders[epType]
}
if epType == "zipkin" {
if !strings.Contains(ep, "http") {
ep = "http://" + ep + "/api/v1/spans"
}
return ep, nil
default:
return "", fmt.Errorf("tracing endpoint type '%s' is not supported", epType)
}
return epType, ep, nil
}
var supportedProviders = map[string]string{
"zipkin": "localhost:9411",
"datadog": "localhost:8126",
}
const (
defEP = "localhost:9411"
defEpType = "zipkin"
defServiceName = "coredns"
)

View File

@@ -20,7 +20,8 @@ func TestTraceParse(t *testing.T) {
{`trace localhost:1234`, false, "http://localhost:1234/api/v1/spans", 1, `coredns`, false},
{`trace http://localhost:1234/somewhere/else`, false, "http://localhost:1234/somewhere/else", 1, `coredns`, false},
{`trace zipkin localhost:1234`, false, "http://localhost:1234/api/v1/spans", 1, `coredns`, false},
{`trace zipkin http://localhost:1234/somewhere/else`, false, "http://localhost:1234/somewhere/else", 1, `coredns`, false},
{`trace datadog localhost`, false, "localhost", 1, `coredns`, false},
{`trace datadog http://localhost:8127`, false, "http://localhost:8127", 1, `coredns`, false},
{"trace {\n every 100\n}", false, "http://localhost:9411/api/v1/spans", 100, `coredns`, false},
{"trace {\n every 100\n service foobar\nclient_server\n}", false, "http://localhost:9411/api/v1/spans", 100, `foobar`, true},
{"trace {\n every 2\n client_server true\n}", false, "http://localhost:9411/api/v1/spans", 2, `coredns`, true},

View File

@@ -3,6 +3,7 @@ package trace
import (
"fmt"
"strings"
"sync"
"sync/atomic"
@@ -10,6 +11,7 @@ import (
// Plugin the trace package.
_ "github.com/coredns/coredns/plugin/pkg/trace"
ddtrace "github.com/DataDog/dd-trace-go/opentracing"
"github.com/miekg/dns"
ot "github.com/opentracing/opentracing-go"
zipkin "github.com/openzipkin/zipkin-go-opentracing"
@@ -40,6 +42,8 @@ func (t *trace) OnStartup() error {
switch t.EndpointType {
case "zipkin":
err = t.setupZipkin()
case "datadog":
err = t.setupDatadog()
default:
err = fmt.Errorf("unknown endpoint type: %s", t.EndpointType)
}
@@ -60,6 +64,22 @@ func (t *trace) setupZipkin() error {
return err
}
func (t *trace) setupDatadog() error {
config := ddtrace.NewConfiguration()
config.ServiceName = t.serviceName
host := strings.Split(t.Endpoint, ":")
config.AgentHostname = host[0]
if len(host) == 2 {
config.AgentPort = host[1]
}
tracer, _, err := ddtrace.NewTracer(config)
t.tracer = tracer
return err
}
// Name implements the Handler interface.
func (t *trace) Name() string {
return "trace"