Repository navigation
Expand file tree
/
Copy pathcontext.go
More file actions
70 lines (59 loc) · 2.6 KB
/
Copy pathcontext.go
File metadata and controls
70 lines (59 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package stack
import (
"context"
"log/slog"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum-optimism/optimism/op-service/log/logfilter"
)
// ContextWithKind annotates the context with the given kind of service
func ContextWithKind(ctx context.Context, kind Kind) context.Context {
return logfilter.AddLogAttrToContext(ctx, "kind", kind)
}
// KindFromContext extracts the kind from the context.
func KindFromContext(ctx context.Context) Kind {
v, _ := logfilter.ValueFromContext[Kind](ctx, "kind")
return v
}
// KindSelector creates a log-filter that applies the given inner log-filter only if it matches the given kind.
// For logs of the specified kind, it applies the inner filter.
// For logs of other kinds, it returns false (filters them out).
func KindSelector(kind Kind) logfilter.Selector {
return logfilter.Select("kind", kind)
}
// ContextWithChainID annotates the context with the given chainID of service
func ContextWithChainID(ctx context.Context, chainID eth.ChainID) context.Context {
return logfilter.AddLogAttrToContext(ctx, "chainID", chainID)
}
// ChainIDFromContext extracts the chain ID from the context.
func ChainIDFromContext(ctx context.Context) eth.ChainID {
v, _ := logfilter.ValueFromContext[eth.ChainID](ctx, "chainID")
return v
}
// ChainIDSelector creates a log-filter that applies the given inner log-filter only if it matches the given chainID.
// This can be composed with logfilter package utils like logfilter.MuteAll or logfilter.Level
// to adjust logging for a specific chain ID.
func ChainIDSelector(chainID eth.ChainID) logfilter.Selector {
return logfilter.Select("chainID", chainID)
}
// ContextWithID attaches a component ID to the context.
// This also automatically attaches the chain ID and component kind to the context, if available from the ID.
func ContextWithID(ctx context.Context, id slog.LogValuer) context.Context {
if idWithChainID, ok := id.(ChainIDProvider); ok {
ctx = ContextWithChainID(ctx, idWithChainID.ChainID())
}
if idWithKind, ok := id.(KindProvider); ok {
ctx = ContextWithKind(ctx, idWithKind.Kind())
}
ctx = logfilter.AddLogAttrToContext(ctx, "id", id)
return ctx
}
func IDFromContext[T slog.LogValuer](ctx context.Context) T {
v, _ := logfilter.ValueFromContext[T](ctx, "id")
return v
}
// IDSelector creates a log-filter that applies the given inner log-filter only if it matches the given ID.
// This can be composed with logfilter package utils like logfilter.MuteAll or logfilter.Level
// to adjust logging for a specific chain ID.
func IDSelector(id slog.LogValuer) logfilter.Selector {
return logfilter.Select("id", id)
}