Skip to content

Commit

Permalink
Improve logging with multiwriter
Browse files Browse the repository at this point in the history
Signed-off-by: Kedar Vijay Kulkarni <[email protected]>
  • Loading branch information
Kedar Vijay Kulkarni committed Nov 8, 2021
1 parent d9838d4 commit c6cdc83
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ This tool allows OpenShift users to run a watcher for Prometheus queries and def
* [x] Add parameter for clearing/not-clearing screen
* [x] Add Parameter for timeout
* [x] Add a Makefile
* [x] File logging the output
* [x] Print output to screen even when logging enabled - simultaneously
* [ ] Notify/Do Something when results don't match conditions
* [ ] Spawn goroutines to keep running queries and evaluating results to handle scale - e.g. when we have very large number of queries in the yaml file, we can divide and concurrently run queries
* [ ] File logging the output, screen will give current status, look at Prometheus alerts



Expand Down
Binary file modified bin/cpa
Binary file not shown.
22 changes: 19 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"io"
"log"
"os"
"time"

"github.com/alexflint/go-arg"
Expand All @@ -15,13 +17,27 @@ import (

func main() {
var args struct {
NoClrscr bool `arg:"--noclrscr" help:"Do not clear screen after each iteration." default:"false"`
Queries string `help:"queries file to use" default:"queries.yaml"`
Timeout time.Duration `help:"Duration to run Continuous Performance Analysis. You can pass values like 4h or 1h10m10s" default:"4h"`
NoClrscr bool `arg:"--noclrscr" help:"Do not clear screen after each iteration. Clears screen by default." default:"false"`
Queries string `arg:"-q,--queries" help:"queries file to use" default:"queries.yaml"`
Timeout time.Duration `arg:"-t,--timeout" help:"Duration to run Continuous Performance Analysis. You can pass values like 4h or 1h10m10s" default:"4h"`
LogOutput bool `arg:"-l,--log-output" help:"Output will be stored in a log file instead of stdout." default:"false"`
}
arg.MustParse(&args)

o.RegisterFailHandler(g.Fail)
if args.LogOutput {
f, err := os.OpenFile("cpa.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
multiWriter := io.MultiWriter(os.Stdout, f)
if err != nil {
log.Fatal(err)
}

//defer to close when you're done with it, not because you think it's idiomatic!
defer f.Close()

//set output of logs to f
log.SetOutput(multiWriter)
}

oc := exutil.NewCLI("prometheus-cpa", exutil.KubeConfigPath())
// secrets, err := oc.AdminKubeClient().CoreV1().Secrets("openshift-monitoring").List(metav1.ListOptions{})
Expand Down

0 comments on commit c6cdc83

Please sign in to comment.