-
Notifications
You must be signed in to change notification settings - Fork 53
/
collector_client.go
90 lines (73 loc) · 2.14 KB
/
collector_client.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
78
79
80
81
82
83
84
85
86
87
88
89
90
package lightstep
import (
"context"
"io"
"net/http"
"github.com/lightstep/lightstep-tracer-common/golang/gogo/collectorpb"
)
var accessTokenHeader = http.CanonicalHeaderKey("Lightstep-Access-Token")
// Connection describes a closable connection. Exposed for testing.
type Connection interface {
io.Closer
}
// ConnectorFactory is for testing purposes.
type ConnectorFactory func() (interface{}, Connection, error)
// collectorResponse encapsulates internal grpc/http responses.
type collectorResponse interface {
GetErrors() []string
Disable() bool
DevMode() bool
}
// Collector encapsulates custom transport of protobuf messages
type Collector interface {
Report(context.Context, *collectorpb.ReportRequest) (*collectorpb.ReportResponse, error)
}
type reportRequest struct {
protoRequest *collectorpb.ReportRequest
httpRequest *http.Request
}
// SplitByParts splits reportRequest into given number of parts.
// Beware, that parts=0 panics.
func (rr reportRequest) SplitByParts(parts int) []reportRequest {
if rr.protoRequest == nil || len(rr.protoRequest.Spans) == 0 || parts <= 1 {
return []reportRequest{rr}
}
spans := rr.protoRequest.Spans
maxSize := len(rr.protoRequest.Spans) / parts
if len(rr.protoRequest.Spans)%parts > 0 {
maxSize++
}
var rrs []reportRequest
for len(spans) > 0 {
s := maxSize
if len(spans) < s {
s = len(spans)
}
r := rr
r.protoRequest.Spans = make([]*collectorpb.Span, s)
copy(r.protoRequest.Spans, spans[:s])
spans = spans[s:]
rrs = append(rrs, r)
}
return rrs
}
// collectorClient encapsulates internal grpc/http transports.
type collectorClient interface {
Report(context.Context, reportRequest) (collectorResponse, error)
Translate(*collectorpb.ReportRequest) (reportRequest, error)
ConnectClient() (Connection, error)
ShouldReconnect() bool
}
func newCollectorClient(opts Options) (collectorClient, error) {
if opts.CustomCollector != nil {
return newCustomCollector(opts), nil
}
if opts.UseHttp {
return newHTTPCollectorClient(opts)
}
if opts.UseGRPC {
return newGrpcCollectorClient(opts)
}
// No transport specified, defaulting to HTTP
return newHTTPCollectorClient(opts)
}