Skip to content

Commit

Permalink
Merge pull request #7 from INFURA/IPFS-170
Browse files Browse the repository at this point in the history
IPFS 170
  • Loading branch information
MichaelMure authored May 11, 2020
2 parents cd06c41 + 3c0cad1 commit 09acc47
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 10 deletions.
56 changes: 52 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# go-ipfs-datadog-plugin

Datadog tracing plugin for go-ipfs.

This repository contains the following `go-ipfs` plugins:
- Datadog tracing plugin configures the Datadog tracer to collect the traces and relay them to the agent. `go-ipfs` tracing instrumentation is partial at the moment but should improve over time.
- Datadog logger plugin allows users to set log levels for each `go-ipfs` subsystem.

## Caveats

- This plugin doesn't implement any tracing, it simply configures the Datadog tracer to collect the traces and relay them to the agent. `go-ipfs` tracing instrumentation is partial at the moment but should improve over time.

- Plugins only work on Linux and MacOS at the moment. You can track the progress of this issue here: https://github.com/golang/go/issues/19282

- If you are using go-ipfs 0.4.22 or older, some traces will be lost. See: https://github.com/ipfs/go-ipfs/pull/6672
Expand Down Expand Up @@ -36,6 +35,55 @@ To update the go-ipfs, run:

Copy `datadog-plugin.so` to `$IPFS_DIR/plugins/datadog-plugin.so` (or run `make install` if you are installing locally)

### Configuration

Define plugin configurations variables in the ipfs config file.

- datadog-logger config:
```
{
...
"Plugins": {
"Plugins": {
...
"datadog-logger": {
"Config": {
"Levels": {
"fatal": ["system1", "system2", ...],
"error": [...]
"warn": [...]
...
},
"DefaultLevel": "info"
},
"Disabled": false
},
...
}
},
...
}
```

- datadog-tracer config:
```
{
...
"Plugins": {
"Plugins": {
...
"datadog-tracer": {
"Config": {
"TracerName": "go-ipfs-custom"
},
"Disabled": false
}
...
}
},
...
}
```

## References

Expand Down
25 changes: 19 additions & 6 deletions plugin/datadog.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package plugin

import (
"os"
"encoding/json"

"github.com/ipfs/go-ipfs/plugin"
logging "github.com/ipfs/go-log"
Expand All @@ -15,28 +15,41 @@ var log = logging.Logger("datadog")

var Plugins = []plugin.Plugin{
&DatadogPlugin{},
&LoggerPlugin{},
}

var _ plugin.PluginTracer = &DatadogPlugin{}

var tracerName = "go-ipfs"

const tracerEnv = "IPFS_TRACER_NAME"
type datadogConfig struct {
TracerName string
}

type DatadogPlugin struct{}

func (d *DatadogPlugin) Name() string {
return "datadog"
return "datadog-tracer"
}

func (d *DatadogPlugin) Version() string {
return "0.0.1"
}

func (d *DatadogPlugin) Init(env *plugin.Environment) error {
maybeName := os.Getenv(tracerEnv)
if maybeName != "" {
tracerName = maybeName
if env == nil || env.Config == nil {
return nil
}
bytes, err := json.Marshal(env.Config)
if err != nil {
return err
}
config := datadogConfig{}
if err := json.Unmarshal(bytes, &config); err != nil {
return err
}
if config.TracerName != "" {
tracerName = config.TracerName
}
return nil
}
Expand Down
76 changes: 76 additions & 0 deletions plugin/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package plugin

import (
"encoding/json"
"fmt"

logging "github.com/ipfs/go-log"

"github.com/ipfs/go-ipfs/plugin"
)

const defaultLevel = "error"

var _ plugin.Plugin = &LoggerPlugin{}

type loggerConfig struct {
Levels map[string][]string
DefaultLevel string
}

type LoggerPlugin struct{}

func (l LoggerPlugin) Name() string {
return "datadog-logger"
}

func (l LoggerPlugin) Version() string {
return "0.0.1"
}

// Set log levels for each system (logger)
func (l LoggerPlugin) Init(env *plugin.Environment) error {
// If no plugin config given, exit with default settings
if env == nil || env.Config == nil {
return nil
}

config, err := l.loadConfig(env.Config)
if err != nil {
return err
}

// set default log level for all loggers
defaultLevel, err := logging.LevelFromString(config.DefaultLevel)
if err != nil {
return err
}

logging.SetAllLoggers(defaultLevel)
for level, subsystems := range config.Levels {
for _, subsystem := range subsystems {
if err := logging.SetLogLevel(subsystem, level); err != nil {
return fmt.Errorf("set log level failed for subsystem: %s. Error: %s", subsystem, err.Error())
}
}
}

return nil
}

func (l LoggerPlugin) loadConfig(envConfig interface{}) (*loggerConfig, error) {
// load config data
bytes, err := json.Marshal(envConfig)
if err != nil {
return nil, err
}

config := loggerConfig{
DefaultLevel: defaultLevel,
}
if err = json.Unmarshal(bytes, &config); err != nil {
return nil, err
}

return &config, nil
}

0 comments on commit 09acc47

Please sign in to comment.