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

v2: migrating contribs to the new Contrib API (urfave/negroni, valyala/fasthttp) #2813

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
2 changes: 1 addition & 1 deletion contrib/database/sql/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
"github.com/DataDog/dd-trace-go/v2/internal/contrib/options"
"github.com/DataDog/dd-trace-go/v2/instrumentation/options"
"github.com/DataDog/dd-trace-go/v2/internal/log"
)

Expand Down
2 changes: 1 addition & 1 deletion contrib/gin-gonic/gin/gintrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
"github.com/DataDog/dd-trace-go/v2/instrumentation/options"
"github.com/DataDog/dd-trace-go/v2/internal/appsec"
"github.com/DataDog/dd-trace-go/v2/internal/contrib/httptrace"
"github.com/DataDog/dd-trace-go/v2/internal/contrib/options"
"github.com/DataDog/dd-trace-go/v2/internal/log"
"github.com/DataDog/dd-trace-go/v2/internal/telemetry"

Expand Down
2 changes: 1 addition & 1 deletion contrib/go-chi/chi.v5/chi.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (

"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
"github.com/DataDog/dd-trace-go/v2/instrumentation/options"
"github.com/DataDog/dd-trace-go/v2/internal/appsec"
"github.com/DataDog/dd-trace-go/v2/internal/contrib/httptrace"
"github.com/DataDog/dd-trace-go/v2/internal/contrib/options"
"github.com/DataDog/dd-trace-go/v2/internal/log"
"github.com/DataDog/dd-trace-go/v2/internal/telemetry"

Expand Down
2 changes: 1 addition & 1 deletion contrib/go-chi/chi/chi.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (

"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
"github.com/DataDog/dd-trace-go/v2/instrumentation/options"
"github.com/DataDog/dd-trace-go/v2/internal/appsec"
"github.com/DataDog/dd-trace-go/v2/internal/contrib/httptrace"
"github.com/DataDog/dd-trace-go/v2/internal/contrib/options"
"github.com/DataDog/dd-trace-go/v2/internal/log"
"github.com/DataDog/dd-trace-go/v2/internal/telemetry"

Expand Down
2 changes: 1 addition & 1 deletion contrib/gorilla/mux/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
httptrace "github.com/DataDog/dd-trace-go/contrib/net/http/v2"
"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
"github.com/DataDog/dd-trace-go/v2/instrumentation/options"
httptraceinternal "github.com/DataDog/dd-trace-go/v2/internal/contrib/httptrace"
"github.com/DataDog/dd-trace-go/v2/internal/contrib/options"
"github.com/DataDog/dd-trace-go/v2/internal/log"
"github.com/DataDog/dd-trace-go/v2/internal/telemetry"

Expand Down
2 changes: 1 addition & 1 deletion contrib/julienschmidt/httprouter/httprouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
httptrace "github.com/DataDog/dd-trace-go/contrib/net/http/v2"
"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
"github.com/DataDog/dd-trace-go/v2/instrumentation/options"
httptraceinternal "github.com/DataDog/dd-trace-go/v2/internal/contrib/httptrace"
"github.com/DataDog/dd-trace-go/v2/internal/contrib/options"
"github.com/DataDog/dd-trace-go/v2/internal/log"
"github.com/DataDog/dd-trace-go/v2/internal/telemetry"

Expand Down
2 changes: 1 addition & 1 deletion contrib/labstack/echo.v4/echotrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (

"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
"github.com/DataDog/dd-trace-go/v2/instrumentation/options"
"github.com/DataDog/dd-trace-go/v2/internal/contrib/httptrace"
"github.com/DataDog/dd-trace-go/v2/internal/contrib/options"
"github.com/DataDog/dd-trace-go/v2/internal/log"
"github.com/DataDog/dd-trace-go/v2/internal/telemetry"

Expand Down
2 changes: 1 addition & 1 deletion contrib/net/http/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
"net/http"

"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
"github.com/DataDog/dd-trace-go/v2/ddtrace/options"
"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
"github.com/DataDog/dd-trace-go/v2/instrumentation"
"github.com/DataDog/dd-trace-go/v2/instrumentation/appsec/emitter/httpsec"
"github.com/DataDog/dd-trace-go/v2/instrumentation/httptrace"
"github.com/DataDog/dd-trace-go/v2/instrumentation/options"
)

const componentName = "net/http"
Expand Down
18 changes: 9 additions & 9 deletions contrib/urfave/negroni/negroni.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ import (

"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
"github.com/DataDog/dd-trace-go/v2/internal/contrib/httptrace"
"github.com/DataDog/dd-trace-go/v2/internal/contrib/options"
"github.com/DataDog/dd-trace-go/v2/internal/log"
"github.com/DataDog/dd-trace-go/v2/internal/telemetry"
"github.com/DataDog/dd-trace-go/v2/instrumentation"
"github.com/DataDog/dd-trace-go/v2/instrumentation/httptrace"
"github.com/DataDog/dd-trace-go/v2/instrumentation/options"

"github.com/urfave/negroni"
)

const componentName = "urfave/negroni"
const component = instrumentation.PackageUrfaveNegroni

var instr *instrumentation.Instrumentation

func init() {
telemetry.LoadIntegration(componentName)
tracer.MarkIntegrationImported("github.com/urfave/negroni")
instr = instrumentation.Load(instrumentation.PackageUrfaveNegroni)
}

// DatadogMiddleware returns middleware that will trace incoming requests.
Expand Down Expand Up @@ -69,9 +69,9 @@ func Middleware(opts ...Option) *DatadogMiddleware {
for _, fn := range opts {
fn.apply(cfg)
}
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.Component, componentName))
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.Component, component))
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.SpanKind, ext.SpanKindServer))
log.Debug("contrib/urgave/negroni: Configuring Middleware: %#v", cfg)
instr.Logger().Debug("contrib/urgave/negroni: Configuring Middleware: %#v", cfg)

m := DatadogMiddleware{
cfg: cfg,
Expand Down
80 changes: 24 additions & 56 deletions contrib/urfave/negroni/negroni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ import (
"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
"github.com/DataDog/dd-trace-go/v2/ddtrace/mocktracer"
"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
"github.com/DataDog/dd-trace-go/v2/internal/contrib/namingschematest"
"github.com/DataDog/dd-trace-go/v2/internal/globalconfig"
"github.com/DataDog/dd-trace-go/v2/internal/normalizer"
"github.com/DataDog/dd-trace-go/v2/instrumentation"
"github.com/DataDog/dd-trace-go/v2/instrumentation/testutils"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/urfave/negroni"
)

Expand Down Expand Up @@ -68,72 +66,74 @@ func TestWithHeaderTags(t *testing.T) {
t.Run("default-off", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
htArgs := []string{"[email protected]*r", "2header", "3header", "x-datadog-header"}
headerTags := instrumentation.NewHeaderTags([]string{"[email protected]*r", "2header", "3header", "x-datadog-header"})
setupReq()
spans := mt.FinishedSpans()
assert := assert.New(t)
assert.Equal(len(spans), 1)
s := spans[0]
for _, arg := range htArgs {
_, tag := normalizer.HeaderTag(arg)
headerTags.Iter(func(header string, tag string) {
assert.NotContains(s.Tags(), tag)
}
})
})
t.Run("integration", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()

htArgs := []string{"[email protected]*r", "2header:tag"}
headerTags := instrumentation.NewHeaderTags(htArgs)

r := setupReq(WithHeaderTags(htArgs))
spans := mt.FinishedSpans()
assert := assert.New(t)
assert.Equal(len(spans), 1)
s := spans[0]

for _, arg := range htArgs {
header, tag := normalizer.HeaderTag(arg)
headerTags.Iter(func(header string, tag string) {
assert.Equal(strings.Join(r.Header.Values(header), ","), s.Tags()[tag])
}
})
assert.NotContains(s.Tags(), "http.headers.x-datadog-header")
})

t.Run("global", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()

header, tag := normalizer.HeaderTag("3header")
globalconfig.SetHeaderTag(header, tag)
htArgs := []string{"3header"}
testutils.SetGlobalHeaderTags(t, htArgs...)
headerTags := instrumentation.NewHeaderTags(htArgs)

r := setupReq()
spans := mt.FinishedSpans()
assert := assert.New(t)
assert.Equal(len(spans), 1)
s := spans[0]

assert.Equal(strings.Join(r.Header.Values(header), ","), s.Tags()[tag])
headerTags.Iter(func(header string, tag string) {
assert.Equal(strings.Join(r.Header.Values(header), ","), s.Tags()[tag])
})
assert.NotContains(s.Tags(), "http.headers.x-datadog-header")
})

t.Run("override", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()

globalH, globalT := normalizer.HeaderTag("3header")
globalconfig.SetHeaderTag(globalH, globalT)

testutils.SetGlobalHeaderTags(t, "3header")
htArgs := []string{"[email protected]*r", "2header:tag"}
headerTags := instrumentation.NewHeaderTags(htArgs)

r := setupReq(WithHeaderTags(htArgs))
spans := mt.FinishedSpans()
assert := assert.New(t)
assert.Equal(len(spans), 1)
s := spans[0]

for _, arg := range htArgs {
header, tag := normalizer.HeaderTag(arg)
headerTags.Iter(func(header string, tag string) {
assert.Equal(strings.Join(r.Header.Values(header), ","), s.Tags()[tag])
}
})
assert.NotContains(s.Tags(), "http.headers.x-datadog-header")
assert.NotContains(s.Tags(), globalT)
assert.NotContains(s.Tags(), "3header")
})
}

Expand Down Expand Up @@ -371,10 +371,7 @@ func TestAnalyticsSettings(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()

rate := globalconfig.AnalyticsRate()
defer globalconfig.SetAnalyticsRate(rate)
globalconfig.SetAnalyticsRate(0.4)

testutils.SetGlobalAnalyticsRate(t, 0.4)
assertRate(t, mt, 0.4)
})

Expand All @@ -394,9 +391,7 @@ func TestAnalyticsSettings(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()

rate := globalconfig.AnalyticsRate()
defer globalconfig.SetAnalyticsRate(rate)
globalconfig.SetAnalyticsRate(0.4)
testutils.SetGlobalAnalyticsRate(t, 0.4)
assertRate(t, mt, 0.23, WithAnalyticsRate(0.23))
})
}
Expand Down Expand Up @@ -440,8 +435,7 @@ func TestServiceName(t *testing.T) {
})

t.Run("global", func(t *testing.T) {
globalconfig.SetServiceName("global-service")
defer globalconfig.SetServiceName("")
testutils.SetGlobalServiceName(t, "global-service")

mt := mocktracer.Start()
defer mt.Stop()
Expand All @@ -460,29 +454,3 @@ func TestServiceName(t *testing.T) {
assertServiceName(t, mt, router, "my-service")
})
}

func TestNamingSchema(t *testing.T) {
genSpans := namingschematest.GenSpansFn(func(t *testing.T, serviceOverride string) []*mocktracer.Span {
var opts []Option
if serviceOverride != "" {
opts = append(opts, WithService(serviceOverride))
}
mt := mocktracer.Start()
defer mt.Stop()

mux := http.NewServeMux()
mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("ok"))
require.NoError(t, err)
})
router := negroni.New()
router.Use(Middleware(opts...))
router.UseHandler(mux)
r := httptest.NewRequest("GET", "/200", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)

return mt.FinishedSpans()
})
namingschematest.NewHTTPServerTest(genSpans, "negroni.router")(t)
}
23 changes: 8 additions & 15 deletions contrib/urfave/negroni/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,16 @@ import (
"net/http"

"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
"github.com/DataDog/dd-trace-go/v2/internal"
"github.com/DataDog/dd-trace-go/v2/internal/globalconfig"
"github.com/DataDog/dd-trace-go/v2/internal/namingschema"
"github.com/DataDog/dd-trace-go/v2/internal/normalizer"
"github.com/DataDog/dd-trace-go/v2/instrumentation"
)

const defaultServiceName = "negroni.router"

type config struct {
serviceName string
spanOpts []tracer.StartSpanOption // additional span options to be applied
analyticsRate float64
isStatusError func(statusCode int) bool
resourceNamer func(r *http.Request) string
headerTags *internal.LockMap
headerTags instrumentation.HeaderTags
}

// Option describes options for the Negroni integration.
Expand All @@ -41,13 +36,12 @@ func (fn OptionFn) apply(cfg *config) {
}

func defaults(cfg *config) {
cfg.serviceName = namingschema.ServiceName(defaultServiceName)
if internal.BoolEnv("DD_TRACE_NEGRONI_ANALYTICS_ENABLED", false) {
cfg.analyticsRate = 1.0
} else {
cfg.analyticsRate = globalconfig.AnalyticsRate()
cfg.serviceName = instr.ServiceName(instrumentation.ComponentServer, nil)
cfg.analyticsRate = instr.AnalyticsRate()
if math.IsNaN(cfg.analyticsRate) {
cfg.analyticsRate = instr.GlobalAnalyticsRate()
}
cfg.headerTags = globalconfig.HeaderTagMap()
cfg.headerTags = instr.HTTPHeadersAsTags()
cfg.isStatusError = isServerError
cfg.resourceNamer = defaultResourceNamer
}
Expand Down Expand Up @@ -119,8 +113,7 @@ func defaultResourceNamer(_ *http.Request) string {
// Using this feature can risk exposing sensitive data such as authorization tokens to Datadog.
// Special headers can not be sub-selected. E.g., an entire Cookie header would be transmitted, without the ability to choose specific Cookies.
func WithHeaderTags(headers []string) OptionFn {
headerTagsMap := normalizer.HeaderTagSlice(headers)
return func(cfg *config) {
cfg.headerTags = internal.NewLockMap(headerTagsMap)
cfg.headerTags = instrumentation.NewHeaderTags(headers)
}
}
16 changes: 8 additions & 8 deletions contrib/valyala/fasthttp/fasthttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ import (

"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
"github.com/DataDog/dd-trace-go/v2/internal/contrib/fasthttptrace"
"github.com/DataDog/dd-trace-go/v2/internal/log"
"github.com/DataDog/dd-trace-go/v2/internal/telemetry"
"github.com/DataDog/dd-trace-go/v2/instrumentation"
"github.com/DataDog/dd-trace-go/v2/instrumentation/fasthttptrace"
)

const componentName = "valyala/fasthttp.v1"
const component = instrumentation.PackageValyalaFastHTTP

var instr *instrumentation.Instrumentation

func init() {
telemetry.LoadIntegration(componentName)
tracer.MarkIntegrationImported(componentName)
instr = instrumentation.Load(instrumentation.PackageValyalaFastHTTP)
}

// WrapHandler wraps a fasthttp.RequestHandler with tracing middleware
Expand All @@ -32,7 +32,7 @@ func WrapHandler(h fasthttp.RequestHandler, opts ...Option) fasthttp.RequestHand
for _, fn := range opts {
fn.apply(cfg)
}
log.Debug("contrib/valyala/fasthttp.v1: Configuring Middleware: cfg: %#v", cfg)
instr.Logger().Debug("contrib/valyala/fasthttp.v1: Configuring Middleware: cfg: %#v", cfg)
spanOpts := []tracer.StartSpanOption{
tracer.ServiceName(cfg.serviceName),
}
Expand Down Expand Up @@ -62,7 +62,7 @@ func WrapHandler(h fasthttp.RequestHandler, opts ...Option) fasthttp.RequestHand

func defaultSpanOptions(fctx *fasthttp.RequestCtx) []tracer.StartSpanOption {
opts := []tracer.StartSpanOption{
tracer.Tag(ext.Component, componentName),
tracer.Tag(ext.Component, component),
tracer.Tag(ext.SpanKind, ext.SpanKindServer),
tracer.SpanType(ext.SpanTypeWeb),
tracer.Tag(ext.HTTPMethod, string(fctx.Method())),
Expand Down
2 changes: 1 addition & 1 deletion contrib/valyala/fasthttp/fasthttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestTrace200(t *testing.T) {
assert.Equal("200", span.Tag(ext.HTTPCode))
assert.Equal("GET", span.Tag(ext.HTTPMethod))
assert.Equal(addr+"/any", span.Tag(ext.HTTPURL))
assert.Equal(componentName, span.Tag(ext.Component))
assert.Equal(string(component), span.Tag(ext.Component))
assert.Equal(ext.SpanKindServer, span.Tag(ext.SpanKind))
}

Expand Down
Loading
Loading