Skip to content

Commit f61fbd4

Browse files
authored
op-devstack,op-service: context-logging, better test contexts, and log filters (ethereum-optimism#16098)
* op-devstack,op-service: context-logging, better test contexts, and log filters * op-devstack/sysgo: review fix - add attrs via WithCtx
1 parent 1a8fe18 commit f61fbd4

53 files changed

Lines changed: 940 additions & 189 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎devnet-sdk/telemetry/slog.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77

88
"go.opentelemetry.io/otel/attribute"
99
"go.opentelemetry.io/otel/trace"
10+
11+
"github.com/ethereum-optimism/optimism/op-service/logmods"
1012
)
1113

1214
func WrapHandler(h slog.Handler) slog.Handler {
@@ -19,6 +21,12 @@ type tracingHandler struct {
1921
slog.Handler
2022
}
2123

24+
var _ logmods.Handler = (*tracingHandler)(nil)
25+
26+
func (h *tracingHandler) Unwrap() slog.Handler {
27+
return h.Handler
28+
}
29+
2230
func (h *tracingHandler) Handle(ctx context.Context, record slog.Record) error {
2331
// Send log entries as events to the tracer
2432
if h.Handler.Enabled(ctx, record.Level) {

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ require (
294294
rsc.io/tmplfunc v0.0.3 // indirect
295295
)
296296

297-
replace github.com/ethereum/go-ethereum => github.com/ethereum-optimism/op-geth v1.101511.0-rc.1
297+
replace github.com/ethereum/go-ethereum => github.com/ethereum-optimism/op-geth v1.101511.1-0.20250523133920-e3f85bf38455
298298

299299
//replace github.com/ethereum/go-ethereum => ../op-geth
300300

‎go.sum‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,8 @@ github.com/elastic/gosigar v0.14.3 h1:xwkKwPia+hSfg9GqrCUKYdId102m9qTJIIr7egmK/u
206206
github.com/elastic/gosigar v0.14.3/go.mod h1:iXRIGg2tLnu7LBdpqzyQfGDEidKCfWcCMS0WKyPWoMs=
207207
github.com/ethereum-optimism/go-ethereum-hdwallet v0.1.3 h1:RWHKLhCrQThMfch+QJ1Z8veEq5ZO3DfIhZ7xgRP9WTc=
208208
github.com/ethereum-optimism/go-ethereum-hdwallet v0.1.3/go.mod h1:QziizLAiF0KqyLdNJYD7O5cpDlaFMNZzlxYNcWsJUxs=
209-
github.com/ethereum-optimism/op-geth v1.101511.0-rc.1 h1:DTQwpncGhqrxUO4YqmY740mYwFc+C5eh9mqGHhRerHg=
210-
github.com/ethereum-optimism/op-geth v1.101511.0-rc.1/go.mod h1:SkytozVEPtnUeBlquwl0Qv5JKvrN/Y5aqh+VkQo/EOI=
211-
github.com/ethereum-optimism/superchain-registry/validation v0.0.0-20250314162817-2c60e5723c64 h1:teDhU4h4ryaE8rSBl+vJJiwKHjxdnnHPkKZ9iNr2R8k=
212-
github.com/ethereum-optimism/superchain-registry/validation v0.0.0-20250314162817-2c60e5723c64/go.mod h1:NZ816PzLU1TLv1RdAvYAb6KWOj4Zm5aInT0YpDVml2Y=
209+
github.com/ethereum-optimism/op-geth v1.101511.1-0.20250523133920-e3f85bf38455 h1:uIZUYbfz+KqMUgVrrhVLzIk9AGZx0BrWr+nKe6ccENo=
210+
github.com/ethereum-optimism/op-geth v1.101511.1-0.20250523133920-e3f85bf38455/go.mod h1:SkytozVEPtnUeBlquwl0Qv5JKvrN/Y5aqh+VkQo/EOI=
213211
github.com/ethereum-optimism/superchain-registry/validation v0.0.0-20250521132634-76f6d741b33f h1:L4Q9iIthJHsrIS1rwEYcZDlMtIeKzwRRByQALNvJ3cQ=
214212
github.com/ethereum-optimism/superchain-registry/validation v0.0.0-20250521132634-76f6d741b33f/go.mod h1:NZ816PzLU1TLv1RdAvYAb6KWOj4Zm5aInT0YpDVml2Y=
215213
github.com/ethereum/c-kzg-4844/v2 v2.1.0 h1:gQropX9YFBhl3g4HYhwE70zq3IHFRgbbNPw0Shwzf5w=

‎op-acceptance-tests/tests/base/advance_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestCLAdvance(gt *testing.T) {
2525
defer span.End()
2626

2727
new_num, num = sys.L2CL.SafeL2BlockRef().Number, new_num
28-
t.Logger().WithContext(ctx).Info("safe head", "number", new_num)
28+
t.Logger().InfoContext(ctx, "safe head", "number", new_num)
2929
return new_num > num
3030
}, 30*time.Second, waitTime)
3131
}

‎op-acceptance-tests/tests/base/faucet_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ func TestFaucetFund(gt *testing.T) {
2222
_, span = tracer.Start(ctx, "transfer funds")
2323
amount := eth.OneEther
2424
funded.Transfer(unfunded.Address(), amount)
25-
t.Logger().WithContext(ctx).Info("funds transferred", "amount", amount)
25+
t.Logger().InfoContext(ctx, "funds transferred", "amount", amount)
2626
span.End()
2727
}

‎op-devstack/devtest/common.go‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package devtest
33
import (
44
"context"
55

6+
"github.com/ethereum/go-ethereum/log"
7+
68
"github.com/stretchr/testify/require"
79
"go.opentelemetry.io/otel/trace"
810
)
@@ -22,8 +24,29 @@ type CommonT interface {
2224
Helper()
2325
Name() string
2426

25-
Logger() Logger
27+
Logger() log.Logger
2628
Tracer() trace.Tracer
2729
Ctx() context.Context
2830
Require() *require.Assertions
2931
}
32+
33+
type testScopeCtxKeyType struct{}
34+
35+
// testScopeCtxKey is a key added to the test-context to identify the test-scope.
36+
var testScopeCtxKey = testScopeCtxKeyType{}
37+
38+
// TestScope retrieves the test-scope from the context
39+
func TestScope(ctx context.Context) string {
40+
scope := ctx.Value(testScopeCtxKey)
41+
if scope == nil {
42+
return ""
43+
}
44+
return scope.(string)
45+
}
46+
47+
// AddTestScope combines the sub-scope with the test-scope of the context,
48+
// and returns a context with the updated scope value.
49+
func AddTestScope(ctx context.Context, scope string) context.Context {
50+
prev := TestScope(ctx)
51+
return context.WithValue(ctx, testScopeCtxKey, prev+"/"+scope)
52+
}

‎op-devstack/devtest/logging.go‎

Lines changed: 0 additions & 64 deletions
This file was deleted.

‎op-devstack/devtest/package.go‎

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ import (
1717
type P interface {
1818
CommonT
1919

20+
// WithCtx makes a copy of P with a specific context.
21+
// The ctx must match the test-scope of the existing context.
22+
// This function is used to create a P with annotated context, e.g. a specific resource.
23+
// The logger may be annotated with additional arguments.
24+
WithCtx(ctx context.Context, args ...any) P
25+
2026
// TempDir creates a temporary directory, and returns the file-path.
2127
// This directory is cleaned up at the end of the package,
2228
// and can be shared safely between tests that run in that package scope.
@@ -42,7 +48,7 @@ type implP struct {
4248
scopeName string
4349

4450
// logger is used for logging. Regular test errors will also be redirected to get logged here.
45-
logger Logger
51+
logger log.Logger
4652

4753
// fail will be called to register a critical failure.
4854
// The implementer can choose to panic, crit-log, exit, etc. as preferred.
@@ -103,7 +109,7 @@ func (t *implP) Name() string {
103109
return t.scopeName
104110
}
105111

106-
func (t *implP) Logger() Logger {
112+
func (t *implP) Logger() log.Logger {
107113
return t.logger
108114
}
109115

@@ -115,6 +121,34 @@ func (t *implP) Ctx() context.Context {
115121
return t.ctx
116122
}
117123

124+
type wrapP struct {
125+
ctx context.Context
126+
logger log.Logger
127+
req *require.Assertions
128+
P
129+
}
130+
131+
var _ P = (*wrapP)(nil)
132+
133+
func (p *wrapP) Ctx() context.Context {
134+
return p.ctx
135+
}
136+
137+
func (p *wrapP) Logger() log.Logger {
138+
return p.logger
139+
}
140+
141+
func (t *implP) WithCtx(ctx context.Context, args ...any) P {
142+
expected := TestScope(t.ctx)
143+
got := TestScope(ctx)
144+
t.req.Equal(expected, got, "cannot replace context with different test-scope")
145+
logger := t.logger.New(args...)
146+
logger.SetContext(ctx)
147+
out := &wrapP{ctx: ctx, logger: logger, P: t}
148+
out.req = require.New(out)
149+
return out
150+
}
151+
118152
func (t *implP) Require() *require.Assertions {
119153
return t.req
120154
}
@@ -166,9 +200,9 @@ func NewP(ctx context.Context, logger log.Logger, onFail func()) P {
166200
ctx, cancel := context.WithCancel(ctx)
167201
out := &implP{
168202
scopeName: "pkg",
169-
logger: &pkgLogger{logger},
203+
logger: logger,
170204
fail: onFail,
171-
ctx: ctx,
205+
ctx: AddTestScope(ctx, "pkg"),
172206
cancel: cancel,
173207
}
174208
out.req = require.New(out)

0 commit comments

Comments
 (0)