Skip to content

Commit 51f83d2

Browse files
(Azure) Resolve invalid JSON Object under properties field (#1015)
* fix(azure): Resolve properties field issue, that contains JSON string with single quotes * Added test cases, for fixPropertiesJsonString for code coverage * Applied recommendation from mattsp1290, which simplifies the if condition Co-authored-by: Matt Spurlin <matt.spurlin@datadoghq.com> * Finalized the simplified function, added missing return statements * Added additional test cases for fixPropertiesJsonString, that handles nested objects and no properties field --------- Co-authored-by: Matt Spurlin <matt.spurlin@datadoghq.com>
1 parent b45e847 commit 51f83d2

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

azure/activity_logs_monitoring/index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,14 +383,32 @@ class EventhubLogHandler {
383383
this.records.push(originalRecord);
384384
}
385385
} else {
386-
this.records.push(originalRecord);
386+
this.records.push(this.fixPropertiesJsonString(originalRecord));
387387
}
388388
} else {
389389
record = this.addTagsToStringLog(record);
390390
this.records.push(record);
391391
}
392392
}
393393

394+
fixPropertiesJsonString(record) {
395+
// Check if properties field is a malformed JSON String
396+
if (Object.hasOwn(record, 'properties') && (typeof record.properties === 'string') && (record.properties.includes("':'"))) {
397+
try {
398+
// If the check is true, find and replace single quotes
399+
// with double quotes, to make a proper JSON
400+
// which is then converted into a JSON Object
401+
record.properties = JSON.parse(record.properties.replace(/'/g, '"'));
402+
return record;
403+
} catch {
404+
this.context.error('Unable to fix properties field to JSON Object');
405+
return record;
406+
}
407+
} else {
408+
return record;
409+
}
410+
}
411+
394412
handleLogs(logs) {
395413
let logsType = this.getLogFormat(logs);
396414
switch (logsType) {

azure/test/activity_logs_monitoring.test.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,3 +818,84 @@ describe('Log Splitting', function() {
818818
});
819819
});
820820
});
821+
describe('EventhubLogHandler Fix Properties Json String', function() {
822+
823+
beforeEach(function() {
824+
this.forwarder = setUp();
825+
});
826+
827+
it('parses properties string with single quotes into object', function() {
828+
const record = { properties: "{'key':'value'}" };
829+
const result = this.forwarder.fixPropertiesJsonString(record);
830+
831+
const expectedProperties = { properties: { key: "value" } };
832+
833+
assert.deepEqual(
834+
expectedProperties,
835+
result
836+
);
837+
assert.equal(
838+
'object',
839+
typeof result.properties
840+
)
841+
});
842+
843+
it('parses object that doesnt have properties', function() {
844+
const record = { hostname: "server_name", subObject: { key:"value"} };
845+
const result = this.forwarder.fixPropertiesJsonString(record);
846+
847+
assert.deepEqual(
848+
record,
849+
result
850+
);
851+
assert.equal(
852+
'object',
853+
typeof result
854+
)
855+
});
856+
857+
it('parses properties string with nested objects', function() {
858+
const record = { properties: "{'key':'value','subObj':{ 'subKey' : 'subValue' }}" };
859+
const result = this.forwarder.fixPropertiesJsonString(record);
860+
861+
const expectedProperties = { properties: { key: "value", subObj: { subKey: "subValue"} } };
862+
863+
assert.deepEqual(
864+
expectedProperties,
865+
result
866+
);
867+
assert.equal(
868+
'object',
869+
typeof result.properties
870+
)
871+
});
872+
873+
it("leaves properties string unchanged when it doesn't match the malformed pattern", function() {
874+
const record = { properties: 'some plain string without colons' };
875+
const result = this.forwarder.fixPropertiesJsonString(record);
876+
877+
assert.deepEqual(
878+
record,
879+
result
880+
);
881+
assert.equal(
882+
'string',
883+
typeof result.properties
884+
)
885+
});
886+
887+
it('logs an error and returns original record when replacement results in invalid JSON', function() {
888+
// includes the "':'" marker so the function attempts replacement, but JSON remains invalid
889+
const badRecord = { properties: "Look i know i shouldn't but, i will do this ':' " };
890+
const result = this.forwarder.fixPropertiesJsonString(badRecord);
891+
892+
assert.deepEqual(
893+
badRecord,
894+
result
895+
);
896+
assert.equal(
897+
'string',
898+
typeof result.properties
899+
)
900+
});
901+
});

0 commit comments

Comments
 (0)