Skip to content

Commit 0a87911

Browse files
committed
fix: accesslog
1 parent f333e8e commit 0a87911

File tree

11 files changed

+43
-8
lines changed

11 files changed

+43
-8
lines changed

cmd/kod/internal/generate_generator.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,8 @@ func (g *generator) generateLocalStubs(p printFn) {
810810
}
811811
`, g.returnsList(mt), m.Name(), g.argList(comp, mt), g.setReturnsList(mt))
812812

813-
p(`info := kod.CallInfo{
813+
p(`info := kod.CallInfo {
814+
Impl: s.impl,
814815
Component: s.name,
815816
FullMethod: "%s.%s",
816817
Method: "%s",

examples/helloworld/kod_gen.go

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

interceptor.go

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import "context"
44

55
// CallInfo contains information about the call.
66
type CallInfo struct {
7+
// The impl of the called component.
8+
Impl any
79
// The component name of the called method.
810
Component string
911
// The full name of the called method, in the format of "package/service.method".

interceptor/kaccesslog/accesslog.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ func Interceptor() kod.Interceptor {
2525
attrs = append(attrs, slog.String("error", err.Error()))
2626
}
2727

28-
kod.FromContext(ctx).L(ctx).LogAttrs(ctx, level, "accesslog", attrs...)
28+
// check if impl L(ctx context.Context) method
29+
if l, ok := info.Impl.(interface {
30+
L(ctx context.Context) *slog.Logger
31+
}); ok {
32+
l.L(ctx).LogAttrs(ctx, level, "accesslog", attrs...)
33+
}
2934

3035
return err
3136
}

tests/case1/case_test.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,13 @@ func TestPanicKod(t *testing.T) {
166166
}
167167

168168
func BenchmarkCase1(b *testing.B) {
169-
kod.RunTest(b, func(ctx context.Context, k *test1Component) {
170-
_, err := k.Foo(ctx, &FooReq{})
171-
assert.Equal(b, "B", k.Config().A)
172-
assert.Equal(b, "test1:B", err.Error())
169+
b.Run("case1", func(b *testing.B) {
170+
kod.RunTest(b, func(ctx context.Context, k *test1Component) {
171+
for i := 0; i < b.N; i++ {
172+
_, err := k.Foo(ctx, &FooReq{})
173+
assert.Equal(b, "B", k.Config().A)
174+
assert.Equal(b, "test1:B", err.Error())
175+
}
176+
})
173177
})
174178
}

tests/case1/kod_gen.go

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/case1/kod_gen_mock.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/case2/kod_gen.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/case3/kod_gen.go

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/case4/kod_gen.go

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/graphcase/kod_gen.go

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)