feat: add peerlog plugin

adds a plugin to log just peerIDs for nodes we connect to, to allow us to see how many unique peers a node connects to over time.

It's set up a preloaded plugin, so it would be enabled by default. We may want to extract it to a seperate repo so we can make it an optional add on.

**Usage**

```console
$ GOLOG_FILE=~/peer.log IPFS_LOGGING_FMT=json ipfs daemon
```

**output**

```json
{"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"}
```

License: MIT
Signed-off-by: Oli Evans <oli@tableflip.io>
This commit is contained in:
Oli Evans 2020-02-10 13:57:16 +00:00
parent d83e07ea90
commit edfe9c5efd
No known key found for this signature in database
GPG Key ID: A2AC979DA8460535
4 changed files with 70 additions and 47 deletions

View File

@ -5,7 +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"
pluginpeeridlog "github.com/ipfs/go-ipfs/plugin/plugins/peeridlog"
pluginpeerlog "github.com/ipfs/go-ipfs/plugin/plugins/peerlog"
)
// DO NOT EDIT THIS FILE
@ -17,5 +17,5 @@ func init() {
Preload(pluginbadgerds.Plugins...)
Preload(pluginflatfs.Plugins...)
Preload(pluginlevelds.Plugins...)
Preload(pluginpeeridlog.Plugins...)
Preload(pluginpeerlog.Plugins...)
}

View File

@ -8,4 +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 *
peeridlog github.com/ipfs/go-ipfs/plugin/plugins/peeridlog *
peerlog github.com/ipfs/go-ipfs/plugin/plugins/peerlog *

View File

@ -1,44 +0,0 @@
package peeridlog
import (
"fmt"
core "github.com/ipfs/go-ipfs/core"
plugin "github.com/ipfs/go-ipfs/plugin"
)
// Plugins is exported list of plugins that will be loaded
var Plugins = []plugin.Plugin{
&peerIDLogPlugin{},
}
// Log all the PeerIDs we connect to.
type peerIDLogPlugin struct{}
var _ plugin.PluginDaemonInternal = (*peerIDLogPlugin)(nil)
// Name returns the plugin's name, satisfying the plugin.Plugin interface.
func (*peerIDLogPlugin) Name() string {
return "peeridlog"
}
// Version returns the plugin's version, satisfying the plugin.Plugin interface.
func (*peerIDLogPlugin) Version() string {
return "0.1.0"
}
// Init initializes plugin, satisfying the plugin.Plugin interface. Put any
// initialization logic here.
func (*peerIDLogPlugin) Init(*plugin.Environment) error {
return nil
}
func (*peerIDLogPlugin) Start(*core.IpfsNode) error {
fmt.Println("peerIDLogPlugin HELLO!")
return nil
}
func (*peerIDLogPlugin) Close() error {
fmt.Println("peerIDLogPlugin GOODBYE!")
return nil
}

View File

@ -0,0 +1,67 @@
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
logging.SetLogLevel("plugin/peerlog", "info")
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
}