Skip to content

Commit

Permalink
contrib: Fixes memory leak for spankind and component (#1618)
Browse files Browse the repository at this point in the history
  • Loading branch information
zarirhamza authored and katiehockman committed Dec 14, 2022
1 parent 90282e2 commit 4639183
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 22 deletions.
4 changes: 2 additions & 2 deletions contrib/gin-gonic/gin/gintrace.go
Expand Up @@ -30,6 +30,8 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
log.Debug("contrib/gin-gonic/gin: Configuring Middleware: Service: %s, %#v", cfg.serviceName, cfg)
spanOpts := []tracer.StartSpanOption{
tracer.ServiceName(cfg.serviceName),
tracer.Tag(ext.Component, "gin-gonic/gin"),
tracer.Tag(ext.SpanKind, ext.SpanKindServer),
}
return func(c *gin.Context) {
if cfg.ignoreRequest(c) {
Expand All @@ -40,8 +42,6 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
opts = append(opts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
}
opts = append(opts, tracer.Tag(ext.HTTPRoute, c.FullPath()))
opts = append(opts, tracer.Tag(ext.Component, "gin-gonic/gin"))
opts = append(opts, tracer.Tag(ext.SpanKind, ext.SpanKindServer))

span, ctx := httptrace.StartRequestSpan(c.Request, opts...)
defer func() {
Expand Down
6 changes: 3 additions & 3 deletions contrib/go-chi/chi.v5/chi.go
Expand Up @@ -29,16 +29,16 @@ func Middleware(opts ...Option) func(next http.Handler) http.Handler {
fn(cfg)
}
log.Debug("contrib/go-chi/chi.v5: Configuring Middleware: %#v", cfg)
spanOpts := append(cfg.spanOpts, tracer.ServiceName(cfg.serviceName))
spanOpts := append(cfg.spanOpts, tracer.ServiceName(cfg.serviceName),
tracer.Tag(ext.Component, "go-chi/chi.v5"),
tracer.Tag(ext.SpanKind, ext.SpanKindServer))
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if cfg.ignoreRequest(r) {
next.ServeHTTP(w, r)
return
}
opts := spanOpts
opts = append(opts, tracer.Tag(ext.Component, "go-chi/chi.v5"))
opts = append(opts, tracer.Tag(ext.SpanKind, ext.SpanKindServer))
if !math.IsNaN(cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
}
Expand Down
6 changes: 3 additions & 3 deletions contrib/go-chi/chi/chi.go
Expand Up @@ -29,16 +29,16 @@ func Middleware(opts ...Option) func(next http.Handler) http.Handler {
fn(cfg)
}
log.Debug("contrib/go-chi/chi: Configuring Middleware: %#v", cfg)
spanOpts := append(cfg.spanOpts, tracer.ServiceName(cfg.serviceName))
spanOpts := append(cfg.spanOpts, tracer.ServiceName(cfg.serviceName),
tracer.Tag(ext.Component, "go-chi/chi"),
tracer.Tag(ext.SpanKind, ext.SpanKindServer))
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if cfg.ignoreRequest(r) {
next.ServeHTTP(w, r)
return
}
opts := spanOpts
opts = append(opts, tracer.Tag(ext.Component, "go-chi/chi"))
opts = append(opts, tracer.Tag(ext.SpanKind, ext.SpanKindServer))
if !math.IsNaN(cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
}
Expand Down
4 changes: 2 additions & 2 deletions contrib/gorilla/mux/mux.go
Expand Up @@ -100,8 +100,6 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
route, _ = match.Route.GetPathTemplate()
}
spanopts = append(spanopts, r.config.spanOpts...)
spanopts = append(spanopts, tracer.Tag(ext.Component, "gorilla/mux"))
spanopts = append(spanopts, tracer.Tag(ext.SpanKind, ext.SpanKindServer))

if r.config.headerTags {
spanopts = append(spanopts, headerTagsFromRequest(req))
Expand All @@ -122,6 +120,8 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// requests and responses served by the router.
func WrapRouter(router *mux.Router, opts ...RouterOption) *Router {
cfg := newConfig(opts)
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.Component, "gorilla/mux"))
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.SpanKind, ext.SpanKindServer))
log.Debug("contrib/gorilla/mux: Configuring Router: %#v", cfg)
return &Router{
Router: router,
Expand Down
7 changes: 4 additions & 3 deletions contrib/julienschmidt/httprouter/httprouter.go
Expand Up @@ -35,6 +35,10 @@ 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.Tag(ext.SpanKind, ext.SpanKindServer))
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.Component, "julienschmidt/httprouter"))

log.Debug("contrib/julienschmidt/httprouter: Configuring Router: %#v", cfg)
return &Router{httprouter.New(), cfg}
}
Expand All @@ -49,9 +53,6 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
resource := req.Method + " " + route

r.config.spanOpts = append(r.config.spanOpts, tracer.Tag(ext.SpanKind, ext.SpanKindServer))
r.config.spanOpts = append(r.config.spanOpts, tracer.Tag(ext.Component, "julienschmidt/httprouter"))

httptrace.TraceAndServe(r.Router, w, req, &httptrace.ServeConfig{
Service: r.config.serviceName,
Resource: resource,
Expand Down
11 changes: 4 additions & 7 deletions contrib/net/http/http.go
Expand Up @@ -10,7 +10,6 @@ import (
"net/http"

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

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
)
Expand All @@ -29,6 +28,8 @@ func NewServeMux(opts ...Option) *ServeMux {
for _, fn := range opts {
fn(cfg)
}
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.SpanKind, ext.SpanKindServer))
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.Component, "net/http"))
log.Debug("contrib/net/http: Configuring ServeMux: %#v", cfg)
return &ServeMux{
ServeMux: http.NewServeMux(),
Expand All @@ -52,9 +53,6 @@ func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
resource = r.Method + " " + route
}

mux.cfg.spanOpts = append(mux.cfg.spanOpts, tracer.Tag(ext.SpanKind, ext.SpanKindServer))
mux.cfg.spanOpts = append(mux.cfg.spanOpts, tracer.Tag(ext.Component, "net/http"))

TraceAndServe(mux.ServeMux, w, r, &ServeConfig{
Service: mux.cfg.serviceName,
Resource: resource,
Expand All @@ -71,6 +69,8 @@ func WrapHandler(h http.Handler, service, resource string, opts ...Option) http.
for _, fn := range opts {
fn(cfg)
}
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.SpanKind, ext.SpanKindServer))
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.Component, "net/http"))
log.Debug("contrib/net/http: Wrapping Handler: Service: %s, Resource: %s, %#v", service, resource, cfg)
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if cfg.ignoreRequest(req) {
Expand All @@ -81,9 +81,6 @@ func WrapHandler(h http.Handler, service, resource string, opts ...Option) http.
resource = r
}

cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.SpanKind, ext.SpanKindServer))
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.Component, "net/http"))

TraceAndServe(h, w, req, &ServeConfig{
Service: service,
Resource: resource,
Expand Down
4 changes: 2 additions & 2 deletions contrib/urfave/negroni/negroni.go
Expand Up @@ -29,8 +29,6 @@ func (m *DatadogMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request, ne
if !math.IsNaN(m.cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, m.cfg.analyticsRate))
}
opts = append(opts, tracer.Tag(ext.Component, "urfave/negroni"))
opts = append(opts, tracer.Tag(ext.SpanKind, ext.SpanKindServer))

span, ctx := httptrace.StartRequestSpan(r, opts...)
defer func() {
Expand Down Expand Up @@ -59,6 +57,8 @@ func Middleware(opts ...Option) *DatadogMiddleware {
for _, fn := range opts {
fn(cfg)
}
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.Component, "urfave/negroni"))
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.SpanKind, ext.SpanKindServer))
log.Debug("contrib/urgave/negroni: Configuring Middleware: %#v", cfg)

m := DatadogMiddleware{
Expand Down

0 comments on commit 4639183

Please sign in to comment.