Skip to content

Commit

Permalink
feat: add WithInterceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
sysulq committed Jan 15, 2025
1 parent 6590534 commit f61d912
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
32 changes: 5 additions & 27 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,14 @@ func Example_openTelemetryTrace() {
otel.SetTracerProvider(tracerProvider)

kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
kod.FromContext(ctx).SetInterceptors(ktrace.Interceptor())

ctx, span := app.Tracer().Start(ctx, "example")
defer span.End()
app.L(ctx).Info("Hello, World!")
app.L(ctx).WarnContext(ctx, "Hello, World!")

app.HelloWorld.Get().SayHello(ctx)
return nil
})
}, kod.WithInterceptors(ktrace.Interceptor()))

fmt.Println(observer.Filter(func(m map[string]any) bool {
return m["trace_id"] != nil && m["span_id"] != nil
Expand Down Expand Up @@ -184,7 +182,7 @@ func Example_openTelemetryMetric() {
// helloWorld shutdown
}

// This example demonstrates how to use [kod.Kod.SetInterceptors] to provide global interceptors to the application.
// This example demonstrates how to use [kod.WithInterceptors] to provide global interceptors to the application.
func Example_interceptorGlobal() {
itcpt := interceptor.Interceptor(func(ctx context.Context, info interceptor.CallInfo, req, res []interface{}, next interceptor.HandleFunc) error {
fmt.Println("Before call")
Expand All @@ -194,11 +192,9 @@ func Example_interceptorGlobal() {
})

kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
kod.FromContext(ctx).SetInterceptors(itcpt)

app.HelloWorld.Get().SayHello(ctx)
return nil
})
}, kod.WithInterceptors(itcpt))
// Output:
// helloWorld init
// Before call
Expand All @@ -207,31 +203,13 @@ func Example_interceptorGlobal() {
// helloWorld shutdown
}

// This example demonstrates how to use [kod.Kod.SetInterceptors] to provide component-specific interceptors to the application.
func Example_interceptorComponent() {
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
app.HelloWorldInterceptor.Get().SayHello(ctx)
return nil
})
// Output:
// helloWorld init
// Before call
// Hello, Interceptor!
// After call
// helloWorld shutdown
}

// This example demonstrates how to use built-in interceptors with [kod.Kod.SetInterceptors].
// This example demonstrates how to use built-in interceptors with [kod.WithInterceptors].
// Such as [krecovery.Interceptor], [ktrace.Interceptor], and [kmetric.Interceptor] ...
func Example_interceptorBuiltin() {
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
kod.FromContext(ctx).SetInterceptors(interceptor.Chain([]interceptor.Interceptor{
krecovery.Interceptor(), ktrace.Interceptor(), kmetric.Interceptor(),
}))

app.HelloWorld.Get().SayHello(ctx)
return nil
})
}, kod.WithInterceptors(krecovery.Interceptor(), ktrace.Interceptor(), kmetric.Interceptor()))
// Output:
// helloWorld init
// Hello, World!
Expand Down
12 changes: 9 additions & 3 deletions kod.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,13 @@ func WithKoanf(cfg *koanf.Koanf) func(*options) {
}
}

// WithInterceptors is an option setter for specifying interceptors.
func WithInterceptors(interceptors ...interceptor.Interceptor) func(*options) {
return func(opts *options) {
opts.interceptor = interceptor.Chain(interceptors)
}
}

// MustRun is a helper function to run the application with the provided main component and options.
// It panics if an error occurs during the execution.
func MustRun[T any, P PointerToMain[T]](ctx context.Context, run func(context.Context, *T) error, opts ...func(*options)) {
Expand Down Expand Up @@ -313,8 +320,6 @@ type Kod struct {

hooker *hooks.Hooker

interceptor interceptor.Interceptor

regs []*Registration
registryByName map[string]*Registration
registryByInterface map[reflect.Type]*Registration
Expand All @@ -331,6 +336,7 @@ type options struct {
fakes map[reflect.Type]any
registrations []*Registration
koanf *koanf.Koanf
interceptor interceptor.Interceptor
}

// newKod creates a new instance of Kod with the provided registrations and options.
Expand Down Expand Up @@ -382,7 +388,7 @@ func (k *Kod) Config() kodConfig {

// SetDefaultInterceptor sets the default interceptor for the Kod instance.
func (k *Kod) SetInterceptors(interceptors ...interceptor.Interceptor) {
k.interceptor = interceptor.Chain(interceptors)
k.opts.interceptor = interceptor.Chain(interceptors)
}

// Defer adds a hook function to the Kod instance.
Expand Down
4 changes: 2 additions & 2 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ func (k *Kod) getIntf(ctx context.Context, t reflect.Type) (any, error) {
}

itcpt := func(ctx context.Context, info interceptor.CallInfo, req, reply []any, invoker interceptor.HandleFunc) error {
if k.interceptor == nil {
if k.opts.interceptor == nil {
return invoker(ctx, info, req, reply)
}

return k.interceptor(ctx, info, req, reply, invoker)
return k.opts.interceptor(ctx, info, req, reply, invoker)
}

if h, ok := impl.(interface {
Expand Down

0 comments on commit f61d912

Please sign in to comment.