File tree Expand file tree Collapse file tree 2 files changed +42
-6
lines changed Expand file tree Collapse file tree 2 files changed +42
-6
lines changed Original file line number Diff line number Diff line change @@ -73,21 +73,31 @@ func detectExporter() (sdktrace.SpanExporter, error) {
73
73
return nil , nil
74
74
}
75
75
76
- func detect () error {
77
- tp = trace .NewNoopTracerProvider ()
78
-
76
+ func getExporter () (sdktrace.SpanExporter , error ) {
79
77
exp , err := detectExporter ()
80
78
if err != nil {
81
- return err
79
+ return nil , err
80
+ }
81
+
82
+ if exp != nil {
83
+ exp = & threadSafeExporterWrapper {
84
+ exporter : exp ,
85
+ }
82
86
}
83
87
84
88
if Recorder != nil {
85
89
Recorder .SpanExporter = exp
86
90
exp = Recorder
87
91
}
92
+ return exp , nil
93
+ }
94
+
95
+ func detect () error {
96
+ tp = trace .NewNoopTracerProvider ()
88
97
89
- if exp == nil {
90
- return nil
98
+ exp , err := getExporter ()
99
+ if err != nil || exp == nil {
100
+ return err
91
101
}
92
102
93
103
// enable log with traceID when valid exporter
Original file line number Diff line number Diff line change
1
+ package detect
2
+
3
+ import (
4
+ "context"
5
+ "sync"
6
+
7
+ sdktrace "go.opentelemetry.io/otel/sdk/trace"
8
+ )
9
+
10
+ // threadSafeExporterWrapper wraps an OpenTelemetry SpanExporter and makes it thread-safe.
11
+ type threadSafeExporterWrapper struct {
12
+ mu sync.Mutex
13
+ exporter sdktrace.SpanExporter
14
+ }
15
+
16
+ func (tse * threadSafeExporterWrapper ) ExportSpans (ctx context.Context , spans []sdktrace.ReadOnlySpan ) error {
17
+ tse .mu .Lock ()
18
+ defer tse .mu .Unlock ()
19
+ return tse .exporter .ExportSpans (ctx , spans )
20
+ }
21
+
22
+ func (tse * threadSafeExporterWrapper ) Shutdown (ctx context.Context ) error {
23
+ tse .mu .Lock ()
24
+ defer tse .mu .Unlock ()
25
+ return tse .exporter .Shutdown (ctx )
26
+ }
You can’t perform that action at this time.
0 commit comments