-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
267 lines (232 loc) · 6.54 KB
/
config.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
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
// Copyright (c) 2025 Z5Labs and Contributors
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package humus
import (
"bytes"
"context"
_ "embed"
"errors"
"fmt"
"io"
"log/slog"
"os"
"time"
"github.com/z5labs/humus/config"
"github.com/z5labs/humus/internal/detector"
bedrockcfg "github.com/z5labs/bedrock/config"
"go.opentelemetry.io/contrib/instrumentation/runtime"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc"
"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/exporters/stdout/stdoutlog"
"go.opentelemetry.io/otel/log/global"
"go.opentelemetry.io/otel/sdk/log"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
// ConfigSource standardizes the template for configuration of humus applications.
// The [io.Reader] is expected to be YAML with support for Go templating. Currently,
// only 2 template functions are supported:
// - env - this allows environment variables to be substituted into the YAML
// - default - define a default value in case the original value is nil
func ConfigSource(r io.Reader) bedrockcfg.Source {
return bedrockcfg.FromYaml(
bedrockcfg.RenderTextTemplate(
r,
bedrockcfg.TemplateFunc("env", func(key string) any {
v, ok := os.LookupEnv(key)
if ok {
return v
}
return nil
}),
bedrockcfg.TemplateFunc("default", func(def, v any) any {
if v == nil {
return def
}
return v
}),
),
)
}
//go:embed default_config.yaml
var defaultConfig []byte
// DefaultConfig returns the default config source which corresponds to the [Config] type.
func DefaultConfig() bedrockcfg.Source {
return ConfigSource(bytes.NewReader(defaultConfig))
}
// Config defines the common configuration for all humus based applications.
type Config struct {
OTel config.OTel `config:"otel"`
}
// InitializeOTel implements the [appbuilder.OTelInitializer] interface.
func (cfg Config) InitializeOTel(ctx context.Context) error {
conn, err := newClientConn(cfg.OTel.OTLP)
if err != nil {
return err
}
r, err := detectResource(ctx, cfg.OTel.Resource)
if err != nil {
return err
}
initers := []initializer{
traceProviderInitializer{
cfg: cfg.OTel.Trace,
cc: conn,
r: r,
newExporter: otlptracegrpc.New,
},
meterProviderInitializer{
cfg: cfg.OTel.Metric,
cc: conn,
r: r,
newExporter: otlpmetricgrpc.New,
},
logProviderInitializer{
cfg: cfg.OTel.Log,
cc: conn,
r: r,
newExporter: otlploggrpc.New,
},
}
for _, initer := range initers {
err := initer.Init(ctx)
if err != nil {
return err
}
}
return nil
}
func newClientConn(cfg config.OTLP) (*grpc.ClientConn, error) {
if !cfg.Enabled {
return nil, nil
}
return grpc.NewClient(
cfg.Target,
// TODO: support secure transport credentials
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
}
func detectResource(ctx context.Context, cfg config.Resource) (*resource.Resource, error) {
return resource.Detect(
ctx,
detector.TelemetrySDK(),
detector.Host(),
detector.ServiceName(cfg.ServiceName),
detector.ServiceVersion(cfg.ServiceVersion),
)
}
var ErrOTLPMustBeEnabled = errors.New("missing otlp client conn")
type initializer interface {
Init(context.Context) error
}
type traceProviderInitializer struct {
cfg config.Trace
cc *grpc.ClientConn
r *resource.Resource
newExporter func(context.Context, ...otlptracegrpc.Option) (*otlptrace.Exporter, error)
}
func (tpi traceProviderInitializer) Init(ctx context.Context) error {
if !tpi.cfg.Enabled {
return nil
}
if tpi.cc == nil {
return fmt.Errorf("can not enable otel tracing: %w", ErrOTLPMustBeEnabled)
}
exp, err := tpi.newExporter(ctx, otlptracegrpc.WithGRPCConn(tpi.cc))
if err != nil {
return err
}
bsp := trace.NewBatchSpanProcessor(
exp,
trace.WithBatchTimeout(tpi.cfg.BatchTimeout),
)
tp := trace.NewTracerProvider(
trace.WithSpanProcessor(bsp),
trace.WithSampler(trace.TraceIDRatioBased(tpi.cfg.Sampling)),
trace.WithResource(tpi.r),
)
otel.SetTracerProvider(tp)
return nil
}
type meterProviderInitializer struct {
cfg config.Metric
cc *grpc.ClientConn
r *resource.Resource
newExporter func(context.Context, ...otlpmetricgrpc.Option) (*otlpmetricgrpc.Exporter, error)
}
func (mpi meterProviderInitializer) Init(ctx context.Context) error {
if !mpi.cfg.Enabled {
return nil
}
if mpi.cc == nil {
return fmt.Errorf("can not enable otel metering: %w", ErrOTLPMustBeEnabled)
}
exp, err := mpi.newExporter(ctx, otlpmetricgrpc.WithGRPCConn(mpi.cc))
if err != nil {
return err
}
pr := metric.NewPeriodicReader(
exp,
metric.WithInterval(mpi.cfg.ExportInterval),
metric.WithProducer(runtime.NewProducer()),
)
mp := metric.NewMeterProvider(
metric.WithReader(pr),
metric.WithResource(mpi.r),
)
otel.SetMeterProvider(mp)
return runtime.Start(
runtime.WithMinimumReadMemStatsInterval(time.Second),
)
}
type logProviderInitializer struct {
cfg config.Log
cc *grpc.ClientConn
r *resource.Resource
newExporter func(context.Context, ...otlploggrpc.Option) (*otlploggrpc.Exporter, error)
}
func (lpi logProviderInitializer) Init(ctx context.Context) error {
p, err := lpi.initLogProcessor(ctx)
if err != nil {
return err
}
lp := log.NewLoggerProvider(
log.WithProcessor(p),
log.WithResource(lpi.r),
)
global.SetLoggerProvider(lp)
log := Logger("otel")
otel.SetErrorHandler(otel.ErrorHandlerFunc(func(err error) {
log.Error("encoutered error from otel sdk", slog.Any("error", err))
}))
return nil
}
func (lpi logProviderInitializer) initLogProcessor(ctx context.Context) (log.Processor, error) {
// TODO: this needs to be made more specific it should either always be OTLP or STDOUT
// the enabled config is a bit confusing to interpret
if !lpi.cfg.Enabled {
exp, err := stdoutlog.New()
if err != nil {
return nil, err
}
sp := log.NewSimpleProcessor(exp)
return sp, nil
}
if lpi.cc == nil {
return nil, fmt.Errorf("can not enable otel logging: %w", ErrOTLPMustBeEnabled)
}
exp, err := lpi.newExporter(ctx, otlploggrpc.WithGRPCConn(lpi.cc))
if err != nil {
return nil, err
}
bsp := log.NewBatchProcessor(exp)
return bsp, nil
}