Skip to content

Commit

Permalink
[IPFS-170] - Use ipfs config file instead env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
Enghin Omer committed May 7, 2020
1 parent fb28109 commit c1ed706
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 19 deletions.
32 changes: 27 additions & 5 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 @@ -22,7 +22,19 @@ var _ plugin.PluginTracer = &DatadogPlugin{}

var tracerName = "go-ipfs"

const tracerEnv = "IPFS_TRACER_NAME"
const tracerNameKey = "IpfsTracerName"

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

type DatadogPlugin struct{}

Expand All @@ -35,9 +47,19 @@ func (d *DatadogPlugin) Version() string {
}

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.IpfsTracerName != "" {
tracerName = config.IpfsTracerName
}
return nil
}
Expand Down
62 changes: 48 additions & 14 deletions plugin/logger.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
package plugin

import (
"encoding/json"
"fmt"
"os"
"strings"

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

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

// LoggerPlugin controls which ipfs subsystems loggers are enabled
// These subsystems will be defined in env variable:
// eg: LOGGER_SUBSYSTEMS_WHITELIST = "dht,relay,corerepo"
// These subsystems are defined in ipfs config Plugins
type LoggerPlugin struct{}

var _ plugin.Plugin = &LoggerPlugin{}

const loggerSubsystemsEnv = "LOGGER_SUBSYSTEMS_WHITELIST"
// "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
}

func (l LoggerPlugin) Name() string {
return "logger"
Expand All @@ -30,20 +44,40 @@ func (l LoggerPlugin) Version() string {
// info level for whitelisted subsystem
// fatal level for all others
func (l LoggerPlugin) Init(env *plugin.Environment) error {
whitelistedSubsystems := os.Getenv(loggerSubsystemsEnv)
// If no subsystems given, exit with default settings
if whitelistedSubsystems == "" {
// 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 log levels
logging.SetAllLoggers(logging.LevelFatal)
for _, s := range strings.Split(whitelistedSubsystems, ",") {
subsystem := strings.TrimSpace(s)
err := logging.SetLogLevel(subsystem, "info")
if err != nil {
fmt.Printf("[Warning] Set log level failed for subsystem: %s. Error: %s", subsystem, err.Error())
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())
}
}

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{
LogLevel: "error",
}
if err = json.Unmarshal(bytes, &config); err != nil {
return nil, err
}

return &config, nil
}

0 comments on commit c1ed706

Please sign in to comment.