forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
47 lines (43 loc) · 1.3 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package syslogexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/syslogexporter"
import "fmt"
type errorWithCount struct {
err error
count int
}
// deduplicateErrors replaces duplicate instances of the same error in a slice
// with a single error containing the number of times it occurred added as a suffix.
// For example, three occurrences of "error: 502 Bad Gateway"
// are replaced with a single instance of "error: 502 Bad Gateway (x3)".
func deduplicateErrors(errs []error) []error {
if len(errs) < 2 {
return errs
}
errorsWithCounts := []errorWithCount{}
for _, err := range errs {
found := false
for i := range errorsWithCounts {
if errorsWithCounts[i].err.Error() == err.Error() {
found = true
errorsWithCounts[i].count++
break
}
}
if !found {
errorsWithCounts = append(errorsWithCounts, errorWithCount{
err: err,
count: 1,
})
}
}
var uniqueErrors []error
for _, errorWithCount := range errorsWithCounts {
if errorWithCount.count == 1 {
uniqueErrors = append(uniqueErrors, errorWithCount.err)
} else {
uniqueErrors = append(uniqueErrors, fmt.Errorf("%w (x%d)", errorWithCount.err, errorWithCount.count))
}
}
return uniqueErrors
}