-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhumus.go
212 lines (178 loc) · 5.16 KB
/
humus.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
// Copyright (c) 2024 Z5Labs and Contributors
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
// Package humus provides a base config and abstraction for running apps.
package humus
import (
"context"
_ "embed"
"log/slog"
"time"
"go.opentelemetry.io/contrib/bridges/otelslog"
"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/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"
semconv "go.opentelemetry.io/otel/semconv/v1.27.0"
"google.golang.org/grpc"
)
//go:embed default_config.yaml
var DefaultConfig []byte
// OTelConfig
type OTelConfig struct {
ServiceName string `config:"service_name"`
ServiceVersion string `config:"service_version"`
Trace OTelTraceConfig `config:"trace"`
Metric OTelMetricConfig `config:"metric"`
Log OTelLogConfig `config:"log"`
OTLP struct {
Enabled bool `config:"enabled"`
Target string `config:"target"`
} `config:"otlp"`
}
type OTelTraceConfig struct {
Enabled bool `config:"enabled"`
Sampling float64 `config:"sampling"`
BatchTimeout time.Duration `config:"batch_timeout"`
}
type OTelMetricConfig struct {
Enabled bool `config:"enabled"`
ExportInterval time.Duration `config:"export_interval"`
}
type OTelLogConfig struct {
Enabled bool `config:"enabled"`
BatchTimeout time.Duration `config:"batch_timeout"`
}
// InitializeOTel implements the [appbuilder.OTelInitializer] interface.
func (oc OTelConfig) InitializeOTel(ctx context.Context) (err error) {
var conn *grpc.ClientConn
if oc.OTLP.Enabled {
conn, err = grpc.NewClient(oc.OTLP.Target)
if err != nil {
return err
}
}
r, err := resource.Detect(
ctx,
resource.StringDetector(semconv.SchemaURL, semconv.ServiceNameKey, func() (string, error) {
return oc.ServiceName, nil
}),
resource.StringDetector(semconv.SchemaURL, semconv.ServiceVersionKey, func() (string, error) {
return oc.ServiceVersion, nil
}),
)
if err != nil {
return err
}
fs := []func(context.Context, *grpc.ClientConn, *resource.Resource) error{
initTracing(oc.Trace),
initMetering(oc.Metric),
initLogging(oc.Log),
}
for _, f := range fs {
err := f(ctx, conn, r)
if err != nil {
return err
}
}
return nil
}
func initTracing(otc OTelTraceConfig) func(context.Context, *grpc.ClientConn, *resource.Resource) error {
return func(ctx context.Context, cc *grpc.ClientConn, r *resource.Resource) error {
if !otc.Enabled && cc != nil {
return nil
}
exp, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(cc))
if err != nil {
return err
}
bsp := trace.NewBatchSpanProcessor(
exp,
trace.WithBatchTimeout(otc.BatchTimeout),
)
tp := trace.NewTracerProvider(
trace.WithSpanProcessor(bsp),
trace.WithSampler(trace.TraceIDRatioBased(otc.Sampling)),
trace.WithResource(r),
)
otel.SetTracerProvider(tp)
return nil
}
}
func initMetering(omc OTelMetricConfig) func(context.Context, *grpc.ClientConn, *resource.Resource) error {
return func(ctx context.Context, cc *grpc.ClientConn, r *resource.Resource) error {
if !omc.Enabled && cc != nil {
return nil
}
exp, err := otlpmetricgrpc.New(ctx, otlpmetricgrpc.WithGRPCConn(cc))
if err != nil {
return err
}
pr := metric.NewPeriodicReader(
exp,
metric.WithInterval(omc.ExportInterval),
metric.WithProducer(runtime.NewProducer()),
)
mp := metric.NewMeterProvider(
metric.WithReader(pr),
metric.WithResource(r),
)
otel.SetMeterProvider(mp)
return runtime.Start(
runtime.WithMinimumReadMemStatsInterval(time.Second),
)
}
}
func initLogging(olc OTelLogConfig) func(context.Context, *grpc.ClientConn, *resource.Resource) error {
return func(ctx context.Context, cc *grpc.ClientConn, r *resource.Resource) error {
p, err := initLogProcessor(ctx, olc, cc)
if err != nil {
return err
}
lp := log.NewLoggerProvider(
log.WithProcessor(p),
log.WithResource(r),
)
global.SetLoggerProvider(lp)
return nil
}
}
func initLogProcessor(ctx context.Context, olc OTelLogConfig, cc *grpc.ClientConn) (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 !olc.Enabled || cc == nil {
exp, err := stdoutlog.New()
if err != nil {
return nil, err
}
sp := log.NewSimpleProcessor(exp)
return sp, nil
}
exp, err := otlploggrpc.New(ctx, otlploggrpc.WithGRPCConn(cc))
if err != nil {
return nil, err
}
bsp := log.NewBatchProcessor(exp)
return bsp, nil
}
// LoggingConfig
type LoggingConfig struct {
Level slog.Level `config:"level"`
}
// Config
type Config struct {
OTelConfig `config:"otel"`
LoggingConfig `config:"logging"`
}
// Logger
func Logger(name string) *slog.Logger {
return otelslog.NewLogger(name)
}