Skip to content

Commit

Permalink
Solve panic due to concurrent access to ExportSpans
Browse files Browse the repository at this point in the history
Signed-off-by: Gahl Saraf <saraf.gahl@gmail.com>
  • Loading branch information
gsaraf committed Dec 4, 2022
1 parent 7615120 commit 8c7c731
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions util/tracing/detect/detect.go
Expand Up @@ -84,6 +84,10 @@ func detect() error {
return nil
}

exp = &threadSafeExporter{
exporter: exp,
}

// enable log with traceID when valid exporter
bklog.EnableLogWithTraceID(true)

Expand Down
25 changes: 25 additions & 0 deletions util/tracing/detect/threadsafe.go
@@ -0,0 +1,25 @@
package detect

import (
"context"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"sync"
)

// The SpanExporter is expected to be called from a synchronous context, and if called from different routines can panic.
type threadSafeExporter struct {
mut sync.Mutex
exporter sdktrace.SpanExporter
}

func (tse *threadSafeExporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadOnlySpan) error {
tse.mut.Lock()
defer tse.mut.Unlock()
return tse.exporter.ExportSpans(ctx, spans)
}

func (tse *threadSafeExporter) Shutdown(ctx context.Context) error {
tse.mut.Lock()
defer tse.mut.Unlock()
return tse.exporter.Shutdown(ctx)
}

0 comments on commit 8c7c731

Please sign in to comment.