From 0148d186cdf1417a896e800ec18a726b0cb8b759 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Fri, 25 Aug 2023 19:05:28 +0200 Subject: [PATCH] Add statistics to forwarder --- cmd/csaf_downloader/forwarder.go | 14 ++++++++++++++ cmd/csaf_downloader/main.go | 5 ++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/cmd/csaf_downloader/forwarder.go b/cmd/csaf_downloader/forwarder.go index 689f0d5f..6efdfbe2 100644 --- a/cmd/csaf_downloader/forwarder.go +++ b/cmd/csaf_downloader/forwarder.go @@ -50,6 +50,9 @@ type forwarder struct { cfg *config cmds chan func(*forwarder) client util.Client + + failed int + succeeded int } // newForwarder creates a new forwarder. @@ -75,6 +78,15 @@ func (f *forwarder) close() { close(f.cmds) } +// log logs the current statistics. +func (f *forwarder) log() { + f.cmds <- func(f *forwarder) { + slog.Info("Forward statistics", + "succeeded", f.succeeded, + "failed", f.failed) + } +} + // httpClient returns a cached HTTP client used for uploading // the advisories to the configured HTTP endpoint. func (f *forwarder) httpClient() util.Client { @@ -204,6 +216,7 @@ func (f *forwarder) storeFailedAdvisory(filename, doc, sha256, sha512 string) er // storeFailed is a logging wrapper around storeFailedAdvisory. func (f *forwarder) storeFailed(filename, doc, sha256, sha512 string) { + f.failed++ if err := f.storeFailedAdvisory(filename, doc, sha256, sha512); err != nil { slog.Error("Storing advisory failed forwarding failed", "error", err) @@ -248,6 +261,7 @@ func (f *forwarder) forward( "status_code", res.StatusCode) f.storeFailed(filename, doc, sha256, sha512) } else { + f.succeeded++ slog.Debug( "forwarding succeeded", "filename", filename) diff --git a/cmd/csaf_downloader/main.go b/cmd/csaf_downloader/main.go index 8e4b1781..5840791c 100644 --- a/cmd/csaf_downloader/main.go +++ b/cmd/csaf_downloader/main.go @@ -33,7 +33,10 @@ func run(cfg *config, domains []string) error { if cfg.ForwardURL != "" { f := newForwarder(cfg) go f.run() - defer f.close() + defer func() { + f.log() + f.close() + }() d.forwarder = f }