forked from openzipkin-contrib/zipkin-go-opentracing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
77 lines (63 loc) · 1.77 KB
/
collector.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package zipkintracer
import (
"strings"
"github.com/openzipkin/zipkin-go-opentracing/thrift/gen-go/zipkincore"
)
// Collector represents a Zipkin trace collector, which is probably a set of
// remote endpoints.
type Collector interface {
Collect(*zipkincore.Span) error
Close() error
}
// NopCollector implements Collector but performs no work.
type NopCollector struct{}
// Collect implements Collector.
func (NopCollector) Collect(*zipkincore.Span) error { return nil }
// Close implements Collector.
func (NopCollector) Close() error { return nil }
// MultiCollector implements Collector by sending spans to all collectors.
type MultiCollector []Collector
// Collect implements Collector.
func (c MultiCollector) Collect(s *zipkincore.Span) error {
return c.aggregateErrors(func(coll Collector) error { return coll.Collect(s) })
}
// Close implements Collector.
func (c MultiCollector) Close() error {
return c.aggregateErrors(func(coll Collector) error { return coll.Close() })
}
func (c MultiCollector) aggregateErrors(f func(c Collector) error) error {
var e *collectionError
for i, collector := range c {
if err := f(collector); err != nil {
if e == nil {
e = &collectionError{
errs: make([]error, len(c)),
}
}
e.errs[i] = err
}
}
return e
}
// CollectionError represents an array of errors returned by one or more
// failed Collector methods.
type CollectionError interface {
Error() string
GetErrors() []error
}
type collectionError struct {
errs []error
}
func (c *collectionError) Error() string {
errs := []string{}
for _, err := range c.errs {
if err != nil {
errs = append(errs, err.Error())
}
}
return strings.Join(errs, "; ")
}
// GetErrors implements CollectionError
func (c *collectionError) GetErrors() []error {
return c.errs
}