Skip to content

Commit

Permalink
Filter executor log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
diptanu committed Jan 9, 2017
1 parent 4b72f11 commit 5c692c9
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 46 deletions.
4 changes: 4 additions & 0 deletions client/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ type Config struct {

// TLSConfig holds various TLS related configurations
TLSConfig *config.TLSConfig

// LogLevel is the level of the logs to putout
LogLevel string
}

func (c *Config) Copy() *Config {
Expand All @@ -172,6 +175,7 @@ func DefaultConfig() *Config {
Region: "global",
StatsCollectionInterval: 1 * time.Second,
TLSConfig: &config.TLSConfig{},
LogLevel: "DEBUG",
}
}

Expand Down
2 changes: 1 addition & 1 deletion client/driver/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle

pluginLogFile := filepath.Join(ctx.TaskDir.Dir, "executor.out")
pluginConfig := &plugin.ClientConfig{
Cmd: exec.Command(bin, "executor", pluginLogFile),
Cmd: exec.Command(bin, "executor", pluginLogFile, ctx.LogLevel),
}

exec, pluginClient, err := createExecutor(pluginConfig, d.config.LogOutput, d.config)
Expand Down
11 changes: 9 additions & 2 deletions client/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,18 @@ type ExecContext struct {

// Alloc ID
AllocID string

// LogLevel is the level of the logs to putout
LogLevel string
}

// NewExecContext is used to create a new execution context
func NewExecContext(td *allocdir.TaskDir, allocID string) *ExecContext {
return &ExecContext{TaskDir: td, AllocID: allocID}
func NewExecContext(td *allocdir.TaskDir, allocID string, logLevel string) *ExecContext {
return &ExecContext{
TaskDir: td,
AllocID: allocID,
LogLevel: logLevel,
}
}

// GetTaskEnv converts the alloc dir, the node, task and alloc into a
Expand Down
2 changes: 1 addition & 1 deletion client/driver/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (d *ExecDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle,
}
pluginLogFile := filepath.Join(ctx.TaskDir.Dir, "executor.out")
pluginConfig := &plugin.ClientConfig{
Cmd: exec.Command(bin, "executor", pluginLogFile),
Cmd: exec.Command(bin, "executor", pluginLogFile, ctx.LogLevel),
}

exec, pluginClient, err := createExecutor(pluginConfig, d.config.LogOutput, d.config)
Expand Down
2 changes: 1 addition & 1 deletion client/driver/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ func (e *UniversalExecutor) pidStats() (map[string]*cstructs.ResourceUsage, erro
for pid, np := range pids {
p, err := process.NewProcess(int32(pid))
if err != nil {
e.logger.Printf("[DEBUG] executor: unable to create new process with pid: %v", pid)
e.logger.Printf("[TRACE] executor: unable to create new process with pid: %v", pid)
continue
}
ms := &cstructs.MemoryStats{}
Expand Down
2 changes: 1 addition & 1 deletion client/driver/java.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (d *JavaDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle,

pluginLogFile := filepath.Join(ctx.TaskDir.Dir, "executor.out")
pluginConfig := &plugin.ClientConfig{
Cmd: exec.Command(bin, "executor", pluginLogFile),
Cmd: exec.Command(bin, "executor", pluginLogFile, ctx.LogLevel),
}

execIntf, pluginClient, err := createExecutor(pluginConfig, d.config.LogOutput, d.config)
Expand Down
13 changes: 11 additions & 2 deletions client/driver/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package driver

import (
"io"
"io/ioutil"
"log"
"net"
"strings"

"github.com/hashicorp/go-plugin"
"github.com/hashicorp/logutils"
)

var HandshakeConfig = plugin.HandshakeConfig{
Expand All @@ -14,9 +17,15 @@ var HandshakeConfig = plugin.HandshakeConfig{
MagicCookieValue: "e4327c2e01eabfd75a8a67adb114fb34a757d57eee7728d857a8cec6e91a7255",
}

func GetPluginMap(w io.Writer) map[string]plugin.Plugin {
func GetPluginMap(w io.Writer, logLevel string) map[string]plugin.Plugin {
e := new(ExecutorPlugin)
e.logger = log.New(w, "", log.LstdFlags)
filter := &logutils.LevelFilter{
Levels: []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERR"},
MinLevel: logutils.LogLevel(strings.ToUpper(logLevel)),
Writer: ioutil.Discard,
}

e.logger = log.New(filter, "", log.LstdFlags)

s := new(SyslogCollectorPlugin)
s.logger = log.New(w, "", log.LstdFlags)
Expand Down
2 changes: 1 addition & 1 deletion client/driver/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (d *QemuDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle,

pluginLogFile := filepath.Join(ctx.TaskDir.Dir, "executor.out")
pluginConfig := &plugin.ClientConfig{
Cmd: exec.Command(bin, "executor", pluginLogFile),
Cmd: exec.Command(bin, "executor", pluginLogFile, ctx.LogLevel),
}

exec, pluginClient, err := createExecutor(pluginConfig, d.config.LogOutput, d.config)
Expand Down
2 changes: 1 addition & 1 deletion client/driver/raw_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (d *RawExecDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandl
}
pluginLogFile := filepath.Join(ctx.TaskDir.Dir, "executor.out")
pluginConfig := &plugin.ClientConfig{
Cmd: exec.Command(bin, "executor", pluginLogFile),
Cmd: exec.Command(bin, "executor", pluginLogFile, ctx.LogLevel),
}

exec, pluginClient, err := createExecutor(pluginConfig, d.config.LogOutput, d.config)
Expand Down
2 changes: 1 addition & 1 deletion client/driver/rkt.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ func (d *RktDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, e

pluginLogFile := filepath.Join(ctx.TaskDir.Dir, fmt.Sprintf("%s-executor.out", task.Name))
pluginConfig := &plugin.ClientConfig{
Cmd: exec.Command(bin, "executor", pluginLogFile),
Cmd: exec.Command(bin, "executor", pluginLogFile, ctx.LogLevel),
}

execIntf, pluginClient, err := createExecutor(pluginConfig, d.config.LogOutput, d.config)
Expand Down
27 changes: 1 addition & 26 deletions client/driver/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/hashicorp/go-plugin"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/client/driver/executor"
"github.com/hashicorp/nomad/client/driver/logging"
cstructs "github.com/hashicorp/nomad/client/driver/structs"
"github.com/hashicorp/nomad/nomad/structs"
)
Expand All @@ -23,7 +22,7 @@ import (
func createExecutor(config *plugin.ClientConfig, w io.Writer,
clientConfig *config.Config) (executor.Executor, *plugin.Client, error) {
config.HandshakeConfig = HandshakeConfig
config.Plugins = GetPluginMap(w)
config.Plugins = GetPluginMap(w, clientConfig.LogLevel)
config.MaxPort = clientConfig.ClientMaxPort
config.MinPort = clientConfig.ClientMinPort

Expand All @@ -47,30 +46,6 @@ func createExecutor(config *plugin.ClientConfig, w io.Writer,
return executorPlugin, executorClient, nil
}

func createLogCollector(config *plugin.ClientConfig, w io.Writer,
clientConfig *config.Config) (logging.LogCollector, *plugin.Client, error) {
config.HandshakeConfig = HandshakeConfig
config.Plugins = GetPluginMap(w)
config.MaxPort = clientConfig.ClientMaxPort
config.MinPort = clientConfig.ClientMinPort
if config.Cmd != nil {
isolateCommand(config.Cmd)
}

syslogClient := plugin.NewClient(config)
rpcCLient, err := syslogClient.Client()
if err != nil {
return nil, nil, fmt.Errorf("error creating rpc client for syslog plugin: %v", err)
}

raw, err := rpcCLient.Dispense("syslogcollector")
if err != nil {
return nil, nil, fmt.Errorf("unable to dispense the syslog plugin: %v", err)
}
logCollector := raw.(logging.LogCollector)
return logCollector, syslogClient, nil
}

func consulContext(clientConfig *config.Config, containerID string) *executor.ConsulContext {
return &executor.ConsulContext{
ConsulConfig: clientConfig.ConsulConfig,
Expand Down
4 changes: 2 additions & 2 deletions client/task_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (r *TaskRunner) RestoreState() error {
return err
}

ctx := driver.NewExecContext(r.taskDir, r.alloc.ID)
ctx := driver.NewExecContext(r.taskDir, r.alloc.ID, r.config.LogLevel)
handle, err := d.Open(ctx, snap.HandleID)

// In the case it fails, we relaunch the task in the Run() method.
Expand Down Expand Up @@ -1071,7 +1071,7 @@ func (r *TaskRunner) startTask() error {
}

// Run prestart
ctx := driver.NewExecContext(r.taskDir, r.alloc.ID)
ctx := driver.NewExecContext(r.taskDir, r.alloc.ID, r.config.LogLevel)
if err := drv.Prestart(ctx, r.task); err != nil {
wrapped := fmt.Errorf("failed to initialize task %q for alloc %q: %v",
r.task.Name, r.alloc.ID, err)
Expand Down
1 change: 1 addition & 0 deletions command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ func (a *Agent) clientConfig() (*clientconfig.Config, error) {
conf.RPCHandler = a.server
}
conf.LogOutput = a.logOutput
conf.LogLevel = a.config.LogLevel
conf.DevMode = a.config.DevMode
if a.config.Region != "" {
conf.Region = a.config.Region
Expand Down
7 changes: 4 additions & 3 deletions command/executor_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,20 @@ func (e *ExecutorPluginCommand) Synopsis() string {
}

func (e *ExecutorPluginCommand) Run(args []string) int {
if len(args) == 0 {
e.Ui.Error("log output file isn't provided")
if len(args) != 2 {
e.Ui.Error("log output file and log level are not provided")
return 1
}
logFileName := args[0]
logLevel := args[1]
stdo, err := os.OpenFile(logFileName, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
if err != nil {
e.Ui.Error(err.Error())
return 1
}
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: driver.HandshakeConfig,
Plugins: driver.GetPluginMap(stdo),
Plugins: driver.GetPluginMap(stdo, logLevel),
})
return 0
}
9 changes: 5 additions & 4 deletions command/syslog_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,24 @@ func (e *SyslogPluginCommand) Help() string {
}

func (s *SyslogPluginCommand) Synopsis() string {
return "internal - lanch a syslog collector plugin"
return "internal - launch a syslog collector plugin"
}

func (s *SyslogPluginCommand) Run(args []string) int {
if len(args) == 0 {
s.Ui.Error("log output file isn't provided")
if len(args) == 2 {
s.Ui.Error("log output file and log level are not provided")
return 1
}
logFileName := args[0]
logLevel := args[1]
stdo, err := os.OpenFile(logFileName, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
if err != nil {
s.Ui.Error(err.Error())
return 1
}
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: driver.HandshakeConfig,
Plugins: driver.GetPluginMap(stdo),
Plugins: driver.GetPluginMap(stdo, logLevel),
})

return 0
Expand Down

0 comments on commit 5c692c9

Please sign in to comment.