forked from jaegertracing/jaeger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
230 lines (197 loc) · 7.39 KB
/
builder.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package app
import (
"fmt"
"net/http"
"github.com/apache/thrift/lib/go/thrift"
"github.com/pkg/errors"
"github.com/uber/jaeger-lib/metrics"
"github.com/uber/tchannel-go"
"go.uber.org/zap"
"github.com/jaegertracing/jaeger/cmd/agent/app/httpserver"
"github.com/jaegertracing/jaeger/cmd/agent/app/processors"
"github.com/jaegertracing/jaeger/cmd/agent/app/reporter"
tchreporter "github.com/jaegertracing/jaeger/cmd/agent/app/reporter/tchannel"
"github.com/jaegertracing/jaeger/cmd/agent/app/servers"
"github.com/jaegertracing/jaeger/cmd/agent/app/servers/thriftudp"
jmetrics "github.com/jaegertracing/jaeger/pkg/metrics"
zipkinThrift "github.com/jaegertracing/jaeger/thrift-gen/agent"
jaegerThrift "github.com/jaegertracing/jaeger/thrift-gen/jaeger"
)
const (
defaultQueueSize = 1000
defaultMaxPacketSize = 65000
defaultServerWorkers = 10
defaultMinPeers = 3
defaultHTTPServerHostPort = ":5778"
jaegerModel model = "jaeger"
zipkinModel = "zipkin"
compactProtocol protocol = "compact"
binaryProtocol = "binary"
)
type model string
type protocol string
var (
errNoReporters = errors.New("agent requires at least one Reporter")
protocolFactoryMap = map[protocol]thrift.TProtocolFactory{
compactProtocol: thrift.NewTCompactProtocolFactory(),
binaryProtocol: thrift.NewTBinaryProtocolFactoryDefault(),
}
)
// Builder Struct to hold configurations
type Builder struct {
Processors []ProcessorConfiguration `yaml:"processors"`
HTTPServer HTTPServerConfiguration `yaml:"httpServer"`
Metrics jmetrics.Builder `yaml:"metrics"`
tchreporter.Builder `yaml:",inline"`
otherReporters []reporter.Reporter
metricsFactory metrics.Factory
}
// ProcessorConfiguration holds config for a processor that receives spans from Server
type ProcessorConfiguration struct {
Workers int `yaml:"workers"`
Model model `yaml:"model"`
Protocol protocol `yaml:"protocol"`
Server ServerConfiguration `yaml:"server"`
}
// ServerConfiguration holds config for a server that receives spans from the network
type ServerConfiguration struct {
QueueSize int `yaml:"queueSize"`
MaxPacketSize int `yaml:"maxPacketSize"`
HostPort string `yaml:"hostPort" validate:"nonzero"`
}
// HTTPServerConfiguration holds config for a server providing sampling strategies and baggage restrictions to clients
type HTTPServerConfiguration struct {
HostPort string `yaml:"hostPort" validate:"nonzero"`
}
// WithReporter adds auxiliary reporters.
func (b *Builder) WithReporter(r reporter.Reporter) *Builder {
b.otherReporters = append(b.otherReporters, r)
return b
}
// WithMetricsFactory sets an externally initialized metrics factory.
func (b *Builder) WithMetricsFactory(mf metrics.Factory) *Builder {
b.metricsFactory = mf
return b
}
func (b *Builder) createMainReporter(mFactory metrics.Factory, logger *zap.Logger) (*tchreporter.Reporter, error) {
return b.CreateReporter(mFactory, logger)
}
func (b *Builder) getMetricsFactory() (metrics.Factory, error) {
if b.metricsFactory != nil {
return b.metricsFactory, nil
}
return b.Metrics.CreateMetricsFactory("jaeger_agent")
}
// CreateAgent creates the Agent
func (b *Builder) CreateAgent(logger *zap.Logger) (*Agent, error) {
mFactory, err := b.getMetricsFactory()
if err != nil {
return nil, errors.Wrap(err, "cannot create metrics factory")
}
mainReporter, err := b.createMainReporter(mFactory, logger)
if err != nil {
return nil, errors.Wrap(err, "cannot create main Reporter")
}
var rep reporter.Reporter = mainReporter
if len(b.otherReporters) > 0 {
reps := append([]reporter.Reporter{mainReporter}, b.otherReporters...)
rep = reporter.NewMultiReporter(reps...)
}
processors, err := b.GetProcessors(rep, mFactory)
if err != nil {
return nil, err
}
httpServer := b.HTTPServer.GetHTTPServer(b.CollectorServiceName, mainReporter.Channel(), mFactory)
if h := b.Metrics.Handler(); b.metricsFactory != nil && h != nil {
httpServer.Handler.(*http.ServeMux).Handle(b.Metrics.HTTPRoute, h)
}
return NewAgent(processors, httpServer, logger), nil
}
// GetProcessors creates Processors with attached Reporter
func (b *Builder) GetProcessors(rep reporter.Reporter, mFactory metrics.Factory) ([]processors.Processor, error) {
retMe := make([]processors.Processor, len(b.Processors))
for idx, cfg := range b.Processors {
protoFactory, ok := protocolFactoryMap[cfg.Protocol]
if !ok {
return nil, fmt.Errorf("cannot find protocol factory for protocol %v", cfg.Protocol)
}
var handler processors.AgentProcessor
switch cfg.Model {
case jaegerModel:
handler = jaegerThrift.NewAgentProcessor(rep)
case zipkinModel:
handler = zipkinThrift.NewAgentProcessor(rep)
default:
return nil, fmt.Errorf("cannot find agent processor for data model %v", cfg.Model)
}
metrics := mFactory.Namespace("", map[string]string{
"protocol": string(cfg.Protocol),
"model": string(cfg.Model),
})
processor, err := cfg.GetThriftProcessor(metrics, protoFactory, handler)
if err != nil {
return nil, err
}
retMe[idx] = processor
}
return retMe, nil
}
// GetHTTPServer creates an HTTP server that provides sampling strategies and baggage restrictions to client libraries.
func (c HTTPServerConfiguration) GetHTTPServer(svc string, channel *tchannel.Channel, mFactory metrics.Factory) *http.Server {
mgr := httpserver.NewCollectorProxy(svc, channel, mFactory)
if c.HostPort == "" {
c.HostPort = defaultHTTPServerHostPort
}
return httpserver.NewHTTPServer(c.HostPort, mgr, mFactory)
}
// GetThriftProcessor gets a TBufferedServer backed Processor using the collector configuration
func (c *ProcessorConfiguration) GetThriftProcessor(
mFactory metrics.Factory,
factory thrift.TProtocolFactory,
handler processors.AgentProcessor,
) (processors.Processor, error) {
c.applyDefaults()
server, err := c.Server.getUDPServer(mFactory)
if err != nil {
return nil, err
}
return processors.NewThriftProcessor(server, c.Workers, mFactory, factory, handler)
}
func (c *ProcessorConfiguration) applyDefaults() {
c.Workers = defaultInt(c.Workers, defaultServerWorkers)
}
func (c *ServerConfiguration) applyDefaults() {
c.QueueSize = defaultInt(c.QueueSize, defaultQueueSize)
c.MaxPacketSize = defaultInt(c.MaxPacketSize, defaultMaxPacketSize)
}
// getUDPServer gets a TBufferedServer backed server using the server configuration
func (c *ServerConfiguration) getUDPServer(mFactory metrics.Factory) (servers.Server, error) {
c.applyDefaults()
if c.HostPort == "" {
return nil, fmt.Errorf("no host:port provided for udp server: %+v", *c)
}
transport, err := thriftudp.NewTUDPServerTransport(c.HostPort)
if err != nil {
return nil, err
}
return servers.NewTBufferedServer(transport, c.QueueSize, c.MaxPacketSize, mFactory)
}
func defaultInt(value int, defaultVal int) int {
if value == 0 {
value = defaultVal
}
return value
}