From 6131333a93df2bb41ea9d78c574c18cb93d3b534 Mon Sep 17 00:00:00 2001 From: David Ashby Date: Mon, 7 Feb 2022 21:55:35 -0500 Subject: [PATCH] suppress error logging when continuous --- main.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index fa4226e..c526517 100644 --- a/main.go +++ b/main.go @@ -55,8 +55,24 @@ var ( Name: "temp_nozzle", Help: "Temperature, in celsius, of the print nozzle", }) + errCount = 0 ) +func errLog(err error) { + errCount++ + // reset metrics + opsFlowFactor.Set(0) + opsPrintSpeed.Set(0) + opsZPosition.Set(0) + opsBed.Set(0) + opsNozzle.Set(0) + if errCount > 5 { + log.Printf("suppressing further error logging") + return + } + log.Printf("error retrieving telemetry: %v", err) +} + func recordMetrics(ctx context.Context, config Config) { go func() { for { @@ -68,20 +84,21 @@ func recordMetrics(ctx context.Context, config Config) { time.Sleep(time.Second * time.Duration(config.QueryInterval)) res, err := http.Get(config.QueryHostname + "/api/telemetry") if err != nil { - log.Printf("error retrieving telemetry: %v", err) + errLog(err) break } b, err := io.ReadAll(res.Body) if err != nil { - log.Printf("error retrieving telemetry: %v", err) + errLog(err) break } t := &Metrics{} err = json.Unmarshal(b, t) if err != nil { - log.Printf("error parsing telemetry: %v", err) + errLog(err) break } + errCount = 0 opsFlowFactor.Set(float64(t.FlowFactor)) opsPrintSpeed.Set(float64(t.PrintingSpeed)) opsZPosition.Set(t.ZPosition)