Skip to content

Commit

Permalink
feat(ARCO-295): new log level in p2p (#695)
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 authored Dec 6, 2024
1 parent a59de33 commit 7e9ae24
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 15 deletions.
4 changes: 4 additions & 0 deletions cmd/arc/services/blocktx.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ func StartBlockTx(logger *slog.Logger, arcConfig *config.ArcConfig) (func(), err
peerOpts = append(peerOpts, p2p.WithUserAgent("ARC", version.Version))
}

if arcConfig.LogLevel != arcConfig.PeerLogLevel {
peerOpts = append(peerOpts, p2p.WithLogLevel(arcConfig.PeerLogLevel, arcConfig.LogFormat))
}

for i, peerSetting := range arcConfig.Broadcasting.Unicast.Peers {
peerURL, err := peerSetting.GetP2PUrl()
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions cmd/arc/services/metamorph.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ func initPeerManager(logger *slog.Logger, s store.MetamorphStore, arcConfig *con
peerOpts = append(peerOpts, p2p.WithUserAgent("ARC", version.Version))
}

if arcConfig.LogLevel != arcConfig.PeerLogLevel {
peerOpts = append(peerOpts, p2p.WithLogLevel(arcConfig.PeerLogLevel, arcConfig.LogFormat))
}

l := logger.With(slog.String("module", "peer"))
for _, peerSetting := range arcConfig.Broadcasting.Unicast.Peers {
peerURL, err := peerSetting.GetP2PUrl()
Expand Down
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ const (
)

type ArcConfig struct {
LogLevel string `mapstructure:"logLevel"`
LogFormat string `mapstructure:"logFormat"`
LogLevel string `mapstructure:"logLevel"`
PeerLogLevel string `mapstructure:"peerLogLevel"`
ProfilerAddr string `mapstructure:"profilerAddr"`
PrometheusEndpoint string `mapstructure:"prometheusEndpoint"`
PrometheusAddr string `mapstructure:"prometheusAddr"`
Expand Down
3 changes: 2 additions & 1 deletion config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (

func getDefaultArcConfig() *ArcConfig {
return &ArcConfig{
LogLevel: "DEBUG",
LogFormat: "text",
LogLevel: "DEBUG",
PeerLogLevel: "DEBUG",
ProfilerAddr: "", // optional
PrometheusEndpoint: "", // optional
PrometheusAddr: "", // optional
Expand Down
7 changes: 4 additions & 3 deletions config/example_config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
logLevel: DEBUG # mode of logging. Value can be one of DEBUG | INFO | WARN | ERROR
logFormat: text # format of logging. Value can be one of text | json | tint
logLevel: DEBUG # mode of logging. Value can be one of DEBUG | INFO | WARN | ERROR
peerLogLevel: DEBUG # mode of logging for peer. Value can be one of TRACE | DEBUG | INFO | WARN | ERROR
profilerAddr: localhost:9999 # address to start profiler server on (optional)
prometheusEndpoint: /metrics # endpoint for prometheus metrics (optional)
prometheusAddr: :2112 # port for serving prometheus metrics
Expand Down Expand Up @@ -149,7 +150,7 @@ callbacker:
dialAddr: localhost:8021 # address for other services to dial callbacker service
health:
serverDialAddr: localhost:8025 # address at which the grpc health server is exposed
delay: 0s # delay before the callback (or batch of callbacks) is actually sent
delay: 0s # delay before the callback (or batch of callbacks) is actually sent
pause: 0s # pause between sending next callback to the same receiver
batchSendInterval: 5s # interval at witch batched callbacks are send (default 5s)
db:
Expand All @@ -167,4 +168,4 @@ callbacker:
pruneOlderThan: 336h # age threshold for pruning callbacks (older than this value will be removed)
failedCallbackCheckInterval: 1m # interval at which the store is checked for failed callbacks to be re-sent
delayDuration: 5s # we try callbacks a few times with this delay after which if it fails consistently we store them in db
expiration: 24h # maximum time a callback can remain unsent before it's put as permanently failed
expiration: 24h # maximum time a callback can remain unsent before it's put as permanently failed
2 changes: 2 additions & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func getSlogLevel(logLevel string) (slog.Level, error) {
return slog.LevelError, nil
case "DEBUG":
return slog.LevelDebug, nil
case "TRACE":
return slog.LevelDebug - 4, nil // simulate trace level
}

return slog.LevelInfo, errors.Join(ErrLoggerInvalidLogLevel, fmt.Errorf("log level: %s", logLevel))
Expand Down
16 changes: 8 additions & 8 deletions internal/p2p/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,9 @@ type Peer struct {
}

func NewPeer(logger *slog.Logger, msgHandler MessageHandlerI, address string, network wire.BitcoinNet, options ...PeerOptions) *Peer {
l := logger.With(
slog.Group("peer",
slog.String("network", network.String()),
slog.String("address", address),
),
)

p := &Peer{
dial: net.Dial,
l: l,
l: logger,
mh: msgHandler,

address: address,
Expand All @@ -88,6 +81,13 @@ func NewPeer(logger *slog.Logger, msgHandler MessageHandlerI, address string, ne
opt(p)
}

p.l = p.l.With(
slog.Group("peer",
slog.String("network", network.String()),
slog.String("address", address),
),
)

if p.writeCh == nil {
p.writeCh = make(chan wire.Message, 128)
}
Expand Down
11 changes: 11 additions & 0 deletions internal/p2p/peer_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net"
"time"

"github.com/bitcoin-sv/arc/internal/logger"
"github.com/libsv/go-p2p/wire"
)

Expand Down Expand Up @@ -46,3 +47,13 @@ func WithDialer(dial func(network, address string) (net.Conn, error)) PeerOption
p.dial = dial
}
}

func WithLogLevel(level, logFormat string) PeerOptions {
return func(p *Peer) {
l, err := logger.NewLogger(level, logFormat)
if err != nil {
l, _ = logger.NewLogger("DEBUG", "json")
}
p.l = l
}
}
5 changes: 3 additions & 2 deletions test/config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
logLevel: INFO
logFormat: tint
logLevel: INFO
peerLogLevel: DEBUG
profilerAddr: localhost:9999
prometheusEndpoint: /metrics
prometheusAddr: :2112
Expand Down Expand Up @@ -151,4 +152,4 @@ callbacker:
sslMode: disable
failedCallbackCheckInterval: 1m # interval at which the store is checked for failed callbacks to be re-sent
delayDuration: 5s # we try callbacks a few times with this delay after which if it fails consistently we store them in db
expiration: 24h # maximum time a callback can remain unsent before it's put as permanently failed
expiration: 24h # maximum time a callback can remain unsent before it's put as permanently failed

0 comments on commit 7e9ae24

Please sign in to comment.