feature: Save logs! #174 (#300)

This commit is contained in:
James Read 2024-04-28 08:58:25 +01:00 committed by GitHub
parent dc6f6c2896
commit 8fd98874e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 63 additions and 0 deletions

View File

@ -22,6 +22,7 @@ type Action struct {
MaxRate []RateSpec MaxRate []RateSpec
Arguments []ActionArgument Arguments []ActionArgument
PopupOnStart string PopupOnStart string
SaveLogs SaveLogsConfig
} }
// ActionArgument objects appear on Actions. // ActionArgument objects appear on Actions.
@ -119,10 +120,16 @@ type Config struct {
InsecureAllowDumpActionMap bool InsecureAllowDumpActionMap bool
InsecureAllowDumpJwtClaims bool InsecureAllowDumpJwtClaims bool
Prometheus PrometheusConfig Prometheus PrometheusConfig
SaveLogs SaveLogsConfig
usedConfigDir string usedConfigDir string
} }
type SaveLogsConfig struct {
ResultsDirectory string
OutputDirectory string
}
type LogDebugOptions struct { type LogDebugOptions struct {
SingleFrontendRequests bool SingleFrontendRequests bool
SingleFrontendRequestHeaders bool SingleFrontendRequestHeaders bool

View File

@ -9,13 +9,16 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promauto"
"gopkg.in/yaml.v3"
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path"
"runtime" "runtime"
"strings" "strings"
"sync" "sync"
@ -105,6 +108,7 @@ func DefaultExecutor() *Executor {
stepExec, stepExec,
stepExecAfter, stepExecAfter,
stepLogFinish, stepLogFinish,
stepSaveLog,
stepTrigger, stepTrigger,
} }
@ -324,6 +328,8 @@ func stepLogStart(req *ExecutionRequest) bool {
} }
func stepLogFinish(req *ExecutionRequest) bool { func stepLogFinish(req *ExecutionRequest) bool {
req.logEntry.ExecutionFinished = true
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"actionTitle": req.logEntry.ActionTitle, "actionTitle": req.logEntry.ActionTitle,
"stdout": req.logEntry.Stdout, "stdout": req.logEntry.Stdout,
@ -460,3 +466,53 @@ func stepTrigger(req *ExecutionRequest) bool {
return true 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)
}
}
}