Skip to content

Commit

Permalink
Fixes #2 by removing all panics and replacing it with reasonable beha…
Browse files Browse the repository at this point in the history
…vior (writing to log, or stdout)
  • Loading branch information
Zef Hemel committed Apr 18, 2018
1 parent e08797d commit c38e31a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
3 changes: 2 additions & 1 deletion cmd/ax/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func watchAlerts(ctx context.Context, rc config.RuntimeConfig, alertConfig confi
case "slack":
alerter = slack.New(alertConfig.Name, rc.DataDir, alertConfig.Service)
default:
panic("No such backend")
fmt.Printf("Back-end type not supported: %s\n", alertConfig.Service["backend"])
return
}
query := querySelectorsToQuery(&alertConfig.Selector)
query.Follow = true
Expand Down
9 changes: 6 additions & 3 deletions pkg/backend/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha1"
"encoding/json"
"fmt"
"log"
"strings"
"time"
)
Expand Down Expand Up @@ -88,7 +89,8 @@ func NewLogMessage() LogMessage {
func MustJsonEncode(obj interface{}) string {
buf, err := json.Marshal(obj)
if err != nil {
panic(err)
log.Printf("Could not JSON encode: %+v\n", obj)
return "{}"
}
return string(buf)
}
Expand All @@ -97,7 +99,7 @@ func MustJsonDecode(jsonString string, dst interface{}) {
decoder := json.NewDecoder(strings.NewReader(jsonString))
err := decoder.Decode(dst)
if err != nil {
panic(err)
log.Fatal(err)
}
}

Expand Down Expand Up @@ -141,7 +143,8 @@ func (f QueryFilter) Matches(m LogMessage) bool {
case "!=":
return !ok || (ok && f.Value != fmt.Sprintf("%v", val))
default:
panic("Not supported operator")
fmt.Printf("Not supported operatior: %s\n", f.Operator)
return false
}
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/backend/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package docker
import (
"context"
"fmt"
"log"
"os/exec"
"strings"

Expand All @@ -21,7 +22,8 @@ func GetRunningContainers(pattern string) []string {
}
allContainers, err := exec.Command("docker", flags...).Output()
if err != nil {
panic(err)
log.Printf("Retrieving all containers failed: %v\n", err)
return []string{}
}
return strings.Split(strings.TrimSpace(string(allContainers)), "\n")
}
Expand Down
18 changes: 13 additions & 5 deletions pkg/backend/subprocess/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package subprocess

import (
"context"
"fmt"
"log"
"os/exec"

"github.com/egnyte/ax/pkg/backend/common"
Expand All @@ -13,21 +15,27 @@ type SubprocessClient struct {
}

func (client *SubprocessClient) Query(ctx context.Context, query common.Query) <-chan common.LogMessage {
resultChan := make(chan common.LogMessage)
cmd := exec.Command(client.command[0], client.command[1:]...)
stdOut, err := cmd.StdoutPipe()
if err != nil {
panic(err)
log.Printf("Could not get stdout pipe: %v", err)
close(resultChan)
return resultChan
}
stdOutStream := stream.New(stdOut)
stdErr, err := cmd.StderrPipe()
if err != nil {
panic(err)
log.Printf("Could not get stderr pipe: %v", err)
close(resultChan)
return resultChan
}
stdErrStream := stream.New(stdErr)
if err := cmd.Start(); err != nil {
panic(err)
fmt.Printf("Could not start process: %s because: %v\n", client.command[0], err)
close(resultChan)
return resultChan
}
resultChan := make(chan common.LogMessage)
go func() {
stdOutQuery := stdOutStream.Query(ctx, query)
stdErrQuery := stdErrStream.Query(ctx, query)
Expand Down Expand Up @@ -57,7 +65,7 @@ func (client *SubprocessClient) Query(ctx context.Context, query common.Query) <
}
close(resultChan)
if err := cmd.Wait(); err != nil {
panic(err)
fmt.Printf("Process exited with error: %v\n", err)
}
}()
return resultChan
Expand Down
6 changes: 4 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,11 @@ func init() {
}

// Set up logging
f, err := os.Create(fmt.Sprintf("%s/ax.log", dataDir))
logPath := fmt.Sprintf("%s/ax.log", dataDir)
f, err := os.Create(logPath)
if err != nil {
panic(err)
fmt.Printf("Could not use %s for logging, logging to stdout instead\n", logPath)
return // Skips log.SetOutput, defaults to stdout
}
log.SetOutput(f)
}

0 comments on commit c38e31a

Please sign in to comment.