Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contrib: Update non-client contrib packages to measure their spans #603

Merged
merged 18 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contrib/Shopify/sarama/sarama.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func WrapPartitionConsumer(pc sarama.PartitionConsumer, opts ...Option) sarama.P
tracer.SpanType(ext.SpanTypeMessageConsumer),
tracer.Tag("partition", msg.Partition),
tracer.Tag("offset", msg.Offset),
tracer.Measured(),
}
if !math.IsNaN(cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
Expand Down
1 change: 1 addition & 0 deletions contrib/confluentinc/confluent-kafka-go/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (c *Consumer) startSpan(msg *kafka.Message) ddtrace.Span {
tracer.SpanType(ext.SpanTypeMessageConsumer),
tracer.Tag("partition", msg.TopicPartition.Partition),
tracer.Tag("offset", msg.TopicPartition.Offset),
tracer.Measured(),
}
if !math.IsNaN(c.cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, c.cfg.analyticsRate))
Expand Down
1 change: 1 addition & 0 deletions contrib/gin-gonic/gin/gintrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
tracer.SpanType(ext.SpanTypeWeb),
tracer.Tag(ext.HTTPMethod, c.Request.Method),
tracer.Tag(ext.HTTPURL, c.Request.URL.Path),
tracer.Measured(),
}
if !math.IsNaN(cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
Expand Down
1 change: 1 addition & 0 deletions contrib/go-chi/chi/chi.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func Middleware(opts ...Option) func(next http.Handler) http.Handler {
tracer.ServiceName(cfg.serviceName),
tracer.Tag(ext.HTTPMethod, r.Method),
tracer.Tag(ext.HTTPURL, r.URL.Path),
tracer.Measured(),
}
if !math.IsNaN(cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
Expand Down
1 change: 1 addition & 0 deletions contrib/google.golang.org/grpc.v12/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func startSpanFromContext(ctx context.Context, method, service string, rate floa
tracer.ResourceName(method),
tracer.Tag(tagMethod, method),
tracer.SpanType(ext.AppTypeRPC),
tracer.Measured(),
}
if !math.IsNaN(rate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, rate))
Expand Down
6 changes: 3 additions & 3 deletions contrib/google.golang.org/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (cs *clientStream) RecvMsg(m interface{}) (err error) {
cs.method,
"grpc.message",
cs.cfg.clientServiceName(),
cs.cfg.analyticsRate,
tracer.AnalyticsRate(cs.cfg.analyticsRate),
)
if p, ok := peer.FromContext(cs.Context()); ok {
setSpanTargetFromPeer(span, *p)
Expand All @@ -50,7 +50,7 @@ func (cs *clientStream) SendMsg(m interface{}) (err error) {
cs.method,
"grpc.message",
cs.cfg.clientServiceName(),
cs.cfg.analyticsRate,
tracer.AnalyticsRate(cs.cfg.analyticsRate),
)
if p, ok := peer.FromContext(cs.Context()); ok {
setSpanTargetFromPeer(span, *p)
Expand Down Expand Up @@ -156,7 +156,7 @@ func doClientRequest(
method,
"grpc.client",
cfg.clientServiceName(),
cfg.analyticsRate,
tracer.AnalyticsRate(cfg.analyticsRate),
)
if methodKind != "" {
span.SetTag(tagMethodKind, methodKind)
Expand Down
10 changes: 3 additions & 7 deletions contrib/google.golang.org/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package grpc // import "gopkg.in/DataDog/dd-trace-go.v1/contrib/google.golang.or

import (
"io"
"math"

"gopkg.in/DataDog/dd-trace-go.v1/contrib/google.golang.org/internal/grpcutil"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
Expand All @@ -24,17 +23,14 @@ import (
)

func startSpanFromContext(
ctx context.Context, method, operation, service string, rate float64,
ctx context.Context, method, operation, service string, opts ...tracer.StartSpanOption,
knusbaum marked this conversation as resolved.
Show resolved Hide resolved
) (ddtrace.Span, context.Context) {
opts := []ddtrace.StartSpanOption{
opts = append(opts,
tracer.ServiceName(service),
tracer.ResourceName(method),
tracer.Tag(tagMethodName, method),
tracer.SpanType(ext.AppTypeRPC),
}
if !math.IsNaN(rate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, rate))
}
)
md, _ := metadata.FromIncomingContext(ctx) // nil is ok
if sctx, err := tracer.Extract(grpcutil.MDCarrier(md)); err == nil {
opts = append(opts, tracer.ChildOf(sctx))
Expand Down
13 changes: 9 additions & 4 deletions contrib/google.golang.org/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package grpc

import (
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"

context "golang.org/x/net/context"
"google.golang.org/grpc"
Expand Down Expand Up @@ -38,7 +39,8 @@ func (ss *serverStream) RecvMsg(m interface{}) (err error) {
ss.method,
"grpc.message",
ss.cfg.serverServiceName(),
ss.cfg.analyticsRate,
tracer.AnalyticsRate(ss.cfg.analyticsRate),
tracer.Measured(),
)
defer func() { finishWithError(span, err, ss.cfg) }()
}
Expand All @@ -53,7 +55,8 @@ func (ss *serverStream) SendMsg(m interface{}) (err error) {
ss.method,
"grpc.message",
ss.cfg.serverServiceName(),
ss.cfg.analyticsRate,
tracer.AnalyticsRate(ss.cfg.analyticsRate),
tracer.Measured(),
)
defer func() { finishWithError(span, err, ss.cfg) }()
}
Expand Down Expand Up @@ -81,7 +84,8 @@ func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {
info.FullMethod,
"grpc.server",
cfg.serviceName,
cfg.analyticsRate,
tracer.AnalyticsRate(cfg.analyticsRate),
tracer.Measured(),
)
switch {
case info.IsServerStream && info.IsClientStream:
Expand Down Expand Up @@ -123,7 +127,8 @@ func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor {
info.FullMethod,
"grpc.server",
cfg.serverServiceName(),
cfg.analyticsRate,
tracer.AnalyticsRate(cfg.analyticsRate),
tracer.Measured(),
)
span.SetTag(tagMethodKind, methodKindUnary)
resp, err := handler(ctx, req)
Expand Down
2 changes: 1 addition & 1 deletion contrib/google.golang.org/grpc/stats_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (h *clientStatsHandler) TagRPC(ctx context.Context, rti *stats.RPCTagInfo)
rti.FullMethodName,
"grpc.client",
h.cfg.clientServiceName(),
h.cfg.analyticsRate,
tracer.AnalyticsRate(h.cfg.analyticsRate),
)
ctx = injectSpanIntoContext(ctx)
return ctx
Expand Down
7 changes: 4 additions & 3 deletions contrib/google.golang.org/grpc/stats_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
package grpc

import (
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"

context "golang.org/x/net/context"
"google.golang.org/grpc/stats"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

// NewServerStatsHandler returns a gRPC server stats.Handler to trace RPC calls.
Expand All @@ -35,7 +35,8 @@ func (h *serverStatsHandler) TagRPC(ctx context.Context, rti *stats.RPCTagInfo)
rti.FullMethodName,
"grpc.server",
h.cfg.serverServiceName(),
h.cfg.analyticsRate,
tracer.AnalyticsRate(h.cfg.analyticsRate),
tracer.Measured(),
)
return ctx
}
Expand Down
1 change: 1 addition & 0 deletions contrib/google.golang.org/grpc/stats_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestServerStatsHandler(t *testing.T) {
"service.name": serviceName,
"resource.name": "/grpc.Fixture/Ping",
tagMethodName: "/grpc.Fixture/Ping",
"_dd.measured": 1,
}, span.Tags())
}

Expand Down
1 change: 1 addition & 0 deletions contrib/gorilla/mux/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func NewRouter(opts ...RouterOption) *Router {
if !math.IsNaN(cfg.analyticsRate) {
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
}
cfg.spanOpts = append(cfg.spanOpts, tracer.Measured())
return &Router{
Router: mux.NewRouter(),
config: cfg,
Expand Down
2 changes: 2 additions & 0 deletions contrib/graph-gophers/graphql-go/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (t *Tracer) TraceQuery(ctx context.Context, queryString string, operationNa
tracer.ServiceName(t.cfg.serviceName),
tracer.Tag(tagGraphqlQuery, queryString),
tracer.Tag(tagGraphqlOperationName, operationName),
tracer.Measured(),
}
if !math.IsNaN(t.cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, t.cfg.analyticsRate))
Expand All @@ -71,6 +72,7 @@ func (t *Tracer) TraceField(ctx context.Context, label string, typeName string,
tracer.ServiceName(t.cfg.serviceName),
tracer.Tag(tagGraphqlField, fieldName),
tracer.Tag(tagGraphqlType, typeName),
tracer.Measured(),
}
if !math.IsNaN(t.cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, t.cfg.analyticsRate))
Expand Down
1 change: 1 addition & 0 deletions contrib/julienschmidt/httprouter/httprouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func New(opts ...RouterOption) *Router {
if !math.IsNaN(cfg.analyticsRate) {
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
}
cfg.spanOpts = append(cfg.spanOpts, tracer.Measured())
return &Router{httprouter.New(), cfg}
}

Expand Down
1 change: 1 addition & 0 deletions contrib/labstack/echo/echotrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func Middleware(opts ...Option) echo.MiddlewareFunc {
tracer.SpanType(ext.SpanTypeWeb),
tracer.Tag(ext.HTTPMethod, request.Method),
tracer.Tag(ext.HTTPURL, request.URL.Path),
tracer.Measured(),
}

if !math.IsNaN(cfg.analyticsRate) {
Expand Down
7 changes: 4 additions & 3 deletions contrib/net/http/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ type Option func(*config)
func defaults(cfg *config) {
cfg.analyticsRate = globalconfig.AnalyticsRate()
cfg.serviceName = "http.router"
cfg.spanOpts = []ddtrace.StartSpanOption{tracer.Measured()}
if !math.IsNaN(cfg.analyticsRate) {
cfg.spanOpts = []ddtrace.StartSpanOption{tracer.Tag(ext.EventSampleRate, cfg.analyticsRate)}
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
}
}

Expand All @@ -47,7 +48,7 @@ func WithAnalytics(on bool) MuxOption {
return func(cfg *config) {
if on {
cfg.analyticsRate = 1.0
cfg.spanOpts = []ddtrace.StartSpanOption{tracer.Tag(ext.EventSampleRate, cfg.analyticsRate)}
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
} else {
cfg.analyticsRate = math.NaN()
}
Expand All @@ -60,7 +61,7 @@ func WithAnalyticsRate(rate float64) MuxOption {
return func(cfg *config) {
if rate >= 0.0 && rate <= 1.0 {
cfg.analyticsRate = rate
cfg.spanOpts = []ddtrace.StartSpanOption{tracer.Tag(ext.EventSampleRate, cfg.analyticsRate)}
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
} else {
cfg.analyticsRate = math.NaN()
}
Expand Down
2 changes: 2 additions & 0 deletions contrib/twitchtv/twirp/twirp.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func WrapServer(h http.Handler, opts ...Option) http.Handler {
tracer.ServiceName(cfg.serverServiceName()),
tracer.Tag(ext.HTTPMethod, r.Method),
tracer.Tag(ext.HTTPURL, r.URL.Path),
tracer.Measured(),
}
if !math.IsNaN(cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
Expand Down Expand Up @@ -155,6 +156,7 @@ func requestReceivedHook(cfg *config) func(context.Context) (context.Context, er
opts := []tracer.StartSpanOption{
tracer.SpanType(ext.SpanTypeWeb),
tracer.ServiceName(cfg.serverServiceName()),
tracer.Measured(),
}
if pkg, ok := twirp.PackageName(ctx); ok {
opts = append(opts, tracer.Tag("twirp.package", pkg))
Expand Down
10 changes: 10 additions & 0 deletions ddtrace/tracer/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,16 @@ func StartTime(t time.Time) StartSpanOption {
}
}

// AnalyticsRate sets a custom analytics rate for a span. It decides the percentage
// of events that will be picked up by the App Analytics product. It's represents a
// float64 between 0 and 1 where 0.5 would represent 50% of events.
func AnalyticsRate(rate float64) StartSpanOption {
if math.IsNaN(rate) {
return func(cfg *ddtrace.StartSpanConfig) {}
}
return Tag(ext.EventSampleRate, rate)
}

// FinishOption is a configuration option for FinishSpan. It is aliased in order
// to help godoc group all the functions returning it together. It is considered
// more correct to refer to it as the type as the origin, ddtrace.FinishOption.
Expand Down