/
option.go
76 lines (62 loc) · 1.82 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
package app
import (
"context"
"go.opentelemetry.io/otel/sdk/resource"
"go.uber.org/zap"
"github.com/go-faster/sdk/autometer"
"github.com/go-faster/sdk/autotracer"
)
type options struct {
zapConfig zap.Config
zapOptions []zap.Option
ctx context.Context
meterOptions []autometer.Option
tracerOptions []autotracer.Option
resourceFn func(ctx context.Context) (*resource.Resource, error)
}
type optionFunc func(*options)
func (f optionFunc) apply(o *options) {
f(o)
}
// Option is a functional option for the application.
type Option interface {
apply(o *options)
}
// WithZapConfig sets the default zap config for the application.
func WithZapConfig(cfg zap.Config) Option {
return optionFunc(func(o *options) {
o.zapConfig = cfg
})
}
// WithZapOptions sets additional zap logger options for the application.
func WithZapOptions(opts ...zap.Option) Option {
return optionFunc(func(o *options) {
o.zapOptions = opts
})
}
// WithMeterOptions sets the default autometer options for the application.
func WithMeterOptions(opts ...autometer.Option) Option {
return optionFunc(func(o *options) {
o.meterOptions = opts
})
}
// WithTracerOptions sets the default autotracer options for the application.
func WithTracerOptions(opts ...autotracer.Option) Option {
return optionFunc(func(o *options) {
o.tracerOptions = opts
})
}
// WithContext sets the base context for the application. Background context is used by default.
func WithContext(ctx context.Context) Option {
return optionFunc(func(o *options) {
o.ctx = ctx
})
}
// WithResource sets the function that will be called to retrieve telemetry resource for application.
//
// Defaults to [Resource] function.
func WithResource(fn func(ctx context.Context) (*resource.Resource, error)) Option {
return optionFunc(func(o *options) {
o.resourceFn = fn
})
}