Skip to content

Commit 53cc995

Browse files
committed
Do not hang on error during the scraping
When there was an error during the scraping, there could be a chan waiting for a response forever if there was an error during the export of metrics. That would cause a pretty big memory leak.
1 parent a4318fd commit 53cc995

File tree

1 file changed

+4
-13
lines changed

1 file changed

+4
-13
lines changed

exporter/exporter.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,13 @@ func NewExporter(ctx context.Context, target, uri, profile, model string, exclud
142142
ExpectContinueTimeout: 1 * time.Second,
143143
TLSClientConfig: &tls.Config{
144144
InsecureSkipVerify: config.GetConfig().SSLVerify,
145+
Renegotiation: tls.RenegotiateOnceAsClient,
145146
},
146147
TLSHandshakeTimeout: 10 * time.Second,
147148
}
148149

150+
defer tr.CloseIdleConnections()
151+
149152
retryClient := retryablehttp.NewClient()
150153
retryClient.CheckRetry = retryablehttp.ErrorPropagatedRetryPolicy
151154
retryClient.HTTPClient.Transport = tr
@@ -526,11 +529,7 @@ func (e *Exporter) collectMetrics(metrics chan<- prometheus.Metric) {
526529
}
527530

528531
func (e *Exporter) scrape() {
529-
530-
var result uint8
531532
state := uint8(1)
532-
scrapes := len(e.pool.Tasks)
533-
scrapeChan := make(chan uint8, scrapes)
534533

535534
// Concurrently call the endpoints to help prevent reaching the maxiumum number of 4 simultaneous sessions
536535
e.pool.Run()
@@ -562,22 +561,14 @@ func (e *Exporter) scrape() {
562561
}
563562

564563
if err != nil {
564+
state = 0
565565
log.Error("error exporting metrics", zap.Error(err), zap.String("url", task.URL), zap.Any("trace_id", e.ctx.Value("traceID")))
566566
continue
567567
}
568-
scrapeChan <- 1
569-
}
570-
571-
// Get scrape results from goroutine(s) and perform bitwise AND, any failures should
572-
// result in a scrape failure
573-
for i := 0; i < scrapes; i++ {
574-
result = <-scrapeChan
575-
state &= result
576568
}
577569

578570
var upMetric = (*e.DeviceMetrics)["up"]
579571
(*upMetric)["up"].WithLabelValues().Set(float64(state))
580-
581572
}
582573

583574
func (e *Exporter) GetContext() context.Context {

0 commit comments

Comments
 (0)