Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
fix memory leak cause by the spanStore.(census-instrumentation/opence… (
Browse files Browse the repository at this point in the history
#1246)

* fix memory leak in the spanStore
  • Loading branch information
NanoRed authored Dec 16, 2020
1 parent 61c7a2b commit e736602
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions trace/spanstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (i internalOnly) ReportActiveSpans(name string) []*SpanData {
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())
}
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func (i internalOnly) ReportSpansByLatency(name string, minLatency, maxLatency t
// 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
Expand All @@ -196,7 +196,7 @@ type spanStore struct {
// 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,
}
Expand Down Expand Up @@ -273,15 +273,15 @@ func (s *spanStore) resize(latencyBucketSize int, errorBucketSize int) {
}

// 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()
}

// 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
Expand Down
4 changes: 2 additions & 2 deletions trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func startSpanInternal(name string, hasParent bool, parent SpanContext, remotePa
ss = spanStoreForNameCreateIfNew(name)
if ss != nil {
s.spanStore = ss
ss.add(NewSpan(s))
ss.add(s)
}
}

Expand All @@ -291,7 +291,7 @@ func (s *span) End() {
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 {
Expand Down

0 comments on commit e736602

Please sign in to comment.