Skip to content

Commit

Permalink
Merge pull request #30 from DataDog/nicolas/services
Browse files Browse the repository at this point in the history
add support for services
  • Loading branch information
LeoCavaille authored Jan 6, 2017
2 parents ce55bd0 + 5eb09be commit 18a1bd0
Show file tree
Hide file tree
Showing 13 changed files with 344 additions and 67 deletions.
1 change: 1 addition & 0 deletions tracer/contrib/gin-gonic/gintrace/gintrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func Middleware(service string) gin.HandlerFunc {
// MiddlewareTracer returns middleware that will trace requests with the given
// tracer.
func MiddlewareTracer(service string, t *tracer.Tracer) gin.HandlerFunc {
t.SetServiceInfo(service, "gin-gonic", ext.AppTypeWeb)
mw := newMiddleware(service, t)
return mw.Handle
}
Expand Down
18 changes: 12 additions & 6 deletions tracer/contrib/gin-gonic/gintrace/gintrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestTrace200(t *testing.T) {
assert.Equal(response.StatusCode, 200)

// verify traces look good
assert.Nil(testTracer.Flush())
assert.Nil(testTracer.FlushTraces())
traces := testTransport.Traces()
assert.Len(traces, 1)
spans := traces[0]
Expand Down Expand Up @@ -109,7 +109,7 @@ func TestDisabled(t *testing.T) {
assert.Equal(response.StatusCode, 200)

// verify traces look good
testTracer.Flush()
testTracer.FlushTraces()
spans := testTransport.Traces()
assert.Len(spans, 0)
}
Expand All @@ -134,7 +134,7 @@ func TestError(t *testing.T) {
assert.Equal(response.StatusCode, 500)

// verify the errors and status are correct
testTracer.Flush()
testTracer.FlushTraces()
traces := testTransport.Traces()
assert.Len(traces, 1)
spans := traces[0]
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestHTML(t *testing.T) {
assert.Equal("hello world", w.Body.String())

// verify the errors and status are correct
testTracer.Flush()
testTracer.FlushTraces()
traces := testTransport.Traces()
assert.Len(traces, 1)
spans := traces[0]
Expand Down Expand Up @@ -220,14 +220,20 @@ func getTestTracer() (*tracer.Tracer, *dummyTransport) {

// dummyTransport is a transport that just buffers spans and encoding
type dummyTransport struct {
traces [][]*tracer.Span
traces [][]*tracer.Span
services map[string]tracer.Service
}

func (t *dummyTransport) Send(traces [][]*tracer.Span) (*http.Response, error) {
func (t *dummyTransport) SendTraces(traces [][]*tracer.Span) (*http.Response, error) {
t.traces = append(t.traces, traces...)
return nil, nil
}

func (t *dummyTransport) SendServices(services map[string]tracer.Service) (*http.Response, error) {
t.services = services
return nil, nil
}

func (t *dummyTransport) Traces() [][]*tracer.Span {
traces := t.traces
t.traces = nil
Expand Down
1 change: 1 addition & 0 deletions tracer/contrib/gorilla/muxtrace/muxtrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type MuxTracer struct {

// NewMuxTracer creates a MuxTracer for the given service and tracer.
func NewMuxTracer(service string, t *tracer.Tracer) *MuxTracer {
t.SetServiceInfo(service, "gorilla", ext.AppTypeWeb)
return &MuxTracer{
tracer: t,
service: service,
Expand Down
18 changes: 12 additions & 6 deletions tracer/contrib/gorilla/muxtrace/muxtrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestMuxTracerDisabled(t *testing.T) {
assert.Equal(writer.Body.String(), "disabled!")

// assert nothing was traced.
assert.Nil(testTracer.Flush())
assert.Nil(testTracer.FlushTraces())
traces := testTransport.Traces()
assert.Len(traces, 0)
}
Expand All @@ -51,7 +51,7 @@ func TestMuxTracerSubrequest(t *testing.T) {
assert.Equal(writer.Body.String(), "200!")

// ensure properly traced
assert.Nil(tracer.Flush())
assert.Nil(tracer.FlushTraces())
traces := transport.Traces()
assert.Len(traces, 1)
spans := traces[0]
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestMuxTracer200(t *testing.T) {
assert.Equal(writer.Body.String(), "200!")

// ensure properly traced
assert.Nil(tracer.Flush())
assert.Nil(tracer.FlushTraces())
traces := transport.Traces()
assert.Len(traces, 1)
spans := traces[0]
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestMuxTracer500(t *testing.T) {
assert.Equal(writer.Body.String(), "500!\n")

// ensure properly traced
assert.Nil(tracer.Flush())
assert.Nil(tracer.FlushTraces())
traces := transport.Traces()
assert.Len(traces, 1)
spans := traces[0]
Expand Down Expand Up @@ -177,14 +177,20 @@ func getTestTracer(service string) (*tracer.Tracer, *dummyTransport, *MuxTracer)

// dummyTransport is a transport that just buffers spans and encoding
type dummyTransport struct {
traces [][]*tracer.Span
traces [][]*tracer.Span
services map[string]tracer.Service
}

func (t *dummyTransport) Send(traces [][]*tracer.Span) (*http.Response, error) {
func (t *dummyTransport) SendTraces(traces [][]*tracer.Span) (*http.Response, error) {
t.traces = append(t.traces, traces...)
return nil, nil
}

func (t *dummyTransport) SendServices(services map[string]tracer.Service) (*http.Response, error) {
t.services = services
return nil, nil
}

func (t *dummyTransport) Traces() [][]*tracer.Span {
traces := t.traces
t.traces = nil
Expand Down
7 changes: 5 additions & 2 deletions tracer/contrib/tracegrpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/DataDog/dd-trace-go/tracer"
"github.com/DataDog/dd-trace-go/tracer/ext"

context "golang.org/x/net/context"
"google.golang.org/grpc"
Expand All @@ -19,7 +20,8 @@ const (
)

// UnaryServerInterceptor will trace requests to the given grpc server.
func UnaryServerInterceptor(t *tracer.Tracer) grpc.UnaryServerInterceptor {
func UnaryServerInterceptor(service string, t *tracer.Tracer) grpc.UnaryServerInterceptor {
t.SetServiceInfo(service, "grpc-server", ext.AppTypeRPC)
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
if !t.Enabled() {
return handler(ctx, req)
Expand All @@ -33,7 +35,8 @@ func UnaryServerInterceptor(t *tracer.Tracer) grpc.UnaryServerInterceptor {
}

// UnaryClientInterceptor will add tracing to a gprc client.
func UnaryClientInterceptor() grpc.UnaryClientInterceptor {
func UnaryClientInterceptor(service string, t *tracer.Tracer) grpc.UnaryClientInterceptor {
t.SetServiceInfo(service, "grpc-client", ext.AppTypeRPC)
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {

var child *tracer.Span
Expand Down
22 changes: 14 additions & 8 deletions tracer/contrib/tracegrpc/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestClient(t *testing.T) {
span.Finish()
assert.Equal(resp.Message, "passed")

testTracer.Flush()
testTracer.FlushTraces()
traces := testTransport.Traces()
assert.Len(traces, 1)
spans := traces[0]
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestDisabled(t *testing.T) {
resp, err := client.Ping(context.Background(), &FixtureRequest{Name: "disabled"})
assert.Nil(err)
assert.Equal(resp.Message, "disabled")
assert.Nil(testTracer.Flush())
assert.Nil(testTracer.FlushTraces())
traces := testTransport.Traces()
assert.Nil(traces)
}
Expand All @@ -93,7 +93,7 @@ func TestChild(t *testing.T) {
resp, err := client.Ping(context.Background(), &FixtureRequest{Name: "child"})
assert.Nil(err)
assert.Equal(resp.Message, "child")
assert.Nil(testTracer.Flush())
assert.Nil(testTracer.FlushTraces())
traces := testTransport.Traces()
assert.Len(traces, 1)
spans := traces[0]
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestPass(t *testing.T) {
resp, err := client.Ping(context.Background(), &FixtureRequest{Name: "pass"})
assert.Nil(err)
assert.Equal(resp.Message, "passed")
assert.Nil(testTracer.Flush())
assert.Nil(testTracer.FlushTraces())
traces := testTransport.Traces()
assert.Len(traces, 1)
spans := traces[0]
Expand Down Expand Up @@ -189,7 +189,7 @@ func (r *rig) Close() {

func newRig(t *tracer.Tracer, traceClient bool) (*rig, error) {

server := grpc.NewServer(grpc.UnaryInterceptor(UnaryServerInterceptor(t)))
server := grpc.NewServer(grpc.UnaryInterceptor(UnaryServerInterceptor("foo", t)))

RegisterFixtureServer(server, newFixtureServer())

Expand All @@ -206,7 +206,7 @@ func newRig(t *tracer.Tracer, traceClient bool) (*rig, error) {
}

if traceClient {
opts = append(opts, grpc.WithUnaryInterceptor(UnaryClientInterceptor()))
opts = append(opts, grpc.WithUnaryInterceptor(UnaryClientInterceptor("foo", t)))
}

conn, err := grpc.Dial(li.Addr().String(), opts...)
Expand All @@ -233,14 +233,20 @@ func getTestTracer() (*tracer.Tracer, *dummyTransport) {

// dummyTransport is a transport that just buffers spans and encoding
type dummyTransport struct {
traces [][]*tracer.Span
traces [][]*tracer.Span
services map[string]tracer.Service
}

func (t *dummyTransport) Send(traces [][]*tracer.Span) (*http.Response, error) {
func (t *dummyTransport) SendTraces(traces [][]*tracer.Span) (*http.Response, error) {
t.traces = append(t.traces, traces...)
return nil, nil
}

func (t *dummyTransport) SendServices(services map[string]tracer.Service) (*http.Response, error) {
t.services = services
return nil, nil
}

func (t *dummyTransport) Traces() [][]*tracer.Span {
traces := t.traces
t.traces = nil
Expand Down
32 changes: 22 additions & 10 deletions tracer/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"github.com/ugorji/go/codec"
)

// Encoder is a generic interface that expects an Encode() method
// for the encoding process, and a Read() method that will be used
// by the http handler
// Encoder is a generic interface that expects encoding methods for traces and
// services, and a Read() method that will be used by the http handler
type Encoder interface {
Encode(traces [][]*Span) error
EncodeTraces(traces [][]*Span) error
EncodeServices(services map[string]Service) error
Read(p []byte) (int, error)
ContentType() string
}
Expand All @@ -36,13 +36,19 @@ func newMsgpackEncoder() *msgpackEncoder {
}
}

// Encode serializes the given traces list into the internal
// buffer, returning the error if any
func (e *msgpackEncoder) Encode(traces [][]*Span) error {
// EncodeTraces serializes the given trace list into the internal buffer,
// returning the error if any.
func (e *msgpackEncoder) EncodeTraces(traces [][]*Span) error {
e.buffer.Reset()
return e.encoder.Encode(traces)
}

// EncodeServices serializes a service map into the internal buffer.
func (e *msgpackEncoder) EncodeServices(services map[string]Service) error {
e.buffer.Reset()
return e.encoder.Encode(services)
}

// Read values from the internal buffer
func (e *msgpackEncoder) Read(p []byte) (int, error) {
return e.buffer.Read(p)
Expand Down Expand Up @@ -72,13 +78,19 @@ func newJSONEncoder() *jsonEncoder {
}
}

// Encode serializes the given traces list into the internal
// buffer, returning the error if any
func (e *jsonEncoder) Encode(traces [][]*Span) error {
// EncodeTraces serializes the given trace list into the internal buffer,
// returning the error if any.
func (e *jsonEncoder) EncodeTraces(traces [][]*Span) error {
e.buffer.Reset()
return e.encoder.Encode(traces)
}

// EncodeServices serializes a service map into the internal buffer.
func (e *jsonEncoder) EncodeServices(services map[string]Service) error {
e.buffer.Reset()
return e.encoder.Encode(services)
}

// Read values from the internal buffer
func (e *jsonEncoder) Read(p []byte) (int, error) {
return e.buffer.Read(p)
Expand Down
4 changes: 2 additions & 2 deletions tracer/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestJSONEncoding(t *testing.T) {
for _, tc := range testCases {
payload := getTestTrace(tc.traces, tc.size)
encoder := newJSONEncoder()
err := encoder.Encode(payload)
err := encoder.EncodeTraces(payload)
assert.Nil(err)

// decode to check the right encoding
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestMsgpackEncoding(t *testing.T) {
for _, tc := range testCases {
payload := getTestTrace(tc.traces, tc.size)
encoder := newMsgpackEncoder()
err := encoder.Encode(payload)
err := encoder.EncodeTraces(payload)
assert.Nil(err)

// decode to check the right encoding
Expand Down
9 changes: 9 additions & 0 deletions tracer/ext/app_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ext

// Application types for services.
const (
AppTypeWeb = "web"
AppTypeDB = "db"
AppTypeCache = "cache"
AppTypeRPC = "rpc"
)
Loading

0 comments on commit 18a1bd0

Please sign in to comment.