Skip to content

Commit

Permalink
Update logger plugin config and add configuration infor in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Enghin Omer committed May 8, 2020
1 parent 7272908 commit 3c0cad1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 42 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
16 changes: 4 additions & 12 deletions plugin/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,14 @@ var _ plugin.PluginTracer = &DatadogPlugin{}

var tracerName = "go-ipfs"

// "Plugins": {
// "datadog": {
// "Config": {
// "IpfsTracerName": "go-ipfs"
// },
// "Disabled": false
// }
// }
type datadogConfig struct {
IpfsTracerName string
TracerName string
}

type DatadogPlugin struct{}

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

func (d *DatadogPlugin) Version() string {
Expand All @@ -56,8 +48,8 @@ func (d *DatadogPlugin) Init(env *plugin.Environment) error {
if err := json.Unmarshal(bytes, &config); err != nil {
return err
}
if config.IpfsTracerName != "" {
tracerName = config.IpfsTracerName
if config.TracerName != "" {
tracerName = config.TracerName
}
return nil
}
Expand Down
45 changes: 19 additions & 26 deletions plugin/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,26 @@ import (
"github.com/ipfs/go-ipfs/plugin"
)

const defaultLevel = "error"

var _ plugin.Plugin = &LoggerPlugin{}

// "Plugins": {
// "logger": {
// "Config": {
// "Subsystems": ["dht","relay","corerepo"],
// "LogLevel": "error"
// },
// "Disabled": false
// }
// }
type loggerConfig struct {
// whitelisted subsystems
Subsystems []string

// log level for the whitelisted subsystems
LogLevel string
Levels map[string][]string
DefaultLevel string
}

// LoggerPlugin controls which ipfs subsystems loggers are enabled
// These subsystems are defined in ipfs config Plugins
type LoggerPlugin struct{}

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

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

// Set log levels for each subsystem
// info level for whitelisted subsystem
// fatal level for all others
// 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 {
Expand All @@ -54,11 +40,18 @@ func (l LoggerPlugin) Init(env *plugin.Environment) error {
return err
}

// set log levels
logging.SetAllLoggers(logging.LevelFatal)
for _, subsystem := range config.Subsystems {
if err := logging.SetLogLevel(subsystem, config.LogLevel); err != nil {
return fmt.Errorf("set log level failed for subsystem: %s. Error: %s", subsystem, err.Error())
// 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())
}
}
}

Expand All @@ -73,7 +66,7 @@ func (l LoggerPlugin) loadConfig(envConfig interface{}) (*loggerConfig, error) {
}

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

0 comments on commit 3c0cad1

Please sign in to comment.