-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
107 lines (92 loc) · 3.02 KB
/
option.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
// Copyright (c) 2024 The nilgo authors
// Use of this source code is governed by a MIT license found in the LICENSE file.
package nilgo
import (
"context"
"log/slog"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)
// WithPreRun provides runs to execute before the main runs provided in Runner.Run.
//
// It's guaranteed that all goroutines for pre-runs start before the main runs start,
// and end after the main runs end if it's blocking with [context.Context.Done].
func WithPreRun(runs ...func(context.Context) error) Option {
return func(options *options) {
options.preRuns = append(options.preRuns, runs...)
}
}
// WithPostRun provides runs to execute after the main runs provided in Runner.Run.
func WithPostRun(runs ...func(context.Context) error) Option {
return func(options *options) {
options.postRuns = append(options.postRuns, runs...)
}
}
// WithStartGate provides gates to block the start of main runs provided in Runner.Run,
// until all start gates returns without error.
//
// All start gates must return in limited time to avoid blocking the main runs.
func WithStartGate(gates ...func(context.Context) error) Option {
return func(options *options) {
options.startGates = append(options.startGates, gates...)
}
}
// WithStopGate provides gates to block the stop of main runs provided in Runner.Run,
// until all stop gates returns.
//
// All stop gates must return in limited time to avoid blocking the main runs.
func WithStopGate(gates ...func(context.Context) error) Option {
return func(options *options) {
options.stopGates = append(options.stopGates, gates...)
}
}
// WithLogger provides a slog.Logger to handle logs.
func WithLogger(logger *slog.Logger) Option {
return func(*options) {
slog.SetDefault(logger)
slog.Info("Logger has been initialized.")
}
}
// WithTraceProvider provides OpenTelemetry trace provider.
func WithTraceProvider(provider trace.TracerProvider) Option {
return func(options *options) {
WithPreRun(func(context.Context) error {
otel.SetTracerProvider(provider)
otel.SetTextMapPropagator(
propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
propagation.Baggage{},
),
)
slog.Info("Trace provider has been initialized.")
return nil
})(options)
if provider, ok := provider.(interface {
Shutdown(ctx context.Context) error
}); ok {
WithPostRun(provider.Shutdown)(options)
}
}
}
// WithMeterProvider provides OpenTelemetry metric provider.
func WithMeterProvider(provider metric.MeterProvider) Option {
return func(options *options) {
WithPreRun(func(context.Context) error {
otel.SetMeterProvider(provider)
slog.Info("Meter provider has been initialized.")
return nil
})(options)
if provider, ok := provider.(interface {
Shutdown(ctx context.Context) error
}); ok {
WithPostRun(provider.Shutdown)(options)
}
}
}
type (
// Option configures the Runner with specific options.
Option func(*options)
options Runner
)