1
|
|
|
package infrastructures |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"sync" |
5
|
|
|
|
6
|
|
|
"github.com/getsentry/sentry-go" |
7
|
|
|
sentryotel "github.com/getsentry/sentry-go/otel" |
8
|
|
|
"go.opentelemetry.io/otel" |
9
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace" |
10
|
|
|
"go.opentelemetry.io/otel/trace" |
11
|
|
|
) |
12
|
|
|
|
13
|
|
|
var ( |
14
|
|
|
once sync.Once //nolint:gochecknoglobals //Singleton |
15
|
|
|
instance *TracerSingleton //nolint:gochecknoglobals //Singleton |
16
|
|
|
) |
17
|
|
|
|
18
|
|
|
type TracerSingleton struct { |
19
|
|
|
tracer trace.Tracer |
20
|
|
|
config SentryConfig |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
// SentryConfig holds the configuration for the sentry client. |
24
|
|
|
type SentryConfig struct { |
25
|
|
|
Environment string |
26
|
|
|
Release string |
27
|
|
|
DSN string |
28
|
|
|
Name string |
29
|
|
|
TracesSampleRate float64 |
30
|
|
|
ProfilesSampleRate float64 |
31
|
|
|
Debug bool |
32
|
|
|
WithStacktrace bool |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
func GetTracerInstance() *TracerSingleton { |
36
|
|
|
once.Do(func() { |
37
|
|
|
instance = &TracerSingleton{} |
38
|
|
|
}) |
39
|
|
|
return instance |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
func NewTracerInstance(cfg SentryConfig) *TracerSingleton { |
43
|
|
|
return GetTracerInstance().WithConfig(cfg) |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
func (t *TracerSingleton) WithConfig(cfg SentryConfig) *TracerSingleton { |
47
|
|
|
t.config = cfg |
48
|
|
|
return t |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
func (t *TracerSingleton) Tracer() trace.Tracer { |
52
|
|
|
return t.tracer |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
func (t *TracerSingleton) ConnectTracer() error { |
56
|
|
|
t.tracer = otel.Tracer(t.config.Name) |
57
|
|
|
|
58
|
|
|
_ = sentry.Init(sentry.ClientOptions{ |
59
|
|
|
Dsn: t.config.DSN, |
60
|
|
|
Debug: t.config.Debug, |
61
|
|
|
AttachStacktrace: t.config.WithStacktrace, |
62
|
|
|
Environment: t.config.Environment, |
63
|
|
|
Release: t.config.Release, |
64
|
|
|
EnableTracing: true, |
65
|
|
|
TracesSampleRate: t.config.TracesSampleRate, |
66
|
|
|
ProfilesSampleRate: t.config.ProfilesSampleRate, |
67
|
|
|
}) |
68
|
|
|
|
69
|
|
|
tp := sdktrace.NewTracerProvider( |
70
|
|
|
sdktrace.WithSpanProcessor(sentryotel.NewSentrySpanProcessor()), |
71
|
|
|
) |
72
|
|
|
|
73
|
|
|
otel.SetTracerProvider(tp) |
74
|
|
|
otel.SetTextMapPropagator(sentryotel.NewSentryPropagator()) |
75
|
|
|
|
76
|
|
|
return nil |
77
|
|
|
} |
78
|
|
|
|