Skip to content

Commit cbbcc18

Browse files
authored
[monitor-opentelemetry-exporter] Fix Nested Object Serialization (Azure#26801)
### Packages impacted by this PR @azure/monitor-opentelemetry-exporter ### Issues associated with this PR microsoft/ApplicationInsights-node.js#1169 ### Describe the problem that is addressed by this PR Fixes an issue with serializing legacy Application Insights logs with nested objects. ### Are there test cases added in this PR? _(If not, why?)_ Yes, added in the `logUtils.tests` file. ### Checklists - [x] Added impacted package name to the issue description - [ ] Does this PR needs any fixes in the SDK Generator?** _(If so, create an Issue in the [Autorest/typescript](https://github.com/Azure/autorest.typescript) repository and link it here)_ - [x] Added a changelog (if necessary)
1 parent 4ec9f84 commit cbbcc18

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

sdk/monitor/monitor-opentelemetry-exporter/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Release History
22

3+
## 1.0.0-beta.15
4+
5+
### Bugs Fixed
6+
7+
- Fix an issue with serializing nested log messages.
8+
39
## 1.0.0-beta.14 (2023-06-15)
410

511
### Features Added

sdk/monitor/monitor-opentelemetry-exporter/src/utils/logUtils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ function getLegacyApplicationInsightsBaseData(log: ReadableLogRecord): MonitorDo
200200
baseData = JSON.parse(log.body) as TelemetryEventData;
201201
break;
202202
}
203+
if (typeof baseData?.message === "object") {
204+
baseData.message = JSON.stringify(baseData.message);
205+
}
203206
} catch (err) {
204207
diag.error("AzureMonitorLogExporter failed to parse Application Insights Telemetry");
205208
}

sdk/monitor/monitor-opentelemetry-exporter/test/internal/logUtils.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,4 +395,43 @@ describe("logUtils.ts", () => {
395395
);
396396
});
397397
});
398+
399+
it("should parse objects if passed as the message field of a legacy ApplicationInsights log", () => {
400+
const log = new LogRecord(logger, {
401+
body: '{"message":{"nested":{"nested2":{"test":"test"}}},"severityLevel":"Information","version":2}',
402+
severityNumber: 12,
403+
attributes: {
404+
"_MS.baseType": "MessageData",
405+
},
406+
});
407+
const expectedTime = new Date(hrTimeToMilliseconds(log.hrTime));
408+
log.setAttributes({
409+
"extra.attribute": "foo",
410+
[SemanticAttributes.MESSAGE_TYPE]: "test message type",
411+
});
412+
const expectedProperties = {
413+
"extra.attribute": "foo",
414+
[SemanticAttributes.MESSAGE_TYPE]: "test message type",
415+
};
416+
const expectedBaseData: Partial<MessageData> = {
417+
message: '{"nested":{"nested2":{"test":"test"}}}',
418+
severityLevel: `Information`,
419+
version: 2,
420+
properties: expectedProperties,
421+
measurements: {},
422+
};
423+
424+
const envelope = logToEnvelope(log, "ikey");
425+
console.log("TEST ENVELOPE!!!", envelope);
426+
assertEnvelope(
427+
envelope,
428+
"Microsoft.ApplicationInsights.Message",
429+
100,
430+
"MessageData",
431+
expectedProperties,
432+
emptyMeasurements,
433+
expectedBaseData,
434+
expectedTime
435+
);
436+
});
398437
});

0 commit comments

Comments
 (0)