Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add peerlog plugin #6887

Merged
merged 5 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ To preload a go-ipfs plugin:
go-ipfs$ make build
```

You can also preload an in-tree but disabled-by-default plugin by adding it to
the IPFS_PLUGINS variable. For example, to enable plugins foo, bar, and baz:

```bash
go-ipfs$ make build IPFS_PLUGINS="foo bar baz"
```

## Creating A Plugin

To create your own out-of-tree plugin, use the [example
Expand Down
7 changes: 5 additions & 2 deletions plugin/loader/Rules.mk
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
include mk/header.mk

IPFS_PLUGINS ?=
export IPFS_PLUGINS

$(d)/preload.go: d:=$(d)
$(d)/preload.go: $(d)/preload_list $(d)/preload.sh
$(d)/preload.go: $(d)/preload_list $(d)/preload.sh ALWAYS
$(d)/preload.sh > $@
go fmt $@ >/dev/null

DEPS_GO += $(d)/preload.go

include mk/footer.mk
5 changes: 5 additions & 0 deletions plugin/loader/preload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

to_preload() {
awk 'NF' "$DIR/preload_list" | sed '/^#/d'
if [[ -n "$IPFS_PLUGINS" ]]; then
for plugin in $IPFS_PLUGINS; do
echo "$plugin github.com/ipfs/go-ipfs/plugin/plugins/$plugin *"
done
fi
}

cat <<EOL
Expand Down
69 changes: 69 additions & 0 deletions plugin/plugins/peerlog/peerlog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package peerlog

import (
"fmt"

core "github.com/ipfs/go-ipfs/core"
plugin "github.com/ipfs/go-ipfs/plugin"
logging "github.com/ipfs/go-log"
network "github.com/libp2p/go-libp2p-core/network"
)

var log = logging.Logger("plugin/peerlog")

// Log all the PeerIDs we see
//
// Usage:
// GOLOG_FILE=~/peer.log IPFS_LOGGING_FMT=json ipfs daemon
// Output:
// {"level":"info","ts":"2020-02-10T13:54:26.639Z","logger":"plugin/peerlog","caller":"peerlog/peerlog.go:51","msg":"connected","peer":"QmS2H72gdrekXJggGdE9SunXPntBqdkJdkXQJjuxcH8Cbt"}
// {"level":"info","ts":"2020-02-10T13:54:59.095Z","logger":"plugin/peerlog","caller":"peerlog/peerlog.go:56","msg":"disconnected","peer":"QmS2H72gdrekXJggGdE9SunXPntBqdkJdkXQJjuxcH8Cbt"}
//
type peerLogPlugin struct{}

var _ plugin.PluginDaemonInternal = (*peerLogPlugin)(nil)

// Plugins is exported list of plugins that will be loaded
var Plugins = []plugin.Plugin{
&peerLogPlugin{},
}

// Name returns the plugin's name, satisfying the plugin.Plugin interface.
func (*peerLogPlugin) Name() string {
return "peerlog"
}

// Version returns the plugin's version, satisfying the plugin.Plugin interface.
func (*peerLogPlugin) Version() string {
return "0.1.0"
}

// Init initializes plugin
func (*peerLogPlugin) Init(*plugin.Environment) error {
fmt.Println("peerLogPlugin enabled - PeerIDs will be logged")
return nil
}

func (*peerLogPlugin) Start(node *core.IpfsNode) error {
// 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)
}
var notifee network.NotifyBundle
notifee.ConnectedF = func(net network.Network, conn network.Conn) {
log.Infow("connected",
"peer", conn.RemotePeer().Pretty(),
)
}
notifee.DisconnectedF = func(net network.Network, conn network.Conn) {
log.Infow("disconnected",
"peer", conn.RemotePeer().Pretty(),
)
}
node.PeerHost.Network().Notify(&notifee)
return nil
}

func (*peerLogPlugin) Close() error {
return nil
}