| 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 |
1×
1×
1×
1×
1×
1×
1×
1×
1×
33×
33×
33×
33×
33×
33×
33×
1×
33×
33×
33×
33×
23×
33×
33×
33×
33×
82×
82×
33×
33×
33×
18×
18×
2×
16×
15×
15×
15×
14×
1×
1×
1×
1×
1×
22×
22×
9×
13×
13×
13×
13×
1×
1×
12×
11×
1×
12×
12×
46×
46×
15×
15×
46×
12×
12×
16×
| // @flow
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import dgram from 'dgram';
import fs from 'fs';
import path from 'path';
import {Thrift} from 'thriftrw';
import NullLogger from '../logger.js';
const HOST = 'localhost';
const PORT = 6832;
const UDP_PACKET_MAX_LENGTH = 65000;
export default class UDPSender {
_host: string;
_port: number;
_maxPacketSize: number;
_process: Process;
_emitSpanBatchOverhead: number;
_logger: Logger;
_client: dgram$Socket;
_agentThrift: Thrift;
_jaegerThrift: Thrift;
_batch: Batch;
_thriftProcessMessage: any;
_maxSpanBytes: number; // maxPacketSize - (batch + tags overhead)
_totalSpanBytes: number; // size of currently batched spans as Thrift bytes
constructor(options: any = {}) {
this._host = options.host || HOST;
this._port = options.port || PORT;
this._maxPacketSize = options.maxPacketSize || UDP_PACKET_MAX_LENGTH;
this._logger = options.logger || new NullLogger();
this._client = dgram.createSocket('udp4');
this._client.on('error', err => {
this._logger.error(`error sending spans over UDP: ${err}`)
})
this._agentThrift = new Thrift({
source: fs.readFileSync(path.join(__dirname, '../thriftrw-idl/agent.thrift'), 'ascii'),
allowOptionalArguments: true,
allowFilesystemAccess: true
});
this._jaegerThrift = new Thrift({
source: fs.readFileSync(path.join(__dirname, '../jaeger-idl/thrift/jaeger.thrift'), 'ascii'),
allowOptionalArguments: true
});
this._totalSpanBytes = 0;
}
_calcBatchSize(batch: Batch) {
return this._agentThrift.Agent.emitBatch.argumentsMessageRW.byteLength(
this._convertBatchToThriftMessage(this._batch)
).length;
}
_calcSpanSize(span: any): number {
return this._jaegerThrift.Span.rw.byteLength(new this._jaegerThrift.Span(span)).length;
}
setProcess(process: Process): void {
// This function is only called once during reporter construction, and thus will
// give us the length of the batch before any spans have been added to the span
// list in batch.
this._process = process;
this._batch = {
'process': this._process,
'spans': []
};
let tagMessages = [];
for (let j = 0; j < this._batch.process.tags.length; j++) {
let tag = this._batch.process.tags[j];
tagMessages.push(new this._jaegerThrift.Tag(tag));
}
this._thriftProcessMessage = new this._jaegerThrift.Process({
serviceName: this._batch.process.serviceName,
tags: tagMessages
});
this._emitSpanBatchOverhead = this._calcBatchSize(this._batch);
this._maxSpanBytes = this._maxPacketSize - this._emitSpanBatchOverhead;
}
append(span: any): SenderResponse {
let spanSize: number = this._calcSpanSize(span);
if (spanSize > this._maxSpanBytes) {
return { err: true, numSpans: 1 };
}
if (this._totalSpanBytes + spanSize <= this._maxSpanBytes) {
this._batch.spans.push(span);
this._totalSpanBytes += spanSize;
if (this._totalSpanBytes < this._maxSpanBytes) {
// still have space in the buffer, don't flush it yet
return {err: false, numSpans: 0};
}
return this.flush();
}
let flushResponse: SenderResponse = this.flush();
this._batch.spans.push(span);
this._totalSpanBytes = spanSize;
return flushResponse;
}
flush(): SenderResponse {
let numSpans: number = this._batch.spans.length;
if (numSpans == 0) {
return {err: false, numSpans: 0}
}
let bufferLen = this._totalSpanBytes + this._emitSpanBatchOverhead;
let thriftBuffer = new Buffer(bufferLen);
let writeResult = this._agentThrift.Agent.emitBatch.argumentsMessageRW.writeInto(
this._convertBatchToThriftMessage(this._batch), thriftBuffer, 0
);
if (writeResult.err) {
this._logger.error(`error writing Thrift object: ${writeResult.err}`);
return {err: true, numSpans: numSpans};
}
// Having the error callback here does not prevent uncaught exception from being thrown,
// that's why in the constructor we also add a general on('error') handler.
this._client.send(thriftBuffer, 0, thriftBuffer.length, this._port, this._host, (err, sent) => {
if (err) {
this._logger.error(`error sending spans over UDP: ${err}, packet size: ${writeResult.offset}, bytes sent: ${sent}`);
}
});
this._reset();
return {err: false, numSpans: numSpans};
}
_convertBatchToThriftMessage() {
let spanMessages = [];
for (let i = 0; i < this._batch.spans.length; i++) {
let span = this._batch.spans[i];
spanMessages.push(new this._jaegerThrift.Span(span))
}
return new this._agentThrift.Agent.emitBatch.ArgumentsMessage({
version: 1,
id: 0,
body: {batch: new this._jaegerThrift.Batch({
process: this._thriftProcessMessage,
spans: spanMessages
})}
});
}
_reset() {
this._batch.spans = [];
this._totalSpanBytes = 0;
}
close(): void {
this._client.close();
}
}
|