Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions azure/activity_logs_monitoring/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const STRING = 'string'; // example: 'some message'
const STRING_ARRAY = 'string-array'; // example: ['one message', 'two message', ...]
const JSON_OBJECT = 'json-object'; // example: {"key": "value"}
const JSON_ARRAY = 'json-array'; // example: [{"key": "value"}, {"key": "value"}, ...] or [{"records": [{}, {}, ...]}, {"records": [{}, {}, ...]}, ...]
const BUFFER_ARRAY = 'buffer-array'; // example: [<Buffer obj>, <Buffer obj>]
const JSON_STRING = 'json-string'; // example: '{"key": "value"}'
const JSON_STRING_ARRAY = 'json-string-array'; // example: ['{"records": [{}, {}]}'] or ['{"key": "value"}']
const INVALID = 'invalid';
Expand Down Expand Up @@ -113,6 +114,9 @@ class EventhubLogForwarder {
case JSON_ARRAY:
promises = this.handleJSONArrayLogs(logs, JSON_ARRAY);
break;
case BUFFER_ARRAY:
this.handleJSONArrayLogs(logs, BUFFER_ARRAY);
break;
case JSON_STRING_ARRAY:
promises = this.handleJSONArrayLogs(logs, JSON_STRING_ARRAY);
break;
Expand All @@ -138,6 +142,20 @@ class EventhubLogForwarder {
return;
}
}
// If the message is a buffer object, the data type has been set to binary.
if (logsType == BUFFER_ARRAY) {
try {
message = JSON.parse(message.toString());
} catch (err) {
this.context.log.warn(
'log is malformed json, sending as string'
);
promises.push(
this.formatLogAndSend(STRING_TYPE, message.toString())
);
return;
}
}
if (message.records != undefined) {
message.records.forEach(message =>
promises.push(this.formatLogAndSend(JSON_TYPE, message))
Expand All @@ -162,6 +180,9 @@ class EventhubLogForwarder {
if (!Array.isArray(logs)) {
return INVALID;
}
if (Buffer.isBuffer(logs[0])) {
return BUFFER_ARRAY;
}
if (typeof logs[0] === 'object') {
return JSON_ARRAY;
}
Expand Down Expand Up @@ -261,6 +282,7 @@ module.exports.forTests = {
STRING_ARRAY,
JSON_OBJECT,
JSON_ARRAY,
BUFFER_ARRAY,
JSON_STRING,
JSON_STRING_ARRAY,
INVALID
Expand Down
30 changes: 30 additions & 0 deletions azure/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,36 @@ describe('Azure Log Monitoring', function() {
testHandleLogs(record, expected, true);
});

it('should handle buffer array properly', function() {
record = [Buffer.from('{"records": [{ "test": "testing"}]}')];
expected = [{ test: 'testing' }];
assert.equal(
EventhubLogForwarderInstance.getLogFormat(record),
constants.BUFFER_ARRAY
);
testHandleLogs(record, expected, true);
});

it('should handle buffer array without records properly', function() {
record = [Buffer.from('{ "test": "example"}')];
expected = [{ test: 'example' }];
assert.equal(
EventhubLogForwarderInstance.getLogFormat(record),
constants.BUFFER_ARRAY
);
testHandleLogs(record, expected, true);
});

it('should handle buffer array with malformed string', function() {
record = [Buffer.from('{"time": "xy')];
expected = ['{"time": "xy'];
assert.equal(
EventhubLogForwarderInstance.getLogFormat(record),
constants.BUFFER_ARRAY
);
testHandleLogs(record, expected, false);
});

it('should handle json-string-array properly records', function() {
record = ['{"records": [{ "time": "xyz"}, {"time": "abc"}]}'];
expected = [{ time: 'xyz' }];
Expand Down