forked from tel-io/tel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrollers.go
More file actions
356 lines (285 loc) · 10.2 KB
/
Copy pathcontrollers.go
File metadata and controls
356 lines (285 loc) · 10.2 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package tel
import (
"context"
"time"
"github.com/go-logr/logr"
"github.com/tel-io/tel/v2/monitoring"
"github.com/tel-io/tel/v2/otlplog/logskd"
"github.com/tel-io/tel/v2/otlplog/otlploggrpc"
"github.com/tel-io/tel/v2/pkg/cardinalitydetector"
"github.com/tel-io/tel/v2/pkg/grpcerr"
"github.com/tel-io/tel/v2/pkg/otelerr"
"github.com/tel-io/tel/v2/pkg/zcore"
sdkmetric "github.com/tel-io/tel/v2/sdk/metric"
sdktrace "github.com/tel-io/tel/v2/sdk/trace"
"go.opentelemetry.io/contrib/instrumentation/host"
rt "go.opentelemetry.io/contrib/instrumentation/runtime"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc/grpclog"
)
// DefaultHistogramBoundaries have been copied from prometheus.DefBuckets.
//
// Note we anticipate the use of a high-precision histogram sketch as
// the standard histogram aggregator for OTLP export.
// (https://github.com/open-telemetry/opentelemetry-specification/issues/982).
var DefaultHistogramBoundaries = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10}
type controllers interface {
apply(context.Context, *Telemetry) func(ctx context.Context)
}
type oLog struct {
res *resource.Resource
}
func withOtelLog(res *resource.Resource) controllers {
return &oLog{res: res}
}
func (o *oLog) apply(ctx context.Context, t *Telemetry) func(context.Context) {
// exporter part
// this initiation controversy SRP, but right now we just speed up our development
opts := []otlploggrpc.Option{
otlploggrpc.WithEndpoint(t.cfg.OtelConfig.Addr),
}
if t.cfg.WithInsecure {
opts = append(opts, otlploggrpc.WithInsecure())
}
if t.cfg.OtelConfig.WithCompression {
opts = append(opts, otlploggrpc.WithCompressor("gzip"))
}
if t.cfg.OtelConfig.IsTLS() {
cred, err := t.cfg.OtelConfig.createClientTLSCredentials()
handleErr(err, "Failed init TLS certificate")
if err == nil {
opts = append(opts, otlploggrpc.WithTLSCredentials(cred))
}
}
if !t.cfg.Logs.EnableRetry {
logRetryOffOpt := otlploggrpc.WithRetry(otlploggrpc.RetryConfig{})
opts = append([]otlploggrpc.Option{logRetryOffOpt}, opts...)
}
logExporter, err := otlploggrpc.New(ctx, o.res, opts...)
handleErr(err, "Failed to create the collector log exporter")
logProvider := logskd.NewBatchLogProcessor(logExporter)
cc := zcore.NewBodyCore(
logProvider,
zap.NewAtomicLevelAt(t.cfg.Level()),
zcore.WithMaxMessageSize(t.cfg.Logs.MaxMessageSize),
zcore.WithSyncInterval(t.cfg.Logs.SyncInterval),
)
logger := zap.L()
if t.cfg.LogEncode == DisableLog {
logger = zap.New(
cc,
zap.WithCaller(true),
zap.AddStacktrace(zapcore.ErrorLevel),
)
} else {
logger = logger.WithOptions(
zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return zapcore.NewTee(core, cc)
}),
)
}
t.Logger = logger.WithOptions(
zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return zcore.NewSampler(
core,
time.Second,
t.cfg.Logs.MaxMessagesPerSecond,
0,
zcore.WithSamplerLevelThresholdString(t.cfg.Logs.MaxLevelMessagesPerSecond),
)
}),
)
zap.ReplaceGlobals(t.Logger)
return func(cxt context.Context) {
_ = logProvider.ForceFlush(ctx)
handleErr(logProvider.Shutdown(cxt), "log provider shutdown")
t.Info("OTEL log provider has been shutdown")
}
}
// user otel.GetTracerProvider() to reach trace
type oTrace struct {
res *resource.Resource
}
func withOtelTrace(res *resource.Resource) controllers {
return &oTrace{res: res}
}
func (o *oTrace) apply(ctx context.Context, t *Telemetry) func(context.Context) {
opts := []otlptracegrpc.Option{otlptracegrpc.WithEndpoint(t.cfg.OtelConfig.Addr)}
if t.cfg.OtelConfig.WithInsecure {
opts = append(opts, otlptracegrpc.WithInsecure())
}
if t.cfg.OtelConfig.WithCompression {
opts = append(opts, otlptracegrpc.WithCompressor("gzip"))
}
if t.cfg.OtelConfig.IsTLS() {
cred, err := t.cfg.OtelConfig.createClientTLSCredentials()
handleErr(err, "Failed init TLS certificate")
if err == nil {
opts = append(opts, otlptracegrpc.WithTLSCredentials(cred))
}
}
if !t.cfg.Traces.EnableRetry {
traceRetryOffOpt := otlptracegrpc.WithRetry(otlptracegrpc.RetryConfig{})
opts = append([]otlptracegrpc.Option{traceRetryOffOpt}, opts...)
}
traceClient := otlptracegrpc.NewClient(opts...)
traceExp, err := otlptrace.New(ctx, traceClient)
handleErr(err, "Failed to create the collector trace exporter")
bsp := tracesdk.NewBatchSpanProcessor(traceExp)
tracerProvider := sdktrace.NewTracerProvider(ctx,
cardinalitydetector.NewOptions(
cardinalitydetector.WithEnable(t.cfg.Traces.CardinalityDetector.Enable),
cardinalitydetector.WithMaxCardinality(t.cfg.Traces.CardinalityDetector.MaxCardinality),
cardinalitydetector.WithMaxInstruments(t.cfg.Traces.CardinalityDetector.MaxInstruments),
cardinalitydetector.WithCheckInterval(t.cfg.Traces.CardinalityDetector.DiagnosticInterval),
),
tracesdk.WithSampler(t.cfg.OtelConfig.Traces.sampler),
tracesdk.WithResource(o.res),
tracesdk.WithSpanProcessor(bsp),
)
// set global propagator to tracecontext (the default is no-op).
otel.SetTextMapPropagator(
propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{}, propagation.Baggage{},
))
otel.SetTracerProvider(tracerProvider)
t.traceProvider = tracerProvider
t.trace = tracerProvider.Tracer(GenServiceName(t.cfg.Namespace, t.cfg.Service) + "_tracer")
return func(cxt context.Context) {
handleErr(tracerProvider.Shutdown(cxt), "trace provider shutdown")
t.Info("OTEL trace provider has been shutdown")
}
}
type oMetric struct {
res *resource.Resource
}
func withOtelMetric(res *resource.Resource) controllers {
return &oMetric{res: res}
}
func (o *oMetric) apply(ctx context.Context, t *Telemetry) func(context.Context) {
opts := []otlpmetricgrpc.Option{otlpmetricgrpc.WithEndpoint(t.cfg.OtelConfig.Addr)}
if t.cfg.OtelConfig.WithInsecure {
opts = append(opts, otlpmetricgrpc.WithInsecure())
}
if t.cfg.OtelConfig.WithCompression {
opts = append(opts, otlpmetricgrpc.WithCompressor("gzip"))
}
if t.cfg.OtelConfig.IsTLS() {
cred, err := t.cfg.OtelConfig.createClientTLSCredentials()
handleErr(err, "Failed init TLS certificate")
if err == nil {
opts = append(opts, otlpmetricgrpc.WithTLSCredentials(cred))
}
}
if !t.cfg.Metrics.EnableRetry {
metricRetryOff := otlpmetricgrpc.WithRetry(otlpmetricgrpc.RetryConfig{})
opts = append([]otlpmetricgrpc.Option{metricRetryOff}, opts...)
}
exp, err := otlpmetricgrpc.New(ctx, opts...)
handleErr(err, "Faild create grpc metric client")
reader := metric.NewPeriodicReader(exp,
//metric.WithTimeout(30*time.Second),
metric.WithInterval(time.Duration(t.cfg.OtelConfig.MetricsPeriodicIntervalSec)*time.Second),
)
var views []metric.View
for _, opt := range t.cfg.OtelConfig.bucketView {
// View to customize histogram buckets and rename a single histogram instrument.
customBucketsView := metric.NewView(
// Match* to match instruments
metric.Instrument{
Name: opt.MetricName,
},
//view.MatchInstrumentationScope(instrumentation.Scope{Name: meterName}),
// With* to modify instruments
metric.Stream{
Aggregation: metric.AggregationExplicitBucketHistogram{
Boundaries: opt.Bucket,
},
},
//view.WithRename("bar"),
)
handleErr(err, "creation histogram view")
views = append(views, customBucketsView)
}
// Default view to keep all instruments
defaultView := metric.NewView(metric.Instrument{Name: "*"}, metric.Stream{})
handleErr(err, "creation default view")
views = append(views, defaultView)
meterProvider := sdkmetric.NewMeterProvider(
ctx,
cardinalitydetector.NewOptions(
cardinalitydetector.WithEnable(t.cfg.Metrics.CardinalityDetector.Enable),
cardinalitydetector.WithMaxCardinality(t.cfg.Metrics.CardinalityDetector.MaxCardinality),
cardinalitydetector.WithMaxInstruments(t.cfg.Metrics.CardinalityDetector.MaxInstruments),
cardinalitydetector.WithCheckInterval(t.cfg.Metrics.CardinalityDetector.DiagnosticInterval),
),
metric.WithReader(reader),
metric.WithResource(o.res),
metric.WithView(views...),
)
otel.SetMeterProvider(meterProvider)
t.metricProvider = meterProvider
// runtime exported
err = rt.Start()
handleErr(err, "Failed to start runtime metric")
// host metrics exporter
err = host.Start()
handleErr(err, "Failed to start host metric")
return func(cxt context.Context) {
// pushes any last exports to the receiver
handleErr(meterProvider.Shutdown(cxt), "metric provider shutdown")
t.Info("OTEL metric provider has been shutdown")
}
}
type oMonitor struct{}
// withMonitor enable monitor system which represent health check with some additional options
// use tel.AddHealthChecker to add health handlers
func withMonitor() controllers {
return &oMonitor{}
}
func (o *oMonitor) apply(ctx context.Context, t *Telemetry) func(context.Context) {
if !t.cfg.MonitorConfig.Enable {
return func(ctx context.Context) {}
}
t.Info("start monitoring", String("addr", t.cfg.MonitorAddr), Bool("debug", t.cfg.Debug))
m := monitoring.NewMon(
monitoring.WithAddr(t.cfg.MonitorAddr),
monitoring.WithDebug(t.cfg.Debug),
monitoring.WithChecker(t.cfg.healthChecker...),
)
go func() {
if err := m.Start(ctx); err != nil {
t.Error("start monitoring", Error(err))
}
}()
return func(cxt context.Context) {
if err := m.GracefulStop(cxt); err != nil {
t.Error("stop monitoring", Error(err))
}
}
}
// log wrapper
type logGrpc struct{}
func withOtelClientLog() controllers { return &logGrpc{} }
func (o *logGrpc) apply(_ context.Context, t *Telemetry) func(context.Context) {
grpclog.SetLoggerV2(grpcerr.New(t.Logger))
return func(context.Context) {}
}
// loggerOtel wrapper
type loggerOtel struct{}
func withOtelProcessor() controllers { return &loggerOtel{} }
func (o *loggerOtel) apply(_ context.Context, t *Telemetry) func(context.Context) {
adapterLog := otelerr.New(t.Logger)
otel.SetErrorHandler(adapterLog)
otel.SetLogger(logr.New(adapterLog))
return func(context.Context) {}
}