From 900cc477725e6afb514911c07678fe3c58111d90 Mon Sep 17 00:00:00 2001 From: JT Olio Date: Thu, 13 Feb 2020 03:16:31 -0700 Subject: [PATCH] traces: fix memory leak for long running traces that aren't being collected Change-Id: I7576e5f420e83c52c5bdb65a30f68aa8ee3d3cc8 --- go.mod | 2 +- go.sum | 8 ++------ pkg/traces/traces.go | 22 ++++++++++++---------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index 10351c6ee..2329a1040 100644 --- a/go.mod +++ b/go.mod @@ -51,7 +51,7 @@ require ( github.com/jtolds/gls v4.2.1+incompatible // indirect github.com/jtolds/go-luar v0.0.0-20170419063437-0786921db8c0 github.com/jtolds/monkit-hw/v2 v2.0.0-20191108235325-141a0da276b3 - github.com/jtolds/tracetagger/v2 v2.0.0-rc0 + github.com/jtolds/tracetagger/v2 v2.0.0-rc2 github.com/klauspost/cpuid v0.0.0-20180405133222-e7e905edc00e // indirect github.com/klauspost/reedsolomon v0.0.0-20180704173009-925cb01d6510 // indirect github.com/lib/pq v1.3.0 diff --git a/go.sum b/go.sum index 01e254a31..d3406daa6 100644 --- a/go.sum +++ b/go.sum @@ -232,8 +232,8 @@ github.com/jtolds/go-luar v0.0.0-20170419063437-0786921db8c0 h1:UyVaeqfY1fLPMt1i github.com/jtolds/go-luar v0.0.0-20170419063437-0786921db8c0/go.mod h1:OtVLEpPHGJkn8jgGrHlYELCA3uXLU0YSfNN0faeDM2M= github.com/jtolds/monkit-hw/v2 v2.0.0-20191108235325-141a0da276b3 h1:dITCBge70U9RDyZUL/Thn/yAT/ct4Rz40mNUX51dFCk= github.com/jtolds/monkit-hw/v2 v2.0.0-20191108235325-141a0da276b3/go.mod h1:eo5po8nCwRcvZIIR8eGi7PKthzXuunpXzUmXzxCBfBc= -github.com/jtolds/tracetagger/v2 v2.0.0-rc0 h1:ND7ncC1SZyJ97ALL2QsKN5whzvRGOeXv0cu8PaCYttA= -github.com/jtolds/tracetagger/v2 v2.0.0-rc0/go.mod h1:EeqZ0b80feEwRK+FutrQ/GJxruNqamJBKOezZVfzSxA= +github.com/jtolds/tracetagger/v2 v2.0.0-rc2 h1:pxmt5o+WRYffMwMYuFlI3wKwYbPVAyiX8EEyM8a/U7o= +github.com/jtolds/tracetagger/v2 v2.0.0-rc2/go.mod h1:EeqZ0b80feEwRK+FutrQ/GJxruNqamJBKOezZVfzSxA= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= @@ -609,9 +609,5 @@ storj.io/common v0.0.0-20200210174421-a3a30fc4862d/go.mod h1:n4sxosrQlw5iDBlbTdB storj.io/drpc v0.0.7-0.20191115031725-2171c57838d2/go.mod h1:/ascUDbzNAv0A3Jj7wUIKFBH2JdJ2uJIBO/b9+2yHgQ= storj.io/drpc v0.0.8 h1:wu68cMmtoT0vSWIAZz29RpJkWdi4o0S8BIrLslpH5FQ= storj.io/drpc v0.0.8/go.mod h1:v39uWro/EbXXk+gNnrM9FQuVVS2zUBWBfeduydgeXUA= -storj.io/uplink v0.0.0-20200131143957-f3b6350b4add h1:/G2nFSq00KxD/9tQIpNJA2vRGkaSHdZvi1ZsX4qAgbQ= -storj.io/uplink v0.0.0-20200131143957-f3b6350b4add/go.mod h1:sqhw1H30vF8pD6coKYkx0nLAkw9M0ISwjLi+3R5b3A8= -storj.io/uplink v0.0.0-20200210111421-04761b3aaccd h1:jjOXtv0L9sd/i5UW0qsaMjFDA857z0BTBIsTIhImumk= -storj.io/uplink v0.0.0-20200210111421-04761b3aaccd/go.mod h1:sqhw1H30vF8pD6coKYkx0nLAkw9M0ISwjLi+3R5b3A8= storj.io/uplink v0.0.0-20200211130624-1f304dca3b7d h1:Co/Sa2VflIZ4tHk+eLj8vSES/ciksSHILX9+fuTGtAQ= storj.io/uplink v0.0.0-20200211130624-1f304dca3b7d/go.mod h1:sqhw1H30vF8pD6coKYkx0nLAkw9M0ISwjLi+3R5b3A8= diff --git a/pkg/traces/traces.go b/pkg/traces/traces.go index 7dc686650..75a1d6cd1 100644 --- a/pkg/traces/traces.go +++ b/pkg/traces/traces.go @@ -10,24 +10,22 @@ import ( "os" "path/filepath" - "github.com/jtolds/tracetagger/v2" - "github.com/spacemonkeygo/monkit/v3" + tracetagger "github.com/jtolds/tracetagger/v2" + monkit "github.com/spacemonkeygo/monkit/v3" "github.com/spacemonkeygo/monkit/v3/collect" ) -// TraceTag is a type to keep track of different types of tags for traces -type TraceTag int - -const ( +var ( // TagDB tags a trace as hitting the database - TagDB TraceTag = 1 + TagDB = tracetagger.NewTagRef() ) // CollectTraces starts storing all known tagged traces on disk, until cancel // is called func CollectTraces() (cancel func()) { path := filepath.Join(os.TempDir(), "storj-traces", fmt.Sprint(os.Getpid())) - return tracetagger.TracesWithTag(TagDB, 10000, func(spans []*collect.FinishedSpan, capped bool) { + disable := TagDB.Enable() + cancelCollect := tracetagger.TracesWithTag(TagDB, 10000, func(spans []*collect.FinishedSpan, capped bool) { name := tracetagger.TracePathPrefix(spans, capped) err := tracetagger.SaveTrace(spans, capped, filepath.Join(path, "unfiltered", name)) if err != nil { @@ -39,14 +37,18 @@ func CollectTraces() (cancel func()) { log.Print(err) } }) + return func() { + cancelCollect() + disable() + } } // Tag tags a trace with the given tag -func Tag(ctx context.Context, tag TraceTag) { +func Tag(ctx context.Context, tag *tracetagger.TagRef) { tracetagger.Tag(ctx, tag) } // TagScope tags all functions on an entire monkit Scope with a given tag -func TagScope(tag TraceTag, scope *monkit.Scope) { +func TagScope(tag *tracetagger.TagRef, scope *monkit.Scope) { tracetagger.TagScope(tag, scope) }