-
Notifications
You must be signed in to change notification settings - Fork 0
/
otlp.go
149 lines (143 loc) · 4.11 KB
/
otlp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package otlp
import (
"context"
"database/sql"
. "github.com/ZenLiuCN/ote/resource"
otlp "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/sdk/trace"
"log/slog"
"time"
)
type TraceConfig struct {
Endpoint string
Compress string
Insecure sql.NullBool
Reconnect time.Duration
Timeout time.Duration
Retry *otlp.RetryConfig
Headers map[string]string
ExportTimeout time.Duration
ExportBatchSize sql.NullInt32
ExportBatchTimeout time.Duration
QueueSize sql.NullInt32
QueueBlocking sql.NullBool
Sampler *SamplerConfig
*Config
}
type SamplerConfig struct {
Name string
Based string
Ratio sql.NullFloat64
Options []string
}
func NewTraceProvider(ctx context.Context, cfg *TraceConfig) (*trace.TracerProvider, error) {
var opt []otlp.Option
{
opt = append(opt, otlp.WithEndpointURL(cfg.Endpoint))
if d := cfg.Compress; d != "" {
opt = append(opt, otlp.WithCompressor(d))
}
if cfg.Insecure.Valid && cfg.Insecure.Bool {
opt = append(opt, otlp.WithInsecure())
}
if d := cfg.Reconnect; d != time.Duration(0) {
opt = append(opt, otlp.WithReconnectionPeriod(d))
}
if d := cfg.Timeout; d != time.Duration(0) {
opt = append(opt, otlp.WithTimeout(d))
}
if d := cfg.Retry; d != nil {
opt = append(opt, otlp.WithRetry(*d))
}
if d := cfg.Headers; len(d) > 0 {
opt = append(opt, otlp.WithHeaders(d))
}
}
traceExporter, err := otlp.New(ctx, opt...)
if err != nil {
return nil, err
}
var opts []trace.TracerProviderOption
{
//!! batch
{
var spanOpt []trace.BatchSpanProcessorOption
if cfg.ExportTimeout != 0 {
spanOpt = append(spanOpt, trace.WithExportTimeout(cfg.ExportTimeout))
}
if cfg.ExportBatchSize.Valid {
spanOpt = append(spanOpt, trace.WithMaxExportBatchSize(int(cfg.ExportBatchSize.Int32)))
}
if cfg.ExportBatchTimeout != 0 {
spanOpt = append(spanOpt, trace.WithBatchTimeout(cfg.ExportBatchTimeout))
}
if cfg.QueueSize.Valid {
spanOpt = append(spanOpt, trace.WithMaxQueueSize(int(cfg.QueueSize.Int32)))
}
if cfg.QueueBlocking.Valid && cfg.QueueBlocking.Bool {
spanOpt = append(spanOpt, trace.WithBlocking())
}
opts = append(opts, trace.WithBatcher(traceExporter, spanOpt...))
}
//!! resource
{
res, err := ParseResource(ctx, cfg.Config)
if err != nil {
return nil, err
}
opts = append(opts, trace.WithResource(res))
}
//!! sampler
{
if sampler := cfg.Sampler; sampler != nil {
switch sampler.Name {
case "always":
opts = append(opts, trace.WithSampler(trace.AlwaysSample()))
case "never":
opts = append(opts, trace.WithSampler(trace.NeverSample()))
case "parent":
var options []trace.ParentBasedSamplerOption
var sam trace.Sampler
switch sampler.Based {
case "always":
sam = trace.AlwaysSample()
case "ratio":
ratio := 1.0
if sampler.Ratio.Valid {
ratio = sampler.Ratio.Float64
}
sam = trace.TraceIDRatioBased(ratio)
case "never":
sam = trace.NeverSample()
default:
slog.Error("telemetry.oltp.trace.sample.based not one of always|never|ratio, will use never as default",
"based", sampler.Based,
)
sam = trace.NeverSample()
}
for _, s := range sampler.Options {
switch s {
case "withRemote":
options = append(options, trace.WithRemoteParentSampled(sam))
case "withoutRemote":
options = append(options, trace.WithRemoteParentNotSampled(sam))
case "withLocal":
options = append(options, trace.WithLocalParentSampled(sam))
case "withoutLocal":
options = append(options, trace.WithLocalParentNotSampled(sam))
default:
slog.Warn("unknown options", "name", s)
}
}
opts = append(opts, trace.WithSampler(trace.ParentBased(sam, options...)))
default:
slog.Error("sampler.name not one of always|never|parent, will use system default",
"name", sampler.Name,
)
}
}
}
}
traceProvider := trace.NewTracerProvider(opts...)
return traceProvider, nil
}