diff --git a/azure/activity_logs_monitoring/index.js b/azure/activity_logs_monitoring/index.js index 189edde6e..05a7c5ef3 100644 --- a/azure/activity_logs_monitoring/index.js +++ b/azure/activity_logs_monitoring/index.js @@ -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: [, ] const JSON_STRING = 'json-string'; // example: '{"key": "value"}' const JSON_STRING_ARRAY = 'json-string-array'; // example: ['{"records": [{}, {}]}'] or ['{"key": "value"}'] const INVALID = 'invalid'; @@ -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; @@ -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)) @@ -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; } @@ -261,6 +282,7 @@ module.exports.forTests = { STRING_ARRAY, JSON_OBJECT, JSON_ARRAY, + BUFFER_ARRAY, JSON_STRING, JSON_STRING_ARRAY, INVALID diff --git a/azure/test/client.test.js b/azure/test/client.test.js index 1ca5b460b..20f629de2 100644 --- a/azure/test/client.test.js +++ b/azure/test/client.test.js @@ -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' }];