Skip to content

Commit c52b31a

Browse files
authored
feat: simplify kod core (#264)
1 parent d2b9ee9 commit c52b31a

17 files changed

+144
-587
lines changed

example_test.go

+33-27
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package kod_test
33
import (
44
"context"
55
"fmt"
6+
"log/slog"
67
"testing"
78

89
"github.com/go-kod/kod"
@@ -11,10 +12,10 @@ import (
1112
"github.com/go-kod/kod/interceptor/kmetric"
1213
"github.com/go-kod/kod/interceptor/krecovery"
1314
"github.com/go-kod/kod/interceptor/ktrace"
14-
lognoop "go.opentelemetry.io/otel/log/noop"
15-
metricnoop "go.opentelemetry.io/otel/metric/noop"
16-
"go.opentelemetry.io/otel/propagation"
17-
tracenoop "go.opentelemetry.io/otel/trace/noop"
15+
"github.com/knadh/koanf/v2"
16+
"go.opentelemetry.io/otel"
17+
"go.opentelemetry.io/otel/sdk/trace"
18+
"go.opentelemetry.io/otel/sdk/trace/tracetest"
1819
"go.uber.org/mock/gomock"
1920
)
2021

@@ -112,14 +113,15 @@ func Example_configGlobal() {
112113
// This example demonstrates how to use logging with OpenTelemetry.
113114
func Example_openTelemetryLog() {
114115
logger, observer := kod.NewTestLogger()
116+
slog.SetDefault(logger)
115117

116118
kod.RunTest(&testing.T{}, func(ctx context.Context, app *helloworld.App) {
117119
app.L(ctx).Debug("Hello, World!")
118120
app.L(ctx).Info("Hello, World!")
119121
app.L(ctx).Warn("Hello, World!")
120122
app.L(ctx).Error("Hello, World!")
121123
app.HelloWorld.Get().SayHello(ctx)
122-
}, kod.WithLogger(logger))
124+
})
123125

124126
fmt.Println(observer.RemoveKeys("trace_id", "span_id", "time"))
125127

@@ -136,6 +138,12 @@ func Example_openTelemetryLog() {
136138
// This example demonstrates how to use tracing with OpenTelemetry.
137139
func Example_openTelemetryTrace() {
138140
logger, observer := kod.NewTestLogger()
141+
slog.SetDefault(logger)
142+
143+
// create otel test exporter
144+
spanRecorder := tracetest.NewSpanRecorder()
145+
tracerProvider := trace.NewTracerProvider(trace.WithSpanProcessor(spanRecorder))
146+
otel.SetTracerProvider(tracerProvider)
139147

140148
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
141149
ctx, span := app.Tracer().Start(ctx, "example")
@@ -145,7 +153,7 @@ func Example_openTelemetryTrace() {
145153

146154
app.HelloWorld.Get().SayHello(ctx)
147155
return nil
148-
}, kod.WithInterceptors(ktrace.Interceptor()), kod.WithLogger(logger))
156+
}, kod.WithInterceptors(ktrace.Interceptor()))
149157

150158
fmt.Println(observer.Filter(func(m map[string]any) bool {
151159
return m["trace_id"] != nil && m["span_id"] != nil
@@ -174,25 +182,6 @@ func Example_openTelemetryMetric() {
174182
// helloWorld shutdown
175183
}
176184

177-
// This example demonstrates how to use OpenTelemetry with a custom OpenTelemetry provider.
178-
func Example_openTelemetryCustomProvider() {
179-
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
180-
app.HelloWorld.Get().SayHello(ctx)
181-
return nil
182-
},
183-
kod.WithTracerProvider(tracenoop.NewTracerProvider()),
184-
kod.WithMeterProvider(metricnoop.NewMeterProvider()),
185-
kod.WithLogProvider(lognoop.NewLoggerProvider()),
186-
kod.WithTextMapPropagator(propagation.NewCompositeTextMapPropagator(
187-
propagation.TraceContext{}, propagation.Baggage{},
188-
)),
189-
)
190-
// Output:
191-
// helloWorld init
192-
// Hello, World!
193-
// helloWorld shutdown
194-
}
195-
196185
// This example demonstrates how to use [kod.WithInterceptors] to provide global interceptors to the application.
197186
func Example_interceptorGlobal() {
198187
interceptor := interceptor.Interceptor(func(ctx context.Context, info interceptor.CallInfo, req, res []interface{}, next interceptor.HandleFunc) error {
@@ -292,17 +281,18 @@ func Example_testWithDefaultConfig() {
292281
// helloWorld shutdown
293282
}
294283

295-
// This example demonstrates how to use [kod.RunTest], [kod.NewTestLogger] and [kod.WithLogger] to run a test function with a custom logger.
284+
// This example demonstrates how to use [kod.RunTest], [kod.NewTestLogger] to run a test function with a custom logger.
296285
func Example_testWithLogObserver() {
297286
logger, observer := kod.NewTestLogger()
287+
slog.SetDefault(logger)
298288

299289
t := &testing.T{}
300290
kod.RunTest(t, func(ctx context.Context, app *helloworld.App) {
301291
app.L(ctx).Debug("Hello, World!")
302292
app.L(ctx).Info("Hello, World!")
303293
app.L(ctx).Warn("Hello, World!")
304294
app.L(ctx).Error("Hello, World!")
305-
}, kod.WithLogger(logger))
295+
})
306296

307297
fmt.Println(observer.Len())
308298
fmt.Println(observer.ErrorCount())
@@ -315,3 +305,19 @@ func Example_testWithLogObserver() {
315305
// 1
316306
// 0
317307
}
308+
309+
// This example demonstrates how to use [kod.RunTest], [kod.WithKoanf] to run a test function with a custom koanf instance.
310+
func Example_testWithKoanf() {
311+
c := koanf.New("_")
312+
c.Set("name", "testName")
313+
314+
kod.RunTest(&testing.T{}, func(ctx context.Context, app *helloworld.App) {
315+
fmt.Println(app.Config().Name)
316+
app.HelloWorld.Get().SayHello(ctx)
317+
}, kod.WithKoanf(c))
318+
// Output:
319+
// helloWorld init
320+
// testName
321+
// Hello, World!
322+
// helloWorld shutdown
323+
}

go.mod

+4-37
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,9 @@ require (
2222
github.com/sony/gobreaker v1.0.0
2323
github.com/spf13/cobra v1.8.1
2424
github.com/stretchr/testify v1.10.0
25-
go.opentelemetry.io/contrib/bridges/otelslog v0.8.0
26-
go.opentelemetry.io/contrib/exporters/autoexport v0.58.0
27-
go.opentelemetry.io/contrib/instrumentation/host v0.58.0
28-
go.opentelemetry.io/contrib/instrumentation/runtime v0.58.0
2925
go.opentelemetry.io/otel v1.33.0
30-
go.opentelemetry.io/otel/log v0.9.0
3126
go.opentelemetry.io/otel/metric v1.33.0
3227
go.opentelemetry.io/otel/sdk v1.33.0
33-
go.opentelemetry.io/otel/sdk/log v0.9.0
34-
go.opentelemetry.io/otel/sdk/metric v1.33.0
3528
go.opentelemetry.io/otel/trace v1.33.0
3629
go.uber.org/goleak v1.3.0
3730
go.uber.org/mock v0.5.0
@@ -40,64 +33,38 @@ require (
4033
)
4134

4235
require (
43-
github.com/beorn7/perks v1.0.1 // indirect
44-
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
45-
github.com/cespare/xxhash/v2 v2.3.0 // indirect
4636
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
47-
github.com/ebitengine/purego v0.8.1 // indirect
48-
github.com/gabriel-vasile/mimetype v1.4.5 // indirect
37+
github.com/gabriel-vasile/mimetype v1.4.7 // indirect
4938
github.com/go-logr/logr v1.4.2 // indirect
5039
github.com/go-logr/stdr v1.2.2 // indirect
5140
github.com/go-ole/go-ole v1.3.0 // indirect
5241
github.com/go-playground/locales v0.14.1 // indirect
5342
github.com/go-playground/universal-translator v0.18.1 // indirect
5443
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
5544
github.com/google/uuid v1.6.0 // indirect
56-
github.com/grpc-ecosystem/grpc-gateway/v2 v2.24.0 // indirect
5745
github.com/inconshreveable/mousetrap v1.1.0 // indirect
58-
github.com/klauspost/compress v1.17.11 // indirect
5946
github.com/knadh/koanf/maps v0.1.1 // indirect
6047
github.com/leodido/go-urn v1.4.0 // indirect
6148
github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683 // indirect
6249
github.com/mattn/go-colorable v0.1.13 // indirect
6350
github.com/mattn/go-isatty v0.0.20 // indirect
6451
github.com/mitchellh/copystructure v1.2.0 // indirect
6552
github.com/mitchellh/reflectwalk v1.0.2 // indirect
66-
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
67-
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
53+
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
6854
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
6955
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
70-
github.com/prometheus/client_golang v1.20.5 // indirect
71-
github.com/prometheus/client_model v0.6.1 // indirect
72-
github.com/prometheus/common v0.61.0 // indirect
73-
github.com/prometheus/procfs v0.15.1 // indirect
74-
github.com/shirou/gopsutil/v4 v4.24.11 // indirect
7556
github.com/shoenig/go-m1cpu v0.1.6 // indirect
7657
github.com/spf13/pflag v1.0.5 // indirect
7758
github.com/tklauser/go-sysconf v0.3.14 // indirect
7859
github.com/tklauser/numcpus v0.9.0 // indirect
7960
github.com/yusufpapurcu/wmi v1.2.4 // indirect
8061
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
81-
go.opentelemetry.io/contrib/bridges/prometheus v0.58.0 // indirect
82-
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.9.0 // indirect
83-
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.9.0 // indirect
84-
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.33.0 // indirect
85-
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.33.0 // indirect
86-
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.33.0 // indirect
87-
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.33.0 // indirect
88-
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.33.0 // indirect
89-
go.opentelemetry.io/otel/exporters/prometheus v0.55.0 // indirect
90-
go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.9.0 // indirect
91-
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.33.0 // indirect
92-
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.33.0 // indirect
93-
go.opentelemetry.io/proto/otlp v1.4.0 // indirect
94-
golang.org/x/crypto v0.30.0 // indirect
62+
golang.org/x/crypto v0.31.0 // indirect
9563
golang.org/x/mod v0.22.0 // indirect
96-
golang.org/x/net v0.32.0 // indirect
64+
golang.org/x/net v0.33.0 // indirect
9765
golang.org/x/sync v0.10.0 // indirect
9866
golang.org/x/sys v0.28.0 // indirect
9967
golang.org/x/text v0.21.0 // indirect
100-
google.golang.org/genproto/googleapis/api v0.0.0-20241209162323-e6fa225c2576 // indirect
10168
google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576 // indirect
10269
google.golang.org/protobuf v1.35.2 // indirect
10370
gopkg.in/yaml.v3 v3.0.1 // indirect

0 commit comments

Comments
 (0)