Skip to content

Commit

Permalink
feature: Save logs! #174 (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesread committed Apr 28, 2024
1 parent dc6f6c2 commit 8fd9887
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Action struct {
MaxRate []RateSpec
Arguments []ActionArgument
PopupOnStart string
SaveLogs SaveLogsConfig
}

// ActionArgument objects appear on Actions.
Expand Down Expand Up @@ -119,10 +120,16 @@ type Config struct {
InsecureAllowDumpActionMap bool
InsecureAllowDumpJwtClaims bool
Prometheus PrometheusConfig
SaveLogs SaveLogsConfig

usedConfigDir string
}

type SaveLogsConfig struct {
ResultsDirectory string
OutputDirectory string
}

type LogDebugOptions struct {
SingleFrontendRequests bool
SingleFrontendRequestHeaders bool
Expand Down
56 changes: 56 additions & 0 deletions internal/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"gopkg.in/yaml.v3"

"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"runtime"
"strings"
"sync"
Expand Down Expand Up @@ -105,6 +108,7 @@ func DefaultExecutor() *Executor {
stepExec,
stepExecAfter,
stepLogFinish,
stepSaveLog,
stepTrigger,
}

Expand Down Expand Up @@ -324,6 +328,8 @@ func stepLogStart(req *ExecutionRequest) bool {
}

func stepLogFinish(req *ExecutionRequest) bool {
req.logEntry.ExecutionFinished = true

log.WithFields(log.Fields{
"actionTitle": req.logEntry.ActionTitle,
"stdout": req.logEntry.Stdout,
Expand Down Expand Up @@ -460,3 +466,53 @@ func stepTrigger(req *ExecutionRequest) bool {

return true
}

func stepSaveLog(req *ExecutionRequest) bool {
filename := fmt.Sprintf("%v.%v.%v", req.logEntry.ActionTitle, req.logEntry.DatetimeStarted.Unix(), req.logEntry.ExecutionTrackingID)

saveLogResults(req, filename)
saveLogOutput(req, filename)

return true
}

func firstNonEmpty(one, two string) string {
if one != "" {
return one
}

return two
}

func saveLogResults(req *ExecutionRequest, filename string) {
dir := firstNonEmpty(req.Action.SaveLogs.ResultsDirectory, req.Cfg.SaveLogs.ResultsDirectory)

if dir != "" {
data, err := yaml.Marshal(req.logEntry)

if err != nil {
log.Warnf("%v", err)
}

filepath := path.Join(dir, filename+".yaml")
err = ioutil.WriteFile(filepath, data, 0644)

if err != nil {
log.Warnf("%v", err)
}
}
}

func saveLogOutput(req *ExecutionRequest, filename string) {
dir := firstNonEmpty(req.Action.SaveLogs.OutputDirectory, req.Cfg.SaveLogs.OutputDirectory)

if dir != "" {
data := req.logEntry.Stdout + "\n" + req.logEntry.Stderr
filepath := path.Join(dir, filename+".log")
err := ioutil.WriteFile(filepath, []byte(data), 0644)

if err != nil {
log.Warnf("%v", err)
}
}
}

0 comments on commit 8fd9887

Please sign in to comment.