From 52bd32914a6184ee4d403f8418eb0546074ac5bb Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Sat, 18 Mar 2023 13:48:41 +0100 Subject: Print metrics based on flag duration to stderr --- cmd_collect.go | 2 +- internal/ctflag/ctflag.go | 7 +++++++ main.go | 14 +++++++++----- utils_housekeep.go | 7 +++---- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/cmd_collect.go b/cmd_collect.go index 0254bb3..2444d95 100644 --- a/cmd_collect.go +++ b/cmd_collect.go @@ -58,7 +58,7 @@ func collect(opts options) error { go func() { await.Add(1) defer await.Done() - handleMetrics(ctx, metricsCh, logs(md)) + handleMetrics(ctx, opts, logs(md), metricsCh) }() defer cancel() diff --git a/internal/ctflag/ctflag.go b/internal/ctflag/ctflag.go index 1309afd..cf8d7db 100644 --- a/internal/ctflag/ctflag.go +++ b/internal/ctflag/ctflag.go @@ -31,6 +31,7 @@ package ctflag import ( "flag" "fmt" + "time" ) // NewFlagSet outputs a new flag set that continues on errors without standard @@ -77,6 +78,12 @@ func Uint64(fs *flag.FlagSet, opt *uint64, short, long string, value uint64) { fs.Uint64Var(opt, long, value, "") } +// Duration adds a new duration option to a flag set +func Duration(fs *flag.FlagSet, opt *time.Duration, short, long string, value time.Duration) { + fs.DurationVar(opt, short, value, "") + fs.DurationVar(opt, long, value, "") +} + // Bool adds a new bool option to a flag set func Bool(fs *flag.FlagSet, opt *bool, short, long string, value bool) { fs.BoolVar(opt, short, value, "") diff --git a/main.go b/main.go index f4763ac..b78b007 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( "fmt" "log" "os" + "time" "git.cs.kau.se/rasmoste/ct-sans/internal/ctflag" ) @@ -43,15 +44,17 @@ Options: -k, --batch-disk: Certificate batch size before persisting (Default: 16384) -q, --batch-req: Certificate batch size to use in request (Default: 512) -a, --http-agent: HTTP agent to use in all request (Default: "git.cs.kau.se/rasmoste/ct-sans") + -m, --metrics: How often to emit metrics to stderr (Default: 15s) ` type options struct { - Directory string - WorkersPerLog uint64 - PersistSize uint64 - BatchSize uint64 - HTTPAgent string + Directory string + WorkersPerLog uint64 + PersistSize uint64 + BatchSize uint64 + HTTPAgent string + MetricsInterval time.Duration logDirectory string metadataFile string @@ -78,6 +81,7 @@ func main() { ctflag.Uint64(&fs, &opts.PersistSize, "batch-disk", "k", 16384) ctflag.Uint64(&fs, &opts.BatchSize, "batch-req", "q", 512) ctflag.String(&fs, &opts.HTTPAgent, "http-agent", "a", "git.cs.kau.se/rasmoste/ct-sans") + ctflag.Duration(&fs, &opts.MetricsInterval, "metrics", "m", 15*time.Second) // Parse command-line options and hardcode additional values if err := ctflag.Parse(fs, os.Args[2:]); err != nil { diff --git a/utils_housekeep.go b/utils_housekeep.go index 670a95b..d34292f 100644 --- a/utils_housekeep.go +++ b/utils_housekeep.go @@ -3,7 +3,6 @@ package main import ( "context" "fmt" - logger "log" "os" "os/signal" "syscall" @@ -39,7 +38,7 @@ func (m *metrics) update(other metrics) { m.avg = float64(m.NumEntries) / float64((other.Timestamp - m.Timestamp)) } -func handleMetrics(ctx context.Context, metricsCh chan metrics, logs []metadata.Log) { +func handleMetrics(ctx context.Context, opts options, logs []metadata.Log, metricsCh chan metrics) { sum := make(map[string]metrics) for _, log := range logs { sum[*log.Description] = metrics{ @@ -48,7 +47,7 @@ func handleMetrics(ctx context.Context, metricsCh chan metrics, logs []metadata. } } - ticker := time.NewTicker(15 * time.Second) + ticker := time.NewTicker(opts.MetricsInterval) defer ticker.Stop() for { select { @@ -62,7 +61,7 @@ func handleMetrics(ctx context.Context, metricsCh chan metrics, logs []metadata. for _, log := range logs { output += sum[*log.Description].String() } - logger.Printf("INFO: periodic status update\n\n%s\n\n", output) + fmt.Fprintf(os.Stderr, "INFO: periodic status update\n\n%s\n\n", output) } } } -- cgit v1.2.3