Skip to content

Commit

Permalink
Fix num batches sent log (#736)
Browse files Browse the repository at this point in the history
* Fix batches sent logs

* remove warn from log

* Fix context in tests

* Fix blob trigger in tests

* Fix mock context again

* Fix tests again

* remove change to log message
  • Loading branch information
parsons90 authored Feb 14, 2024
1 parent 21a10c1 commit bf612fa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
14 changes: 6 additions & 8 deletions azure/blobs_logs_monitoring/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ const DD_LOG_SPLITTING_CONFIG = {
// preserve_fields: true
// }
};
let finishCount = 0;
let totalRetriedCount = 0;


function getLogSplittingConfig() {
Expand Down Expand Up @@ -120,7 +118,7 @@ class Batcher {
}

if (droppedLogs > 0) {
this.context.log.warn(droppedLogs + "Batcher batch() Logs DROPPED for log file " + this.context.bindingData.blobTrigger);
this.context.log.warn(droppedLogs + " Batcher batch() Logs DROPPED for log file " + this.context.bindingData.blobTrigger);
}

if (sizeCount > 0) {
Expand Down Expand Up @@ -158,14 +156,14 @@ class HTTPClient {
4 * 1000 * 1000,
400
);
this.numBatchesSent = 0;
this.numBatchesRetried = 0;
}

async sendAll(records) {
var totalNumLogsAddedToPromise = 0;
var batches = this.batcher.batch(records);
var promises = [];
finishCount = 0;
totalRetriedCount = 0;
this.context.log("SendAll : Number of batches created in this batch is " + batches.length);
for (var i = 0; i < batches.length; i++) {
this.context.log("Batch #" + (i + 1) + " for file" + this.context.bindingData.blobTrigger + ": Number of logs in this batch is " + batches[i].length);
Expand All @@ -176,7 +174,7 @@ class HTTPClient {
var resolvedPromises = await Promise.all(
promises.map(p => p.catch(e => this.context.log.error(e)))
);
this.context.log(`Num batches sent: ${finishCount}. Num batches retried: ${totalRetriedCount}`); // Log the counter after all promises have resolved
this.context.log(`Num batches sent: ${this.numBatchesSent}. Num batches retried: ${this.numBatchesRetried}`); // Log the counter after all promises have resolved
return resolvedPromises;
}

Expand All @@ -197,7 +195,7 @@ class HTTPClient {
`unable to send request after 2 tries, err: ${err}`
);
});
totalRetriedCount++;
this.numBatchesRetried++;
});
})
.catch(err => {
Expand All @@ -219,7 +217,7 @@ class HTTPClient {
reject(error);
})
.on('finish', () => {
finishCount++; // Increment the counter each time the 'finish' event is emitted
this.numBatchesSent++; // Increment the counter each time the 'finish' event is emitted
this.context.log(`Sent batch#: ${batchnum}, file: ${this.context.bindingData.blobTrigger}`);
});
req.write(this.scrubber.scrub(JSON.stringify(record)));
Expand Down
13 changes: 8 additions & 5 deletions azure/test/blobs_logs_monitoring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ function fakeContext() {
contextSpy.log = sinon.spy();
contextSpy.log.error = function(x) {}; // do nothing
contextSpy.log.warn = function(x) {}; // do nothing

contextSpy.bindingData = sinon.spy();
contextSpy.bindingData.blobTrigger = "test/file.log"

return contextSpy;
}
Expand Down Expand Up @@ -576,21 +579,21 @@ describe('Azure Activity Log Monitoring', function() {
describe('Batching', function() {
describe('#batch', function() {
it('should return two batches because of size', function() {
batcher = new client.Batcher(15, 15, 1);
batcher = new client.Batcher(fakeContext(), 15, 15, 1);
logs = [{ hi: 'bye' }, 'bleh'];
actual = batcher.batch(logs);
expected = [[{ hi: 'bye' }], ['bleh']];
assert.deepEqual(actual, expected);
});
it('should return two batches because of batch size bytes', function() {
batcher = new client.Batcher(5, 12, 10);
batcher = new client.Batcher(fakeContext(), 15, 12, 10);
logs = [{ hi: 'bye' }, 'bleh'];
actual = batcher.batch(logs);
expected = [[{ hi: 'bye' }], ['bleh']];
assert.deepEqual(actual, expected);
});
it('should drop message based on message bytes size', function() {
batcher = new client.Batcher(5, 5, 1);
batcher = new client.Batcher(fakeContext(), 5, 5, 1);
logs = [{ hi: 'bye' }, 'bleh'];
actual = batcher.batch(logs);
expected = [['bleh']];
Expand All @@ -599,15 +602,15 @@ describe('Batching', function() {
});
describe('#getSizeInBytes', function() {
it('should return 5 for string', function() {
batcher = new client.Batcher(15, 15, 1);
batcher = new client.Batcher(fakeContext(), 15, 15, 1);
log = 'aaaaa';
actual = batcher.getSizeInBytes(log);
expected = 5;
assert.equal(actual, expected);
});

it('should return 7 for object', function() {
batcher = new client.Batcher(15, 15, 1);
batcher = new client.Batcher(fakeContext(), 15, 15, 1);
log = { a: 2 };
actual = batcher.getSizeInBytes(log);
expected = 7;
Expand Down

0 comments on commit bf612fa

Please sign in to comment.