Skip to content

Commit

Permalink
Add statistics to forwarder
Browse files Browse the repository at this point in the history
  • Loading branch information
s-l-teichmann committed Aug 25, 2023
1 parent c86656c commit 0148d18
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
14 changes: 14 additions & 0 deletions cmd/csaf_downloader/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion cmd/csaf_downloader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 0148d18

Please sign in to comment.