Skip to content

Commit

Permalink
refactor: add WithOpenTelemetryDisabled
Browse files Browse the repository at this point in the history
  • Loading branch information
sysulq committed Jun 11, 2024
1 parent ae171ce commit 99d8a2a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 28 deletions.
32 changes: 17 additions & 15 deletions kod.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,22 +195,17 @@ func WithInterceptors(interceptors ...interceptor.Interceptor) func(*options) {
}
}

// WithOpenTelemetryEnabled is an option setter for enabling OpenTelemetry.
func WithOpenTelemetryEnabled() func(*options) {
// WithOpenTelemetryDisabled is an option setter for enabling OpenTelemetry.
func WithOpenTelemetryDisabled() func(*options) {
return func(opts *options) {
opts.enableOpenTelemetry = true
opts.enableOpenTelemetry = false
}
}

// Run initializes and runs the application with the provided main component and options.
func Run[T any, _ PointerToMain[T]](ctx context.Context, run func(context.Context, *T) error, opts ...func(*options)) error {
opt := &options{}
for _, o := range opts {
o(opt)
}

// Create a new Kod instance.
kod, err := newKod(ctx, *opt)
kod, err := newKod(ctx, opts...)
if err != nil {
return err
}
Expand Down Expand Up @@ -281,7 +276,7 @@ type Kod struct {
registryByImpl map[reflect.Type]*Registration

components map[string]any
opts options
opts *options
}

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

// newKod creates a new instance of Kod with the provided registrations and options.
func newKod(ctx context.Context, opts options) (*Kod, error) {
func newKod(ctx context.Context, opts ...func(*options)) (*Kod, error) {
opt := &options{
enableOpenTelemetry: true,
}
for _, o := range opts {
o(opt)
}

kod := &Kod{
mu: &sync.Mutex{},
config: kodConfig{
Expand All @@ -309,12 +311,12 @@ func newKod(ctx context.Context, opts options) (*Kod, error) {
registryByInterface: make(map[reflect.Type]*Registration),
registryByImpl: make(map[reflect.Type]*Registration),
components: make(map[string]any),
opts: opts,
opts: opt,
}

kod.register(opts.registrations)
kod.register(opt.registrations)

if err := kod.parseConfig(opts.configFilename); err != nil {
if err := kod.parseConfig(opt.configFilename); err != nil {
return nil, err
}

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

if opts.enableOpenTelemetry && os.Getenv("OTEL_SDK_DISABLED") != "true" {
if opt.enableOpenTelemetry && os.Getenv("OTEL_SDK_DISABLED") != "true" {
kod.initOpenTelemetry(ctx)
} else {
kod.log = kod.newSlog(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Expand Down
6 changes: 3 additions & 3 deletions kod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ func TestMain(m *testing.M) {
}

func TestConfigNoSuffix(t *testing.T) {
k, err := newKod(context.Background(), options{})
k, err := newKod(context.Background())
assert.Nil(t, err)

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

func TestConfigEnv(t *testing.T) {
k, err := newKod(context.Background(), options{})
k, err := newKod(context.Background())
assert.Nil(t, err)

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

k, err = newKod(context.Background(), options{})
k, err = newKod(context.Background())
assert.Nil(t, err)

assert.Equal(t, k.config.Name, "test")
Expand Down
13 changes: 4 additions & 9 deletions testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Fake[T any](impl any) fakeComponent {
}

type runner struct {
options options
options []func(*options)
}

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

func runTest(t testing.TB, testBody any, opts ...func(*options)) {
options := &options{}
for _, o := range opts {
o(options)
}

err := runner{options: *options}.sub(t, testBody)
err := runner{options: opts}.sub(t, testBody)
if err != nil {
t.Logf("runTest failed: %v", err)
t.FailNow()
Expand All @@ -67,7 +62,7 @@ func (r runner) sub(t testing.TB, testBody any) error {
cancelFn()
}()

runner, err := newKod(ctx, r.options)
runner, err := newKod(ctx, r.options...)
if err != nil {
return fmt.Errorf("newKod: %v", err)
}
Expand All @@ -88,7 +83,7 @@ func (r runner) sub(t testing.TB, testBody any) error {
// runner.Fakes = append(runner.Fakes, kod.Fake[Foo](...))
// runner.Test(t, func(t *testing.T, f *foo) {...})
for _, intf := range intfs {
if _, ok := r.options.fakes[intf]; ok {
if _, ok := runner.opts.fakes[intf]; ok {
return fmt.Errorf("Component %v has both fake and component implementation pointer", intf)
}
}
Expand Down
19 changes: 18 additions & 1 deletion tests/case1/case_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ func TestImpl(t *testing.T) {

func TestInterface(t *testing.T) {
t.Parallel()
kod.RunTest(t, func(ctx context.Context, k Test1Component) {
// ctx = StartTrace(ctx)

ctx, span := otel.Tracer("").Start(ctx, "Run", trace.WithSpanKind(trace.SpanKindInternal))
defer func() {
span.End()
fmt.Println("!!!!!!")
}()

_, err := k.Foo(ctx, &FooReq{Id: 1})
res, err := k.Foo(ctx, &FooReq{Id: 2})
fmt.Println(err)
require.Equal(t, "test1:B", err.Error())
require.False(t, span.SpanContext().IsValid())
require.Equal(t, 2, res.Id)
}, kod.WithOpenTelemetryDisabled())

kod.RunTest(t, func(ctx context.Context, k Test1Component) {
// ctx = StartTrace(ctx)

Expand All @@ -61,7 +78,7 @@ func TestInterface(t *testing.T) {
require.Equal(t, "test1:B", err.Error())
require.True(t, span.SpanContext().IsValid())
require.Equal(t, 2, res.Id)
}, kod.WithOpenTelemetryEnabled())
})
}

func TestInterfacePanic(t *testing.T) {
Expand Down

0 comments on commit 99d8a2a

Please sign in to comment.