-
Notifications
You must be signed in to change notification settings - Fork 133
Fix buffer size counting bug and add logging to UDP sender #151
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// @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. | ||
|
||
declare class dgram$Socket { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've never seen this naming convention. Is this part of ES6's style? |
||
on(event: string, listener: Function): this; | ||
close(): void; | ||
send( | ||
msg: Buffer, | ||
offset: number, | ||
length: number, | ||
port: number, | ||
address: string, | ||
callback?: (err: ?any, bytes: any) => mixed, | ||
): void; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ 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; | ||
|
@@ -34,20 +35,24 @@ export default class UDPSender { | |
_maxPacketSize: number; | ||
_process: Process; | ||
_emitSpanBatchOverhead: number; | ||
_maxSpanBytes: number; | ||
_client: any; | ||
_byteBufferSize: 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._byteBufferSize = 0; | ||
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, | ||
|
@@ -57,6 +62,7 @@ export default class UDPSender { | |
source: fs.readFileSync(path.join(__dirname, '../jaeger-idl/thrift/jaeger.thrift'), 'ascii'), | ||
allowOptionalArguments: true | ||
}); | ||
this._totalSpanBytes = 0; | ||
} | ||
|
||
_calcBatchSize(batch: Batch) { | ||
|
@@ -99,18 +105,19 @@ export default class UDPSender { | |
return { err: true, numSpans: 1 }; | ||
} | ||
|
||
this._byteBufferSize += spanSize; | ||
if (this._byteBufferSize <= this._maxSpanBytes) { | ||
if (this._totalSpanBytes + spanSize <= this._maxSpanBytes) { | ||
this._batch.spans.push(span); | ||
if (this._byteBufferSize < this._maxSpanBytes) { | ||
this._totalSpanBytes += spanSize; | ||
if (this._totalSpanBytes < this._maxSpanBytes) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this extra logic is needed? ie if totalspanbytes == maxspanbytes, the next time round it will be flushed on L118. Do we really need this extra check that has only a 1 byte tolerance? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
minor optimization, but seems worth it given that it doesn't complicate the code that much. |
||
// 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._byteBufferSize = spanSize; | ||
this._totalSpanBytes = spanSize; | ||
return flushResponse; | ||
} | ||
|
||
|
@@ -120,23 +127,24 @@ export default class UDPSender { | |
return {err: false, numSpans: 0} | ||
} | ||
|
||
let bufferLen = this._byteBufferSize + this._emitSpanBatchOverhead; | ||
let bufferLen = this._totalSpanBytes + this._emitSpanBatchOverhead; | ||
let thriftBuffer = new Buffer(bufferLen); | ||
let bufferResult = this._agentThrift.Agent.emitBatch.argumentsMessageRW.writeInto( | ||
let writeResult = this._agentThrift.Agent.emitBatch.argumentsMessageRW.writeInto( | ||
this._convertBatchToThriftMessage(this._batch), thriftBuffer, 0 | ||
); | ||
|
||
if (bufferResult.err) { | ||
console.log('err', bufferResult.err); | ||
if (writeResult.err) { | ||
this._logger.error(`error writing Thrift object: ${writeResult.err}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to emit a metric here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the sender is not integrated with metrics currently. Also the two new errors are a sign of misconfiguration, not something that can keep happening repeatedly, thus I think the logs are a lot more useful. |
||
return {err: true, numSpans: numSpans}; | ||
} | ||
|
||
// https://nodejs.org/api/dgram.html#dgram_socket_send_msg_offset_length_port_address_callback | ||
this._client.on('error', err => { | ||
console.log(`error sending span: ${err}`) | ||
}) | ||
|
||
this._client.send(thriftBuffer, 0, thriftBuffer.length, this._port, this._host); | ||
// 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}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above. |
||
} | ||
}); | ||
this._reset(); | ||
|
||
return {err: false, numSpans: numSpans}; | ||
|
@@ -161,7 +169,7 @@ export default class UDPSender { | |
|
||
_reset() { | ||
this._batch.spans = []; | ||
this._byteBufferSize = 0; | ||
this._totalSpanBytes = 0; | ||
} | ||
|
||
close(): void { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should update the license.sh to say 2017