diff --git a/plugin/loader/preload.go b/plugin/loader/preload.go index 2c6d512bf8c..4be60625fc6 100644 --- a/plugin/loader/preload.go +++ b/plugin/loader/preload.go @@ -5,6 +5,7 @@ import ( pluginflatfs "github.com/ipfs/go-ipfs/plugin/plugins/flatfs" pluginipldgit "github.com/ipfs/go-ipfs/plugin/plugins/git" pluginlevelds "github.com/ipfs/go-ipfs/plugin/plugins/levelds" + pluginpeerlog "github.com/ipfs/go-ipfs/plugin/plugins/peerlog" ) // DO NOT EDIT THIS FILE @@ -16,4 +17,5 @@ func init() { Preload(pluginbadgerds.Plugins...) Preload(pluginflatfs.Plugins...) Preload(pluginlevelds.Plugins...) + Preload(pluginpeerlog.Plugins...) } diff --git a/plugin/loader/preload_list b/plugin/loader/preload_list index 509ea5a2201..d648da935c4 100644 --- a/plugin/loader/preload_list +++ b/plugin/loader/preload_list @@ -8,3 +8,4 @@ ipldgit github.com/ipfs/go-ipfs/plugin/plugins/git * badgerds github.com/ipfs/go-ipfs/plugin/plugins/badgerds * flatfs github.com/ipfs/go-ipfs/plugin/plugins/flatfs * levelds github.com/ipfs/go-ipfs/plugin/plugins/levelds * +peerlog github.com/ipfs/go-ipfs/plugin/plugins/peerlog * \ No newline at end of file diff --git a/plugin/plugins/Rules.mk b/plugin/plugins/Rules.mk index 12c7cf0b350..334ea449142 100644 --- a/plugin/plugins/Rules.mk +++ b/plugin/plugins/Rules.mk @@ -1,6 +1,6 @@ include mk/header.mk -$(d)_plugins:=$(d)/git $(d)/badgerds $(d)/flatfs $(d)/levelds +$(d)_plugins:=$(d)/git $(d)/badgerds $(d)/flatfs $(d)/levelds $(d)/peerlog $(d)_plugins_so:=$(addsuffix .so,$($(d)_plugins)) $(d)_plugins_main:=$(addsuffix /main/main.go,$($(d)_plugins)) diff --git a/plugin/plugins/peerlog/peerlog.go b/plugin/plugins/peerlog/peerlog.go index 97f11eee68a..33fb2637220 100644 --- a/plugin/plugins/peerlog/peerlog.go +++ b/plugin/plugins/peerlog/peerlog.go @@ -45,6 +45,7 @@ type plEvent struct { // {"level":"info","ts":"2020-02-10T13:54:59.095Z","logger":"plugin/peerlog","caller":"peerlog/peerlog.go:56","msg":"identified","peer":"QmS2H72gdrekXJggGdE9SunXPntBqdkJdkXQJjuxcH8Cbt","agent":"go-ipfs/0.5.0/"} // type peerLogPlugin struct { + enabled bool droppedCount uint64 events chan plEvent } @@ -67,8 +68,25 @@ func (*peerLogPlugin) Version() string { } // Init initializes plugin -func (pl *peerLogPlugin) Init(*plugin.Environment) error { +func (pl *peerLogPlugin) Init(env *plugin.Environment) error { pl.events = make(chan plEvent, eventQueueSize) + + // plugin is disabled by default, unless Enabled=true + if env.Config != nil { + mapIface, ok := env.Config.(map[string]interface{}) + if !ok { + return nil + } + enabledIface, ok := mapIface["Enabled"] + if !ok || enabledIface == nil { + return nil + } + enabled, ok := enabledIface.(bool) + if !ok { + return nil + } + pl.enabled = enabled + } return nil } @@ -153,6 +171,10 @@ func (pl *peerLogPlugin) emit(evt eventType, p peer.ID) { } func (pl *peerLogPlugin) Start(node *core.IpfsNode) error { + if !pl.enabled { + return nil + } + // Ensure logs from this plugin get printed regardless of global IPFS_LOGGING value if err := logging.SetLogLevel("plugin/peerlog", "info"); err != nil { return fmt.Errorf("failed to set log level: %w", err) diff --git a/test/sharness/t0280-plugin-peerlog.sh b/test/sharness/t0280-plugin-peerlog.sh new file mode 100755 index 00000000000..f240582b82c --- /dev/null +++ b/test/sharness/t0280-plugin-peerlog.sh @@ -0,0 +1,53 @@ +#!/usr/bin/env bash +# +# Copyright (c) 2017 Jakub Sztandera +# MIT Licensed; see the LICENSE file in this repository. +# + +test_description="Test peerlog plugin" + +. lib/test-lib.sh + +test_expect_success "setup testbed" ' + iptb testbed create -type localipfs -count 2 -force -init +' + +startup_cluster 2 + +test_expect_success "peerlog is disabled by default" ' + go-sleep 100ms + iptb logs 0 >node0logs + test_expect_code 1 grep peerlog node0logs +' + +test_expect_success 'stop iptb' 'iptb stop' + + + +test_expect_success "setup testbed" ' + iptb testbed create -type localipfs -count 2 -force -init +' + +test_expect_success "enable peerlog config setting" ' + iptb run -- ipfs config --json Plugins.Plugins.peerlog.Config.Enabled true +' + +startup_cluster 2 + +test_expect_success "peerlog plugin is logged" ' + go-sleep 100ms + iptb logs 0 >node0logs + grep peerlog node0logs +' + +test_expect_success 'peer id' ' + PEERID_1=$(iptb attr get 1 id) +' + +test_expect_success "peer id is logged" ' + iptb logs 0 | grep -q "$PEERID_1" +' + +test_expect_success 'stop iptb' 'iptb stop' + +test_done