Passed
Pull Request — main (#166)
by Yume
02:23
created

infrastructures.initSentry   A

Complexity

Conditions 1

Size

Total Lines 13
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
dl 0
loc 13
rs 9.75
c 0
b 0
f 0
nop 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A infrastructures.*TracerSingleton.Tracer 0 2 1
A infrastructures.*TracerSingleton.WithConfig 0 3 1
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