Skip to content

Commit 99d8a2a

Browse files
committed
refactor: add WithOpenTelemetryDisabled
1 parent ae171ce commit 99d8a2a

File tree

4 files changed

+42
-28
lines changed

4 files changed

+42
-28
lines changed

kod.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -195,22 +195,17 @@ func WithInterceptors(interceptors ...interceptor.Interceptor) func(*options) {
195195
}
196196
}
197197

198-
// WithOpenTelemetryEnabled is an option setter for enabling OpenTelemetry.
199-
func WithOpenTelemetryEnabled() func(*options) {
198+
// WithOpenTelemetryDisabled is an option setter for enabling OpenTelemetry.
199+
func WithOpenTelemetryDisabled() func(*options) {
200200
return func(opts *options) {
201-
opts.enableOpenTelemetry = true
201+
opts.enableOpenTelemetry = false
202202
}
203203
}
204204

205205
// Run initializes and runs the application with the provided main component and options.
206206
func Run[T any, _ PointerToMain[T]](ctx context.Context, run func(context.Context, *T) error, opts ...func(*options)) error {
207-
opt := &options{}
208-
for _, o := range opts {
209-
o(opt)
210-
}
211-
212207
// Create a new Kod instance.
213-
kod, err := newKod(ctx, *opt)
208+
kod, err := newKod(ctx, opts...)
214209
if err != nil {
215210
return err
216211
}
@@ -281,7 +276,7 @@ type Kod struct {
281276
registryByImpl map[reflect.Type]*Registration
282277

283278
components map[string]any
284-
opts options
279+
opts *options
285280
}
286281

287282
// options defines the configuration options for Kod.
@@ -295,7 +290,14 @@ type options struct {
295290
}
296291

297292
// newKod creates a new instance of Kod with the provided registrations and options.
298-
func newKod(ctx context.Context, opts options) (*Kod, error) {
293+
func newKod(ctx context.Context, opts ...func(*options)) (*Kod, error) {
294+
opt := &options{
295+
enableOpenTelemetry: true,
296+
}
297+
for _, o := range opts {
298+
o(opt)
299+
}
300+
299301
kod := &Kod{
300302
mu: &sync.Mutex{},
301303
config: kodConfig{
@@ -309,12 +311,12 @@ func newKod(ctx context.Context, opts options) (*Kod, error) {
309311
registryByInterface: make(map[reflect.Type]*Registration),
310312
registryByImpl: make(map[reflect.Type]*Registration),
311313
components: make(map[string]any),
312-
opts: opts,
314+
opts: opt,
313315
}
314316

315-
kod.register(opts.registrations)
317+
kod.register(opt.registrations)
316318

317-
if err := kod.parseConfig(opts.configFilename); err != nil {
319+
if err := kod.parseConfig(opt.configFilename); err != nil {
318320
return nil, err
319321
}
320322

@@ -326,7 +328,7 @@ func newKod(ctx context.Context, opts options) (*Kod, error) {
326328
return nil, err
327329
}
328330

329-
if opts.enableOpenTelemetry && os.Getenv("OTEL_SDK_DISABLED") != "true" {
331+
if opt.enableOpenTelemetry && os.Getenv("OTEL_SDK_DISABLED") != "true" {
330332
kod.initOpenTelemetry(ctx)
331333
} else {
332334
kod.log = kod.newSlog(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{

kod_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ func TestMain(m *testing.M) {
2020
}
2121

2222
func TestConfigNoSuffix(t *testing.T) {
23-
k, err := newKod(context.Background(), options{})
23+
k, err := newKod(context.Background())
2424
assert.Nil(t, err)
2525

2626
assert.EqualError(t, k.parseConfig("nosuffix"), "read config file: Unsupported Config Type \"\"")
2727
}
2828

2929
func TestConfigEnv(t *testing.T) {
30-
k, err := newKod(context.Background(), options{})
30+
k, err := newKod(context.Background())
3131
assert.Nil(t, err)
3232

3333
assert.Equal(t, k.config.Name, "kod.test")
@@ -38,7 +38,7 @@ func TestConfigEnv(t *testing.T) {
3838
os.Setenv("KOD_VERSION", "1.0.0")
3939
os.Setenv("KOD_ENV", "dev")
4040

41-
k, err = newKod(context.Background(), options{})
41+
k, err = newKod(context.Background())
4242
assert.Nil(t, err)
4343

4444
assert.Equal(t, k.config.Name, "test")

testing.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func Fake[T any](impl any) fakeComponent {
2929
}
3030

3131
type runner struct {
32-
options options
32+
options []func(*options)
3333
}
3434

3535
// RunTest runs a test function with one component.
@@ -48,12 +48,7 @@ func RunTest3[T1, T2, T3 any](t testing.TB, body func(context.Context, T1, T2, T
4848
}
4949

5050
func runTest(t testing.TB, testBody any, opts ...func(*options)) {
51-
options := &options{}
52-
for _, o := range opts {
53-
o(options)
54-
}
55-
56-
err := runner{options: *options}.sub(t, testBody)
51+
err := runner{options: opts}.sub(t, testBody)
5752
if err != nil {
5853
t.Logf("runTest failed: %v", err)
5954
t.FailNow()
@@ -67,7 +62,7 @@ func (r runner) sub(t testing.TB, testBody any) error {
6762
cancelFn()
6863
}()
6964

70-
runner, err := newKod(ctx, r.options)
65+
runner, err := newKod(ctx, r.options...)
7166
if err != nil {
7267
return fmt.Errorf("newKod: %v", err)
7368
}
@@ -88,7 +83,7 @@ func (r runner) sub(t testing.TB, testBody any) error {
8883
// runner.Fakes = append(runner.Fakes, kod.Fake[Foo](...))
8984
// runner.Test(t, func(t *testing.T, f *foo) {...})
9085
for _, intf := range intfs {
91-
if _, ok := r.options.fakes[intf]; ok {
86+
if _, ok := runner.opts.fakes[intf]; ok {
9287
return fmt.Errorf("Component %v has both fake and component implementation pointer", intf)
9388
}
9489
}

tests/case1/case_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ func TestImpl(t *testing.T) {
4646

4747
func TestInterface(t *testing.T) {
4848
t.Parallel()
49+
kod.RunTest(t, func(ctx context.Context, k Test1Component) {
50+
// ctx = StartTrace(ctx)
51+
52+
ctx, span := otel.Tracer("").Start(ctx, "Run", trace.WithSpanKind(trace.SpanKindInternal))
53+
defer func() {
54+
span.End()
55+
fmt.Println("!!!!!!")
56+
}()
57+
58+
_, err := k.Foo(ctx, &FooReq{Id: 1})
59+
res, err := k.Foo(ctx, &FooReq{Id: 2})
60+
fmt.Println(err)
61+
require.Equal(t, "test1:B", err.Error())
62+
require.False(t, span.SpanContext().IsValid())
63+
require.Equal(t, 2, res.Id)
64+
}, kod.WithOpenTelemetryDisabled())
65+
4966
kod.RunTest(t, func(ctx context.Context, k Test1Component) {
5067
// ctx = StartTrace(ctx)
5168

@@ -61,7 +78,7 @@ func TestInterface(t *testing.T) {
6178
require.Equal(t, "test1:B", err.Error())
6279
require.True(t, span.SpanContext().IsValid())
6380
require.Equal(t, 2, res.Id)
64-
}, kod.WithOpenTelemetryEnabled())
81+
})
6582
}
6683

6784
func TestInterfacePanic(t *testing.T) {

0 commit comments

Comments
 (0)