fix memory leak cause by the spanStore.(census-instrumentation/openceā¦ (#1246)
* fix memory leak in the spanStore
diff --git a/trace/spanstore.go b/trace/spanstore.go
index bdcc8db..e601f76 100644
--- a/trace/spanstore.go
+++ b/trace/spanstore.go
@@ -49,7 +49,7 @@
s.mu.Lock()
defer s.mu.Unlock()
for activeSpan := range s.active {
- if s, ok := activeSpan.internal.(*span); ok {
+ if s, ok := activeSpan.(*span); ok {
out = append(out, s.makeSpanData())
}
}
@@ -187,7 +187,7 @@
// bucketed by latency.
type spanStore struct {
mu sync.Mutex // protects everything below.
- active map[*Span]struct{}
+ active map[SpanInterface]struct{}
errors map[int32]*bucket
latency []bucket
maxSpansPerErrorBucket int
@@ -196,7 +196,7 @@
// newSpanStore creates a span store.
func newSpanStore(name string, latencyBucketSize int, errorBucketSize int) *spanStore {
s := &spanStore{
- active: make(map[*Span]struct{}),
+ active: make(map[SpanInterface]struct{}),
latency: make([]bucket, len(defaultLatencies)+1),
maxSpansPerErrorBucket: errorBucketSize,
}
@@ -273,7 +273,7 @@
}
// add adds a span to the active bucket of the spanStore.
-func (s *spanStore) add(span *Span) {
+func (s *spanStore) add(span SpanInterface) {
s.mu.Lock()
s.active[span] = struct{}{}
s.mu.Unlock()
@@ -281,7 +281,7 @@
// finished removes a span from the active set, and adds a corresponding
// SpanData to a latency or error bucket.
-func (s *spanStore) finished(span *Span, sd *SpanData) {
+func (s *spanStore) finished(span SpanInterface, sd *SpanData) {
latency := sd.EndTime.Sub(sd.StartTime)
if latency < 0 {
latency = 0
diff --git a/trace/trace.go b/trace/trace.go
index 58217d1..861df9d 100644
--- a/trace/trace.go
+++ b/trace/trace.go
@@ -266,7 +266,7 @@
ss = spanStoreForNameCreateIfNew(name)
if ss != nil {
s.spanStore = ss
- ss.add(NewSpan(s))
+ ss.add(s)
}
}
@@ -291,7 +291,7 @@
sd := s.makeSpanData()
sd.EndTime = internal.MonotonicEndTime(sd.StartTime)
if s.spanStore != nil {
- s.spanStore.finished(NewSpan(s), sd)
+ s.spanStore.finished(s, sd)
}
if mustExport {
for e := range exp {